-1

I need to parallel the code that interprets an output of the underwater sonar. The code is heavily dependent on a dozen of constants specific to this device, and all of them are global constants. Is there any way to make those constants visible on device so I don't have to pass every single one of them as a kernel function parameter?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Barsik the Cat
  • 363
  • 1
  • 2
  • 10
  • 1
    Like.. `#define`? – Eugene Sh. May 08 '17 at 14:04
  • @EugeneSh. you mean constants declared with `#define` will actually be visible on device? Not a single source mentioned it. – Barsik the Cat May 08 '17 at 14:07
  • What do you mean "device"? It is visible by the code if defined in the proper scope.. – Eugene Sh. May 08 '17 at 14:07
  • @EugeneSh. just to make sure - have you ever worked with CUDA? – Barsik the Cat May 08 '17 at 14:08
  • 1
    `#define` is usable in device code, as are `const` variables of POD types, declared at global or file scope. Documentational support for the 2nd statement (`const`) is provided [here](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#const-variables). The programming guide also contains an example using `#define` [here](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared-memory) in the shared memory code example, for the constant `BLOCK_SIZE` – Robert Crovella May 08 '17 at 14:18
  • 1
    And there are questions discussing this such as [here](http://stackoverflow.com/questions/16119923/using-constants-with-cuda/19866287#19866287) – Robert Crovella May 08 '17 at 14:24
  • @RobertCrovella `#define` is not exactly an option - in screws a lot of array definitions down the code. and `const` global variables are not visible in device code. – Barsik the Cat May 08 '17 at 14:52
  • 2
    A `const` global variable of a POD type is definitely visible in device code according to some limitations. I have already pointed out the documentation reference that states this. I'm using "POD type" to loosely refer to the set of types for which it is supported, and the circumstances, as spelled out in the documentation. – Robert Crovella May 08 '17 at 14:54
  • @RobertCrovella what is POD? – Barsik the Cat May 08 '17 at 14:56
  • 1
    Please just read the documentation link I provided. It spells out the types and circumstances under which a `const` global variable is available in device code. – Robert Crovella May 08 '17 at 14:57
  • @RobertCrovella sorry, didn't see it first, it was several pages lower that the link shows. `OK except when the Microsoft compiler is used as the host compiler` MSVC screws me over again. Any workaround for constant float-point variables? – Barsik the Cat May 08 '17 at 15:08
  • 1
    OK, maybe now you should read [this link](http://stackoverflow.com/questions/16119923/using-constants-with-cuda/19866287#19866287) (<-- click here) which I've also already provided, which discusses `__constant__` variables. A non-array `__constant__` variable can be statically defined/initialized at global scope, including floating point variables in MSVC. – Robert Crovella May 08 '17 at 15:19

1 Answers1

0

If I understood your question, you want to use constant data from the GPU without passing it as a kernel parameter. You may declare your data as __constant__, and use cudaMemcpyToSymbol. The solutions proposed by RobertCrovella allow it for limited cases, but without the need for memcopy. You solely need to do the memcopy once.

Florent DUGUET
  • 2,786
  • 16
  • 28
  • Not really, I was looking for a way to do it without `cudaMemcpyToSymbol` to avoid flooding my code with repetitive operations. Unfortunately, you can't use `__constant__` with dynamically initialized global variables, so, apparently `cudaMemcpyToSymbol` is the only option here. – Barsik the Cat May 14 '17 at 05:24