3

Following the sample code in CUDA samples about C++ integration I was writing the wrapper functions that call CUDA code in a .cu file.

In that file I have a function that initializes CUDA context and allocates global memory and puts some data in constant memory.

extern "C" void initEngine(int *d_data, int *d_coefA, int *d_coefB)
{
   //Allocations
   //Copy to constant memory
}

Now what I want to do is overload that initEngine function to receive to more parameters but I think there's no way to overload that function as it has C linkage am I right?

extern "C" void initEngine(int *d_data, int *d_coefA, int *d_coefB, int *d_coefC, 
                           int *d_random)
{
   //Allocations
   //Copy to constant memory
}

Could you give me some advise to have only one initEngine function? or is not possible and I have to have 2 functions or probably have only one function with all the parameters and just pass NULL references or something like that.

BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • 1
    Why not make your own overloaded C++ function, that calls initEngine in the ways you want? – Neil Kirk Aug 22 '13 at 12:26
  • You mean, before calling any function in my `.cu` file (which has only extern "C" functions) create a function that checks the parameters and then calls the correct initEngine in the .cu file? I thought about that but I was trying to find a way (if possible) to do everything on the .cu file side. Thanks for answering by the way. – BRabbit27 Aug 22 '13 at 12:30
  • I don't know anything about CUDA so I can't help too much but you can't overload a C function, sorry :( – Neil Kirk Aug 22 '13 at 12:32
  • 1
    Why not get rid of the `extern "C"` declarations? You can just use C++ in your .cu file. – Tom Aug 22 '13 at 12:49
  • Can I just get rid of them and everything should work fine? I mean, I am using them because thats what I saw in the C++ integration sample from Nvidia. I'll try it. – BRabbit27 Aug 22 '13 at 13:40

2 Answers2

3

Yes, overloading is a C++ feature.

If you want to have overloaded functions that are callable from other modules, use C++ throughout (i.e. don't use extern "C", and keep all your files as .cpp or .cu).

If you don't wish to do that, you could try resorting to macros or some other magic.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Well getting rid of the `extern "C"` let indeed overload the function, but then... why/when would I need to use the `extern "C"` trick? was it used in previous versions of CUDA? I'll check again the C++ integration example. – BRabbit27 Aug 22 '13 at 13:59
  • `extern "C"` is used whenever you want to have a C++ compiled module linked with a C compiled module. The linkage structures are different, so `extern "C"` instructs the c++ linker to use c-style linkage, so that the C module and C++ module can interact. This is a C/C++ question and has nothing to do with CUDA, or which version of CUDA you are using. – Robert Crovella Aug 22 '13 at 14:07
  • Ok I see, . I agree is not a CUDA question but as I saw it on the CUDA samples and I was using it in a CUDA app I thought it was related to some feature in early CUDA versions, but now I know. Thanks for the help. – BRabbit27 Aug 22 '13 at 14:23
0

This probably doesn't address OP's concern, but I ran into this same issue myself when trying to link with cublas in C++ code. Instead of including cublas.h, I solved the problem with

#include <cublas_v2.h>

NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44