Debian Codegraph @

examples/prism-opencl.html

raw symbols SCIP
1 <p><strong>Note:</strong> Use the class <code class="language-none">"language-opencl"</code> for OpenCL kernel code.
2 Host code is automatically highlighted with the <code class="language-none">"language-c"</code>
3 or <code class="language-none">"language-cpp"</code> class.
4 </p>
5
6 <h2>OpenCL host code</h2>
7 <pre class="language-cpp"><code>// OpenCL functions, constants, etc. are also highlighted in OpenCL host code in the c or cpp language
8 cl::Event KernelFilterImages::runSingle(const cl::Image2D& imgSrc, SPImage2D& imgDst)
9 {
10 const size_t rows = imgSrc.getImageInfo&lt;CL_IMAGE_HEIGHT>();
11 const size_t cols = imgSrc.getImageInfo&lt;CL_IMAGE_WIDTH>();
12
13 ASSERT(rows > 0 && cols > 0, "The image object seems to be invalid, no rows/cols set");
14 ASSERT(imgSrc.getImageInfo&lt;CL_IMAGE_FORMAT>().image_channel_data_type == CL_FLOAT, "Only float type images are supported");
15 ASSERT(imgSrc.getInfo&lt;CL_MEM_FLAGS>() == CL_MEM_READ_ONLY || imgSrc.getInfo&lt;CL_MEM_FLAGS>() == CL_MEM_READ_WRITE, "Can't read the input image");
16
17 imgDst = std::make_shared&lt;cl::Image2D>(*context, CL_MEM_READ_WRITE, cl::ImageFormat(CL_R, CL_FLOAT), cols, rows);
18
19 cl::Kernel kernel(*program, "filter_single");
20 kernel.setArg(0, imgSrc);
21 kernel.setArg(1, *imgDst);
22 kernel.setArg(2, bufferKernel1);
23 kernel.setArg(3, kernel1.rows);
24 kernel.setArg(4, kernel1.rows / 2);
25 kernel.setArg(5, kernel1.cols);
26 kernel.setArg(6, kernel1.cols / 2);
27 kernel.setArg(7, border);
28
29 cl::Event eventFilter;
30 const cl::NDRange global(cols, rows);
31 queue->enqueueNDRangeKernel(kernel, cl::NullRange, global, cl::NullRange, &events, &eventFilter);
32 }</code></pre>
33
34 <h2>OpenCL kernel code</h2>
35 <pre><code>// CLK_ADDRESS_CLAMP_TO_EDGE = aaa|abcd|ddd
36 constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
37 typedef float type_single;
38
39 type_single filter_sum_single_3x3(read_only image2d_t imgIn,
40 constant float* filterKernel,
41 const int2 coordBase,
42 const int border)
43 {
44 type_single sum = (type_single)(0.0f);
45 const int rows = get_image_height(imgIn);
46 const int cols = get_image_width(imgIn);
47 int2 coordCurrent;
48 int2 coordBorder;
49 float color;
50
51 // Image patch is row-wise accessed
52 // Filter kernel is centred in the middle
53 #pragma unroll
54 for (int y = -ROWS_HALF_3x3; y &lt;= ROWS_HALF_3x3; ++y) // Start at the top left corner of the filter
55 {
56 coordCurrent.y = coordBase.y + y;
57 #pragma unroll
58 for (int x = -COLS_HALF_3x3; x &lt;= COLS_HALF_3x3; ++x) // And end at the bottom right corner
59 {
60 coordCurrent.x = coordBase.x + x;
61 coordBorder = borderCoordinate(coordCurrent, rows, cols, border);
62 color = read_imagef(imgIn, sampler, coordBorder).x;
63
64 const int idx = (y + ROWS_HALF_3x3) * COLS_3x3 + x + COLS_HALF_3x3;
65 sum += color * filterKernel[idx];
66 }
67 }
68
69 return sum;
70 }
71
72 kernel void filter_single_3x3(read_only image2d_t imgIn,
73 write_only image2d_t imgOut,
74 constant float* filterKernel,
75 const int border)
76 {
77 int2 coordBase = (int2)(get_global_id(0), get_global_id(1));
78
79 type_single sum = filter_sum_single_3x3(imgIn, filterKernel, coordBase, border);
80
81 write_imagef(imgOut, coordBase, sum);
82 }</code></pre>