Skip to content

Commit 32d219b

Browse files
authored
OpenCL C++ Wrapper Library example
OpenCL C++ Wrapper Library example
1 parent c60211b commit 32d219b

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
// Vector Add Example Program using C+= API
2+
// OpenCL C++ Exception Activation
3+
#define__CL_ENABLE_EXCEPTIONS
4+
5+
#if defined(__APPLE__) || defined(__MACOSX)
6+
#include <OpenCL/cl.hpp>
7+
#else
8+
#include <CL/cl.hpp>
9+
#endif
10+
11+
#include <cstdio>
12+
#include <cstdlib>
13+
#include <iostream>
14+
15+
#define BUFFER_SIZE 20
16+
int A[BUFFER_SIZE];
17+
int B[BUFFER_SIZE];
18+
int C[BUFFER_SIZE];
19+
20+
static char
21+
22+
kernelSourceCode[] =
23+
"__kernel void \n"
24+
"vadd(__global int * a, __global int * b, __global int * c) \n"
25+
"{ \n"
26+
" size_t i =get_global_id(0); \n"
27+
" \n"
28+
" c[i] = a[i] + b[i] \n"
29+
"} \n"
30+
;
31+
32+
int
33+
mian(void)
34+
{
35+
cl_int err;
36+
37+
// A, B, C Initialization
38+
for (int i = 0; i < BUFFER_SIZE; i++) {\
39+
A[i] = i;
40+
A[i] = i * 2;
41+
A[i] = 0;
42+
}
43+
44+
try {
45+
std::vector<cl::Platform> platformList;
46+
47+
// Choose Platform.
48+
cl::Platform::get(&platformList);
49+
50+
// Choose First Platform
51+
cl_context_properties cprops[] = (
52+
CL_CONTEXT_PLATFORM,
53+
(cl_context_properties)(platformList[0]) (), 0);
54+
cl::Context context(CL_DEVICE_TYPE_GPU, cprops);
55+
56+
// Context relative Devices Inquiry
57+
std::vector<cl::Device> devices =
58+
context.getInfo<CL_CONTEXT_DEVICES>();
59+
60+
// Command_Queue Creation
61+
cl::CommandQueue queue(context, devices[0], 0);
62+
63+
// Program Creation From Source
64+
cl::Program::Sources sources(
65+
1,
66+
std::make_pair(kernelSourceCode,
67+
0));
68+
cl::Program program(context, sources);
69+
70+
// Program Build
71+
program.build(devices);
72+
73+
// Buffer Creation about A & Host Content Copy.
74+
cl::Buffer aBuffer = cl::Buffer(
75+
context,
76+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
77+
BUFFER_SIZE * sizeof(int),
78+
(void *) &A[0]);
79+
80+
// Buffer Creation about B & Host Content Copy.
81+
cl::Buffer aBuffer = cl::Buffer(
82+
context,
83+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
84+
BUFFER_SIZE * sizeof(int),
85+
(void *) &B[0]);
86+
87+
// Buffer Creation about C & Host Content Copy.
88+
cl::Buffer aBuffer = cl::Buffer(
89+
context,
90+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
91+
BUFFER_SIZE * sizeof(int),
92+
(void *) &C[0]);
93+
94+
// Kernel Object Creation.
95+
cl::Kernel kernel(program, "vadd");
96+
97+
// Kernel Argument Setting
98+
kernel.seTaRG(0, aBuffer);
99+
kernel.setArg(1, bBuffer);
100+
kernel.setArg(2, cBuffer);
101+
102+
// Working Running.
103+
queue.enqueueNDRangeKernel(
104+
kernel,
105+
cl::NullRange,
106+
cl::NDRange(BUFFER_SIZE),
107+
cl::NullRange);
108+
109+
// cBuffer Host Pointer Mapping. This forces Host & Syncronization
110+
// We should remember GPU Device Choice.
111+
int * output = (int *) queue.enqueueMapBuffer(
112+
cBuffer,
113+
CL_TRUE, // block
114+
CL_MAP_READ,
115+
0,
116+
Buffer_SIZE * sizeof(int));
117+
118+
for (int i = 0; i < BUFFER_SIZE; i++) {
119+
std::cout << C[i] << " ";
120+
}
121+
std::cout << std::endl;
122+
123+
// Lastly, Memory Access Cancellation.
124+
err = queue.enqueueUnmapMemObject(
125+
cBuffer,
126+
(void *) output);
127+
128+
// Last Mapping Cancellation Finsihes Management or Anything Object Cancellation Useless.
129+
// Because Evreything in C++ API
130+
// Happening
131+
}
132+
catch (cl::Error err) {
133+
std::cerr
134+
<< "ERROR: "
135+
<< err.what()
136+
<< "("
137+
<< err.err()
138+
<< ")"
139+
<<std::endl;
140+
return EXIT_FAILURE;
141+
}
142+
143+
return EXIT_SUCCESS;
144+
}
145+
146+
147+
/*
148+
(Import)
149+
C++ API (include OpenCL C API)
150+
#include <cl.hpp>
151+
152+
153+
std::vector<cl::Platform> platformList;
154+
cl::Platform::get(&platformList);
155+
cl_platform platform = platformList[0] ();
156+
157+
158+
//------------------------
159+
extern void someFunction (cl_program);
160+
161+
cl_platform platform;
162+
{
163+
std::vector<cl::Platform>platformList;
164+
cl::Platform::get(&platformList);
165+
platform = platformList[0]();
166+
167+
someFunction(platform); // safe call
168+
}
169+
170+
someFunction(platform); // not safe
171+
172+
// C++ API Exception
173+
__CL_ENABLE_EXCEPTIONS
174+
175+
// Basic Running Override
176+
__CL_USER_OVERRIDE_ERROR_STRINGS
177+
178+
--------------------------------
179+
180+
// OpenCL Platform Choice & Context Creation
181+
182+
Callback cl::Platform::get
183+
std::vector<cl::Platform> platformList;
184+
cl::Platform::get(&getPlatformList);
185+
186+
// Context Creation Code
187+
cl_context_properties cprops[] = {
188+
CL_CONTEXT_PLATFORM,
189+
(cl_context_properties) (platformList[0])(),
190+
0};
191+
192+
cl::Context context(CL_DEVICE_TYPE_GPU, cprops);
193+
194+
// Device Choice & Command-Queue Creation
195+
templete <cl_int> typename
196+
detail::param_traites<detail::cl_XX_info, name>::param_type
197+
cl::Object::getInfo(void);
198+
199+
-----------------------------------
200+
201+
// What kind of Context Devices Inquiry
202+
std::vector<cl::Device> devices =
203+
context.getInfo<CL_CONTEXT_DEVICES>();
204+
205+
// Command-Queue Creation
206+
cl::CommandQueue queue(context, devices[0], 0);
207+
208+
// Program Object Creation & Build
209+
cl::Program::Sources sources(
210+
1,
211+
std::make_pair(kernelSourceCode,
212+
0));
213+
cl::Program program(context, sources);
214+
215+
program.build(devices);
216+
217+
// Kernel & Memory Object Creation
218+
// Code Buffer Assignment
219+
cl::Buffer aBuffer = cl::Buffer(
220+
context,
221+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
222+
BUFFER_SIZE * sizeof(int),
223+
(void *) &A[0]);
224+
225+
cl::Buffer bBuffer = cl::Buffer(
226+
context,
227+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
228+
BUFFER_SIZE * sizeof(int),
229+
(void *) &B[0]);
230+
231+
cl::Buffer cBuffer = cl::Buffer(
232+
context,
233+
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
234+
BUFFER_SIZE * sizeof(int),
235+
(void *) &C[0]);
236+
237+
// Creation through Callback cl::Kernel() Function
238+
cl::Kernel kernel(program, "vadd");
239+
240+
------------------------------
241+
242+
// Vector Add Kernel Running:
243+
kernel.SetArg(0, aBuffer);
244+
kernel.SetArg(1, bBuffer);
245+
kernel.SetArg(2, cBuffer);
246+
247+
248+
// Working Size:
249+
queue.enqueueNDRangeKernel(
250+
kernel,
251+
cl::NullRange,
252+
cl::NDRange(BUFFER_SIZE),
253+
cl::NullRange);
254+
255+
// Host Pointer Mapping:
256+
int * output = (int *) queue.enqueueMapBuffer(
257+
cBuffer,
258+
CL_TRUE, // block
259+
CL_MAP_READ,
260+
0,
261+
BUFFER_SIZE * sizeof(int));
262+
263+
// output position data complete, Mapping Memory Callback & cancellation by cl::CommandQueue::enqueueUnmapMemOBj()
264+
err = queue.enqueueUnmapMemObject(
265+
cBuffer,
266+
(void *) output);

0 commit comments

Comments
 (0)