|
|
3636846 |
#include <stdio.h>
|
|
|
3636846 |
#include <string.h>
|
|
|
3636846 |
#include <CL/cl.h>
|
|
|
3636846 |
|
|
|
3636846 |
int main() {
|
|
|
3636846 |
cl_int ret;
|
|
|
3636846 |
|
|
|
3636846 |
cl_uint num_platforms;
|
|
|
3636846 |
cl_platform_id *platforms;
|
|
|
3636846 |
cl_platform_id pocl_platform = NULL;
|
|
|
3636846 |
unsigned i;
|
|
|
3636846 |
cl_uint num_devices;
|
|
|
3636846 |
cl_device_id device;
|
|
|
3636846 |
cl_context context;
|
|
|
3636846 |
const char* src = "kernel void test(global float *out) { *out = M_PI_F; }";
|
|
|
3636846 |
size_t src_len = strlen(src) + 1;
|
|
|
3636846 |
cl_program program;
|
|
|
3636846 |
cl_kernel kernel;
|
|
|
3636846 |
float out;
|
|
|
3636846 |
cl_mem buffer;
|
|
|
3636846 |
cl_command_queue queue;
|
|
|
3636846 |
size_t global_work_size = 1;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clGetPlatformIDs(0, NULL, &num_platforms);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
platforms = malloc(num_platforms * sizeof(cl_platform_id));
|
|
|
3636846 |
if (!platforms)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clGetPlatformIDs(num_platforms, platforms, NULL);
|
|
|
3636846 |
|
|
|
3636846 |
for (i = 0; i < num_platforms; i++) {
|
|
|
3636846 |
char *platform_name;
|
|
|
3636846 |
char *platform_vendor;
|
|
|
3636846 |
size_t param_size;
|
|
|
3636846 |
cl_platform_id platform = platforms[i];
|
|
|
3636846 |
|
|
|
3636846 |
/* Get platform name */
|
|
|
3636846 |
ret = clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, ¶m_size);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
platform_name = malloc(param_size);
|
|
|
3636846 |
if (!platform_name)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clGetPlatformInfo(platform, CL_PLATFORM_NAME, param_size,
|
|
|
3636846 |
platform_name, NULL);
|
|
|
3636846 |
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
if (!strcmp(platform_name, "Portable Computing Language")) {
|
|
|
3636846 |
pocl_platform = platform;
|
|
|
3636846 |
free(platform_name);
|
|
|
3636846 |
break;
|
|
|
3636846 |
}
|
|
|
3636846 |
|
|
|
3636846 |
free(platform_name);
|
|
|
3636846 |
}
|
|
|
3636846 |
|
|
|
3636846 |
if (!pocl_platform)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clGetDeviceIDs(pocl_platform, CL_DEVICE_TYPE_CPU, 0, NULL,
|
|
|
3636846 |
&num_devices);
|
|
|
3636846 |
if (ret != CL_SUCCESS || num_devices < 1)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clGetDeviceIDs(pocl_platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
context = clCreateContext(NULL, 1, &device, NULL, NULL, &ret;;
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
program = clCreateProgramWithSource(context, 1, &src, &src_len, &ret;;
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
|
|
|
3636846 |
if (ret != CL_SUCCESS) {
|
|
|
3636846 |
size_t param_size;
|
|
|
3636846 |
char *log;
|
|
|
3636846 |
ret = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL,
|
|
|
3636846 |
¶m_size);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
log = malloc(param_size);
|
|
|
3636846 |
ret = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, param_size,
|
|
|
3636846 |
log, NULL);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
fprintf(stderr, "%s\n", log);
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
}
|
|
|
3636846 |
|
|
|
3636846 |
kernel = clCreateKernel(program, "test", &ret;;
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
buffer = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, sizeof(out), &out, &ret;;
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clSetKernelArg(kernel, 0, sizeof(buffer), &buffer);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
queue = clCreateCommandQueue(context, device, 0, &ret;;
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_work_size, NULL,
|
|
|
3636846 |
0, NULL, NULL);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
ret = clFinish(queue);
|
|
|
3636846 |
if (ret != CL_SUCCESS)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
printf("pi = %f\n", out);
|
|
|
3636846 |
if (out != CL_M_PI_F)
|
|
|
3636846 |
return EXIT_FAILURE;
|
|
|
3636846 |
|
|
|
3636846 |
return EXIT_SUCCESS;
|
|
|
3636846 |
}
|