0

curand_uniform() が 1.0 を返す回数を数えようとしています。ただし、次のコードが機能するようには見えません。

#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);
}

端末エラーが発生します (Linux の場合):

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

そして、私はこのようにコンパイルしています:

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

このエラー メッセージがわかりません。

4

1 に答える 1