Guest User

Memory corruption using clEnqueueWriteBuffer

a guest
Sep 17th, 2014
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #ifdef __APPLE__
  5. #include <OpenCL/opencl.h>
  6. #else
  7. #include <CL/cl.h>
  8. #endif
  9.  
  10. #define MAX_SOURCE_SIZE (512)
  11.  
  12. int main()
  13. {
  14.     std::vector<cl_device_id> deviceVector;
  15.     cl_context context = NULL;
  16.     cl_command_queue command_queue = NULL;
  17.     cl_program program = NULL;
  18.     cl_kernel kernel = NULL;
  19.     cl_platform_id platforms[8];
  20.     cl_uint ret_num_devices;
  21.     cl_uint ret_num_platforms;
  22.     cl_int ret;
  23.     cl_mem memArray = NULL;
  24.     float *Array;
  25.    
  26.     //--------------------------KERNEL---------------------------------
  27.     char *source_str;
  28.     source_str = (char*)malloc(MAX_SOURCE_SIZE);
  29.  
  30.     source_str =    "#if defined cl_intel_printf \n" \
  31.                     "   #pragma OPENCL EXTENSION cl_intel_printf :enable \n" \
  32.                     "#else \n" \
  33.                     "   #pragma OPENCL EXTENSION cl_amd_printf :enable \n" \
  34.                     "#endif \n" \
  35.                     " \n" \
  36.                     "__kernel void KernelTest(__global const float * Array) \n" \
  37.                     "   { \n" \
  38.                     "   const int i = get_global_id(0); \n" \
  39.                     "   const int j = get_global_id(1); \n" \
  40.                     "   const int k = get_global_id(2); \n" \
  41.                     " \n" \
  42.                     "   if(i==0 && j==0 && k==0) \n" \
  43.                     "       printf(\"\\r\\nArray GPU: %f \", Array[0]); \n" \
  44.                     "   } \n" \
  45.                     "\n";
  46.  
  47.     size_t source_size = strlen(source_str);
  48.     //------------------------------------------------------------------   
  49.    
  50.  
  51.     std::cout << "\r\nInitializing" << std::endl;
  52.     //--------------------------INITIALIZE---------------------------------
  53.     ret = clGetPlatformIDs(8, platforms, &ret_num_platforms);
  54.     std::cout << " clGetPlatformIDs " << ret << std::endl;
  55.     std::cout << " Number of Platforms:  " << ret_num_platforms << std::endl;
  56.  
  57.     for (int i=0; i<ret_num_platforms; i++)
  58.     {
  59.         cl_device_id devices[8];
  60.         ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 8, devices, &ret_num_devices);
  61.         std::cout << " clGetDeviceIDs " << ret << std::endl << std::endl;
  62.  
  63.         for(int j=0; j<ret_num_devices; j++){
  64.             if(std::find(deviceVector.begin(), deviceVector.end(), devices[j]) == deviceVector.end())
  65.                 deviceVector.push_back(devices[j]);
  66.         }
  67.     }
  68.  
  69.     for (int i=0; i<deviceVector.size(); i++)
  70.     {
  71.         char buffer[1024];
  72.         cl_uint buf_uint;
  73.         cl_ulong buf_ulong;
  74.         clGetDeviceInfo(deviceVector[i], CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
  75.         std::cout << " -- "<< i << " -- DEVICE_NAME: " << buffer << std::endl;
  76.     }
  77.        
  78.     int option = 0;
  79.     do{
  80.         std::cout << "\r\nOption: ";
  81.         std::cin >> option;
  82.     }while(option<0 || option>deviceVector.size()-1);
  83.  
  84.     context = clCreateContext(NULL, 1, &deviceVector[option], NULL, NULL, &ret);
  85.     std::cout << " clCreateContext " << ret << std::endl;
  86.  
  87.     command_queue = clCreateCommandQueue(context, deviceVector[option], 0, &ret);
  88.     std::cout << " clCreateCommandQueue " << ret << std::endl;
  89.  
  90.     program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
  91.     std::cout << " clCreateProgramWithSource " << ret << std::endl;
  92.  
  93.     ret = clBuildProgram(program, 1, &deviceVector[option], NULL, NULL, NULL);
  94.     std::cout << " clBuildProgram " << ret << std::endl;
  95.  
  96.     if(ret){
  97.         size_t log_size;
  98.         clGetProgramBuildInfo(program, deviceVector[option], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  99.         char *log = (char *) malloc(log_size);
  100.         clGetProgramBuildInfo(program, deviceVector[option], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
  101.         std::cout << log << std::endl;
  102.  
  103.         getchar();getchar();
  104.         return ret;
  105.     }
  106.  
  107.     kernel = clCreateKernel(program, "KernelTest", &ret);
  108.     std::cout << " clCreateKernel " << ret << std::endl;
  109.     //------------------------------------------------------------------
  110.  
  111.  
  112.     std::cout << "\r\nKerneling" << std::endl;
  113.     //--------------------------EXECUTE---------------------------------
  114.     size_t mGlobalWorkSizePtr[] = { 100, 100, 62 };
  115.     size_t mLocalWorkSizePtr[] = { 5, 5, 1 };
  116.  
  117.     int SizeArray = 0;
  118.     for(int j=1;j<100;j++){
  119.  
  120.         //Array memory allocation, starting with 4MB in first iteration to 400MB in last one
  121.         SizeArray = j * 1000000 * sizeof(float);
  122.         Array = (float*)malloc(SizeArray);
  123.         memset(Array, 0, SizeArray);
  124.  
  125.         //Give the array's first element some nonzero value
  126.         //This is the value that is expected to be printed by the kernel execution
  127.         Array[0] = j;
  128.         std::cout << "\r\n\r\n Size: " << SizeArray/sizeof(float) << std::endl << std::endl;
  129.  
  130.         //Create the buffer where the content of the array will be stored
  131.         memArray = clCreateBuffer(context, CL_MEM_READ_WRITE, SizeArray, NULL, &ret);
  132.         std::cout << " clCreateBuffer Array " << ret << std::endl;
  133.    
  134.         //Write the array contents into the buffer inside the device
  135.         ret = clEnqueueWriteBuffer(command_queue, memArray, CL_TRUE, 0, SizeArray, Array, 0, NULL, NULL);
  136.         std::cout << " clEnqueueWriteBuffer Array " << ret << std::endl;
  137.         ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memArray);
  138.         std::cout << " clSetKernelArg memArray " << ret << std::endl;
  139.  
  140.         std::cout << "\r\nArray CPU : " << Array[0] << std::endl;
  141.         getchar();
  142.  
  143.         //Execute the kernel where the content of the first element of the array will be printed
  144.         ret = clEnqueueNDRangeKernel(command_queue, kernel, 3, NULL, mGlobalWorkSizePtr, mLocalWorkSizePtr, 0, NULL,NULL);
  145.         ret = clFinish(command_queue);
  146.  
  147. /****** FAIL! Kernel prints correct value of Array's first element ONLY IN SOME ITERATIONS (when it fails zero values are printed)! Depending on SizeArray :?? ******/
  148.  
  149.         free(Array);
  150.         ret = clReleaseMemObject(memArray);
  151.     }
  152.  
  153.     ret = clFlush(command_queue);
  154.     ret = clFinish(command_queue);
  155.     ret = clReleaseKernel(kernel);
  156.     ret = clReleaseProgram(program);
  157.     ret = clReleaseCommandQueue(command_queue);
  158.     ret = clReleaseContext(context);
  159.  
  160.     free(source_str);
  161.     //------------------------------------------------------------------
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment