Debian Codegraph @

Samples/2_Concepts_and_Techniques/MC_EstimatePiP/src/test.cpp

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