-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencl_target.h
113 lines (90 loc) · 3.73 KB
/
opencl_target.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//-----------------------------------------------------------------------------
// Copyright (c) 2017 Hugues Evrard, MIT License
#ifndef __OPENCL_TARGET__
#define __OPENCL_TARGET__
#ifdef __cplusplus
extern "C" {
#endif
#include <CL/cl.h>
#include <stdlib.h> // getenv, abort
#include <string.h> // strstr
#include <stdio.h> // printf
#define OPENCL_TARGET_CL_CALL(func, ...) do { \
cl_int __cl_err = 0; \
__cl_err = func(__VA_ARGS__); \
if (__cl_err != CL_SUCCESS) { \
fprintf(stderr, "%s:%d OPENCL_TARGET PANIC: OpenCL primitive failed: %s\n", __FILE__, __LINE__, #func ); \
abort(); \
} \
} while (0)
//-----------------------------------------------------------------------------
typedef struct {
cl_platform_id platform_id;
cl_device_id device_id;
} opencl_target;
//-----------------------------------------------------------------------------
void opencl_target_init(opencl_target *target)
{
cl_platform_id *platforms = NULL;
cl_device_id *devices = NULL;
char *device_version = NULL;
if (target == NULL) {
fprintf(stderr, "OPENCL_TARGET PANIC: opencl_target_device_id() is called with NULL pointer as argument\n");
abort();
}
target->platform_id = NULL;
target->device_id = NULL;
char *envname;
envname = getenv("OPENCL_TARGET_DEVICE");
if (envname == NULL) {
envname = "";
}
cl_uint num_platforms = 0;
OPENCL_TARGET_CL_CALL (clGetPlatformIDs, 0, NULL, &num_platforms);
if (num_platforms <= 0) {
goto opencl_target_exit;
}
platforms = malloc(num_platforms * sizeof(cl_platform_id));
OPENCL_TARGET_CL_CALL (clGetPlatformIDs, num_platforms, platforms, NULL);
for (cl_uint i = 0; i < num_platforms; i++) {
target->platform_id = platforms[i];
cl_uint num_devices = 0;
OPENCL_TARGET_CL_CALL (clGetDeviceIDs, target->platform_id, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
if (num_devices <= 0) {
continue;
}
devices = realloc(devices, num_devices * sizeof(cl_device_id));
OPENCL_TARGET_CL_CALL (clGetDeviceIDs, target->platform_id, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
for (cl_uint j = 0; j < num_devices; j++) {
target->device_id = devices[j];
size_t device_version_size = 0;
OPENCL_TARGET_CL_CALL (clGetDeviceInfo, target->device_id, CL_DEVICE_VERSION, 0, NULL, &device_version_size);
if (device_version_size <= 0) {
continue;
}
device_version = realloc(device_version, device_version_size);
if (device_version == NULL) {
goto opencl_target_exit;
}
OPENCL_TARGET_CL_CALL (clGetDeviceInfo, target->device_id, CL_DEVICE_VERSION, device_version_size, device_version, NULL);
int name_match = ( strstr(device_version, envname) != NULL );
free(device_version);
device_version = NULL;
if (name_match) {
goto opencl_target_exit;
}
}
}
// reaching here means no device was found;
target->device_id = NULL;
opencl_target_exit:
if (platforms != NULL) { free(platforms); }
if (devices != NULL) { free(devices); }
if (device_version != NULL) { free(device_version); }
}
#undef OPENCL_TARGET_CL_CALL
#endif // __OPENCL_TARGET__
#ifdef __cplusplus
}
#endif
//-----------------------------------------------------------------------------