Reputation: 1575
So lets say I have code like the following:
//MyFile.h
void MyFunction;
//MyFile.cpp
#include "MyFile.h"
void MyFunction()
{
float foo = sqrtf(85.3);
}
Now, my actual function(s) I'm using are more complicated but the main point is that they make use of functions like sqrtf(), sin(), etc.
I want to use this function both on the host and in my kernel but I don't want to define it twice because that seems like bad practice. Right now I try to pass "MyFile.h" into my cl_program using the method clCreateProgramWithSource().
If I run this on the device as is it would work fine because openCL has built in math functions. However, if I want it to run on the host, I need to include cmath or another library in order to define sqrt, sin, etc. But then if I include cmath so that it works on the host, it would no longer be able to run on the device because openCL can't include/import cmath.
So I can't seem to find a way to define it once and use it in both places. If the host weren't dependent on cmath to get those math functions it would not be an issue, but currently I don't know what to do.
Upvotes: 1
Views: 1212
Reputation: 29724
All you need then is to add conditional statement to include if this is being compiled on host. There are #ifdef/#ifndef
preprocessor macros that will check for the presence of definition and take actions based on this test
#ifndef __OPENCL_C_VERSION__
// I am host, so include <cmath>
#include <cmath>
#else
// I am device
// ...
#endif
https://gcc.gnu.org/onlinedocs/cpp/Ifdef.html
Upvotes: 1
Reputation: 9925
You can test for the presence (or absence) of an OpenCL C specific preprocessor macro to optionally include host-only header files.
For example:
#ifndef __OPENCL_C_VERSION__
#include <cmath>
#endif
Upvotes: 2