2 Replies Latest reply on Nov 24, 2010 8:00 AM by himanshu.gautam

    Problem with the squares problem from tutorial

    kranti

      Hi, I am using the code given in http://developer.amd.com/zones/OpenCLZone/courses/Documents/Introduction_to_OpenCL_Programming%20Training_Guide%20(201005).pdf to find the squares of the numbers from 1 to 10. What I need to do is, send a argument to the function, and get the square of that number. The code is attached. When I try to run the file, I get the following errors : 

       

       

      devuser@devuser-desktop:~/Desktop/opencl/hello$ g++ -o sqr -I$ATISTREAMSDKROOT/include -L$ATISTREAMSDKROOT/lib/x86 squares.cpp -lOpenCL

      squares.cpp: In function ‘int square(int)’:

      squares.cpp:75: error: invalid conversion from ‘int’ to ‘const void*’

      squares.cpp:75: error:   initializing argument 6 of ‘cl_int clEnqueueWriteBuffer(_cl_command_queue*, _cl_mem*, cl_bool, size_t, size_t, const void*, cl_uint, _cl_event* const*, _cl_event**)’

      squares.cpp:89: error: invalid conversion from ‘int’ to ‘void*’

      squares.cpp:89: error:   initializing argument 6 of ‘cl_int clEnqueueReadBuffer(_cl_command_queue*, _cl_mem*, cl_bool, size_t, size_t, void*, cl_uint, _cl_event* const*, _cl_event**)’

      What is going wrong?


      #include <stdio.h> #include "CL/cl.h" //#define DATA_SIZE 10 const char *KernelSource = "__kernel void hello(__global float *input, __global float *output)\n"\ "{\n"\ " size_t id = get_global_id(0);\n"\ " output[id] = input[id] * input[id];\n"\ "}\n"\ "\n"; int square(int val) { cl_context context; cl_context_properties properties[3]; cl_kernel kernel; cl_command_queue command_queue; cl_program program; cl_int err; cl_uint num_of_platforms=0; cl_platform_id platform_id; cl_device_id device_id; cl_uint num_of_devices=0; cl_mem input, output; size_t global; int inputData=val; int results=0; int i; // retreives a list of platforms available if (clGetPlatformIDs(1, &platform_id, &num_of_platforms)!= CL_SUCCESS) { printf("Unable to get platform_id\n"); return 1; } // try to get a supported GPU device if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices) != CL_SUCCESS) { printf("Unable to get device_id\n"); return 1; } // context properties list - must be terminated with 0 properties[0]= CL_CONTEXT_PLATFORM; properties[1]= (cl_context_properties) platform_id; properties[2]= 0; // create a context with the GPU device context = clCreateContext(properties,1,&device_id,NULL,NULL,&err); // create command queue using the context and device command_queue = clCreateCommandQueue(context, device_id, 0, &err); // create a program from the kernel source code program = clCreateProgramWithSource(context,1,(const char **) &KernelSource, NULL, &err); // compile the program if (clBuildProgram(program, 0, NULL, NULL, NULL, NULL) != CL_SUCCESS) { printf("Error building program\n"); return 1; } // specify which kernel from the program to execute kernel = clCreateKernel(program, "hello", &err); // create buffers for the input and ouput input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) , NULL, NULL); output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) , NULL, NULL); // load data into the input buffer /* ****************************************** ERROR IS HERE ********************************************/ clEnqueueWriteBuffer(command_queue, input, CL_TRUE, 0, sizeof(int) , inputData, 0, NULL, NULL); // set the argument list for the kernel command clSetKernelArg(kernel, 0, sizeof(cl_mem), &input); clSetKernelArg(kernel, 1, sizeof(cl_mem), &output); // global=DATA_SIZE; // enqueue the kernel command for execution clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL); clFinish(command_queue); // copy the results from out of the output buffer /* ****************************************** ERROR IS HERE ********************************************/ clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0, sizeof(int), results, 0, NULL, NULL); // print the results printf("output: "); // for(i=0;i<DATA_SIZE; i++) { printf("%d ",results); } // cleanup - release OpenCL resources clReleaseMemObject(input); clReleaseMemObject(output); clReleaseProgram(program); clReleaseKernel(kernel); clReleaseCommandQueue(command_queue); clReleaseContext(context); return results; } int main() { int i = square(6); return 0; }

        • Problem with the squares problem from tutorial
          kranti

          I have modified the code, and now I am not getting the error mentioned above. Though I didnt understand why the error was coming The problem with this code is that it gives all the outputs as ZERO

           

          #include <stdio.h> #include "CL/cl.h" #define DATASIZE 4 const char *KernelSource = "__kernel void hello(__global float *input, __global float *output)\n"\ "{\n"\ " size_t id = get_global_id(0);\n"\ " output[id] = input[id] * input[id];\n"\ "}\n"\ "\n"; int square(int val[]) { cl_context context; cl_context_properties properties[3]; cl_kernel kernel; cl_command_queue command_queue; cl_program program; cl_int err; cl_uint num_of_platforms=0; cl_platform_id platform_id; cl_device_id device_id; cl_uint num_of_devices=0; cl_mem input, output; size_t global; int *inputData; inputData = val; float results[DATASIZE]={0}; int i; // retreives a list of platforms available if (clGetPlatformIDs(1, &platform_id, &num_of_platforms)!= CL_SUCCESS) { printf("Unable to get platform_id\n"); return 1; } // try to get a supported GPU device if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices) != CL_SUCCESS) { printf("Unable to get device_id\n"); return 1; } // context properties list - must be terminated with 0 properties[0]= CL_CONTEXT_PLATFORM; properties[1]= (cl_context_properties) platform_id; properties[2]= 0; // create a context with the GPU device context = clCreateContext(properties,1,&device_id,NULL,NULL,&err); // create command queue using the context and device command_queue = clCreateCommandQueue(context, device_id, 0, &err); // create a program from the kernel source code program = clCreateProgramWithSource(context,1,(const char **) &KernelSource, NULL, &err); // compile the program if (clBuildProgram(program, 0, NULL, NULL, NULL, NULL) != CL_SUCCESS) { printf("Error building program\n"); return 1; } // specify which kernel from the program to execute kernel = clCreateKernel(program, "hello", &err); // create buffers for the input and ouput input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) , NULL, NULL); output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) , NULL, NULL); // load data into the input buffer clEnqueueWriteBuffer(command_queue, input, CL_TRUE, 0,sizeof(int) * DATASIZE , val, 0, NULL, NULL); // set the argument list for the kernel command clSetKernelArg(kernel, 0, sizeof(cl_mem), &input); clSetKernelArg(kernel, 1, sizeof(cl_mem), &output); // global=DATA_SIZE; // enqueue the kernel command for execution clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL); clFinish(command_queue); // copy the results from out of the output buffer clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0,sizeof(float) * DATASIZE, results, 0, NULL, NULL); // print the results printf("output: "); for(i=0;i<DATASIZE; i++) { printf("%f ",results[i]); } // cleanup - release OpenCL resources clReleaseMemObject(input); clReleaseMemObject(output); clReleaseProgram(program); clReleaseKernel(kernel); clReleaseCommandQueue(command_queue); clReleaseContext(context); return 0; } int main() { int value[] = {6,7,8,9}; int i = square(value); return 0; }

            • Problem with the squares problem from tutorial
              himanshu.gautam

              That code should work. You were trying to reduce the dimensions of arrays but did not took proper care about using pointers and out of bond access.

              #include <stdio.h> #include "CL/cl.h" #include<stdlib.h> #define DATASIZE 4 #define FORCED_EXIT 1 const char *KernelSource = "__kernel void hello(__global int *input, __global int *output)\n"\ "{\n"\ " int id = get_global_id(0);\n"\ " output[id] = input[id] * input[id];\n"\ "}\n"\ "\n"; int square(int val[]) { cl_context context; cl_context_properties properties[3]; cl_kernel kernel; cl_command_queue command_queue; cl_program program; //cl_int err; cl_uint num_of_platforms=0; cl_platform_id platform_id; cl_device_id device_id; cl_uint num_of_devices=0; cl_mem input, output; size_t global; //int *inputData= val; int results[DATASIZE]={0,0,0,0}; int i,Status; // retreives a list of platforms available if (clGetPlatformIDs(1, &platform_id, &num_of_platforms)!= CL_SUCCESS) { printf("Unable to get platform_id\n"); return 1; } // try to get a supported GPU device if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_CPU, 1, &device_id, &num_of_devices) != CL_SUCCESS) { printf("Unable to get device_id\n"); return 1; } // context properties list - must be terminated with 0 properties[0]= CL_CONTEXT_PLATFORM; properties[1]= (cl_context_properties) platform_id; properties[2]= 0; // create a context with the GPU device context = clCreateContext(properties,1,&device_id,NULL,NULL,&Status); if(Status != CL_SUCCESS) { printf("Error in creating context: %d",Status); exit(FORCED_EXIT); } // create command queue using the context and device command_queue = clCreateCommandQueue(context, device_id, 0, &Status); if(Status != CL_SUCCESS) { printf("Error in creating command queue: %d",Status); exit(FORCED_EXIT); } // create a program from the kernel source code program = clCreateProgramWithSource(context,1,(const char **) &KernelSource, NULL, &Status); if(Status != CL_SUCCESS) { printf("Error in creating program with source: %d",Status); exit(FORCED_EXIT); } // compile the program if (clBuildProgram(program, 0, NULL, NULL, NULL, NULL) != CL_SUCCESS) { printf("Error building program\n"); return 1; } // specify which kernel from the program to execute kernel = clCreateKernel(program, "hello", &Status); if(Status != CL_SUCCESS) { printf("Error in creating kernel: %d",Status); exit(FORCED_EXIT); } // create buffers for the input and ouput input = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*DATASIZE , NULL, &Status); if(Status != CL_SUCCESS) { printf("Error in creating kernel: %d",Status); exit(FORCED_EXIT); } output = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*DATASIZE , NULL, &Status); if(Status != CL_SUCCESS) { printf("Error in creating kernel: %d",Status); exit(FORCED_EXIT); } // load data into the input buffer Status=clEnqueueWriteBuffer(command_queue, input, CL_FALSE, 0,sizeof(int) * DATASIZE , (const void*)val, 0, NULL, NULL); if(Status != CL_SUCCESS) { printf("Error in writing buffer inputbuffer: %d",Status); exit(FORCED_EXIT); } // set the argument list for the kernel command Status=clSetKernelArg(kernel, 0, sizeof(cl_mem), &input); Status|=clSetKernelArg(kernel, 1, sizeof(cl_mem), &output); if(Status != CL_SUCCESS) { printf("Error in kernel argument: %d",Status); exit(FORCED_EXIT); } global=DATASIZE; // enqueue the kernel command for execution Status=clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL); if(Status != CL_SUCCESS) { printf("Error in ndrange kernel: %d",Status); exit(FORCED_EXIT); } Status=clFinish(command_queue); if(Status != CL_SUCCESS) { printf("Error in clfinish: %d",Status); exit(FORCED_EXIT); } // copy the results from out of the output buffer Status=clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0,sizeof(int) * DATASIZE, results, 0, NULL, NULL); if(Status != CL_SUCCESS) { printf("Error in reading buffer outputbuffer0: %d",Status); exit(FORCED_EXIT); } // print the results printf("output: "); for(i=0;i<DATASIZE; i++) { printf("%d ",results[i]); } // cleanup - release OpenCL resources clReleaseMemObject(input); clReleaseMemObject(output); clReleaseProgram(program); clReleaseKernel(kernel); clReleaseCommandQueue(command_queue); clReleaseContext(context); return 0; } int main() { int value[] = {6,7,8,9}; int i = square(value); return 0; }