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/ca0cb7fde0506537cac4 to your computer and use it in GitHub Desktop.
Save ginkgo/ca0cb7fde0506537cac4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <fstream>
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 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_ALL, cps);
// Get available devices
vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Device device = devices[DEVICE_ID];
// ---- Compile program ----
string compile_flags = "-cl-std=CL1.2";
string log;
string options;
vector<cl::Program> objects;
objects.emplace_back(context, test1_source);
objects.emplace_back(context, test2_source);
// 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 = obj.compile(compile_flags.c_str());
// 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;
}
obj.getBuildInfo(device, CL_PROGRAM_BUILD_LOG, &log);
obj.getBuildInfo(device, CL_PROGRAM_BUILD_OPTIONS, &options);
cout << "Compiler options: " << options << endl;
cout << "Compiler log:" << endl;
cout << log << endl;
}
// Link objects
string link_flags = "";
cl_int status_p;
cout << "--------------------------------------------------------------------------------" << endl;
cout << "LINKING PROGRAM:" << endl << endl;
cl::Program program = cl::linkProgram(objects[0], objects[1], link_flags.c_str(),
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;
}
program.getBuildInfo(device, CL_PROGRAM_BUILD_LOG, &log);
program.getBuildInfo(device, CL_PROGRAM_BUILD_OPTIONS, &options);
cout << "Linker options: " << options << endl;
cout << "Linker log:" << endl;
cout << log << endl;
}
// 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;
}
--------------------------------------------------------------------------------
COMPILING OBJECT 0:
Compilation of program object 0 succeeded
Compiler options: -cl-std=CL1.2
Compiler log:
--------------------------------------------------------------------------------
COMPILING OBJECT 1:
Compilation of program object 1 succeeded
Compiler options: -cl-std=CL1.2
Compiler log:
--------------------------------------------------------------------------------
LINKING PROGRAM:
Linkage of program succeeded
Linker options:
Linker log:
Warning: Input OpenCL binaries has inconsistent compile options. Using compile options from the first input binary!
DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment