Debian Codegraph @

Samples/2_Concepts_and_Techniques/MC_EstimatePiP/src/main.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 // Monte Carlo: Estimate Pi
30 // ========================
31 //
32 // This sample demonstrates a very simple Monte Carlo estimation for Pi.
33 //
34 // This file, main.cpp, contains the setup information to run the test, for
35 // example parsing the command line and integrating this sample with the
36 // samples framework. As such it is perhaps less interesting than the guts of
37 // the sample. Readers wishing to skip the clutter are advised to skip straight
38 // to Test.operator() in test.cpp.
39 ///////////////////////////////////////////////////////////////////////////////
40
41
42 #include <iostream>
43 #include <iomanip>
44 #include <stdexcept>
45 #include <cuda_runtime.h>
46 #include <helper_timer.h>
47 #include <helper_cuda.h>
48 #include <math.h>
49
50 #include "../inc/test.h"
51
52 // SDK information
53 static const char *printfFile = "MonteCarloEstimatePiP.txt";
54
55 // Forward declarations
56 void showHelp(const int argc, const char **argv);
57 template <typename Real>
58 void runTest(int argc, const char **argv);
59
60 int main(int argc, char **argv) {
61 using std::invalid_argument;
62 using std::string;
63
64 // Open the log file
65 printf("Monte Carlo Estimate Pi (with batch PRNG)\n");
66 printf("=========================================\n\n");
67
68 // If help flag is set, display help and exit immediately
69 if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
70 printf("Displaying help on console\n");
71 showHelp(argc, (const char **)argv);
72 exit(EXIT_SUCCESS);
73 }
74
75 // Check the precision (checked against the device capability later)
76 try {
77 char *value;
78
79 if (getCmdLineArgumentString(argc, (const char **)argv, "precision",
80 &value)) {
81 // Check requested precision is valid
82 string prec(value);
83
84 if (prec.compare("single") == 0 || prec.compare("\"single\"") == 0) {
85 runTest<float>(argc, (const char **)argv);
86 } else if (prec.compare("double") == 0 ||
87 prec.compare("\"double\"") == 0) {
88 runTest<double>(argc, (const char **)argv);
89 } else {
90 printf("specified precision (%s) is invalid, must be \"single\".\n",
91 value);
92 throw invalid_argument("precision");
93 }
94 } else {
95 runTest<float>(argc, (const char **)argv);
96 }
97 } catch (invalid_argument &e) {
98 printf("invalid command line argument (%s)\n", e.what());
99 exit(EXIT_FAILURE);
100 }
101
102 // Finish
103 exit(EXIT_SUCCESS);
104 }
105
106 template <typename Real>
107 void runTest(int argc, const char **argv) {
108 using std::invalid_argument;
109 using std::runtime_error;
110
111 try {
112 Test<Real> test;
113 int deviceCount = 0;
114 cudaError_t cudaResult = cudaSuccess;
115
116 // by default specify GPU Device == 0
117 test.device = 0;
118
119 // Get number of available devices
120 cudaResult = cudaGetDeviceCount(&deviceCount);
121
122 if (cudaResult != cudaSuccess) {
123 printf("could not get device count.\n");
124 throw runtime_error("cudaGetDeviceCount");
125 }
126
127 // (default parameters)
128 test.numSims = k_sims_qa;
129 test.threadBlockSize = k_bsize_qa;
130 test.seed = k_seed_def;
131
132 {
133 char *value = 0;
134
135 if (getCmdLineArgumentString(argc, argv, "device", &value)) {
136 test.device = (int)atoi(value);
137
138 if (test.device >= deviceCount) {
139 printf(
140 "invalid target device specified on command line (device %d does "
141 "not exist).\n",
142 test.device);
143 throw invalid_argument("device");
144 }
145 } else {
146 test.device = gpuGetMaxGflopsDeviceId();
147 }
148
149 if (getCmdLineArgumentString(argc, argv, "sims", &value)) {
150 test.numSims = (unsigned int)atoi(value);
151
152 if (test.numSims < k_sims_min || test.numSims > k_sims_max) {
153 printf(
154 "specified number of simulations (%d) is invalid, must be "
155 "between %d and %d.\n",
156 test.numSims, k_sims_min, k_sims_max);
157 throw invalid_argument("sims");
158 }
159 } else {
160 test.numSims = k_sims_def;
161 }
162
163 if (getCmdLineArgumentString(argc, argv, "block-size", &value)) {
164 // Determine max threads per block
165 cudaDeviceProp deviceProperties;
166 cudaResult = cudaGetDeviceProperties(&deviceProperties, test.device);
167
168 if (cudaResult != cudaSuccess) {
169 printf("cound not get device properties for device %d.\n",
170 test.device);
171 throw runtime_error("cudaGetDeviceProperties");
172 }
173
174 // Check requested size is valid
175 test.threadBlockSize = (unsigned int)atoi(value);
176
177 if (test.threadBlockSize < k_bsize_min ||
178 test.threadBlockSize > static_cast<unsigned int>(
179 deviceProperties.maxThreadsPerBlock)) {
180 printf(
181 "specified block size (%d) is invalid, must be between %d and %d "
182 "for device %d.\n",
183 test.threadBlockSize, k_bsize_min,
184 deviceProperties.maxThreadsPerBlock, test.device);
185 throw invalid_argument("block-size");
186 }
187
188 if (test.threadBlockSize & test.threadBlockSize - 1) {
189 printf(
190 "specified block size (%d) is invalid, must be a power of two "
191 "(see reduction function).\n",
192 test.threadBlockSize);
193 throw invalid_argument("block-size");
194 }
195 } else {
196 test.threadBlockSize = k_bsize_def;
197 }
198
199 if (getCmdLineArgumentString(argc, argv, "seed", &value)) {
200 // Check requested seed is valid
201 test.seed = (unsigned int)atoi(value);
202
203 if (test.seed == 0) {
204 printf("specified seed (%d) is invalid, must be non-zero.\n",
205 test.seed);
206 throw invalid_argument("seed");
207 }
208 } else {
209 test.seed = k_seed_def;
210 }
211 }
212
213 // Execute
214 test();
215 } catch (invalid_argument &e) {
216 printf("invalid command line argument (%s)\n", e.what());
217 exit(EXIT_FAILURE);
218 } catch (runtime_error &e) {
219 printf("runtime error (%s)\n", e.what());
220 exit(EXIT_FAILURE);
221 }
222 }
223
224 void showHelp(int argc, const char **argv) {
225 using std::cout;
226 using std::endl;
227 using std::left;
228 using std::setw;
229
230 if (argc > 0) {
231 cout << endl << argv[0] << endl;
232 }
233
234 cout << endl << "Syntax:" << endl;
235 cout << left;
236 cout << " " << setw(20) << "--device=<device>"
237 << "Specify device to use for execution" << endl;
238 cout << " " << setw(20) << "--sims=<N>"
239 << "Specify number of Monte Carlo simulations" << endl;
240 cout << " " << setw(20) << "--block-size=<N>"
241 << "Specify number of threads per block" << endl;
242 cout << " " << setw(20) << "--seed=<N>"
243 << "Specify the seed to use for the random number generator" << endl;
244 cout << " " << setw(20) << "--precision=<P>"
245 << "Specify the precision (\"single\" or \"double\")" << endl;
246 cout << endl;
247 cout << " " << setw(20) << "--noprompt"
248 << "Skip prompt before exit" << endl;
249 cout << endl;
250 }