#include #include #include //#define BUILD_PROGRAM //#define CREATE_KERNEL void main() { const char* kernelSource = "kernel void K(global read_only float* buffIn, global write_only float* buffOut){int index = get_global_id( 0 ); buffOut[ index ] = buffIn[ index ];}"; const char* kernelName = "K"; cl_int error; cl_uint platformCount; clGetPlatformIDs( 0, 0, &platformCount ); cl_platform_id *platformList = new cl_platform_id[ platformCount ]; clGetPlatformIDs( platformCount, platformList, 0 ); size_t paramSize; clGetPlatformInfo( platformList[0], CL_PLATFORM_NAME, 0, 0, ¶mSize ); char *platformName = new char[paramSize]; clGetPlatformInfo( platformList[0], CL_PLATFORM_NAME, paramSize, platformName, 0 ); cl_uint deviceCount; clGetDeviceIDs( platformList[0], CL_DEVICE_TYPE_DEFAULT, 0, 0, &deviceCount ); cl_device_id *deviceList = new cl_device_id[ deviceCount ]; clGetDeviceIDs( platformList[0], CL_DEVICE_TYPE_DEFAULT, deviceCount, deviceList, 0 ); cl_context_properties contextPropertyList[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformList[0], 0 }; cl_context context = clCreateContext( contextPropertyList, deviceCount, deviceList, 0, 0, 0 ); size_t bufferSize = 100; float *arrayIn = new float[bufferSize]; for( int i = 0; i < bufferSize; i++ ) arrayIn[i] = i; float *arrayOut = new float[bufferSize]; cl_mem bufferIn = clCreateBuffer( context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, bufferSize * sizeof(float), arrayIn, 0 ); cl_mem bufferOut = clCreateBuffer( context, CL_MEM_WRITE_ONLY, bufferSize * sizeof(float), 0, 0 ); cl_command_queue queue = clCreateCommandQueue( context, deviceList[0], 0, 0 ); cl_program program = clCreateProgramWithSource( context, 1, &kernelSource, 0, 0 ); #ifdef BUILD_PROGRAM while( true ) #endif clBuildProgram( program, 0, 0, 0, 0, 0 ); // leaks #ifdef CREATE_KERNEL while( true ){ #endif cl_kernel kernel = clCreateKernel( program, kernelName, 0 ); // leaks #ifdef CREATE_KERNEL clReleaseKernel( kernel );} #endif clSetKernelArg( kernel, 0, sizeof( cl_mem ), &bufferIn ); clSetKernelArg( kernel, 1, sizeof( cl_mem ), &bufferOut ); size_t globalWorkSize[] = { bufferSize }; while( true ) { clEnqueueNDRangeKernel( queue, kernel, 1, 0, globalWorkSize, 0, 0, 0, 0 ); clEnqueueReadBuffer( queue, bufferOut, (cl_bool)1, 0, bufferSize * sizeof(float), arrayOut, 0, 0, 0 ); } #ifndef CREATE_KERNEL clReleaseKernel( kernel ); #endif clReleaseProgram( program ); clReleaseCommandQueue( queue ); clReleaseMemObject( bufferOut ); clReleaseMemObject( bufferIn ); clReleaseContext( context ); delete[] arrayOut; delete[] arrayIn; delete[] deviceList; delete[] platformName; delete[] platformList; }