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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CL/cl.h>
#define MAX_PLATFORMS 32
#define MAX_DEVICES 32
#define MAX_BINARIES 32
char kernel[] = "__kernel void k() {\n return;\n}";
int
main(void){
cl_int err;
cl_platform_id platforms[MAX_PLATFORMS];
cl_uint nplatforms;
cl_device_id devices[MAX_DEVICES + 1]; // + 1 for duplicate test
cl_device_id device_id0;
cl_uint num_devices;
size_t i;
size_t num_binaries;
const unsigned char **binaries = NULL;
size_t *binary_sizes = NULL;
size_t num_bytes_copied;
cl_int binary_statuses[MAX_BINARIES];
cl_int binary_statuses2[MAX_BINARIES];
cl_program program = NULL;
cl_program program_with_binary = NULL;
err = clGetPlatformIDs(MAX_PLATFORMS, platforms, &nplatforms);
if (err != CL_SUCCESS && !nplatforms)
return EXIT_FAILURE;
err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, MAX_DEVICES,
devices, &num_devices);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
cl_context context = clCreateContext(NULL, num_devices, devices, NULL, NULL, &err);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
size_t kernel_size = strlen(kernel);
char* kernel_buffer = kernel;
program = clCreateProgramWithSource(context, 1, (const char**)&kernel_buffer,
&kernel_size, &err);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
err = clBuildProgram(program, num_devices, devices, NULL, NULL, NULL);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 0, 0, &num_binaries);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
num_binaries = num_binaries/sizeof(size_t);
binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t));
binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*));
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
num_binaries*sizeof(size_t), binary_sizes ,
&num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
for (i = 0; i < num_binaries; ++i)
binaries[i] = (const unsigned char*) malloc(binary_sizes[i] *
sizeof(const unsigned char));
err = clGetProgramInfo(program, CL_PROGRAM_BINARIES,
num_binaries*sizeof(char*), binaries, &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
cl_uint num = num_binaries < num_devices ? num_binaries : num_devices;
if (num == 0)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes,
binaries, binary_statuses, &err);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
clReleaseProgram(program_with_binary);
for (i = 0; i < num; i++)
{
if (binary_statuses[i] != CL_SUCCESS)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
}
// negative test1: invalid device
device_id0 = devices[0];
devices[0] = NULL; // invalid device
program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes,
binaries, binary_statuses, &err);
if (err != CL_INVALID_DEVICE || program_with_binary != NULL)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
err = CL_SUCCESS;
devices[0] = device_id0;
for (i = 0; i < num_binaries; ++i) free((void*)binaries[i]);
free(binary_sizes);
free(binaries);
// negative test2: duplicate device
num_binaries = 2;
devices[1] = devices[0]; // duplicate
binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t));
binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*));
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 1*sizeof(size_t),
binary_sizes , &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
binary_sizes[1] = binary_sizes[0];
binaries[0] = (const unsigned char*) malloc(binary_sizes[0] *
sizeof(const unsigned char));
binaries[1] = (const unsigned char*) malloc(binary_sizes[1] *
sizeof(const unsigned char));
err = clGetProgramInfo(program, CL_PROGRAM_BINARIES, 1 * sizeof(char*),
binaries, &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
memcpy((void*)binaries[1], (void*)binaries[0], binary_sizes[0]);
program_with_binary = clCreateProgramWithBinary(context, 2, devices, binary_sizes,
binaries, binary_statuses2, &err);
if (err != CL_INVALID_DEVICE || program_with_binary != NULL)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
err = CL_SUCCESS;
FREE_AND_EXIT:
// Free resources
for (i = 0; i < num_binaries; ++i)
if (binaries)
if(binaries[i])
free((void*)binaries[i]);
if (binary_sizes)
free(binary_sizes);
if (binaries)
free(binaries);
if (program)
clReleaseProgram(program);
if (program_with_binary)
clReleaseProgram(program_with_binary);
return err == CL_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
}
|