3
$\begingroup$

I used Mathematica/wolfram 12.1 IDE and installed CUDA toolkit 10.2. All installed correct. I try to use library CURAND with #include with random integer generator on CUDA, but get a error:

Message[CUDAFunctionLoad::cmperr, "PATH_CUDA_TOOLKIT/.../include\\curand_kernel.h: \ error: this declaration may not have extern \"C\" linkage"]

What is wrong, with my code?

secondKernelCode = "
  #include<curand_kernel.h> // problem here !!!
  // my kernel
  __device__ float f(float x) {
     return tanf(x);
  }

  __global__ void secondKernel(float * a, float * b, float * c, float* d, mint nIterations) {
  int index = threadIdx.x + blockIdx.x * blockDim.x;
  //....
  }";

secondKernel = CUDAFunctionLoad[secondKernelCode, "secondKernel", {{"Float"}, {"Float"}, {"Float"}, {"Float"}, _Integer}, 16]
$\endgroup$
3
  • $\begingroup$ related $\endgroup$
    – andre314
    Commented Jun 4, 2020 at 5:54
  • $\begingroup$ See also the Wolfram Community, a question from John Doty $\endgroup$
    – andre314
    Commented Jun 4, 2020 at 5:55
  • $\begingroup$ Thanks! It is good work. $\endgroup$
    – Alex Titov
    Commented Jun 4, 2020 at 7:30

1 Answer 1

3
$\begingroup$

It's worked. Extern "C" makes a function-name in mathematica have 'C' linkage. Code show below.

(*CUDA-function code*)
secondKernelCode = "
}
 #include <curand_kernel.h>
 extern \"C\" {
 __device__ float f(float x) {
  return tanf(x);
}

__global__ void secondKernel(float * a, float * b, float * c, float* d, mint nIterations) {
 int index = threadIdx.x + blockIdx.x * blockDim.x;
 curandState_t curand_state;
  curand_init ( 0, 0, 0, &curand_state );
  *a = curand_normal( &curand_state );
  *b= curand_normal( &curand_state );
  *c = curand_normal( &curand_state );
  *d = curand_normal( &curand_state );
}";

(*CUDA function load*)
secondKernel = CUDAFunctionLoad[secondKernelCode, "secondKernel", {{"Float"}, {"Float"}, {"Float"}, {"Float"}, _Integer}, 16]

Out[1] = CUDAFunction["<>", "secondKernel", {{"Float"}, {"Float"}, {"Float"}, {"Float"}, "Integer64"}]

The function call from mathematica

a = 10;
b = 1;
c = 1;
d = 1;
secondKernel[{a}, {a}, {a}, {a}, 1]

Out[2]={{0.292537}, {-0.718359}, {0.958011}, {0.633711}}

$\endgroup$
1

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.