| 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 |
|
| 29 |
#include "../inc/test.h" |
| 30 |
|
| 31 |
#include <sstream> |
| 32 |
#include <iomanip> |
| 33 |
#include <stdexcept> |
| 34 |
#include <memory> |
| 35 |
#include <iostream> |
| 36 |
#include <cassert> |
| 37 |
#include <typeinfo> |
| 38 |
#include <stdio.h> |
| 39 |
#include <helper_timer.h> |
| 40 |
#include <math.h> |
| 41 |
#include <cuda_runtime.h> |
| 42 |
|
| 43 |
#include "../inc/piestimator.h" |
| 44 |
|
| 45 |
template <typename Real> |
| 46 |
bool Test<Real>::operator()() { |
| 47 |
using std::endl; |
| 48 |
using std::setw; |
| 49 |
using std::stringstream; |
| 50 |
|
| 51 |
StopWatchInterface *timer = NULL; |
| 52 |
sdkCreateTimer(&timer); |
| 53 |
|
| 54 |
// Get device properties |
| 55 |
struct cudaDeviceProp deviceProperties; |
| 56 |
cudaError_t cudaResult = cudaGetDeviceProperties(&deviceProperties, device); |
| 57 |
|
| 58 |
if (cudaResult != cudaSuccess) { |
| 59 |
std::string msg("Could not get device properties: "); |
| 60 |
msg += cudaGetErrorString(cudaResult); |
| 61 |
throw std::runtime_error(msg); |
| 62 |
} |
| 63 |
|
| 64 |
// Evaluate on GPU |
| 65 |
printf("Estimating Pi on GPU (%s)\n\n", deviceProperties.name); |
| 66 |
PiEstimator<Real> estimator(numSims, device, threadBlockSize, seed); |
| 67 |
sdkStartTimer(&timer); |
| 68 |
Real result = estimator(); |
| 69 |
sdkStopTimer(&timer); |
| 70 |
elapsedTime = sdkGetAverageTimerValue(&timer) / 1000.0f; |
| 71 |
|
| 72 |
// Tolerance to compare result with expected |
| 73 |
// This is just to check that nothing has gone very wrong with the |
| 74 |
// test, the actual accuracy of the result depends on the number of |
| 75 |
// Monte Carlo trials |
| 76 |
const Real tolerance = static_cast<Real>(0.01); |
| 77 |
|
| 78 |
// Display results |
| 79 |
Real abserror = fabs(result - static_cast<Real>(PI)); |
| 80 |
Real relerror = abserror / static_cast<float>(PI); |
| 81 |
printf("Precision: %s\n", |
| 82 |
(typeid(Real) == typeid(double)) ? "double" : "single"); |
| 83 |
printf("Number of sims: %d\n", numSims); |
| 84 |
printf("Tolerance: %e\n", tolerance); |
| 85 |
printf("GPU result: %e\n", result); |
| 86 |
printf("Expected: %e\n", PI); |
| 87 |
printf("Absolute error: %e\n", abserror); |
| 88 |
printf("Relative error: %e\n\n", relerror); |
| 89 |
|
| 90 |
// Check result |
| 91 |
if (relerror > tolerance) { |
| 92 |
printf("computed result (%e) does not match expected result (%e).\n", |
| 93 |
result, PI); |
| 94 |
pass = false; |
| 95 |
} else { |
| 96 |
pass = true; |
| 97 |
} |
| 98 |
|
| 99 |
// Print results |
| 100 |
printf( |
| 101 |
"MonteCarloEstimatePiInlineP, Performance = %.2f sims/s, Time = " |
| 102 |
"%.2f(ms), NumDevsUsed = %u, Blocksize = %u\n", |
| 103 |
numSims / elapsedTime, elapsedTime * 1000.0f, 1, threadBlockSize); |
| 104 |
|
| 105 |
sdkDeleteTimer(&timer); |
| 106 |
|
| 107 |
return pass; |
| 108 |
} |
| 109 |
|
| 110 |
// Explicit template instantiation |
| 111 |
template struct Test<float>; |
| 112 |
template struct Test<double>; |