Skip to content

Commit ceec574

Browse files
committed
Add possibility to override device ID within a platform at run-time
Fix memory leaks in utility functions
1 parent 3fe7b2a commit ceec574

4 files changed

+66
-33
lines changed

common/util.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ inline static void device_info_ulongarray(cl_device_id device, cl_device_info pa
7171
fprintf(stderr, "%lu, ", size[i]);
7272
}
7373
fprintf(stderr, "\n");
74+
75+
free(size);
7476
}
7577

7678
// Displays available platforms and devices
@@ -124,7 +126,9 @@ inline static void display_device_info(cl_platform_id** platforms, cl_uint* plat
124126
device_info_ulong(devices[j] , CL_DEVICE_LOCAL_MEM_SIZE , "LOCAL_MEM_SIZE" );
125127
device_info_device_type(devices[j], CL_DEVICE_TYPE , "TYPE" );
126128
fprintf(stderr, "================================================================================\n\n");
127-
}
129+
}
130+
131+
free(devices);
128132
}
129133
}
130134
}
@@ -198,7 +202,7 @@ inline static void select_device_type(cl_device_type* device_type)
198202
}
199203

200204
// Validates device type selection and exports context properties
201-
inline static void validate_selection(cl_platform_id* platforms, cl_uint* platformCount, cl_context_properties* ctxprop, cl_device_type* device_type)
205+
inline static void validate_selection(cl_platform_id* platforms, cl_uint* platformCount, cl_context_properties* ctxprop, cl_device_type* device_type, cl_uint deviceID)
202206
{
203207
unsigned i;
204208
cl_int error;
@@ -226,11 +230,18 @@ inline static void validate_selection(cl_platform_id* platforms, cl_uint* platfo
226230
exit(-1);
227231
}
228232
}
233+
else if (deviceID >= deviceCount)
234+
{
235+
fprintf(stderr, "================================================================================\n");
236+
fprintf(stderr, "Platform number: %d\n", i);
237+
fprintf(stderr, "No matching device ID, moving to next platform, if any.\n");
238+
fprintf(stderr, "================================================================================\n\n");
239+
}
229240
else
230241
{
231242
devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
232243
CL_SAFE_CALL( clGetDeviceIDs(platforms[i], *device_type, deviceCount, devices, NULL) );
233-
CL_SAFE_CALL( clGetDeviceInfo(devices[0], CL_DEVICE_NAME, STRING_BUFFER_LEN, &deviceName, NULL) );
244+
CL_SAFE_CALL( clGetDeviceInfo(devices[deviceID], CL_DEVICE_NAME, STRING_BUFFER_LEN, &deviceName, NULL) );
234245

235246
fprintf(stderr, "================================================================================\n");
236247
fprintf(stderr, "Selected platform number: %d\n", i);
@@ -242,6 +253,8 @@ inline static void validate_selection(cl_platform_id* platforms, cl_uint* platfo
242253
ctxprop[0] = CL_CONTEXT_PLATFORM;
243254
ctxprop[1] = (cl_context_properties)platforms[i];
244255
ctxprop[2] = 0;
256+
257+
free(devices);
245258
break;
246259
}
247260
}
@@ -374,6 +387,8 @@ inline static void clBuildProgram_SAFE(cl_program program, cl_uint num_devices,
374387
fprintf(stderr, "Build options: %s\n\n", options);
375388
fprintf(stderr, "%s", log);
376389
fprintf(stderr, "================================ End of compilation log ================================\n");
390+
391+
free(log);
377392
}
378393
exit(-1);
379394
}

fpga-mem-bench-host-blk2d.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static cl_command_queue queue;
3535
static cl_command_queue queue_read, queue_write;
3636
#endif
3737
static cl_device_id* deviceList;
38-
static cl_int deviceCount;
38+
static cl_uint deviceCount;
39+
static cl_uint deviceID = 0; // default to first device
3940

4041
static inline void init()
4142
{
@@ -48,7 +49,7 @@ static inline void init()
4849

4950
display_device_info(&platforms, &platformCount);
5051
select_device_type(&deviceType);
51-
validate_selection(platforms, &platformCount, ctxprop, &deviceType);
52+
validate_selection(platforms, &platformCount, ctxprop, &deviceType, deviceID);
5253

5354
// create OpenCL context
5455
context = clCreateContextFromType(ctxprop, deviceType, NULL, NULL, &error);
@@ -61,7 +62,7 @@ static inline void init()
6162

6263
// get list of devices
6364
CL_SAFE_CALL( clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceSize) );
64-
deviceCount = (int) (deviceSize / sizeof(cl_device_id));
65+
deviceCount = deviceSize / sizeof(cl_device_id);
6566
if(deviceCount < 1)
6667
{
6768
printf("ERROR: No devices found.\n");
@@ -80,23 +81,23 @@ static inline void init()
8081

8182
// create command queue for the first device
8283
#if defined(BLK2D)
83-
queue = clCreateCommandQueue(context, deviceList[0], 0, NULL);
84+
queue = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
8485
if(!queue)
8586
{
8687
printf("ERROR: clCreateCommandQueue(queue) failed with error code: ");
8788
display_error_message(error, stdout);
8889
exit(-1);
8990
}
9091
#elif defined(CHBLK2D)
91-
queue_read = clCreateCommandQueue(context, deviceList[0], 0, NULL);
92+
queue_read = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
9293
if(!queue_read)
9394
{
9495
printf("ERROR: clCreateCommandQueue(queue_read) failed with error code: ");
9596
display_error_message(error, stdout);
9697
exit(-1);
9798
}
9899

99-
queue_write = clCreateCommandQueue(context, deviceList[0], 0, NULL);
100+
queue_write = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
100101
if(!queue_write)
101102
{
102103
printf("ERROR: clCreateCommandQueue(queue_write) failed with error code: ");
@@ -110,7 +111,7 @@ static inline void init()
110111

111112
static inline void usage(char **argv)
112113
{
113-
printf("\nUsage: %s -x <row width> -y <column height> -n <number of iterations> -pad <array padding indexes> -pad_x <row padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114+
printf("\nUsage: %s -id <OpenCL device ID> -x <row width> -y <column height> -n <number of iterations> -pad <array padding indexes> -pad_x <row padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114115
}
115116

116117
int main(int argc, char **argv)
@@ -138,7 +139,12 @@ int main(int argc, char **argv)
138139
int arg = 1;
139140
while (arg < argc)
140141
{
141-
if(strcmp(argv[arg], "-x") == 0)
142+
if(strcmp(argv[arg], "-id") == 0)
143+
{
144+
deviceID = atoi(argv[arg + 1]);
145+
arg += 2;
146+
}
147+
else if(strcmp(argv[arg], "-x") == 0)
142148
{
143149
dim_x = atoi(argv[arg + 1]);
144150
arg += 2;
@@ -212,7 +218,7 @@ int main(int argc, char **argv)
212218
#ifdef INTEL_FPGA
213219
size_t kernelFileSize;
214220
char *kernelSource = read_kernel("fpga-mem-bench-kernel.aocx", &kernelFileSize);
215-
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[0], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
221+
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[deviceID], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
216222
if(error != CL_SUCCESS)
217223
{
218224
printf("ERROR: clCreateProgramWithBinary() failed with error: ");
@@ -248,7 +254,7 @@ int main(int argc, char **argv)
248254
#endif
249255

250256
// compile kernel file
251-
clBuildProgram_SAFE(prog, 1, &deviceList[0], clOptions, NULL, NULL);
257+
clBuildProgram_SAFE(prog, 1, &deviceList[deviceID], clOptions, NULL, NULL);
252258

253259
// create kernel objects
254260
#if defined(BLK2D)

fpga-mem-bench-host-blk3d.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static cl_command_queue queue;
3535
static cl_command_queue queue_read, queue_write;
3636
#endif
3737
static cl_device_id* deviceList;
38-
static cl_int deviceCount;
38+
static cl_uint deviceCount;
39+
static cl_uint deviceID = 0; // default to first device
3940

4041
static inline void init()
4142
{
@@ -48,7 +49,7 @@ static inline void init()
4849

4950
display_device_info(&platforms, &platformCount);
5051
select_device_type(&deviceType);
51-
validate_selection(platforms, &platformCount, ctxprop, &deviceType);
52+
validate_selection(platforms, &platformCount, ctxprop, &deviceType, deviceID);
5253

5354
// create OpenCL context
5455
context = clCreateContextFromType(ctxprop, deviceType, NULL, NULL, &error);
@@ -61,7 +62,7 @@ static inline void init()
6162

6263
// get list of devices
6364
CL_SAFE_CALL( clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceSize) );
64-
deviceCount = (int) (deviceSize / sizeof(cl_device_id));
65+
deviceCount = deviceSize / sizeof(cl_device_id);
6566
if(deviceCount < 1)
6667
{
6768
printf("ERROR: No devices found.\n");
@@ -80,23 +81,23 @@ static inline void init()
8081

8182
// create command queue for the first device
8283
#if defined(BLK3D)
83-
queue = clCreateCommandQueue(context, deviceList[0], 0, NULL);
84+
queue = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
8485
if(!queue)
8586
{
8687
printf("ERROR: clCreateCommandQueue(queue) failed with error code: ");
8788
display_error_message(error, stdout);
8889
exit(-1);
8990
}
9091
#elif defined(CHBLK3D)
91-
queue_read = clCreateCommandQueue(context, deviceList[0], 0, NULL);
92+
queue_read = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
9293
if(!queue_read)
9394
{
9495
printf("ERROR: clCreateCommandQueue(queue_read) failed with error code: ");
9596
display_error_message(error, stdout);
9697
exit(-1);
9798
}
9899

99-
queue_write = clCreateCommandQueue(context, deviceList[0], 0, NULL);
100+
queue_write = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
100101
if(!queue_write)
101102
{
102103
printf("ERROR: clCreateCommandQueue(queue_write) failed with error code: ");
@@ -110,7 +111,7 @@ static inline void init()
110111

111112
static inline void usage(char **argv)
112113
{
113-
printf("\nUsage: %s -x <row width> -y <column height> -z <plane size> -n <number of iterations> -pad <array padding indexes> -pad_x <row padding indexes> -pad_y <column padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114+
printf("\nUsage: %s -id <OpenCL device ID> -x <row width> -y <column height> -z <plane size> -n <number of iterations> -pad <array padding indexes> -pad_x <row padding indexes> -pad_y <column padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114115
}
115116

116117
int main(int argc, char **argv)
@@ -140,7 +141,12 @@ int main(int argc, char **argv)
140141
int arg = 1;
141142
while (arg < argc)
142143
{
143-
if(strcmp(argv[arg], "-x") == 0)
144+
if(strcmp(argv[arg], "-id") == 0)
145+
{
146+
deviceID = atoi(argv[arg + 1]);
147+
arg += 2;
148+
}
149+
else if(strcmp(argv[arg], "-x") == 0)
144150
{
145151
dim_x = atoi(argv[arg + 1]);
146152
arg += 2;
@@ -224,7 +230,7 @@ int main(int argc, char **argv)
224230
#ifdef INTEL_FPGA
225231
size_t kernelFileSize;
226232
char *kernelSource = read_kernel("fpga-mem-bench-kernel.aocx", &kernelFileSize);
227-
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[0], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
233+
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[deviceID], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
228234
if(error != CL_SUCCESS)
229235
{
230236
printf("ERROR: clCreateProgramWithBinary() failed with error: ");
@@ -260,7 +266,7 @@ int main(int argc, char **argv)
260266
#endif
261267

262268
// compile kernel file
263-
clBuildProgram_SAFE(prog, 1, &deviceList[0], clOptions, NULL, NULL);
269+
clBuildProgram_SAFE(prog, 1, &deviceList[deviceID], clOptions, NULL, NULL);
264270

265271
// create kernel objects
266272
#if defined(BLK3D)

fpga-mem-bench-host-std.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static cl_command_queue queue;
3535
static cl_command_queue queue_read, queue_write;
3636
#endif
3737
static cl_device_id* deviceList;
38-
static cl_int deviceCount;
38+
static cl_uint deviceCount;
39+
static cl_uint deviceID = 0; // default to first device
3940

4041
static inline void init()
4142
{
@@ -48,7 +49,7 @@ static inline void init()
4849

4950
display_device_info(&platforms, &platformCount);
5051
select_device_type(&deviceType);
51-
validate_selection(platforms, &platformCount, ctxprop, &deviceType);
52+
validate_selection(platforms, &platformCount, ctxprop, &deviceType, deviceID);
5253

5354
// create OpenCL context
5455
context = clCreateContextFromType(ctxprop, deviceType, NULL, NULL, &error);
@@ -61,7 +62,7 @@ static inline void init()
6162

6263
// get list of devices
6364
CL_SAFE_CALL( clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceSize) );
64-
deviceCount = (int) (deviceSize / sizeof(cl_device_id));
65+
deviceCount = deviceSize / sizeof(cl_device_id);
6566
if(deviceCount < 1)
6667
{
6768
printf("ERROR: No devices found.\n");
@@ -80,23 +81,23 @@ static inline void init()
8081

8182
// create command queue for the first device
8283
#if defined(STD)
83-
queue = clCreateCommandQueue(context, deviceList[0], 0, NULL);
84+
queue = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
8485
if(!queue)
8586
{
8687
printf("ERROR: clCreateCommandQueue(queue) failed with error code: ");
8788
display_error_message(error, stdout);
8889
exit(-1);
8990
}
9091
#elif defined(CHSTD)
91-
queue_read = clCreateCommandQueue(context, deviceList[0], 0, NULL);
92+
queue_read = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
9293
if(!queue_read)
9394
{
9495
printf("ERROR: clCreateCommandQueue(queue_read) failed with error code: ");
9596
display_error_message(error, stdout);
9697
exit(-1);
9798
}
9899

99-
queue_write = clCreateCommandQueue(context, deviceList[0], 0, NULL);
100+
queue_write = clCreateCommandQueue(context, deviceList[deviceID], 0, NULL);
100101
if(!queue_write)
101102
{
102103
printf("ERROR: clCreateCommandQueue(queue_write) failed with error code: ");
@@ -110,7 +111,7 @@ static inline void init()
110111

111112
static inline void usage(char **argv)
112113
{
113-
printf("\nUsage: %s -s <buffer size in MiB> -n <number of iterations> -pad <array padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114+
printf("\nUsage: %s -id <OpenCL device ID> -s <buffer size in MiB> -n <number of iterations> -pad <array padding indexes> -hw <halo width> --verbose --verify\n", argv[0]);
114115
}
115116

116117
int main(int argc, char **argv)
@@ -136,7 +137,12 @@ int main(int argc, char **argv)
136137
int arg = 1;
137138
while (arg < argc)
138139
{
139-
if(strcmp(argv[arg], "-s") == 0)
140+
if(strcmp(argv[arg], "-id") == 0)
141+
{
142+
deviceID = atoi(argv[arg + 1]);
143+
arg += 2;
144+
}
145+
else if(strcmp(argv[arg], "-s") == 0)
140146
{
141147
size_MiB = atoi(argv[arg + 1]);
142148
arg += 2;
@@ -199,7 +205,7 @@ int main(int argc, char **argv)
199205
#ifdef INTEL_FPGA
200206
size_t kernelFileSize;
201207
char *kernelSource = read_kernel("fpga-mem-bench-kernel.aocx", &kernelFileSize);
202-
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[0], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
208+
cl_program prog = clCreateProgramWithBinary(context, 1, &deviceList[deviceID], &kernelFileSize, (const unsigned char**)&kernelSource, NULL, &error);
203209
if(error != CL_SUCCESS)
204210
{
205211
printf("ERROR: clCreateProgramWithBinary() failed with error: ");
@@ -235,7 +241,7 @@ int main(int argc, char **argv)
235241
#endif
236242

237243
// compile kernel file
238-
clBuildProgram_SAFE(prog, 1, &deviceList[0], clOptions, NULL, NULL);
244+
clBuildProgram_SAFE(prog, 1, &deviceList[deviceID], clOptions, NULL, NULL);
239245

240246
// create kernel objects
241247
#if defined(STD)

0 commit comments

Comments
 (0)