标签:style c class blog code java
#include <CL/cl.h> #include <iostream> #include <string> #include <fstream> #pragma comment(lib, "OpenCL.lib") const char * loadfile(const char * fileName) { std::ifstream fs(fileName, std::ios::binary); fs.seekg(0, std::ios::end); int size = fs.tellg(); char * data = new char[size + 1]; fs.seekg(0); fs.read(data, size); fs.close(); data[size] = 0; return data; } int main() { cl_platform_id platform; clGetPlatformIDs(1, &platform, NULL); cl_device_id device; clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); cl_command_queue queue = clCreateCommandQueue(context, device, 0, NULL); const char * clSourceFile = loadfile("H:/QtTool/build/TritonRayTracing/kernel.txt"); cl_program program = clCreateProgramWithSource(context, 1, &clSourceFile, NULL, NULL); cl_int result = clBuildProgram(program, 1, &device, NULL, NULL, NULL); if (result) { std::cout << "Error buring compilation" << std::endl; } cl_kernel kernel = clCreateKernel(program, "main", NULL); cl_mem output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 10 * sizeof(cl_int), NULL, 0); cl_mem buffer1 = clCreateBuffer(context, CL_MEM_READ_WRITE, 10 * sizeof(cl_int), NULL, 0); cl_mem buffer2 = clCreateBuffer(context, CL_MEM_READ_WRITE, 10 * sizeof(cl_int), NULL, 0); clSetKernelArg(kernel, 0, sizeof(output), (void *)&output); clSetKernelArg(kernel, 1, sizeof(buffer1), (void *)&buffer1); clSetKernelArg(kernel, 2, sizeof(buffer2), (void *)&buffer2); cl_int * buffer1Ptr = (cl_int *)clEnqueueMapBuffer(queue, buffer1, CL_TRUE, CL_MAP_WRITE, 0, 10 * sizeof(cl_int), 0, NULL, NULL, NULL); cl_int * buffer2Ptr = (cl_int *)clEnqueueMapBuffer(queue, buffer2, CL_TRUE, CL_MAP_WRITE, 0, 10 * sizeof(cl_int), 0, NULL, NULL, NULL); for (int i = 0; i < 10; ++i) { buffer1Ptr[i] = i; buffer2Ptr[i] = i; } clEnqueueUnmapMemObject(queue, buffer1, buffer1Ptr, 0, 0, 0); clEnqueueUnmapMemObject(queue, buffer2, buffer2Ptr, 0, 0, 0); size_t global_work_size = 10; clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL); cl_int * resultBufferPtr = (cl_int *)clEnqueueMapBuffer(queue, output, CL_TRUE, CL_MAP_READ, 0, 10 * sizeof(cl_int), 0, NULL, NULL, NULL); for (int i = 0; i < 10; i++) { std::cout << "ptr[" << i << "] = " << resultBufferPtr[i] << std::endl; } return 0; }
标签:style c class blog code java
原文地址:http://www.cnblogs.com/nmgxbc/p/3754431.html