Generate TRandom in parallel with GPU and CUDA

I wrote a simple CUDA code with ROOT library TRandom3 included:

#include <cuda_runtime.h>
#include <iostream>
#include <stdio.h>
#include <TRandom.h>
#include <TRandom3.h>

using namespace std;

__global__ void Random(void) {
        TRandom *casuale = new TRandom3();
        int a = casuale -> Integer(100);
        printf("%d ----> %d",a,threadIdx.x);
}

int main() {
        Random <<<1,10>>>();
        cudaDeviceReset();

        return 0;
}

This should generate 10 integer random numbers up to 100 IN PARALLEL!
But when i compile this code i get:

casa@casa-server:~/programmi/gpu$ nvcc prova_cuda.cu -m64 -L/usr/local/lib/root -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -lm -ldl
prova_cuda.cu(11): error: calling a __host__ function("Integer") from a __global__ function("Random") is not allowed

1 error detected in the compilation of "/tmp/tmpxft_00000d6a_00000000-8_prova_cuda.cpp1.ii".

What i have to do?

Hi, you cannot call any kind of code in CUDA code. What the compiler is saying is that you are trying to call code compiled for the CPU only (__host__) in the GPU (__device__). TRandom does not have support for CUDA, unfortunately, so if you want to generate random numbers on the GPU, I recommend looking into the Nvidia documentation for curand or similar. Cheers,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.