|
| 1 | +/* |
| 2 | +We can pass data through multiple kernels with multiple |
| 3 | +clEnqueueNDRangeKernel calls on a single command queue. |
| 4 | +*/ |
| 5 | + |
| 6 | +#include "common.h" |
| 7 | + |
| 8 | +int main(void) { |
| 9 | + const char *source = |
| 10 | + "__kernel void add(__global int *out) {\n" |
| 11 | + " out[get_global_id(0)]++;\n" |
| 12 | + "}\n" |
| 13 | + "__kernel void mul(__global int *out) {\n" |
| 14 | + " out[get_global_id(0)] *= 2;\n" |
| 15 | + "}\n" |
| 16 | + ; |
| 17 | + cl_int input[] = {1, 2}; |
| 18 | + cl_kernel kernel_add, kernel_mul; |
| 19 | + cl_mem buffer; |
| 20 | + cl_program program; |
| 21 | + Common common; |
| 22 | + const size_t global_work_size = sizeof(input) / sizeof(input[0]); |
| 23 | + |
| 24 | + /* Run kernel. */ |
| 25 | + common_init(&common, NULL); |
| 26 | + common_create_program(&common, source, NULL, &program); |
| 27 | + kernel_add = clCreateKernel(program, "add", NULL); |
| 28 | + kernel_mul = clCreateKernel(program, "mul", NULL); |
| 29 | + buffer = clCreateBuffer(common.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(input), input, NULL); |
| 30 | + clSetKernelArg(kernel_add, 0, sizeof(buffer), &buffer); |
| 31 | + clSetKernelArg(kernel_mul, 0, sizeof(buffer), &buffer); |
| 32 | + clEnqueueNDRangeKernel(common.command_queue, kernel_add, 1, NULL, &global_work_size, NULL, 0, NULL, NULL); |
| 33 | + clEnqueueNDRangeKernel(common.command_queue, kernel_mul, 1, NULL, &global_work_size, NULL, 0, NULL, NULL); |
| 34 | + clFlush(common.command_queue); |
| 35 | + clFinish(common.command_queue); |
| 36 | + clEnqueueReadBuffer(common.command_queue, buffer, CL_TRUE, 0, sizeof(input), input, 0, NULL, NULL); |
| 37 | + |
| 38 | + /* Assertions. */ |
| 39 | + assert(input[0] == 4); |
| 40 | + assert(input[1] == 6); |
| 41 | + |
| 42 | + /* Cleanup. */ |
| 43 | + clReleaseMemObject(buffer); |
| 44 | + common_deinit(&common); |
| 45 | + return EXIT_SUCCESS; |
| 46 | +} |
0 commit comments