forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass_by_value.c
40 lines (32 loc) · 1.17 KB
/
pass_by_value.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
/*
Kernel takes an integer value `int` instead of a pointer.
cl_int is passed directly to clSetKernelArg instead of using
a buffer obtained from clCreateBuffer.
Does not need to be __global because it is not a pointer.
In practice, this is often used to pass problem size parameters to the kernel.
*/
#include "common.h"
int main(void) {
const char *source =
"__kernel void kmain(int in, __global int *out) {\n"
" out[0] = in + 1;\n"
"}\n";
cl_int in = 1, out;
cl_mem buffer;
Common common;
/* Run kernel. */
common_init(&common, source);
clSetKernelArg(common.kernel, 0, sizeof(in), &in);
buffer = clCreateBuffer(common.context, CL_MEM_READ_WRITE, sizeof(out), NULL, NULL);
clSetKernelArg(common.kernel, 1, sizeof(buffer), &buffer);
clEnqueueTask(common.command_queue, common.kernel, 0, NULL, NULL);
clFlush(common.command_queue);
clFinish(common.command_queue);
clEnqueueReadBuffer(common.command_queue, buffer, CL_TRUE, 0, sizeof(out), &out, 0, NULL, NULL);
/* Assertions. */
assert(out == 2);
/* Cleanup. */
clReleaseMemObject(buffer);
common_deinit(&common);
return EXIT_SUCCESS;
}