Globals in host and device code?

I have a curiosity about the best way to handle this. I have a simulation which has macroscopic parameters controlled by a structure in global memory, with some host functions for getting and setting the globals via cudaMemcpyToSymbol. This works well.

However, some of the functions are declared as device and host so that I can have a unit testing framework exercise the individual functions. Some of those functions refer to the globals, so they can’t be exercised from the host, because of course the host can’t access the global in the same way.

I tried implementing a host-side globals structure, which would work–but I’d need some way for the functions themselves to tell if they were running on the device or the host, and I can’t find a way to do that. Is there any good pattern for this sort of thing?

This is certainly not the nicest way, but I would do this by passing an extra parameter which indicates if it is called from the host or the device. Therefore you need some additional if-statements and the performance could be decreased, so this is really just a workaround…

Or you can do your unit testing just in emulation mode. Then you can use the given DEVICE_EMULATION