1
$\begingroup$
Needs["CUDALink`"]
gausvSource = "
#include <curand_kernel.h>
__global__ void gausv( float *out  )
{
    curandState_t curand_state;
    curand_init ( 0, 0, 0, &curand_state );
    *out = curand_normal( &curand_state );
}";
CUDAFunctionLoad[gausvSource, "gausv", {{"Float", _, "Output"}}, 256]

yields a series of errors like:

curand_kernel.h(558): error: this declaration may not have extern "C" linkage

Wrapping the include, the function, or the whole thing in extern "C" { } changes the message to:

CUDALink encountered an invalid program.

So, how does one correctly use library code with CUDALink?

$\endgroup$

1 Answer 1

3
$\begingroup$

The compiler accepts the following code :

Needs["CUDALink`"]
gausvSource = "
}
#include <curand_kernel.h>
extern \"C\" {
__global__ void gausv( float *out  )
{
    curandState_t curand_state;
    curand_init ( 0, 0, 0, &curand_state );
    *out = curand_normal( &curand_state );
}";
cudaFun=CUDAFunctionLoad[gausvSource, "gausv", {{"Float", _, "Output"}}, 256]  

The function is callable :

x=1.
cudaFun[{x}]  

{{0.00459315}}

I have not tested further.

The idea is that Mathematica encloses the whole CUDA-C program in a extern { }, so one can exit it with a } (the third line of the code above).

$\endgroup$
2
  • 1
    $\begingroup$ my "answer" is uniquely for information, not sure that's a good advice $\endgroup$
    – andre314
    Commented Apr 11, 2018 at 20:37
  • $\begingroup$ Good answer. It was crashing my kernel until I rebooted Ubuntu. Now it's working. CUDAlink seems a bit fragile. Thank you. $\endgroup$
    – John Doty
    Commented Apr 12, 2018 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.