annotate src/test/OpenCL_gpu.c @ 301:609bf62768b9

add -DUSE_CUDA=1 flag to cmake
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 12 Feb 2017 12:35:11 +0900
parents 2b41bd298fe8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
1 #include <stdio.h>
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
2 #include <stdlib.h>
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
3
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
4 #ifdef __APPLE__
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
5 #include <OpenCL/opencl.h>
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
6 #else
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
7 #include <CL/cl.h>
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
8 #endif
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
9
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
10 #define MEM_SIZE (128)
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
11 #define MAX_SOURCE_SIZE (0x100000)
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
12
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
13 int main()
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
14 {
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
15 cl_device_id device_id = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
16 cl_context context = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
17 cl_command_queue command_queue = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
18 cl_mem memobj = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
19 cl_program program = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
20 cl_kernel kernel = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
21 cl_platform_id platform_id = NULL;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
22 cl_uint ret_num_devices;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
23 cl_uint ret_num_platforms;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
24 cl_int ret;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
25
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
26 char string[MEM_SIZE];
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
27
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
28 FILE *fp;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
29 char fileName[] = "./hello.cl";
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
30 char *source_str;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
31 size_t source_size;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
32
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
33 /* Load the source code containing the kernel*/
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
34 fp = fopen(fileName, "r");
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
35 if (!fp) {
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
36 fprintf(stderr, "Failed to load kernel.\n");
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
37 exit(1);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
38 }
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
39 source_str = (char*)malloc(MAX_SOURCE_SIZE);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
40 source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
41 fclose(fp);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
42
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
43 /* Get Platform and Device Info */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
44 ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
45 ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
46
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
47 /* Create OpenCL context */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
48 context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
49
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
50 /* Create Command Queue */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
51 command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
52
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
53 /* Create Memory Buffer */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
54 memobj = clCreateBuffer(context, CL_MEM_READ_WRITE,MEM_SIZE * sizeof(char), NULL, &ret);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
55
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
56 /* Create Kernel Program from the source */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
57 program = clCreateProgramWithSource(context, 1, (const char **)&source_str,
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
58 (const size_t *)&source_size, &ret);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
59
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
60 /* Build Kernel Program */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
61 ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
62
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
63 /* Create OpenCL Kernel */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
64 kernel = clCreateKernel(program, "hello", &ret);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
65
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
66 /* Set OpenCL Kernel Parameters */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
67 ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memobj);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
68
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
69 /* Execute OpenCL Kernel */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
70 ret = clEnqueueTask(command_queue, kernel, 0, NULL,NULL);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
71
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
72 /* Copy results from the memory buffer */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
73 ret = clEnqueueReadBuffer(command_queue, memobj, CL_TRUE, 0,
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
74 MEM_SIZE * sizeof(char),string, 0, NULL, NULL);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
75
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
76 /* Display Result */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
77 puts(string);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
78
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
79 /* Finalization */
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
80 ret = clFlush(command_queue);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
81 ret = clFinish(command_queue);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
82 ret = clReleaseKernel(kernel);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
83 ret = clReleaseProgram(program);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
84 ret = clReleaseMemObject(memobj);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
85 ret = clReleaseCommandQueue(command_queue);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
86 ret = clReleaseContext(context);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
87
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
88 free(source_str);
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
89
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
90 return 0;
2b41bd298fe8 add openCL test files
mir3636
parents:
diff changeset
91 }