| 1 |
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted provided that the following conditions |
| 5 |
* are met: |
| 6 |
* * Redistributions of source code must retain the above copyright |
| 7 |
* notice, this list of conditions and the following disclaimer. |
| 8 |
* * Redistributions in binary form must reproduce the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer in the |
| 10 |
* documentation and/or other materials provided with the distribution. |
| 11 |
* * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 |
* contributors may be used to endorse or promote products derived |
| 13 |
* from this software without specific prior written permission. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "../inc/piestimator.h" |
| 29 |
|
| 30 |
#include <string> |
| 31 |
#include <vector> |
| 32 |
#include <numeric> |
| 33 |
#include <stdexcept> |
| 34 |
#include <typeinfo> |
| 35 |
#include <cuda_runtime.h> |
| 36 |
#include <cooperative_groups.h> |
| 37 |
|
| 38 |
namespace cg = cooperative_groups; |
| 39 |
#include <curand_kernel.h> |
| 40 |
|
| 41 |
using std::string; |
| 42 |
using std::vector; |
| 43 |
|
| 44 |
// RNG init kernel |
| 45 |
__global__ void initRNG(curandState *const rngStates, const unsigned int seed) { |
| 46 |
// Determine thread ID |
| 47 |
unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x; |
| 48 |
|
| 49 |
// Initialise the RNG |
| 50 |
curand_init(seed, tid, 0, &rngStates[tid]); |
| 51 |
} |
| 52 |
|
| 53 |
__device__ unsigned int reduce_sum(unsigned int in, cg::thread_block cta) { |
| 54 |
extern __shared__ unsigned int sdata[]; |
| 55 |
|
| 56 |
// Perform first level of reduction: |
| 57 |
// - Write to shared memory |
| 58 |
unsigned int ltid = threadIdx.x; |
| 59 |
|
| 60 |
sdata[ltid] = in; |
| 61 |
cg::sync(cta); |
| 62 |
|
| 63 |
// Do reduction in shared mem |
| 64 |
for (unsigned int s = blockDim.x / 2; s > 0; s >>= 1) { |
| 65 |
if (ltid < s) { |
| 66 |
sdata[ltid] += sdata[ltid + s]; |
| 67 |
} |
| 68 |
|
| 69 |
cg::sync(cta); |
| 70 |
} |
| 71 |
|
| 72 |
return sdata[0]; |
| 73 |
} |
| 74 |
|
| 75 |
__device__ inline void getPoint(float &x, float &y, curandState &state) { |
| 76 |
x = curand_uniform(&state); |
| 77 |
y = curand_uniform(&state); |
| 78 |
} |
| 79 |
__device__ inline void getPoint(double &x, double &y, curandState &state) { |
| 80 |
x = curand_uniform_double(&state); |
| 81 |
y = curand_uniform_double(&state); |
| 82 |
} |
| 83 |
|
| 84 |
// Estimator kernel |
| 85 |
template <typename Real> |
| 86 |
__global__ void computeValue(unsigned int *const results, |
| 87 |
curandState *const rngStates, |
| 88 |
const unsigned int numSims) { |
| 89 |
// Handle to thread block group |
| 90 |
cg::thread_block cta = cg::this_thread_block(); |
| 91 |
// Determine thread ID |
| 92 |
unsigned int bid = blockIdx.x; |
| 93 |
unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x; |
| 94 |
unsigned int step = gridDim.x * blockDim.x; |
| 95 |
|
| 96 |
// Initialise the RNG |
| 97 |
curandState localState = rngStates[tid]; |
| 98 |
|
| 99 |
// Count the number of points which lie inside the unit quarter-circle |
| 100 |
unsigned int pointsInside = 0; |
| 101 |
|
| 102 |
for (unsigned int i = tid; i < numSims; i += step) { |
| 103 |
Real x; |
| 104 |
Real y; |
| 105 |
getPoint(x, y, localState); |
| 106 |
Real l2norm2 = x * x + y * y; |
| 107 |
|
| 108 |
if (l2norm2 < static_cast<Real>(1)) { |
| 109 |
pointsInside++; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Reduce within the block |
| 114 |
pointsInside = reduce_sum(pointsInside, cta); |
| 115 |
|
| 116 |
// Store the result |
| 117 |
if (threadIdx.x == 0) { |
| 118 |
results[bid] = pointsInside; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
template <typename Real> |
| 123 |
PiEstimator<Real>::PiEstimator(unsigned int numSims, unsigned int device, |
| 124 |
unsigned int threadBlockSize, unsigned int seed) |
| 125 |
: m_numSims(numSims), |
| 126 |
m_device(device), |
| 127 |
m_threadBlockSize(threadBlockSize), |
| 128 |
m_seed(seed) {} |
| 129 |
|
| 130 |
template <typename Real> |
| 131 |
Real PiEstimator<Real>::operator()() { |
| 132 |
cudaError_t cudaResult = cudaSuccess; |
| 133 |
struct cudaDeviceProp deviceProperties; |
| 134 |
struct cudaFuncAttributes funcAttributes; |
| 135 |
|
| 136 |
// Get device properties |
| 137 |
cudaResult = cudaGetDeviceProperties(&deviceProperties, m_device); |
| 138 |
|
| 139 |
if (cudaResult != cudaSuccess) { |
| 140 |
string msg("Could not get device properties: "); |
| 141 |
msg += cudaGetErrorString(cudaResult); |
| 142 |
throw std::runtime_error(msg); |
| 143 |
} |
| 144 |
|
| 145 |
// Check precision is valid |
| 146 |
if (typeid(Real) == typeid(double) && |
| 147 |
(deviceProperties.major < 1 || |
| 148 |
(deviceProperties.major == 1 && deviceProperties.minor < 3))) { |
| 149 |
throw std::runtime_error("Device does not have double precision support"); |
| 150 |
} |
| 151 |
|
| 152 |
// Attach to GPU |
| 153 |
cudaResult = cudaSetDevice(m_device); |
| 154 |
|
| 155 |
if (cudaResult != cudaSuccess) { |
| 156 |
string msg("Could not set CUDA device: "); |
| 157 |
msg += cudaGetErrorString(cudaResult); |
| 158 |
throw std::runtime_error(msg); |
| 159 |
} |
| 160 |
|
| 161 |
// Determine how to divide the work between cores |
| 162 |
dim3 block; |
| 163 |
dim3 grid; |
| 164 |
block.x = m_threadBlockSize; |
| 165 |
grid.x = (m_numSims + m_threadBlockSize - 1) / m_threadBlockSize; |
| 166 |
|
| 167 |
// Aim to launch around ten or more times as many blocks as there |
| 168 |
// are multiprocessors on the target device. |
| 169 |
unsigned int blocksPerSM = 10; |
| 170 |
unsigned int numSMs = deviceProperties.multiProcessorCount; |
| 171 |
|
| 172 |
while (grid.x > 2 * blocksPerSM * numSMs) { |
| 173 |
grid.x >>= 1; |
| 174 |
} |
| 175 |
|
| 176 |
// Get initRNG function properties and check the maximum block size |
| 177 |
cudaResult = cudaFuncGetAttributes(&funcAttributes, initRNG); |
| 178 |
|
| 179 |
if (cudaResult != cudaSuccess) { |
| 180 |
string msg("Could not get function attributes: "); |
| 181 |
msg += cudaGetErrorString(cudaResult); |
| 182 |
throw std::runtime_error(msg); |
| 183 |
} |
| 184 |
|
| 185 |
if (block.x > (unsigned int)funcAttributes.maxThreadsPerBlock) { |
| 186 |
throw std::runtime_error( |
| 187 |
"Block X dimension is too large for initRNG kernel"); |
| 188 |
} |
| 189 |
|
| 190 |
// Get computeValue function properties and check the maximum block size |
| 191 |
cudaResult = cudaFuncGetAttributes(&funcAttributes, computeValue<Real>); |
| 192 |
|
| 193 |
if (cudaResult != cudaSuccess) { |
| 194 |
string msg("Could not get function attributes: "); |
| 195 |
msg += cudaGetErrorString(cudaResult); |
| 196 |
throw std::runtime_error(msg); |
| 197 |
} |
| 198 |
|
| 199 |
if (block.x > (unsigned int)funcAttributes.maxThreadsPerBlock) { |
| 200 |
throw std::runtime_error( |
| 201 |
"Block X dimension is too large for computeValue kernel"); |
| 202 |
} |
| 203 |
|
| 204 |
// Check the dimensions are valid |
| 205 |
if (block.x > (unsigned int)deviceProperties.maxThreadsDim[0]) { |
| 206 |
throw std::runtime_error("Block X dimension is too large for device"); |
| 207 |
} |
| 208 |
|
| 209 |
if (grid.x > (unsigned int)deviceProperties.maxGridSize[0]) { |
| 210 |
throw std::runtime_error("Grid X dimension is too large for device"); |
| 211 |
} |
| 212 |
|
| 213 |
// Allocate memory for RNG states |
| 214 |
curandState *d_rngStates = 0; |
| 215 |
cudaResult = |
| 216 |
cudaMalloc((void **)&d_rngStates, grid.x * block.x * sizeof(curandState)); |
| 217 |
|
| 218 |
if (cudaResult != cudaSuccess) { |
| 219 |
string msg("Could not allocate memory on device for RNG states: "); |
| 220 |
msg += cudaGetErrorString(cudaResult); |
| 221 |
throw std::runtime_error(msg); |
| 222 |
} |
| 223 |
|
| 224 |
// Allocate memory for result |
| 225 |
// Each thread block will produce one result |
| 226 |
unsigned int *d_results = 0; |
| 227 |
cudaResult = cudaMalloc((void **)&d_results, grid.x * sizeof(unsigned int)); |
| 228 |
|
| 229 |
if (cudaResult != cudaSuccess) { |
| 230 |
string msg("Could not allocate memory on device for partial results: "); |
| 231 |
msg += cudaGetErrorString(cudaResult); |
| 232 |
throw std::runtime_error(msg); |
| 233 |
} |
| 234 |
|
| 235 |
// Initialise RNG |
| 236 |
initRNG<<<grid, block>>>(d_rngStates, m_seed); |
| 237 |
|
| 238 |
// Count the points inside unit quarter-circle |
| 239 |
computeValue<Real><<<grid, block, block.x * sizeof(unsigned int)>>>( |
| 240 |
d_results, d_rngStates, m_numSims); |
| 241 |
|
| 242 |
// Copy partial results back |
| 243 |
vector<unsigned int> results(grid.x); |
| 244 |
cudaResult = cudaMemcpy(&results[0], d_results, grid.x * sizeof(unsigned int), |
| 245 |
cudaMemcpyDeviceToHost); |
| 246 |
|
| 247 |
if (cudaResult != cudaSuccess) { |
| 248 |
string msg("Could not copy partial results to host: "); |
| 249 |
msg += cudaGetErrorString(cudaResult); |
| 250 |
throw std::runtime_error(msg); |
| 251 |
} |
| 252 |
|
| 253 |
// Complete sum-reduction on host |
| 254 |
Real value = |
| 255 |
static_cast<Real>(std::accumulate(results.begin(), results.end(), 0)); |
| 256 |
|
| 257 |
// Determine the proportion of points inside the quarter-circle, |
| 258 |
// i.e. the area of the unit quarter-circle |
| 259 |
value /= m_numSims; |
| 260 |
|
| 261 |
// Value is currently an estimate of the area of a unit quarter-circle, so we |
| 262 |
// can scale to a full circle by multiplying by four. Now since the area of a |
| 263 |
// circle is pi * r^2, and r is one, the value will be an estimate for the |
| 264 |
// value of pi. |
| 265 |
value *= 4; |
| 266 |
|
| 267 |
// Cleanup |
| 268 |
if (d_rngStates) { |
| 269 |
cudaFree(d_rngStates); |
| 270 |
d_rngStates = 0; |
| 271 |
} |
| 272 |
|
| 273 |
if (d_results) { |
| 274 |
cudaFree(d_results); |
| 275 |
d_results = 0; |
| 276 |
} |
| 277 |
|
| 278 |
return value; |
| 279 |
} |
| 280 |
|
| 281 |
// Explicit template instantiation |
| 282 |
template class PiEstimator<float>; |
| 283 |
template class PiEstimator<double>; |