Search code examples
cudathrustcurand

terminate called after throwing an instance of 'thrust::system::system_error' what(): parallel_for failed: cudaErrorInvalidValue: invalid argument


I am trying to count the number of times curand_uniform() returns 1.0. However i cant seem to get the following code to work for me:

#include <stdio.h>
#include <stdlib.h>  
#include <thrust/device_vector.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand_kernel.h>
using namespace std;

__global__
void counts(int length, int *sum, curandStatePhilox4_32_10_t*  state) {
  int tempsum = int(0);
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  curandStatePhilox4_32_10_t localState  =  state[i];
  for(; i < length; i += blockDim.x * gridDim.x) {
    double thisnum = curand_uniform( &localState );
    if ( thisnum == 1.0 ){
      tempsum += 1;
    }
  }
  atomicAdd(sum, tempsum);
}

__global__
void curand_setup(curandStatePhilox4_32_10_t *state, long seed) {
    int id = threadIdx.x + blockIdx.x * blockDim.x;
    curand_init(seed, id, 0, &state[id]);
}

int main(int argc, char *argv[]) {
  const int N = 1e5;

  int* count_h = 0;
  int* count_d;
  cudaMalloc(&count_d, sizeof(int) );
  cudaMemcpy(count_d, count_h, sizeof(int), cudaMemcpyHostToDevice);

  int threads_per_block = 64;
  int Nblocks = 32*6;

  thrust::device_vector<curandStatePhilox4_32_10_t> d_state(Nblocks*threads_per_block);
  curand_setup<<<Nblocks, threads_per_block>>>(d_state.data().get(), time(0));
  counts<<<Nblocks, threads_per_block>>>(N, count_d, d_state.data().get());

  cudaMemcpy(count_h, count_d, sizeof(int), cudaMemcpyDeviceToHost);

  cout << count_h << endl;

  cudaFree(count_d);
  free(count_h);
}

I am getting the terminal error (on linux):

terminate called after throwing an instance of 'thrust::system::system_error'
  what():  parallel_for failed: cudaErrorInvalidValue: invalid argument
Aborted (core dumped)

And i am compiling like this:

nvcc -Xcompiler "-fopenmp" -o test uniform_one_hit_count.cu

I don't understand this error message.


Solution

  • This line:

    thrust::device_vector<curandStatePhilox4_32_10_t> d_state(Nblocks*threads_per_block);
    

    is initializing a new vector on the device. When thrust does that, it calls the constructor for the object in use, in this case curandStatePhilox4_32_10, a struct whose definition is in /usr/local/cuda/include/curand_philox4x32_x.h (on linux, anyway). Unfortunately that struct definition doesn't provide any constructors decorated with __device__, and this is causing trouble for thrust.

    A simple workaround would be to assemble the vector on the host and copy it to the device:

    thrust::host_vector<curandStatePhilox4_32_10_t> h_state(Nblocks*threads_per_block);
    thrust::device_vector<curandStatePhilox4_32_10_t> d_state = h_state;
    

    Alternatively, just use cudaMalloc to allocate space:

    curandStatePhilox4_32_10_t *d_state;
    cudaMalloc(&d_state, (Nblocks*threads_per_block)*sizeof(d_state[0]));
    

    You have at least one other problem as well. This is not actually providing a proper allocation of storage for what the pointer should be pointing to:

    int* count_h = 0;
    

    after that, you should do something like:

    count_h = (int *)malloc(sizeof(int));
    memset(count_h, 0, sizeof(int));
    

    and on your print-out line, you most likely want to do this:

    cout << count_h[0] << endl;
    

    The other way to address the count_h issue would be to start with:

    int count_h = 0;
    

    and this would necessitate a different set of changes to your code (to the cudaMemcpy operations).