forked from rarias/jungle
128 lines
4.2 KiB
C
128 lines
4.2 KiB
C
#include <CL/cl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define BUF_LEN 128
|
|
|
|
cl_int exit_err = CL_SUCCESS;
|
|
|
|
#define CHECK(cmd) \
|
|
do { \
|
|
cl_int err = cmd; \
|
|
if (err != CL_SUCCESS) { \
|
|
printf("[ERROR] " #cmd " (Error code: %d)\n" \
|
|
"@ "__FILE__ \
|
|
":%d", \
|
|
err, __LINE__); \
|
|
exit_err = err; \
|
|
goto cleanup; \
|
|
} \
|
|
} while (0)
|
|
|
|
const char *kernel_source = "__kernel void vector_add(__global const float *a, "
|
|
"__global const float *b, __global float *c) {\n"
|
|
" int gid = get_global_id(0);\n"
|
|
" c[gid] = a[gid] + b[gid];\n"
|
|
"}\n";
|
|
|
|
cl_int test_kernel_compilation(cl_platform_id platform) {
|
|
cl_device_id device = NULL;
|
|
cl_context context = NULL;
|
|
cl_program program = NULL;
|
|
cl_int err = CL_SUCCESS;
|
|
char device_name[BUF_LEN];
|
|
|
|
// Get first device for this platform
|
|
CHECK(clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL));
|
|
|
|
// Get device name
|
|
CHECK(clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(device_name),
|
|
device_name, NULL));
|
|
printf(" Device: %s\n", device_name);
|
|
|
|
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
|
|
CHECK(err);
|
|
|
|
program = clCreateProgramWithSource(context, 1, &kernel_source, NULL, &err);
|
|
CHECK(err);
|
|
|
|
err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
|
|
if (err != CL_SUCCESS) {
|
|
printf(" [ERROR] Kernel compilation failed (Error code: %d)\n", err);
|
|
|
|
// Build log
|
|
size_t log_size;
|
|
CHECK(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL,
|
|
&log_size));
|
|
char *log = (char *)malloc(log_size);
|
|
CHECK(clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size,
|
|
log, NULL));
|
|
printf(" Build log:\n%s\n", log);
|
|
free(log);
|
|
goto cleanup;
|
|
}
|
|
|
|
printf(" [OK] Kernel compiled successfully!\n");
|
|
|
|
cleanup:
|
|
if (program)
|
|
clReleaseProgram(program);
|
|
if (context)
|
|
clReleaseContext(context);
|
|
printf("\n");
|
|
|
|
return exit_err;
|
|
}
|
|
|
|
int main() {
|
|
cl_uint num_platforms;
|
|
cl_platform_id *platforms = NULL;
|
|
|
|
// Get number of platforms
|
|
CHECK(clGetPlatformIDs(0, NULL, &num_platforms));
|
|
|
|
printf("Found %d OpenCL platform(s)\n\n", num_platforms);
|
|
|
|
if (num_platforms == 0) {
|
|
printf("No OpenCL platforms found!\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// Allocate memory for platforms
|
|
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);
|
|
|
|
// Get platform IDs
|
|
CHECK(clGetPlatformIDs(num_platforms, platforms, NULL));
|
|
|
|
// Query each platform and test kernel compilation
|
|
for (cl_uint i = 0; i < num_platforms; i++) {
|
|
char platform_name[BUF_LEN];
|
|
char platform_vendor[BUF_LEN];
|
|
char platform_version[BUF_LEN];
|
|
|
|
CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,
|
|
sizeof(platform_name), platform_name, NULL));
|
|
CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
|
|
sizeof(platform_vendor), platform_vendor, NULL));
|
|
CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
|
|
sizeof(platform_version), platform_version, NULL));
|
|
|
|
printf("Platform %d: %s\n", i, platform_name);
|
|
printf(" Vendor: %s\n", platform_vendor);
|
|
printf(" Version: %s\n", platform_version);
|
|
|
|
// Test kernel compilation
|
|
CHECK(test_kernel_compilation(platforms[i]));
|
|
}
|
|
|
|
cleanup:
|
|
if (platforms)
|
|
free(platforms);
|
|
|
|
if (exit_err == CL_SUCCESS)
|
|
return EXIT_SUCCESS;
|
|
|
|
return EXIT_FAILURE;
|
|
}
|