|
@@ -70,13 +70,13 @@ cl_kernel OpenCL::kernel(string kernelName)
|
|
|
return this->kernelArray[kernelName];
|
|
|
}
|
|
|
|
|
|
-void OpenCL::createKernel(string kernelName)
|
|
|
+void OpenCL::createKernel(string kernelName, int device_idx)
|
|
|
{
|
|
|
cl_kernel kernel = clCreateKernel(this->program, kernelName.c_str(), NULL);
|
|
|
kernelArray[kernelName] = kernel;
|
|
|
|
|
|
// Get the kernel work group size.
|
|
|
- clGetKernelWorkGroupInfo(kernelArray[kernelName], device_id[0], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &lwsize, NULL);
|
|
|
+ clGetKernelWorkGroupInfo(kernelArray[kernelName], device_id[device_idx], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &lwsize, NULL);
|
|
|
if (lwsize == 0)
|
|
|
{
|
|
|
cout << "Error: clGetKernelWorkGroupInfo() returned a max work group size of zero!" << endl;
|
|
@@ -103,7 +103,7 @@ void OpenCL::createKernel(string kernelName)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void OpenCL::buildKernel()
|
|
|
+void OpenCL::buildKernel(int device_idx)
|
|
|
{
|
|
|
/* Load the source code for all of the kernels into the array source_str */
|
|
|
FILE* theFile;
|
|
@@ -155,10 +155,10 @@ void OpenCL::buildKernel()
|
|
|
char* build_log;
|
|
|
size_t log_size;
|
|
|
// First call to know the proper size
|
|
|
- clGetProgramBuildInfo(program, device_id[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
|
|
+ clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
|
|
build_log = new char[log_size + 1];
|
|
|
// Second call to get the log
|
|
|
- clGetProgramBuildInfo(program, device_id[0], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
|
|
|
+ clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
|
|
|
build_log[log_size] = '\0';
|
|
|
cout << build_log << endl;
|
|
|
delete[] build_log;
|
|
@@ -179,10 +179,10 @@ void OpenCL::buildKernel()
|
|
|
char* build_log;
|
|
|
size_t log_size;
|
|
|
// First call to know the proper size
|
|
|
- clGetProgramBuildInfo(program, device_id[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
|
|
+ clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
|
|
build_log = new char[log_size + 1];
|
|
|
// Second call to get the log
|
|
|
- clGetProgramBuildInfo(program, device_id[0], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
|
|
|
+ clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
|
|
|
build_log[log_size] = '\0';
|
|
|
cout << build_log << endl;
|
|
|
delete[] build_log;
|
|
@@ -192,15 +192,21 @@ void OpenCL::buildKernel()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void OpenCL::getDevices(cl_device_type deviceType)
|
|
|
+void OpenCL::getDevices(int platform_idx, int device_idx, cl_device_type deviceType)
|
|
|
{
|
|
|
cl_uint platforms_n = 0;
|
|
|
cl_uint devices_n = 0;
|
|
|
+
|
|
|
+ // create OpenCL context
|
|
|
+ clGetPlatformIDs(0, NULL, &platforms_n);
|
|
|
+
|
|
|
+ cl_platform_id *platforms_ids;
|
|
|
+ platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platforms_n);
|
|
|
|
|
|
/* The following code queries the number of platforms and devices, and
|
|
|
* lists the information about both.
|
|
|
*/
|
|
|
- clGetPlatformIDs(100, platform_id, &platforms_n);
|
|
|
+ clGetPlatformIDs(platforms_n, platforms_ids, NULL);
|
|
|
if (VERBOSE)
|
|
|
{
|
|
|
printf("\n=== %d OpenCL platform(s) found: ===\n", platforms_n);
|
|
@@ -208,26 +214,27 @@ void OpenCL::getDevices(cl_device_type deviceType)
|
|
|
{
|
|
|
char buffer[10240];
|
|
|
printf(" -- %d --\n", i);
|
|
|
- clGetPlatformInfo(platform_id[i], CL_PLATFORM_PROFILE, 10240, buffer,
|
|
|
+ clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_PROFILE, 10240, buffer,
|
|
|
NULL);
|
|
|
printf(" PROFILE = %s\n", buffer);
|
|
|
- clGetPlatformInfo(platform_id[i], CL_PLATFORM_VERSION, 10240, buffer,
|
|
|
+ clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VERSION, 10240, buffer,
|
|
|
NULL);
|
|
|
printf(" VERSION = %s\n", buffer);
|
|
|
- clGetPlatformInfo(platform_id[i], CL_PLATFORM_NAME, 10240, buffer, NULL);
|
|
|
+ clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_NAME, 10240, buffer, NULL);
|
|
|
printf(" NAME = %s\n", buffer);
|
|
|
- clGetPlatformInfo(platform_id[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL);
|
|
|
+ clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL);
|
|
|
printf(" VENDOR = %s\n", buffer);
|
|
|
- clGetPlatformInfo(platform_id[i], CL_PLATFORM_EXTENSIONS, 10240, buffer,
|
|
|
+ clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_EXTENSIONS, 10240, buffer,
|
|
|
NULL);
|
|
|
printf(" EXTENSIONS = %s\n", buffer);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ printf("DEBUG %d \n", platform_idx);
|
|
|
|
|
|
- clGetDeviceIDs(platform_id[1], deviceType, 100, device_id, &devices_n);
|
|
|
+ clGetDeviceIDs(platforms_ids[platform_idx], deviceType, 100, device_id, &devices_n);
|
|
|
if (VERBOSE)
|
|
|
{
|
|
|
- printf("Using the default platform (platform 0)...\n\n");
|
|
|
printf("=== %d OpenCL device(s) found on platform:\n", devices_n);
|
|
|
for (int i = 0; i < devices_n; i++)
|
|
|
{
|
|
@@ -265,16 +272,15 @@ void OpenCL::getDevices(cl_device_type deviceType)
|
|
|
printf("\n");
|
|
|
}
|
|
|
|
|
|
- // Create an OpenCL context.
|
|
|
- context = clCreateContext(NULL, devices_n, device_id, NULL, NULL, &ret);
|
|
|
- if (ret != CL_SUCCESS)
|
|
|
- {
|
|
|
- printf("\nError at clCreateContext! Error code %i\n\n", ret);
|
|
|
- exit(1);
|
|
|
- }
|
|
|
+ cl_context_properties ctxprop[] = {CL_CONTEXT_PLATFORM, (cl_context_properties) platforms_ids[platform_idx], 0};
|
|
|
+ context = clCreateContextFromType(ctxprop, deviceType, NULL, NULL, NULL);
|
|
|
+ if (!context) {
|
|
|
+ printf("ERROR: clCreateContextFromType failed\n");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
|
|
|
// Create a command queue.
|
|
|
- command_queue = clCreateCommandQueue(context, device_id[0], 0, &ret);
|
|
|
+ command_queue = clCreateCommandQueue(context, device_id[device_idx], 0, &ret);
|
|
|
if (ret != CL_SUCCESS)
|
|
|
{
|
|
|
printf("\nError at clCreateCommandQueue! Error code %i\n\n", ret);
|
|
@@ -282,12 +288,12 @@ void OpenCL::getDevices(cl_device_type deviceType)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void OpenCL::init(int isGPU)
|
|
|
+void OpenCL::init(int platform_idx, int device_idx, int use_gpu)
|
|
|
{
|
|
|
- if (isGPU)
|
|
|
- getDevices(CL_DEVICE_TYPE_GPU);
|
|
|
+ if (use_gpu)
|
|
|
+ getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_GPU);
|
|
|
else
|
|
|
- getDevices(CL_DEVICE_TYPE_CPU);
|
|
|
+ getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_CPU);
|
|
|
|
|
|
- buildKernel();
|
|
|
+ buildKernel(device_idx);
|
|
|
}
|