MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / main

Function main

test/interop_opencl_custom_kernel_snippet.cpp:24–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 }
23
24int main() {
25 size_t length = 10;
26
27 // Create ArrayFire array objects:
28 af::array A = af::randu(length, f32);
29 af::array B = af::constant(0, length, f32);
30
31 // ... additional ArrayFire operations here
32
33 // 2. Obtain the device, context, and queue used by ArrayFire
34 static cl_context af_context = afcl::getContext();
35 static cl_device_id af_device_id = afcl::getDeviceId();
36 static cl_command_queue af_queue = afcl::getQueue();
37
38 // 3. Obtain cl_mem references to af::array objects
39 cl_mem* d_A = A.device<cl_mem>();
40 cl_mem* d_B = B.device<cl_mem>();
41
42 // 4. Load, build, and use your kernels.
43 // For the sake of readability, we have omitted error checking.
44 int status = CL_SUCCESS;
45
46 // A simple copy kernel, uses C++11 syntax for multi-line strings.
47 const char* kernel_name = "copy_kernel";
48 const char* source = R"(
49 void __kernel
50 copy_kernel(__global float* gA, __global float* gB) {
51 int id = get_global_id(0);
52 gB[id] = gA[id];
53 }
54 )";
55
56 // Create the program, build the executable, and extract the entry point
57 // for the kernel.
58 cl_program program = clCreateProgramWithSource(af_context, 1, &source, NULL, &status);
59 OCL_CHECK(status);
60 OCL_CHECK(clBuildProgram(program, 1, &af_device_id, NULL, NULL, NULL));
61 cl_kernel kernel = clCreateKernel(program, kernel_name, &status);
62 OCL_CHECK(status);
63
64 // Set arguments and launch your kernels
65 OCL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), d_A));
66 OCL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), d_B));
67 OCL_CHECK(clEnqueueNDRangeKernel(af_queue, kernel, 1, NULL, &length, NULL,
68 0, NULL, NULL));
69
70 // 5. Return control of af::array memory to ArrayFire
71 A.unlock();
72 B.unlock();
73
74 /// A and B should not be the same because of the copy_kernel user code
75 assert(af::allTrue<bool>(A == B));
76
77 // Delete the pointers returned by the device function. This does NOT
78 // delete the cl_mem memory and only deletes the pointers
79 delete d_A;
80 delete d_B;
81

Callers

nothing calls this directly

Calls 6

randuFunction · 0.85
constantFunction · 0.85
getContextFunction · 0.50
getDeviceIdFunction · 0.50
getQueueFunction · 0.50
unlockMethod · 0.45

Tested by

no test coverage detected