Skip to content

Commit

Permalink
update the C++ bindings code example (#151)
Browse files Browse the repository at this point in the history
* update the C++ bindings code example

* fix the name of std::call_once
  • Loading branch information
bashbaug authored Nov 10, 2021
1 parent 6d833a5 commit 814e7b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
24 changes: 13 additions & 11 deletions examples/src/headerexample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,39 @@ const int numElements = 32;

int main(void)
{
// Filter for a 2.0 platform and set it as the default
// Filter for a 2.0 or newer platform and set it as the default
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl::Platform plat;
for (auto &p : platforms) {
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
if (platver.find("OpenCL 2.") != std::string::npos) {
if (platver.find("OpenCL 2.") != std::string::npos ||
platver.find("OpenCL 3.") != std::string::npos) {
// Note: an OpenCL 3.x platform may not support all required features!
plat = p;
}
}
if (plat() == 0) {
std::cout << "No OpenCL 2.0 platform found.";
std::cout << "No OpenCL 2.0 or newer platform found.\n";
return -1;
}

cl::Platform newP = cl::Platform::setDefault(plat);
if (newP != plat) {
std::cout << "Error setting default platform.";
std::cout << "Error setting default platform.\n";
return -1;
}

// Use C++11 raw string literals for kernel source code
// C++11 raw string literal for the first kernel
std::string kernel1{R"CLC(
global int globalA;
kernel void updateGlobal()
{
globalA = 75;
}
)CLC"};

// Raw string literal for the second kernel
std::string kernel2{R"CLC(
typedef struct { global int *bar; } Foo;
kernel void vectorAdd(global const Foo* aNum, global const int *inputA, global const int *inputB,
Expand All @@ -66,7 +70,6 @@ int main(void)
}
)CLC"};

// New simpler string interface style
std::vector<std::string> programStrings;
programStrings.push_back(kernel1);
programStrings.push_back(kernel2);
Expand Down Expand Up @@ -108,10 +111,9 @@ int main(void)
std::vector<int, cl::SVMAllocator<int, cl::SVMTraitCoarse<>>> inputA(numElements, 1, svmAlloc);
cl::coarse_svm_vector<int> inputB(numElements, 2, svmAlloc);

//
//////////////

// Traditional cl_mem allocations

std::vector<int> output(numElements, 0xdeadbeef);
cl::Buffer outputBuffer(begin(output), end(output), false);
cl::Pipe aPipe(sizeof(cl_int), numElements / 2);
Expand All @@ -135,8 +137,8 @@ int main(void)
// This one was not passed as a parameter
vectorAddKernel.setSVMPointers(anSVMInt);

cl_int error;
vectorAddKernel(
cl_int error;
vectorAddKernel(
cl::EnqueueArgs(
cl::NDRange(numElements/2),
cl::NDRange(numElements/2)),
Expand All @@ -147,7 +149,7 @@ int main(void)
3,
aPipe,
defaultDeviceQueue,
error
error
);

cl::copy(outputBuffer, begin(output), end(output));
Expand Down
42 changes: 21 additions & 21 deletions include/CL/opencl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
* bindings, including support for the optional exception feature and
* also the supplied vector and string classes, see following sections for
* decriptions of these features.
*
* Note: the C++ bindings use std::call_once and therefore may need to be
* compiled using special command-line options (such as "-pthread") on some
* platforms!
*
* \code
#define CL_HPP_ENABLE_EXCEPTIONS
Expand All @@ -232,35 +236,39 @@
int main(void)
{
// Filter for a 2.0 platform and set it as the default
// Filter for a 2.0 or newer platform and set it as the default
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl::Platform plat;
for (auto &p : platforms) {
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
if (platver.find("OpenCL 2.") != std::string::npos) {
if (platver.find("OpenCL 2.") != std::string::npos ||
platver.find("OpenCL 3.") != std::string::npos) {
// Note: an OpenCL 3.x platform may not support all required features!
plat = p;
}
}
if (plat() == 0) {
std::cout << "No OpenCL 2.0 platform found.";
if (plat() == 0) {
std::cout << "No OpenCL 2.0 or newer platform found.\n";
return -1;
}
cl::Platform newP = cl::Platform::setDefault(plat);
if (newP != plat) {
std::cout << "Error setting default platform.";
std::cout << "Error setting default platform.\n";
return -1;
}
// Use C++11 raw string literals for kernel source code
// C++11 raw string literal for the first kernel
std::string kernel1{R"CLC(
global int globalA;
kernel void updateGlobal()
{
globalA = 75;
}
)CLC"};
// Raw string literal for the second kernel
std::string kernel2{R"CLC(
typedef struct { global int *bar; } Foo;
kernel void vectorAdd(global const Foo* aNum, global const int *inputA, global const int *inputB,
Expand All @@ -287,8 +295,9 @@
}
)CLC"};
// New simpler string interface style
std::vector<std::string> programStrings {kernel1, kernel2};
std::vector<std::string> programStrings;
programStrings.push_back(kernel1);
programStrings.push_back(kernel2);
cl::Program vectorAddProgram(programStrings);
try {
Expand Down Expand Up @@ -327,10 +336,9 @@
std::vector<int, cl::SVMAllocator<int, cl::SVMTraitCoarse<>>> inputA(numElements, 1, svmAlloc);
cl::coarse_svm_vector<int> inputB(numElements, 2, svmAlloc);
//
//////////////
// Traditional cl_mem allocations
std::vector<int> output(numElements, 0xdeadbeef);
cl::Buffer outputBuffer(begin(output), end(output), false);
cl::Pipe aPipe(sizeof(cl_int), numElements / 2);
Expand All @@ -354,14 +362,8 @@
// This one was not passed as a parameter
vectorAddKernel.setSVMPointers(anSVMInt);
// Hand control of coarse allocations to runtime
cl::enqueueUnmapSVM(anSVMInt);
cl::enqueueUnmapSVM(fooPointer);
cl::unmapSVM(inputB);
cl::unmapSVM(output2);
cl_int error;
vectorAddKernel(
cl_int error;
vectorAddKernel(
cl::EnqueueArgs(
cl::NDRange(numElements/2),
cl::NDRange(numElements/2)),
Expand All @@ -372,12 +374,10 @@
3,
aPipe,
defaultDeviceQueue,
error
error
);
cl::copy(outputBuffer, begin(output), end(output));
// Grab the SVM output vector using a map
cl::mapSVM(output2);
cl::Device d = cl::Device::getDefault();
Expand Down

0 comments on commit 814e7b2

Please sign in to comment.