sobol_gpu.cu 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <starpu.h>
  41. #define k_2powneg32 2.3283064E-10F
  42. __global__ void sobolGPU_kernel(unsigned n_vectors, unsigned n_dimensions, unsigned *d_directions, float *d_output)
  43. {
  44. __shared__ unsigned int v[n_directions];
  45. // Offset into the correct dimension as specified by the
  46. // block y coordinate
  47. d_directions = d_directions + n_directions * blockIdx.y;
  48. d_output = d_output + n_vectors * blockIdx.y;
  49. // Copy the direction numbers for this dimension into shared
  50. // memory - there are only 32 direction numbers so only the
  51. // first 32 (n_directions) threads need participate.
  52. if (threadIdx.x < n_directions)
  53. {
  54. v[threadIdx.x] = d_directions[threadIdx.x];
  55. }
  56. __syncthreads();
  57. // Set initial index (i.e. which vector this thread is
  58. // computing first) and stride (i.e. step to the next vector
  59. // for this thread)
  60. int i0 = threadIdx.x + blockIdx.x * blockDim.x;
  61. int stride = gridDim.x * blockDim.x;
  62. // Get the gray code of the index
  63. // c.f. Numerical Recipes in C, chapter 20
  64. // http://www.nrbook.com/a/bookcpdf/c20-2.pdf
  65. unsigned int g = i0 ^ (i0 >> 1);
  66. // Initialisation for first point x[i0]
  67. // In the Bratley and Fox paper this is equation (*), where
  68. // we are computing the value for x[n] without knowing the
  69. // value of x[n-1].
  70. unsigned int X = 0;
  71. unsigned int mask;
  72. for (unsigned int k = 0 ; k < __ffs(stride) - 1 ; k++)
  73. {
  74. // We want X ^= g_k * v[k], where g_k is one or zero.
  75. // We do this by setting a mask with all bits equal to
  76. // g_k. In reality we keep shifting g so that g_k is the
  77. // LSB of g. This way we avoid multiplication.
  78. mask = - (g & 1);
  79. X ^= mask & v[k];
  80. g = g >> 1;
  81. }
  82. if (i0 < n_vectors)
  83. {
  84. d_output[i0] = (float)X * k_2powneg32;
  85. }
  86. // Now do rest of points, using the stride
  87. // Here we want to generate x[i] from x[i-stride] where we
  88. // don't have any of the x in between, therefore we have to
  89. // revisit the equation (**), this is easiest with an example
  90. // so assume stride is 16.
  91. // From x[n] to x[n+16] there will be:
  92. // 8 changes in the first bit
  93. // 4 changes in the second bit
  94. // 2 changes in the third bit
  95. // 1 change in the fourth
  96. // 1 change in one of the remaining bits
  97. //
  98. // What this means is that in the equation:
  99. // x[n+1] = x[n] ^ v[p]
  100. // x[n+2] = x[n+1] ^ v[q] = x[n] ^ v[p] ^ v[q]
  101. // ...
  102. // We will apply xor with v[1] eight times, v[2] four times,
  103. // v[3] twice, v[4] once and one other direction number once.
  104. // Since two xors cancel out, we can skip even applications
  105. // and just apply xor with v[4] (i.e. log2(16)) and with
  106. // the current applicable direction number.
  107. // Note that all these indices count from 1, so we need to
  108. // subtract 1 from them all to account for C arrays counting
  109. // from zero.
  110. unsigned int v_log2stridem1 = v[__ffs(stride) - 2];
  111. unsigned int v_stridemask = stride - 1;
  112. for (unsigned int i = i0 + stride ; i < n_vectors ; i += stride)
  113. {
  114. // x[i] = x[i-stride] ^ v[b] ^ v[c]
  115. // where b is log2(stride) minus 1 for C array indexing
  116. // where c is the index of the rightmost zero bit in i,
  117. // not including the bottom log2(stride) bits, minus 1
  118. // for C array indexing
  119. // In the Bratley and Fox paper this is equation (**)
  120. X ^= v_log2stridem1 ^ v[__ffs(~((i - stride) | v_stridemask)) - 1];
  121. d_output[i] = (float)X * k_2powneg32;
  122. }
  123. }
  124. extern "C"
  125. void sobolGPU(int n_vectors, int n_dimensions, unsigned int *d_directions, float *d_output)
  126. {
  127. const int threadsperblock = 64;
  128. // Set up the execution configuration
  129. dim3 dimGrid;
  130. dim3 dimBlock;
  131. // This implementation of the generator outputs all the draws for
  132. // one dimension in a contiguous region of memory, followed by the
  133. // next dimension and so on.
  134. // Therefore all threads within a block will be processing different
  135. // vectors from the same dimension. As a result we want the total
  136. // number of blocks to be a multiple of the number of dimensions.
  137. dimGrid.y = n_dimensions;
  138. // If the number of dimensions is large then we will set the number
  139. // of blocks to equal the number of dimensions (i.e. dimGrid.x = 1)
  140. // but if the number of dimensions is small (e.g. less than 32) then
  141. // we'll partition the vectors across blocks (as well as threads).
  142. // We also need to cap the dimGrid.x where the number of vectors
  143. // is too small to be partitioned.
  144. dimGrid.x = 1 + 31 / n_dimensions;
  145. if (dimGrid.x > (unsigned int)(n_vectors / threadsperblock))
  146. {
  147. dimGrid.x = (n_vectors + threadsperblock - 1) / threadsperblock;
  148. }
  149. // Fix the number of threads
  150. dimBlock.x = threadsperblock;
  151. // Execute GPU kernel
  152. sobolGPU_kernel<<<dimGrid, dimBlock, 0, starpu_cuda_get_local_stream()>>>(n_vectors, n_dimensions, d_directions, d_output);
  153. }