EasyOpenCL<T>::EasyOpenCL(bool printData) { info = printData; cl_uint numPlatforms; //the NO. of platforms // Fetch the different platforms on which we can run our kernel cl_platform_id platform = NULL; status = clGetPlatformIDs(0, NULL, &numPlatforms); checkError("clGetPlatformIDs"); // Take the first platform available if (numPlatforms > 0) { cl_platform_id* platforms = (cl_platform_id*) malloc(numPlatforms * sizeof(cl_platform_id)); status = clGetPlatformIDs(numPlatforms, platforms, NULL); platform = platforms[0]; free(platforms); } // Get the devices which are available on said platform cl_uint numDevices = 0; status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices); if (numDevices) { //Use the first GPU available devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id)); status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL); } else { // If there is no GPU support, fall back to the CPU if(info) { std::cout << "No supported GPU device available." << std::endl; std::cout << "Falling back to using the CPU." << std::endl; std::cout << std::endl; } status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices); devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id)); status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, numDevices, devices, NULL); } //Print the data of the selected device if (info) { printDeviceProperty(*devices); } //Create an OpenCL context and a command queue context = clCreateContext(NULL, 1, devices, NULL, NULL, &status); checkError("clCreateContext"); commandQueue = clCreateCommandQueueWithProperties(context, devices[0], 0, &status); checkError("clCreateCommandQueueWithProperties"); }
OpenCLFramework<T>::OpenCLFramework(bool printData) { info = printData; cl_uint numPlatforms; //the NO. of platforms cl_platform_id platform = NULL; //the chosen platform status = clGetPlatformIDs(0, NULL, &numPlatforms); checkError("clGetPlatformIDs"); //Just take the first platform available if (numPlatforms > 0) { cl_platform_id* platforms = (cl_platform_id*)malloc(numPlatforms* sizeof(cl_platform_id)); status = clGetPlatformIDs(numPlatforms, platforms, NULL); platform = platforms[0]; free(platforms); } //Try to get the GPU, if not available, take the CPU cl_uint numDevices = 0; status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices); checkError("clGetDeviceIDs"); if (numDevices == 0) //no GPU available. { std::cout << "No GPU device available." << std::endl; std::cout << "Choose CPU as default device." << std::endl; status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices); devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id)); status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, numDevices, devices, NULL); } else { //Pick the GPU devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id)); status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL); } //Print the data about the picked device if (info) { printDeviceProperty(*devices); } //Create an OpenCL context and a command queue context = clCreateContext(NULL, 1, devices, NULL, NULL, &status); checkError("clCreateContext"); commandQueue = clCreateCommandQueueWithProperties(context, devices[0], 0, &status); checkError("clCreateCommandQueueWithProperties"); }