Advertisement
Guest User

Memory corruption using clEnqueueWriteBuffer 2

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