Skip to content

Instantly share code, notes, and snippets.

@ginkgo
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginkgo/cfcb51f95c2da221a259 to your computer and use it in GitHub Desktop.
Save ginkgo/cfcb51f95c2da221a259 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <fstream>
#include <assert.h>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
// Default OpenCL platform and device IDs
#define PLATFORM_ID 0
#define DEVICE_ID 0
string test1_source = " void func1 () \n"
"{ \n"
" \n"
"} \n"
" \n"
"void kernel test1(__global int* a) \n"
"{ \n"
" a[get_global_id(0)] = 1; \n"
"} \n";
string test2_source = "void kernel test2(__global int* b) \n"
"{ \n"
" b[get_global_id(0)] = 2; \n"
"} \n"
" \n"
"void kernel test3(__global int* b) \n"
"{ \n"
" b[get_global_id(0)] = 2; \n"
"} \n";
void print_program_build_info(cl_program program, cl_device_id dev, const string phase)
{
size_t size = 8*1024;
char buffer[size];
cl_int status = clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_OPTIONS, size, buffer, NULL);
assert(status == CL_SUCCESS);
cout << phase << " options: " << buffer << endl;
status = clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG, size, buffer, NULL);
assert(status == CL_SUCCESS);
cout << phase << " log: " << endl
<< buffer << endl;
}
void test_opencl()
{
// ---- Setup OpenCL device and command queue ----
// Get available platforms
vector<cl::Platform> platforms;
// Select default platform and create a context
cl::Platform::get(&platforms);
cl_context_properties cps[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[PLATFORM_ID])(), 0};
cl::Context context(CL_DEVICE_TYPE_GPU, cps);
// Get available devices
vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Device device = devices[DEVICE_ID];
// Get C API handles
cl_context clctx = context();
cl_device_id cldev = device();
// ---- Compile program ----
string compile_flags = "-cl-std=CL1.2";
string log;
string options;
vector<cl_program> objects;
cl_int status;
vector<const char*> srcstrn = {test1_source.c_str()};
vector<size_t> srcsize = {test1_source.size()};
objects.push_back(clCreateProgramWithSource(clctx, 1, srcstrn.data(), srcsize.data(), &status));
assert(status == CL_SUCCESS);
srcstrn = {test2_source.c_str()};
srcsize = {test2_source.size()};
objects.push_back(clCreateProgramWithSource(clctx, 1, srcstrn.data(), srcsize.data(), &status));
assert(status == CL_SUCCESS);
// Compile indidividual objects
for (size_t i = 0; i < objects.size(); ++i) {
cl_program obj = objects[i];
cout << "--------------------------------------------------------------------------------" << endl;
cout << "COMPILING OBJECT " << i << ":" << endl << endl;
cl_int c_status = clCompileProgram(obj, 1, &cldev, compile_flags.c_str(),
0,nullptr, nullptr, nullptr, nullptr);
// Check compilation success
if (c_status == CL_SUCCESS) {
cout << "Compilation of program object " << i << " succeeded" << endl;
} else {
cout << "Compilation of program object " << i << " returned " << c_status << endl;
}
print_program_build_info(obj, cldev, "Compiler");
}
// Link objects
string link_flags = "";
cl_int status_p;
cout << "--------------------------------------------------------------------------------" << endl;
cout << "LINKING PROGRAM:" << endl << endl;
cl_program program = clLinkProgram(clctx, 1, &cldev, link_flags.c_str(),
objects.size(), objects.data(),
nullptr, nullptr, &status_p);
// Check linker success
if (status_p == CL_SUCCESS) {
cout << "Linkage of program succeeded" << endl;
} else {
cout << "Linkage of program returned " << status_p << endl;
}
print_program_build_info(program, cldev, "Linker");
}
// Main function
int main (int argc, char** argv)
{
cl_int status = CL_SUCCESS;
try {
test_opencl();
} catch (cl::Error err) {
cerr << "ERROR: " << err.what() << "(" << err.err() << ")" << endl;
return 1;
}
cout << "DONE" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment