Debian Codegraph @

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