// **************************************************************************** // Method: OpenCLPlatform::OpenCLPlatform // // Purpose: // Constructor. Creates a new OpenCL platform representation for the // specified patform ID. // // Arguments: // clPlatform: the platform to be created // // Returns: // // Note: // // Programmer: Gabriel Marin // Creation: September 22, 2009 // // Modifications: // // **************************************************************************** OpenCLPlatform::OpenCLPlatform (cl::Platform &clPlatform) : Platform<OpenCLDeviceInfo>() { int err; platformName = clPlatform.getInfo<CL_PLATFORM_NAME>(); platformVendor = clPlatform.getInfo<CL_PLATFORM_VENDOR>(); platformVersion = clPlatform.getInfo<CL_PLATFORM_VERSION>(); platformExtensions = clPlatform.getInfo<CL_PLATFORM_EXTENSIONS>(); // query devices std::vector<cl::Device> devs; err = clPlatform.getDevices( CL_DEVICE_TYPE_ALL, &devs ); // I should not print an error message here if no devices are present. // I will just report that there are zero devices. // CL_CHECK_ERROR( err ); for( vector<cl::Device>::iterator diter = devs.begin(); diter != devs.end(); diter++ ) { OpenCLDeviceInfo* openclDevice = new OpenCLDeviceInfo( (*diter)() ); devices.push_back( openclDevice ); ++ deviceCount; } }
void printAllDevices(cl::Platform platform) { // init device std::vector<cl::Device> all_devices; platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices); std::cout << "Finding devices: " << all_devices.size() << std::endl; // walk through devices for (auto &device : all_devices) { std::cout << "CL_DEVICE_NAME: " << device.getInfo<CL_DEVICE_NAME>() << std::endl; std::cout << "CL_DEVICE_TYPE: " << beautyDevType(device.getInfo<CL_DEVICE_TYPE>()) << std::endl; std::cout << "CL_DEVICE_MAX_COMPUTE_UNITS: " << device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << std::endl; std::cout << "CL_DEVICE_MAX_WORK_GROUP_SIZE: " << device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>() << std::endl; std::cout << "CL_DEVICE_MAX_WORK_ITEM_SIZES[0]: " << device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>()[0] << std::endl; std::cout << "CL_DEVICE_GLOBAL_MEM_SIZE: " << beautyMem(device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()) << std::endl; std::cout << "CL_DEVICE_LOCAL_MEM_SIZE: " << beautyMem(device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>()) << std::endl; std::cout << std::endl; } }
void CLHelper::printDevices(cl::Platform platform, cl_device_type deviceType) { cl_int err; std::vector<cl::Device> deviceList; err = platform.getDevices(deviceType, &deviceList); CHECK_OPENCL_ERROR(err, "cl::Platform::getDevices() failed."); int deviceNum; std::vector<cl::Device>::iterator device; for(deviceNum = 0, device = deviceList.begin(); device != deviceList.end(); deviceNum++, device++) { std::string deviceName; err = device->getInfo(CL_DEVICE_NAME, &deviceName); CHECK_OPENCL_ERROR(err, "cl::Device::getInfo() failed."); std::cout << "Device " << deviceNum << " : " << deviceName << std::endl; } }
cl::Device get_device_by_name(cl::Platform& platform, std::string name) { vector<cl::Device> devices; platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); if (devices.size() == 0) { throw new std::runtime_error("No devices found"); } int i = 0; for (auto& device : devices) { const string name2 = device.getInfo<CL_DEVICE_NAME>(); const std::size_t found = name2.find(name); if (found != std::string::npos) { break; } i++; } return devices[i]; }
vector<cl::Device> get_devices(cl::Platform& platform) { vector<cl::Device> devices; platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); return devices; }
CL_helper(const char *kernelFileName, std::vector<std::string> kernelNames, bool loadBinary, bool writeBinary, bool usePreProcArgs = false, std::map<std::string, std::string> preProcArgs = std::map<std::string, std::string>()){ cl::Platform::get(&platforms); if(platforms.size()==0) throw std::runtime_error("No OpenCL platforms found.\n"); int selectedPlatform=0; platform=platforms.at(selectedPlatform); platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); if(devices.size()==0) throw std::runtime_error("No opencl devices found.\n"); int selectedDevice=0; device=devices.at(selectedDevice); context = cl::Context(devices); try{ if(loadBinary){ std::string vendor=platform.getInfo<CL_PLATFORM_VENDOR>(); size_t found = vendor.find("NVIDIA"); if(found != std::string::npos){ FILE* fp; fp = fopen("src/kernels/julia_filter.ptx", "r"); if (!fp) { std::cerr << "Error loading kernel binary" << std::endl; std::cerr << "Building kernel from .cl file" << std::endl; loadBinary = false; } else { fseek(fp, 0, SEEK_END); size_t kernel_sz = ftell(fp); rewind(fp); char* kernel_str = (char*)malloc(kernel_sz); unsigned bytes = fread(kernel_str, 1, kernel_sz, fp); fclose(fp); binaries.push_back(std::make_pair((void*)kernel_str,kernel_sz+1)); program = cl::Program(context, devices, binaries); program.build(devices); } } else{ std::cerr << "Vendor not NVIDIA, cannot load .ptx binary" << std::endl; std::cerr << "Building kernel from .cl file" << std::endl; loadBinary = false; } } if(!loadBinary){ std::string kernelSource=CL_helper::LoadSource(kernelFileName); sources.push_back(std::make_pair(kernelSource.c_str(), kernelSource.size()+1)); program = cl::Program(context, sources); if(usePreProcArgs && !preProcArgs.empty()){ std::string preProcArgsString; for(auto& arg : preProcArgs) { preProcArgsString += "-D" + arg.first + "=" + arg.second + " "; } program.build(devices, preProcArgsString.c_str()); } else { //std::string params = "-cl-unsafe-math-optimizations"; program.build(devices); } } }catch (cl::Error er) { for(unsigned i=0;i<devices.size();i++){ std::cerr <<"Log for device " << devices[i].getInfo<CL_DEVICE_NAME>().c_str()<<std::endl; std::cerr << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[i]).c_str() <<std::endl; } std::cerr << "ERROR:" << er.what() << " Code " << er.err()<<std::endl; throw; } for(unsigned i=0; i<kernelNames.size();i++){ kernels.push_back( cl::Kernel(program, kernelNames.at(i).c_str()) ); } queue = cl::CommandQueue(context, device); if(writeBinary){ size_t bin_sz; program.getInfo( CL_PROGRAM_BINARY_SIZES, &bin_sz); unsigned char *bin = (unsigned char *)malloc(bin_sz); program.getInfo(CL_PROGRAM_BINARIES, &bin); FILE* fp = fopen("src/kernels/julia_filter.ptx", "wb"); fwrite(bin, sizeof(char), bin_sz, fp); fclose(fp); free(bin); } }