How to have OpenCL and gcc in same project with different targets?

Hi. I’m trying to learn OpenCL. I have a background in C/C++ and Java programming. I want to compile my project first just in gcc and test out the functions and then later I want to use the same code in OpenCL. It should be possible to have both C and OpenCL code in the same project since OpenCL uses C as the language. I can then specify with a precompiler flag that I want to use gcc or OpenCL kernel to run. This should be similar how I used to create both Linux and Windows executable from the same code base.

Something like

// Use gcc
#ifndef USE_OPENCI
#include <math.h>
#include <stdlib.h>
#include <CL/cl_platform.h>
#else
// Use OpenCI kernel
#endif

float4 do_vector_stuff(double* data) {
}

Is this actually possible? Is it even what I should be doing? Again, I just want to test my code with gcc and then at a later stage to run it in OpenCL.

hi,

the question is not very clear.

you want the same project code for two different OS. It wont work. only the OpenCL code “Kernel” may be portable, it depends ;)).

Thank you for the reply. I actually already finished what I asked here. Using the clang compiler and the float2, float3, etc. extensions it was quite easy. I used a precompiler flag USE_OPENCL to have the (-almost- it’s actually the same code because the opencl_utils.h will act as a bridge) same code for both C and OpenCL. Now I can use the code base and test it with google-test using the clang compiler and then use OpenCL to run the kernel.

You can check out the code here:
https://github.com/devent/anl-opencl/tree/develop/anlopencl/src/main/cpp
And the tests:
https://github.com/devent/anl-opencl/tree/develop/anlopencl/src/test/cpp
This header will bridge the code between clang and OpenCL:
https://github.com/devent/anl-opencl/blob/develop/anlopencl/src/main/cpp/opencl_utils.h

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.