Generating sample from a non uniform discrete distribution.

Hi,

I have a discrete distribution represented by a CudaTensor (I’m using torch) of the probability of each element, and I would like to sample a bunch of values from this distribution.
Apparently there is curand_discrete which can do this, but its argument is a curandDiscreteDistribution_t, and it’s not very clear to me how I should create this. curand_discrete ends up calling _curand_M2_double, which uses three values in the histogram: V, K and shift.
How should this be set up?

Best regards.

I tried to look at what curandCreatePoissonDistribution creates, but in gdb I see:

(gdb) p *poisson_distribution
$1 = {self_host_ptr = 0x0, M2 = 0x0, host_M2 = 0x0, stddev = 0, mean = 0, method = CURAND_CHOOSE_BEST, host_gen = 0}

I also have tried to create the distribtion myself:

curandHistogramM2_t histogram = (curandHistogramM2_t) malloc(sizeof(curandHistogramM2_t));
  histogram->host_V = (double*) malloc(vocabSize * sizeof(double));
  histogram->host_K = (unsigned int*) malloc(vocabSize * sizeof(unsigned int));
  squareHistogram(unigram_data, vocabSize, histogram->host_V, histogram->host_K);

  printf("Ok3\n"); 
  THCudaCheck(cudaMalloc((void**)&histogram->V, vocabSize * sizeof(double)));
  THCudaCheck(cudaMemcpy(histogram->V, histogram->host_V, vocabSize * sizeof(double), cudaMemcpyHostToDevice));

  THCudaCheck(cudaMalloc((void**)&histogram->K, vocabSize * sizeof(unsigned int)));
  THCudaCheck(cudaMemcpy(histogram->K, histogram->host_K, vocabSize * sizeof(unsigned int), cudaMemcpyHostToDevice));

  printf("Ok4\n"); 
  curandDistributionM2Shift_t M2 = (curandDistributionM2Shift_t) malloc(sizeof(curandDistributionM2Shift_t));
  M2->histogram = histogram;
  M2->shift = 1;
  M2->length = vocabSize;
  printf("Ok5\n"); 
  curandDiscreteDistribution_t curand_distribution = (curandDiscreteDistribution_t) malloc(sizeof(curandDiscreteDistribution_t));;
  curand_distribution->M2 = M2;
  curand_distribution->method = CURAND_M2;

// Kernel call
  CURAND_CALL(curandDestroyDistribution(curand_distribution));

But I get some segmentation faults.