sobol_gpu.cu 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 1993-2009 NVIDIA Corporation. All rights reserved.
  3. *
  4. * NVIDIA Corporation and its licensors retain all intellectual property and
  5. * proprietary rights in and to this software and related documentation and
  6. * any modifications thereto. Any use, reproduction, disclosure, or distribution
  7. * of this software and related documentation without an express license
  8. * agreement from NVIDIA Corporation is strictly prohibited.
  9. *
  10. */
  11. /*
  12. * Portions Copyright (c) 1993-2009 NVIDIA Corporation. All rights reserved.
  13. * Portions Copyright (c) 2009 Mike Giles, Oxford University. All rights reserved.
  14. * Portions Copyright (c) 2008 Frances Y. Kuo and Stephen Joe. All rights reserved.
  15. *
  16. * Sobol Quasi-random Number Generator example
  17. *
  18. * Based on CUDA code submitted by Mike Giles, Oxford University, United Kingdom
  19. * http://people.maths.ox.ac.uk/~gilesm/
  20. *
  21. * and C code developed by Stephen Joe, University of Waikato, New Zealand
  22. * and Frances Kuo, University of New South Wales, Australia
  23. * http://web.maths.unsw.edu.au/~fkuo/sobol/
  24. *
  25. * For theoretical background see:
  26. *
  27. * P. Bratley and B.L. Fox.
  28. * Implementing Sobol's quasirandom sequence generator
  29. * http://portal.acm.org/citation.cfm?id=42288
  30. * ACM Trans. on Math. Software, 14(1):88-100, 1988
  31. *
  32. * S. Joe and F. Kuo.
  33. * Remark on algorithm 659: implementing Sobol's quasirandom sequence generator.
  34. * http://portal.acm.org/citation.cfm?id=641879
  35. * ACM Trans. on Math. Software, 29(1):49-57, 2003
  36. *
  37. */
  38. #include "sobol.h"
  39. #include "sobol_gpu.h"
  40. #define k_2powneg32 2.3283064E-10F
  41. __global__ void sobolGPU_kernel(unsigned n_vectors, unsigned n_dimensions, unsigned *d_directions, float *d_output)
  42. {
  43. __shared__ unsigned int v[n_directions];
  44. // Offset into the correct dimension as specified by the
  45. // block y coordinate
  46. d_directions = d_directions + n_directions * blockIdx.y;
  47. d_output = d_output + n_vectors * blockIdx.y;
  48. // Copy the direction numbers for this dimension into shared
  49. // memory - there are only 32 direction numbers so only the
  50. // first 32 (n_directions) threads need participate.
  51. if (threadIdx.x < n_directions)
  52. {
  53. v[threadIdx.x] = d_directions[threadIdx.x];
  54. }
  55. __syncthreads();
  56. // Set initial index (i.e. which vector this thread is
  57. // computing first) and stride (i.e. step to the next vector
  58. // for this thread)
  59. int i0 = threadIdx.x + blockIdx.x * blockDim.x;
  60. int stride = gridDim.x * blockDim.x;
  61. // Get the gray code of the index
  62. // c.f. Numerical Recipes in C, chapter 20
  63. // http://www.nrbook.com/a/bookcpdf/c20-2.pdf
  64. unsigned int g = i0 ^ (i0 >> 1);
  65. // Initialisation for first point x[i0]
  66. // In the Bratley and Fox paper this is equation (*), where
  67. // we are computing the value for x[n] without knowing the
  68. // value of x[n-1].
  69. unsigned int X = 0;
  70. unsigned int mask;
  71. for (unsigned int k = 0 ; k < __ffs(stride) - 1 ; k++)
  72. {
  73. // We want X ^= g_k * v[k], where g_k is one or zero.
  74. // We do this by setting a mask with all bits equal to
  75. // g_k. In reality we keep shifting g so that g_k is the
  76. // LSB of g. This way we avoid multiplication.
  77. mask = - (g & 1);
  78. X ^= mask & v[k];
  79. g = g >> 1;
  80. }
  81. if (i0 < n_vectors)
  82. {
  83. d_output[i0] = (float)X * k_2powneg32;
  84. }
  85. // Now do rest of points, using the stride
  86. // Here we want to generate x[i] from x[i-stride] where we
  87. // don't have any of the x in between, therefore we have to
  88. // revisit the equation (**), this is easiest with an example
  89. // so assume stride is 16.
  90. // From x[n] to x[n+16] there will be:
  91. // 8 changes in the first bit
  92. // 4 changes in the second bit
  93. // 2 changes in the third bit
  94. // 1 change in the fourth
  95. // 1 change in one of the remaining bits
  96. //
  97. // What this means is that in the equation:
  98. // x[n+1] = x[n] ^ v[p]
  99. // x[n+2] = x[n+1] ^ v[q] = x[n] ^ v[p] ^ v[q]
  100. // ...
  101. // We will apply xor with v[1] eight times, v[2] four times,
  102. // v[3] twice, v[4] once and one other direction number once.
  103. // Since two xors cancel out, we can skip even applications
  104. // and just apply xor with v[4] (i.e. log2(16)) and with
  105. // the current applicable direction number.
  106. // Note that all these indices count from 1, so we need to
  107. // subtract 1 from them all to account for C arrays counting
  108. // from zero.
  109. unsigned int v_log2stridem1 = v[__ffs(stride) - 2];
  110. unsigned int v_stridemask = stride - 1;
  111. for (unsigned int i = i0 + stride ; i < n_vectors ; i += stride)
  112. {
  113. // x[i] = x[i-stride] ^ v[b] ^ v[c]
  114. // where b is log2(stride) minus 1 for C array indexing
  115. // where c is the index of the rightmost zero bit in i,
  116. // not including the bottom log2(stride) bits, minus 1
  117. // for C array indexing
  118. // In the Bratley and Fox paper this is equation (**)
  119. X ^= v_log2stridem1 ^ v[__ffs(~((i - stride) | v_stridemask)) - 1];
  120. d_output[i] = (float)X * k_2powneg32;
  121. }
  122. }
  123. extern "C"
  124. void sobolGPU(int n_vectors, int n_dimensions, unsigned int *d_directions, float *d_output)
  125. {
  126. const int threadsperblock = 64;
  127. // Set up the execution configuration
  128. dim3 dimGrid;
  129. dim3 dimBlock;
  130. // This implementation of the generator outputs all the draws for
  131. // one dimension in a contiguous region of memory, followed by the
  132. // next dimension and so on.
  133. // Therefore all threads within a block will be processing different
  134. // vectors from the same dimension. As a result we want the total
  135. // number of blocks to be a multiple of the number of dimensions.
  136. dimGrid.y = n_dimensions;
  137. // If the number of dimensions is large then we will set the number
  138. // of blocks to equal the number of dimensions (i.e. dimGrid.x = 1)
  139. // but if the number of dimensions is small (e.g. less than 32) then
  140. // we'll partition the vectors across blocks (as well as threads).
  141. // We also need to cap the dimGrid.x where the number of vectors
  142. // is too small to be partitioned.
  143. dimGrid.x = 1 + 31 / n_dimensions;
  144. if (dimGrid.x > (unsigned int)(n_vectors / threadsperblock))
  145. {
  146. dimGrid.x = (n_vectors + threadsperblock - 1) / threadsperblock;
  147. }
  148. // Fix the number of threads
  149. dimBlock.x = threadsperblock;
  150. // Execute GPU kernel
  151. sobolGPU_kernel<<<dimGrid, dimBlock>>>(n_vectors, n_dimensions, d_directions, d_output);
  152. }