Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #ifdef __APPLE__
- #include <OpenCL/opencl.h>
- #else
- #include <CL/cl.h>
- #endif
- #define MAX_SOURCE_SIZE (512)
- int main()
- {
- std::vector<cl_device_id> deviceVector;
- cl_context context = NULL;
- cl_command_queue command_queue = NULL;
- cl_program program = NULL;
- cl_kernel kernel = NULL;
- cl_platform_id platforms[8];
- cl_uint ret_num_devices;
- cl_uint ret_num_platforms;
- cl_int ret;
- cl_mem memArray = NULL;
- float *Array;
- //--------------------------KERNEL---------------------------------
- char *source_str;
- source_str = (char*)malloc(MAX_SOURCE_SIZE);
- source_str = "#if defined cl_intel_printf \n" \
- " #pragma OPENCL EXTENSION cl_intel_printf :enable \n" \
- "#else \n" \
- " #pragma OPENCL EXTENSION cl_amd_printf :enable \n" \
- "#endif \n" \
- " \n" \
- " int printf (__constant const char *, ...);" \
- "__kernel void KernelTest(__global float * Array) \n" \
- " { \n" \
- " const int i = get_global_id(0); \n" \
- " const int j = get_global_id(1); \n" \
- " const int k = get_global_id(2); \n" \
- " \n" \
- " if(i==0 && j==0 && k==0){ \n" \
- " Array[1] = Array[0];/**/ \n" \
- " printf(\"\\r\\nArray[0] GPU: %f \", Array[0]);\n" \
- " } \n" \
- " } \n" \
- "\n";
- size_t source_size = strlen(source_str);
- //------------------------------------------------------------------
- std::cout << "\r\nInitializing" << std::endl;
- //--------------------------INITIALIZE---------------------------------
- ret = clGetPlatformIDs(8, platforms, &ret_num_platforms);
- std::cout << " clGetPlatformIDs " << ret << std::endl;
- std::cout << " Number of Platforms: " << ret_num_platforms << std::endl;
- for (int i=0; i<ret_num_platforms; i++)
- {
- cl_device_id devices[8];
- ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 8, devices, &ret_num_devices);
- std::cout << " clGetDeviceIDs " << ret << std::endl << std::endl;
- for(int j=0; j<ret_num_devices; j++){
- if(std::find(deviceVector.begin(), deviceVector.end(), devices[j]) == deviceVector.end())
- deviceVector.push_back(devices[j]);
- }
- }
- for (int i=0; i<deviceVector.size(); i++)
- {
- char buffer[1024];
- cl_uint buf_uint;
- cl_ulong buf_ulong;
- clGetDeviceInfo(deviceVector[i], CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
- std::cout << " -- "<< i << " -- DEVICE_NAME: " << buffer << std::endl;
- }
- int option = 0;
- do{
- std::cout << "\r\nOption: ";
- std::cin >> option;
- }while(option<0 || option>deviceVector.size()-1);
- context = clCreateContext(NULL, 1, &deviceVector[option], NULL, NULL, &ret);
- std::cout << " clCreateContext " << ret << std::endl;
- command_queue = clCreateCommandQueue(context, deviceVector[option], 0, &ret);
- std::cout << " clCreateCommandQueue " << ret << std::endl;
- program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
- std::cout << " clCreateProgramWithSource " << ret << std::endl;
- ret = clBuildProgram(program, 1, &deviceVector[option], NULL, NULL, NULL);
- std::cout << " clBuildProgram " << ret << std::endl;
- if(ret){
- size_t log_size;
- clGetProgramBuildInfo(program, deviceVector[option], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
- char *log = (char *) malloc(log_size);
- clGetProgramBuildInfo(program, deviceVector[option], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
- std::cout << log << std::endl;
- getchar();getchar();
- return ret;
- }
- kernel = clCreateKernel(program, "KernelTest", &ret);
- std::cout << " clCreateKernel " << ret << std::endl;
- //------------------------------------------------------------------
- std::cout << "\r\nKerneling" << std::endl;
- //--------------------------EXECUTE---------------------------------
- size_t mGlobalWorkSizePtr[] = { 100, 100, 62 };
- size_t mLocalWorkSizePtr[] = { 5, 5, 1 };
- int SizeArray = 0;
- for(int j=1;j<100;j++){
- //Array memory allocation, starting with 4MB in first iteration to 400MB in last one
- SizeArray = j * 1000000 * sizeof(float);
- Array = (float*)malloc(SizeArray);
- memset(Array, 0, SizeArray);
- //Give the array's first element some nonzero value
- //This is the value that is expected to be printed by the kernel execution
- Array[0] = j;
- std::cout << "\r\n\r\n Size: " << SizeArray/sizeof(float) << std::endl << std::endl;
- //Create the buffer where the content of the array will be stored
- memArray = clCreateBuffer(context, CL_MEM_READ_WRITE, SizeArray, NULL, &ret);
- std::cout << " clCreateBuffer Array " << ret << std::endl;
- //Write the array contents into the buffer inside the device
- ret = clEnqueueWriteBuffer(command_queue, memArray, CL_TRUE, 0, SizeArray, Array, 0, NULL, NULL);
- std::cout << " clEnqueueWriteBuffer Array " << ret << std::endl;
- ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memArray);
- std::cout << " clSetKernelArg memArray " << ret << std::endl;
- std::cout << "\r\nArray[0] CPU : " << Array[0] << std::endl;
- /*
- //Execute the kernel where the content of the first element of the array will be printed
- ret = clEnqueueNDRangeKernel(command_queue, kernel, 3, NULL, mGlobalWorkSizePtr, mLocalWorkSizePtr, 0, NULL,NULL);
- ret = clFinish(command_queue);
- */
- float *ArrayOut;
- ArrayOut = (float*)malloc(SizeArray);
- memset(ArrayOut, 0, SizeArray);
- ret = clEnqueueReadBuffer(command_queue, memArray, CL_TRUE, 0, SizeArray, ArrayOut, 0, NULL, NULL);
- std::cout << "\r\n\r\nArrayOut[0] : " << ArrayOut[0] << std::endl;
- getchar();
- /****** FAIL! Kernel prints correct value of Array's first element ONLY IN SOME ITERATIONS (when it fails zero values are printed)! Depending on SizeArray :?? ******/
- free(Array);
- free(ArrayOut);
- ret = clReleaseMemObject(memArray);
- }
- ret = clFlush(command_queue);
- ret = clFinish(command_queue);
- ret = clReleaseKernel(kernel);
- ret = clReleaseProgram(program);
- ret = clReleaseCommandQueue(command_queue);
- ret = clReleaseContext(context);
- free(source_str);
- //------------------------------------------------------------------
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement