-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-cl.c
75 lines (60 loc) · 2.44 KB
/
simple-cl.c
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
#include "timing.h"
#include "cl-helper.h"
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "need an argument!\n");
abort();
}
const cl_long n = atol(argv[1]);
cl_context ctx;
cl_command_queue queue;
create_context_on(CHOOSE_INTERACTIVELY, CHOOSE_INTERACTIVELY, 0, &ctx, &queue, 0);
// --------------------------------------------------------------------------
// load kernels
// --------------------------------------------------------------------------
char *knl_text = read_file("simple.cl");
cl_kernel knl = kernel_from_string(ctx, knl_text, "sum", NULL);
free(knl_text);
// --------------------------------------------------------------------------
// allocate and initialize CPU memory
// --------------------------------------------------------------------------
float *a = (float *) malloc(sizeof(float) * n);
if (!a) { perror("alloc x"); abort(); }
// --------------------------------------------------------------------------
// allocate device memory
// --------------------------------------------------------------------------
cl_int status;
cl_mem buf_a = clCreateBuffer(ctx, CL_MEM_READ_WRITE,
sizeof(float) * n, 0, &status);
CHECK_CL_ERROR(status, "clCreateBuffer");
// --------------------------------------------------------------------------
// run code on device
// --------------------------------------------------------------------------
SET_2_KERNEL_ARGS(knl, buf_a, n);
size_t ldim[] = { 128 };
size_t gdim[] = { ((n + ldim[0] - 1)/ldim[0])*ldim[0] };
CALL_CL_GUARDED(clEnqueueNDRangeKernel,
(queue, knl,
/*dimensions*/ 1, NULL, gdim, ldim,
0, NULL, NULL));
// --------------------------------------------------------------------------
// transfer back & check
// --------------------------------------------------------------------------
CALL_CL_GUARDED(clEnqueueReadBuffer, (
queue, buf_a, /*blocking*/ CL_TRUE, /*offset*/ 0,
n * sizeof(float), a,
0, NULL, NULL));
for (size_t i = 0; i < n; ++i)
printf("%f ", a[i]);
printf("\n");
// --------------------------------------------------------------------------
// clean up
// --------------------------------------------------------------------------
CALL_CL_GUARDED(clReleaseMemObject, (buf_a));
CALL_CL_GUARDED(clReleaseKernel, (knl));
CALL_CL_GUARDED(clReleaseCommandQueue, (queue));
CALL_CL_GUARDED(clReleaseContext, (ctx));
return 0;
}