OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-CompactHash commits / 32885d5

Updating gpu detection for new opencl
32885d523b1df73f661c303509fbfa6e6e1eda53
by brobey <brobey@lanl.gov> · 2019-04-14T03:21:08.000Z

Changed files (2) +260 -57

gpu.c +258 -57
……@@ -57,78 +57,147 @@ typedef cl_float4 cl_real4;
5757#endif
5858
5959#ifndef DEVICE_DETECT_DEBUG
60#define DEVICE_DETECT_DEBUG 1
60#define DEVICE_DETECT_DEBUG 0
6161#endif
6262
6363#ifdef HAVE_OPENCL
6464void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_program *program, char *filename, char *addlibsource) {
65 printf("============== Hardware detection report ===============\n");
65 if (DEVICE_DETECT_DEBUG){
66 printf("============== Hardware detection report ===============\n");
67 }
6668
67 cl_platform_id* platform;
69 cl_platform_id* platforms;
70 cl_platform_id platform = NULL;
6871 cl_uint num_platforms;
6972 cl_uint num_devices;
70 cl_device_id* device;
73 cl_device_id* devices;
74 int *device_appropriate;
75 int device_selected = -99;
76 cl_int platform_selected = -1;
7177 //cl_program program;
7278 cl_int error = 0;
7379
7480 error = clGetPlatformIDs(0, NULL, &num_platforms);
75 printf("\nGPUInit: %d opencl platform(s) detected\n",num_platforms);
76 if(error != CL_SUCCESS) {
77 printf("Error getting number of platforms\n");
78 exit(error);
81 if (DEVICE_DETECT_DEBUG){
82 printf("\nGPUInit: %d opencl platform(s) detected\n",num_platforms);
7983 }
80 platform = (cl_platform_id*)malloc(num_platforms*sizeof(cl_platform_id));
81 error = clGetPlatformIDs(num_platforms, platform, 0);
8284 if(error != CL_SUCCESS) {
83 printf("Error getting platform id\n");
84 exit(error);
85 printf("GPU_INIT: Error with clGetPlatformIDs call in file %s at line %d\n", __FILE__, __LINE__);
86 if (error == CL_INVALID_VALUE){
87 printf("GPU_INIT: Invalid value in clGetPlatformID call\n");
88 }
89 exit(error);
90 }
91 if (num_platforms == 0) {
92 printf("GPU_INIT: Error -- No opencl platforms detected in file %s at line %d\n", __FILE__, __LINE__);
93 exit(-1);
94 }
95 if (DEVICE_DETECT_DEBUG){
96 printf("\n\nGPU_INIT: %d opencl platform(s) detected\n",num_platforms);
97 }
98
99 platforms = (cl_platform_id*)malloc(num_platforms*sizeof(cl_platform_id));
100 error = clGetPlatformIDs(num_platforms, platforms, NULL);
101 if (error != CL_SUCCESS){
102 printf("GPU_INIT: Error with clGetPlatformIDs call in file %s at line %d\n", __FILE__, __LINE__);
103 if (error == CL_INVALID_VALUE){
104 printf("Invalid value in clGetPlatformID call\n");
105 }
106 }
107
108 if (DEVICE_DETECT_DEBUG){
109 char info[1024];
110 for (uint iplatform=0; iplatform<num_platforms; iplatform++){
111 printf(" Platform %d:\n",iplatform+1);
112
113 //clGetPlatformInfo(platforms[iplatform],CL_PLATFORM_PROFILE, 1024L,info,0);
114 //printf(" CL_PLATFORM_PROFILE : %s\n",info);
115
116 clGetPlatformInfo(platforms[iplatform],CL_PLATFORM_VERSION, 1024L,info,0);
117 printf(" CL_PLATFORM_VERSION : %s\n",info);
118
119 clGetPlatformInfo(platforms[iplatform],CL_PLATFORM_NAME, 1024L,info,0);
120 printf(" CL_PLATFORM_NAME : %s\n",info);
121
122 clGetPlatformInfo(platforms[iplatform],CL_PLATFORM_VENDOR, 1024L,info,0);
123 printf(" CL_PLATFORM_VENDOR : %s\n",info);
124
125 //clGetPlatformInfo(platforms[iplatform],CL_PLATFORM_EXTENSIONS,1024L,info,0);
126 //printf(" CL_PLATFORM_EXTENSIONS : %s\n",info);
127 }
128 printf("\n");
85129 }
130
86131 char info[1024];
87 clGetPlatformInfo(platform[0],CL_PLATFORM_VENDOR, 1024, info, 0);
132 clGetPlatformInfo(platforms[0],CL_PLATFORM_VENDOR, 1024, info, 0);
88133
89 error = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
90 if (error != CL_SUCCESS) error = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_ACCELERATOR, 0, NULL, &num_devices);
91 if(error != CL_SUCCESS) {
92 printf("Error getting device ids1\n");
93 exit(error);
134 // Get the number of devices, allocate, and get the devices
135 for (uint iplatform=0; iplatform<num_platforms; iplatform++){
136 error = clGetDeviceIDs(platforms[iplatform],CL_DEVICE_TYPE_GPU,0,NULL,&num_devices);
137 if (error == CL_DEVICE_NOT_FOUND) {
138 if (DEVICE_DETECT_DEBUG) {
139 printf("Warning: Device of requested type not found for platform %d in clGetDeviceID call\n",iplatform);
140 }
141 continue;
142 }
143 if (error != CL_SUCCESS) {
144 /* Possible Errors
145 * CL_INVALID_PLATFORM:
146 * CL_INVALID_DEVICE_TYPE:
147 * CL_INVALID_VALUE:
148 * CL_DEVICE_NOT_FOUND:
149 */
150 printf("GPU_INIT clGetDeviceIDs error %d file %s line %d\n", error, __FILE__, __LINE__);
151 }
152 if (DEVICE_DETECT_DEBUG){
153 printf("GPU_INIT: %d opencl devices(s) detected\n",num_devices);
154 }
155 platform_selected = iplatform;
156 platform = platforms[iplatform];
157 //nDevices_selected = nDevices;
94158 }
95 device = (cl_device_id*)malloc(num_devices*sizeof(cl_device_id));
159
160 if (platform_selected == -1){
161 printf("Warning: Device of requested type not found in clGetDeviceID call\n");
162 exit(-1);
163 }
164
165 devices = (cl_device_id*)malloc(num_devices*sizeof(cl_device_id));
166 device_appropriate = malloc(num_devices*sizeof(int));
96167
97 error = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_GPU, num_devices, device, 0);
98 if(error != CL_SUCCESS) error = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_ACCELERATOR, num_devices, device, 0);
168 error = clGetDeviceIDs(platforms[platform_selected], CL_DEVICE_TYPE_GPU, num_devices, devices, NULL);
99169 if(error != CL_SUCCESS) {
100170 printf("Error getting device ids\n");
101171 exit(error);
102172 }
103
104 // char platform_info[1024];
105 for (int iplatform=0; iplatform<num_platforms; iplatform++){
106 printf(" Platform %d:\n",iplatform+1);
107
108 //clGetPlatformInfo(platform[iplatform],CL_PLATFORM_PROFILE, 1024L,info,0);
109 //printf(" CL_PLATFORM_PROFILE : %s\n",info);
110
111 clGetPlatformInfo(platform[iplatform],CL_PLATFORM_VERSION, 1024L,info,0);
112 printf(" CL_PLATFORM_VERSION : %s\n",info);
113
114 clGetPlatformInfo(platform[iplatform],CL_PLATFORM_NAME, 1024L,info,0);
115 printf(" CL_PLATFORM_NAME : %s\n",info);
116
117 clGetPlatformInfo(platform[iplatform],CL_PLATFORM_VENDOR, 1024L,info,0);
118 printf(" CL_PLATFORM_VENDOR : %s\n",info);
119
120 //clGetPlatformInfo(platform[iplatform],CL_PLATFORM_EXTENSIONS,1024L,info,0);
121 //printf(" CL_PLATFORM_EXTENSIONS : %s\n",info);
173
174 int idevice_appropriate = 0;
175 for (uint idevice=0; idevice<num_devices; idevice++){
176 device_appropriate[idevice] = device_double_support(devices[idevice]);;
177 if (device_appropriate[idevice] == 1){
178 if (device_selected == -99) device_selected = idevice;
179 devices[idevice_appropriate] = devices[idevice];
180 idevice_appropriate++;
181 }
182 if (DEVICE_DETECT_DEBUG){
183 printf( " Device %d:\n", idevice+1);
184 device_info(devices[idevice]);
185 }
122186 }
123
187 num_devices = idevice_appropriate;
188
124189 *device_type = VENDOR_UNKNOWN;
125190 char *device_string[] = {"Unknown", "Nvidia", "Tesla K20Xm", "Tesla M2090",
126191 "Tesla 2050/2070", "GeForce GT 650M", "ATI", "MIC"};
127192
128 printf("\n");
129 printf(" Device:\n");
130 clGetDeviceInfo(device[0], CL_DEVICE_NAME, sizeof(info), &info, NULL);
131 printf(" CL_DEVICE_NAME : %s\n",info);
193 if (DEVICE_DETECT_DEBUG){
194 printf("\n");
195 printf(" Device:\n");
196 }
197 clGetDeviceInfo(devices[0], CL_DEVICE_NAME, sizeof(info), &info, NULL);
198 if (DEVICE_DETECT_DEBUG){
199 printf(" CL_DEVICE_NAME : %s\n",info);
200 }
132201
133202 if (! strncmp(info,"Intel(R) Many Integrated Core",29) ) *device_type = MIC;
134203 if (! strncmp(info,"Tesla K20Xm",11) ) *device_type = TESLA_K20Xm;
……@@ -136,8 +205,10 @@ void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_
136205 if (! strncmp(info,"Tesla 2050 / 2070",17) ) *device_type = TESLA_2050_2070;
137206 if (! strncmp(info,"GeForce GT 650M",15) ) *device_type = GeFORCE_GT_650M;
138207
139 clGetDeviceInfo(device[0], CL_DEVICE_VENDOR, sizeof(info), &info, NULL);
140 printf(" CL_DEVICE_VENDOR : %s\n",info);
208 clGetDeviceInfo(devices[0], CL_DEVICE_VENDOR, sizeof(info), &info, NULL);
209 if (DEVICE_DETECT_DEBUG){
210 printf(" CL_DEVICE_VENDOR : %s\n",info);
211 }
141212
142213 if (*device_type == VENDOR_UNKNOWN){
143214 if (! strncmp(info,"ATI",5) ) *device_type = ATI;
……@@ -145,24 +216,35 @@ void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_
145216 }
146217
147218 size_t isize;
148 clGetDeviceInfo(device[0], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(isize), &isize, NULL);
149 printf(" CL_DEVICE_MAX_WORK_GROUP_SIZE : %lu\n",isize);
150 printf("\n");
219 clGetDeviceInfo(devices[0], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(isize), &isize, NULL);
220 if (DEVICE_DETECT_DEBUG){
221 printf(" CL_DEVICE_MAX_WORK_GROUP_SIZE : %lu\n",isize);
222 printf("\n");
151223
152 printf(" device vendor type set : %s\n",device_string[*device_type]);
224 printf(" device vendor type set : %s\n",device_string[*device_type]);
225 }
226
227 cl_context_properties context_properties[3]=
228 {
229 CL_CONTEXT_PLATFORM,
230 (cl_context_properties)platform,
231 0 // 0 terminates list
232 };
153233
154 *context = clCreateContext(0, 1, device, NULL, NULL, &error);
234 *context = clCreateContext(context_properties, num_devices, devices, NULL, NULL, &error);
155235 if(error != CL_SUCCESS) {
156236 printf("Error creating context\n");
157237 exit(error);
158238 }
159 *queue = clCreateCommandQueue(*context, device[0], CL_QUEUE_PROFILING_ENABLE, &error);
239 *queue = clCreateCommandQueue(*context, devices[0], CL_QUEUE_PROFILING_ENABLE, &error);
160240 if(error != CL_SUCCESS) {
161241 printf("Error creating command queue\n");
162242 exit(error);
163243 }
164244
165 printf("============== Hardware detection report ===============\n");
245 if (DEVICE_DETECT_DEBUG){
246 printf("============== Hardware detection report ===============\n");
247 }
166248
167249 // Load the kernel source code into the array source
168250 struct stat statbuf;
……@@ -224,11 +306,11 @@ void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_
224306 if (*device_type == TESLA_2050_2070) strcat(CompileString, " -DIS_NVIDIA");
225307 if (*device_type == GeFORCE_GT_650M) strcat(CompileString, " -DIS_NVIDIA");
226308
227 error = clBuildProgram(*program, 1, device, CompileString, NULL, NULL);
309 error = clBuildProgram(*program, 1, devices, CompileString, NULL, NULL);
228310
229311 if (error != CL_SUCCESS){
230312 printf("clBuildProgram returned an error %d at line %d in file %s\n", error,__LINE__,__FILE__);
231 error = clGetProgramBuildInfo(*program, device[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &nReportSize);
313 error = clGetProgramBuildInfo(*program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &nReportSize);
232314 if (error != CL_SUCCESS) {
233315 switch (error){
234316 case CL_INVALID_DEVICE:
……@@ -245,7 +327,7 @@ void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_
245327
246328 BuildReport = (char *)malloc(nReportSize);
247329
248 error = clGetProgramBuildInfo(*program, device[0], CL_PROGRAM_BUILD_LOG, nReportSize, BuildReport, NULL);
330 error = clGetProgramBuildInfo(*program, devices[0], CL_PROGRAM_BUILD_LOG, nReportSize, BuildReport, NULL);
249331 if (error != CL_SUCCESS) {
250332 switch (error){
251333 case CL_INVALID_DEVICE:
……@@ -262,4 +344,123 @@ void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_
262344 printf("%s\n", BuildReport);
263345 }
264346}
347
348int device_double_support(cl_device_id device){
349 int have_double = 0;
350 char info[1024];
351
352 clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, sizeof(info), &info, NULL);
353
354 if (!(strstr(info,"cl_khr_fp64") == NULL)){
355 if (DEVICE_DETECT_DEBUG){
356 printf( " Device has double : %s\n\n", strstr(info,"cl_khr_fp64"));
357 }
358 have_double = 1;
359 }
360
361 return(have_double);
362}
363
364void device_info(cl_device_id device){
365 if (device == NULL) {
366 printf(" Error with device in device_info\n");
367 }
368 char info[1024];
369 cl_bool iflag;
370 cl_uint inum;
371 size_t isize;
372 cl_ulong ilong;
373 cl_device_type device_type;
374 cl_command_queue_properties iprop;
375
376 clGetDeviceInfo(device,CL_DEVICE_TYPE,sizeof(device_type),&device_type,0);
377 if( device_type & CL_DEVICE_TYPE_CPU )
378 printf(" CL_DEVICE_TYPE : %s\n", "CL_DEVICE_TYPE_CPU");
379 if( device_type & CL_DEVICE_TYPE_GPU )
380 printf(" CL_DEVICE_TYPE : %s\n", "CL_DEVICE_TYPE_GPU");
381 if( device_type & CL_DEVICE_TYPE_ACCELERATOR )
382 printf(" CL_DEVICE_TYPE : %s\n", "CL_DEVICE_TYPE_ACCELERATOR");
383 if( device_type & CL_DEVICE_TYPE_DEFAULT )
384 printf(" CL_DEVICE_TYPE : %s\n", "CL_DEVICE_TYPE_DEFAULT");
385
386 clGetDeviceInfo(device,CL_DEVICE_AVAILABLE,sizeof(iflag),&iflag,0);
387 if (iflag == CL_TRUE) {
388 printf( " CL_DEVICE_AVAILABLE : TRUE\n");
389 } else {
390 printf( " CL_DEVICE_AVAILABLE : FALSE\n");
391 }
392
393 clGetDeviceInfo(device, CL_DEVICE_VENDOR, sizeof(info), &info, NULL);
394 printf( " CL_DEVICE_VENDOR : %s\n", info);
395
396 clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(info), &info, NULL);
397 printf( " CL_DEVICE_NAME : %s\n", info);
398
399 clGetDeviceInfo(device, CL_DRIVER_VERSION, sizeof(info), &info, NULL);
400 printf( " CL_DRIVER_VERSION : %s\n", info);
401
402 clGetDeviceInfo(device, CL_DEVICE_VERSION, sizeof(info), &info, NULL);
403 printf( " CL_DEVICE_VERSION : %s\n", info);
404
405 clGetDeviceInfo(device,CL_DEVICE_MAX_COMPUTE_UNITS,sizeof(inum),&inum,0);
406 printf( " CL_DEVICE_MAX_COMPUTE_UNITS : %d\n", inum);
407
408 clGetDeviceInfo(device,CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,sizeof(inum),&inum,0);
409 printf( " CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS : %d\n", inum);
410
411 size_t *item_sizes = (size_t *)malloc(inum*sizeof(size_t));
412 clGetDeviceInfo(device,CL_DEVICE_MAX_WORK_ITEM_SIZES,sizeof(item_sizes),item_sizes,0);
413 printf( " CL_DEVICE_MAX_WORK_ITEM_SIZES : %ld %ld %ld\n",
414 item_sizes[0], item_sizes[1], item_sizes[2]);
415 free(item_sizes);
416
417 clGetDeviceInfo(device,CL_DEVICE_MAX_WORK_GROUP_SIZE,sizeof(isize),&isize,0);
418 printf( " CL_DEVICE_MAX_WORK_GROUP_SIZE : %ld\n", isize);
419
420 clGetDeviceInfo(device,CL_DEVICE_MAX_CLOCK_FREQUENCY,sizeof(inum),&inum,0);
421 printf( " CL_DEVICE_MAX_CLOCK_FREQUENCY : %d\n", inum);
422
423 clGetDeviceInfo(device,CL_DEVICE_MAX_MEM_ALLOC_SIZE,sizeof(inum),&inum,0);
424 printf( " CL_DEVICE_MAX_MEM_ALLOC_SIZE : %d\n", inum);
425
426#ifdef __APPLE_CC__
427 clGetDeviceInfo(device,CL_DEVICE_GLOBAL_MEM_SIZE,sizeof(ilong),&ilong,0);
428 printf( " CL_DEVICE_GLOBAL_MEM_SIZE : %llu\n", ilong);
429
430 clGetDeviceInfo(device,CL_DEVICE_GLOBAL_MEM_CACHE_SIZE,sizeof(ilong),&ilong,0);
431 printf( " CL_DEVICE_GLOBAL_MEM_CACHE_SIZE : %llu\n", ilong);
432#else
433 clGetDeviceInfo(device,CL_DEVICE_GLOBAL_MEM_SIZE,sizeof(ilong),&ilong,0);
434 printf( " CL_DEVICE_GLOBAL_MEM_SIZE : %lu\n", ilong);
435
436 clGetDeviceInfo(device,CL_DEVICE_GLOBAL_MEM_CACHE_SIZE,sizeof(ilong),&ilong,0);
437 printf( " CL_DEVICE_GLOBAL_MEM_CACHE_SIZE : %lu\n", ilong);
438#endif
439 clGetDeviceInfo(device,CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE,sizeof(inum),&inum,0);
440 printf( " CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE : %d\n", inum);
441
442 clGetDeviceInfo(device,CL_DEVICE_MAX_CONSTANT_ARGS,sizeof(inum),&inum,0);
443 printf( " CL_DEVICE_GLOBAL_MAX_CONSTANT_ARGS : %d\n", inum);
444
445 clGetDeviceInfo(device,CL_DEVICE_ERROR_CORRECTION_SUPPORT,sizeof(iflag),&iflag,0);
446 if (iflag == CL_TRUE) {
447 printf( " CL_DEVICE_ERROR_CORRECTION_SUPPORT : TRUE\n");
448 } else {
449 printf( " CL_DEVICE_ERROR_CORRECTION_SUPPORT : FALSE\n");
450 }
451
452 clGetDeviceInfo(device,CL_DEVICE_PROFILING_TIMER_RESOLUTION,sizeof(isize),&isize,0);
453 printf( " CL_DEVICE_PROFILING_TIMER_RESOLUTION : %ld nanosecs\n", isize);
454
455 clGetDeviceInfo(device,CL_DEVICE_QUEUE_PROPERTIES,sizeof(iprop),&iprop,0);
456 if (iprop & CL_QUEUE_PROFILING_ENABLE) {
457 printf( " CL_DEVICE_QUEUE PROFILING : AVAILABLE\n");
458 } else {
459 printf( " CL_DEVICE_QUEUE PROFILING : NOT AVAILABLE\n");
460 }
461
462 clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, sizeof(info), &info, NULL);
463 printf( " CL_DEVICE_EXTENSIONS : %s\n\n", info);
464
465}
265466#endif
gpu.h +2 -0
……@@ -66,6 +66,8 @@ enum device_vendor_type {
6666cl_kernel interpolate_kernel;
6767
6868void GPUInit(cl_context *context, cl_command_queue *queue, int *device_type, cl_program *program, char *filename, char *addlibsource);
69int device_double_support(cl_device_id device);
70void device_info(cl_device_id device);
6971#endif
7072
7173#endif // __GPU_H__