Array of Random Float Curand

Hi all, I’m starting to approach to CUDA and I’m trying to realize an array of random float using CuRand. I do not know why the code generates practically only zeros… I hope someone can help me.

I’m starting generating 10 numbers, but ofc the objective is to generate a really big array

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#include <curand.h>
#include <curand_kernel.h>
#include <cuda_runtime_api.h>

#define N 10

__global__ void init_seed(curandState *state, int seed) {
   int idx = blockIdx.x * blockDim.x + threadIdx.x;
   curand_init(seed, idx, 0, &state[idx]);
}
__global__ void D_rand(curandState *state, float *randArray) {
 int idx = blockIdx.x * blockDim.x + threadIdx.x;
  randArray[idx] = curand_uniform(&state[idx]);
}
int main() {

  float randArray[N];

  curandState *d_state;
  cudaMalloc(&d_state, (N* sizeof(curandState)));

  init_seed<<<1, N>>>(d_state, time(NULL));
  D_rand<<<1, N>>>(d_state, randArray);

  for (int i=0; i<N; i++)
    printf("%f   ;", randArray[i]);

 cudaFree(d_state);
 return 0;
}

You’ll need to learn more about CUDA. This is a host data array:

float randArray[N];

You cannot pass that array directly to the device and use it:

D_rand<<<1, N>>>(d_state, randArray);
                          ^^^^^^^^^

Instead, learn how the CUDA vectorAdd sample code works. Then you will learn how to pass data from host to device and back.

Also, it’s good practice to always use proper CUDA error checking, and run your codes with cuda-memcheck, any time you are having trouble with a CUDA code.

Not sure what “proper CUDA error checking” is? Google “proper CUDA error checking” and take the first hit, and apply it to your codes.

oh wow, that was a quite obvious mistake, I do not understand how i did not think about it!
Thanks for the hints!