ProcessingUnit::ProcessingUnit(const DeviceContext *context, std::size_t bitsThread) : context(context), cmdQueue(context->getCommandQueue()) { auto programContext = context->getProgramContext(); auto dlContext = programContext->getDeviceListContext(); auto &clContext = dlContext->getClContext(); auto &device = context->getDevice(); auto bitsGlobal = programContext->getBitsGlobal(); kernel = cl::Kernel(programContext->getProgram(), "des_kernel"); items = std::size_t(1) << (bitsGlobal - bitsThread); resultBits = programContext->getVectorLevel() + bitsGlobal; groupSize = std::min(items, kernel.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(device)); groupCount = BLOCK_COUNT(groupSize, items); cdBaseBufferSize = (56 - bitsGlobal) * programContext->getVectorBytes(); resultBufferSize = (MAX_FOUND_KEYS + 1) * sizeof(cl_uint); cdBaseBuffer = cl::Buffer(clContext, CL_MEM_READ_ONLY, cdBaseBufferSize); resultBuffer = cl::Buffer(clContext, CL_MEM_WRITE_ONLY, resultBufferSize); mappedCdBaseBuffer = cmdQueue.enqueueMapBuffer( cdBaseBuffer, true, CL_MAP_WRITE, 0, cdBaseBufferSize); mappedResultBuffer = nullptr; kernel.setArg<cl::Buffer>(0, programContext->getRefInputBuffer()); kernel.setArg<cl::Buffer>(1, programContext->getRefOutputBuffer()); kernel.setArg<cl::Buffer>(2, cdBaseBuffer); kernel.setArg<cl::Buffer>(3, resultBuffer); kernel.setArg<cl_uint> (4, static_cast<cl_uint>(bitsThread)); }
int main() { int elemnum = 1024 * 1024 * 16; clContext clCxt; getClContext(&clCxt); timeRcd.min_kerneltime=1000000; timeRcd.min_totaltime=1000000; test_scan(&clCxt,elemnum); printf("kernel min kernel time:%lf ",timeRcd.min_kerneltime); printf("kernel min total time:%lf \n",timeRcd.min_totaltime); return 0; }
// Creates an Interop struct int newInterop(struct Interop* result) { result->context = getClContext(&result->deviceId); result->clMems = NULL; memset(&result->clContext, 0, sizeof(struct ClContext)); if (!result->context) { puts("No OpenCL devices found"); return -1; } glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.f, 1.f, 0.f, 1.f, -1.f, 1.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glGenTextures(1, &result->glTexture); glGenBuffers(1, &result->glBuffer); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, result->glTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (PrintErr((int)glGetError())) return -1; cl_int openclError; result->command_queue = clCreateCommandQueue(result->context, result->deviceId, 0, &openclError); if (PrintErr(openclError)) return -1; return 0; }
cl_program getOrBuildProgram(const Context* ctx, const cv::ocl::ProgramEntry* source, const String& options) { cl_int status = 0; cl_program program = NULL; std::vector<char> binary; if (!enable_disk_cache || !readConfigurationFromFile(options, binary)) { program = clCreateProgramWithSource(getClContext(ctx), 1, (const char**)&source->programStr, NULL, &status); openCLVerifyCall(status); cl_device_id device = getClDeviceID(ctx); status = clBuildProgram(program, 1, &device, options.c_str(), NULL, NULL); if(status == CL_SUCCESS) { if (enable_disk_cache) { size_t binarySize; openCLSafeCall(clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &binarySize, NULL)); std::vector<char> binary(binarySize); char* ptr = &binary[0]; openCLSafeCall(clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(char*), &ptr, NULL)); if (!writeConfigurationToFile(options, binary)) { std::cerr << "Can't write data to file: " << fileName_ << std::endl; } } } } else { cl_device_id device = getClDeviceID(ctx); size_t size = binary.size(); const char* ptr = &binary[0]; program = clCreateProgramWithBinary(getClContext(ctx), 1, &device, (const size_t *)&size, (const unsigned char **)&ptr, NULL, &status); openCLVerifyCall(status); status = clBuildProgram(program, 1, &device, options.c_str(), NULL, NULL); } if(status != CL_SUCCESS) { if (status == CL_BUILD_PROGRAM_FAILURE || status == CL_INVALID_BUILD_OPTIONS) { size_t buildLogSize = 0; openCLSafeCall(clGetProgramBuildInfo(program, getClDeviceID(ctx), CL_PROGRAM_BUILD_LOG, 0, NULL, &buildLogSize)); std::vector<char> buildLog; buildLog.resize(buildLogSize); memset(&buildLog[0], 0, buildLogSize); openCLSafeCall(clGetProgramBuildInfo(program, getClDeviceID(ctx), CL_PROGRAM_BUILD_LOG, buildLogSize, &buildLog[0], NULL)); std::cout << std::endl << "BUILD LOG: " << (source->name ? source->name : "dynamic program") << ": " << options << "\n"; std::cout << &buildLog[0] << std::endl; } openCLVerifyCall(status); } return program; }
cl_program getOrBuildProgram(const Context* ctx, const cv::ocl::ProgramEntry* source, const String& options) { cl_int status = 0; cl_program program = NULL; std::vector<char> binary; if (!enable_disk_cache || !readConfigurationFromFile(options, binary)) { program = clCreateProgramWithSource(getClContext(ctx), 1, (const char**)&source->programStr, NULL, &status); openCLVerifyCall(status); cl_device_id device = getClDeviceID(ctx); status = clBuildProgram(program, 1, &device, options.c_str(), NULL, NULL); if(status == CL_SUCCESS) { if (enable_disk_cache) { size_t binarySize; openCLSafeCall(clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &binarySize, NULL)); std::vector<char> binary(binarySize); char* ptr = &binary[0]; openCLSafeCall(clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(char*), &ptr, NULL)); if (!writeConfigurationToFile(options, binary)) { std::cerr << "Can't write data to file: " << fileName_ << std::endl; } } } } else { cl_device_id device = getClDeviceID(ctx); size_t size = binary.size(); const char* ptr = &binary[0]; program = clCreateProgramWithBinary(getClContext(ctx), 1, &device, (const size_t *)&size, (const unsigned char **)&ptr, NULL, &status); openCLVerifyCall(status); status = clBuildProgram(program, 1, &device, options.c_str(), NULL, NULL); } if(status != CL_SUCCESS) { if(status == CL_BUILD_PROGRAM_FAILURE) { cl_int logStatus; char *buildLog = NULL; size_t buildLogSize = 0; logStatus = clGetProgramBuildInfo(program, getClDeviceID(ctx), CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, &buildLogSize); if(logStatus != CL_SUCCESS) std::cout << "Failed to build the program and get the build info." << std::endl; buildLog = new char[buildLogSize]; CV_DbgAssert(!!buildLog); memset(buildLog, 0, buildLogSize); openCLSafeCall(clGetProgramBuildInfo(program, getClDeviceID(ctx), CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, NULL)); std::cout << "\nBUILD LOG: " << options << "\n"; std::cout << buildLog << std::endl; delete [] buildLog; } openCLVerifyCall(status); } return program; }