#define CL_USE_DEPRECATED_OPENCL_1_2_APIS #include #include #include #include #include #include "FreeImage.h" const int N = 512; const int MAX_PLATFORMS = 5; const int MAX_DEVICES = 5; size_t RoundUp(int groupSize, int globalSize) { int r = globalSize % groupSize; if (r == 0) { return globalSize; } else { return globalSize + groupSize - r; } } bool SaveImage(char *fileName, unsigned char *buffer, int width, int height) { FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(fileName); FIBITMAP *image = FreeImage_ConvertFromRawBits((BYTE*)buffer, width, height, width * 4, 32, 0xFF000000, 0x00FF0000, 0x0000FF00); return (FreeImage_Save(format, image, fileName) == TRUE) ? true : false; } void DisplayPlatformInfo( cl_platform_id id, cl_platform_info name, std::string str) { cl_int errNum; std::size_t paramValueSize; errNum = clGetPlatformInfo( id, name, 0, NULL, ¶mValueSize); if (errNum != CL_SUCCESS) { std::cerr << "Failed to find OpenCL platform " << str << "." << std::endl; return; } char * info = (char *)alloca(sizeof(char) * paramValueSize); errNum = clGetPlatformInfo( id, name, paramValueSize, info, NULL); if (errNum != CL_SUCCESS) { std::cerr << "Failed to find OpenCL platform " << str << "." << std::endl; return; } std::cout << "\t" << str << ":\t" << info << std::endl; } cl_context CreateContext( cl_uint *numDevices, cl_device_id *devices) { cl_int errNum; cl_uint numPlatforms; cl_platform_id platformIds[MAX_PLATFORMS]; cl_context context = NULL; errNum = clGetPlatformIDs(0, NULL, &numPlatforms); std::cout << "Number of platforms: \t" << numPlatforms << std::endl; if (errNum != CL_SUCCESS || numPlatforms <= 0) { std::cerr << "Failed to find any OpenCL platforms." << std::endl; return NULL; } errNum = clGetPlatformIDs(numPlatforms, platformIds, NULL); cl_uint selPlatform = 0; DisplayPlatformInfo( platformIds[selPlatform], CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE"); DisplayPlatformInfo( platformIds[selPlatform], CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION"); DisplayPlatformInfo( platformIds[selPlatform], CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR"); cl_context_properties contextProperties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformIds[selPlatform], 0 }; errNum = clGetDeviceIDs( platformIds[selPlatform], CL_DEVICE_TYPE_ALL, 0, NULL, numDevices); std::cout << "\tNumber of devices: \t" << *numDevices << std::endl; errNum = clGetDeviceIDs( platformIds[selPlatform], CL_DEVICE_TYPE_ALL, *numDevices, devices, NULL); context = clCreateContext(contextProperties, *numDevices, devices, NULL, NULL, NULL); return context; } cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device, cl_uint deviceNum, cl_device_id *devices) { cl_command_queue commandQueue = NULL; size_t deviceBufferSize = -1; commandQueue = clCreateCommandQueue(context, devices[deviceNum], 0, NULL); if (commandQueue == NULL) { delete[] devices; std::cerr << "Failed to create commandQueue for device 0"; return NULL; } *device = devices[deviceNum]; return commandQueue; } cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName) { cl_int errNum; cl_program program; std::ifstream kernelFile(fileName, std::ios::in); if (!kernelFile.is_open()) { std::cerr << "Failed to open file for reading: " << fileName << std::endl; return NULL; } std::ostringstream oss; oss << kernelFile.rdbuf(); std::string srcStdStr = oss.str(); const char *srcStr = srcStdStr.c_str(); program = clCreateProgramWithSource(context, 1, (const char**)&srcStr, NULL, NULL); if (program == NULL) { std::cerr << "Failed to create CL program from source." << std::endl; return NULL; } errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (errNum != CL_SUCCESS) { char buildLog[16384]; clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buildLog), buildLog, NULL); std::cerr << "Error in kernel: " << std::endl; std::cerr << buildLog; clReleaseProgram(program); return NULL; } return program; } void Cleanup(cl_context context, cl_command_queue commandQueue, cl_program program, cl_kernel kernel, cl_mem memObjects[1]) { for (int i = 0; i < 1; i++) { if (memObjects[i] != 0) clReleaseMemObject(memObjects[i]); } if (commandQueue != 0) clReleaseCommandQueue(commandQueue); if (kernel != 0) clReleaseKernel(kernel); if (program != 0) clReleaseProgram(program); if (context != 0) clReleaseContext(context); } int main(int argc, char** argv) { cl_context context = 0; cl_command_queue commandQueue = 0; cl_program program = 0; cl_device_id device = 0; cl_device_id devices[MAX_DEVICES]; cl_uint numDevices; cl_kernel kernel = 0; cl_mem imgObjects[1] = { 0 }; cl_int errNum; context = CreateContext(&numDevices, devices); if (context == NULL) { std::cerr << "Failed to create OpenCL context." << std::endl; return 1; } commandQueue = CreateCommandQueue(context, &device, 0, devices); if (commandQueue == NULL) { Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } program = CreateProgram(context, device, "../device.cl"); if (program == NULL) { Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } kernel = clCreateKernel(program, "helloWave", NULL); if (kernel == NULL) { std::cerr << "Failed to create kernel" << std::endl; Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } cl_bool imageSupport = CL_FALSE; clGetDeviceInfo(device, CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &imageSupport, NULL); if (imageSupport != CL_TRUE) { std::cerr << "OpenCL device does not support images." << std::endl; Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } cl_image_format clImageFormat; clImageFormat.image_channel_order = CL_BGRA; clImageFormat.image_channel_data_type = CL_UNORM_INT8; imgObjects[0] = clCreateImage2D(context, CL_MEM_WRITE_ONLY, &clImageFormat, N, N, 0, NULL, &errNum); if (imgObjects[0] == NULL) { std::cerr << "Error creating memory objects." << std::endl; return false; } errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), &imgObjects[0]); errNum |= clSetKernelArg(kernel, 1, sizeof(int), &N); if (errNum != CL_SUCCESS) { std::cerr << "Error setting kernel arguments." << std::endl;Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } size_t localWorkSize[2] = { 16, 16 }; size_t globalWorkSize[2] = { RoundUp(localWorkSize[0], N), RoundUp(localWorkSize[1], N) }; LARGE_INTEGER perfFrequency; LARGE_INTEGER performanceCountNDRangeStart; LARGE_INTEGER performanceCountNDRangeStop; QueryPerformanceCounter(&performanceCountNDRangeStart); errNum = clEnqueueNDRangeKernel(commandQueue, kernel, 2, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL); clFinish(commandQueue); QueryPerformanceCounter(&performanceCountNDRangeStop); QueryPerformanceFrequency(&perfFrequency); printf("NDRange performance counter time %f ms.\n", 1000.0f*(float)(performanceCountNDRangeStop.QuadPart - performanceCountNDRangeStart.QuadPart) / (float)perfFrequency.QuadPart); if (errNum != CL_SUCCESS) { std::cerr << "Error queuing kernel for execution." << std::endl; Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } unsigned char* result = new unsigned char[N * N * 4]; size_t origin[3] = { 0, 0, 0 }; size_t region[3] = { N, N, 1 }; errNum = clEnqueueReadImage(commandQueue, imgObjects[0], CL_TRUE, origin, region, 0, 0, result, 0, NULL, NULL); if (errNum != CL_SUCCESS) { std::cerr << "Error reading result buffer." << std::endl; Cleanup(context, commandQueue, program, kernel, imgObjects); return 1; } SaveImage("image.bmp", result, N, N); std::cout << "Executed program succesfully." << std::endl; Cleanup(context, commandQueue, program, kernel, imgObjects); system("pause"); return 0; }