Debian Codegraph @

Samples/2_Concepts_and_Techniques/MC_EstimatePiInlineP/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 #include <cuda_runtime.h>
42 #include <helper_cuda.h>
43
44 #include <iomanip>
45 #include <iostream>
46 #include <stdexcept>
47
48 #include "../inc/test.h"
49
50 // SDK information
51 static const char *printfFile = "MonteCarloEstimatePiInlineP.txt";
52
53 // Forward declarations
54 void showHelp(const int argc, const char **argv);
55 template <typename Real>
56 void runTest(int argc, const char **argv);
57
58 int main(int argc, char **argv) {
59 using std::invalid_argument;
60 using std::string;
61
62 // Open the log file
63 printf("Monte Carlo Estimate Pi (with inline PRNG)\n");
64 printf("==========================================\n\n");
65
66 // If help flag is set, display help and exit immediately
67 if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
68 printf("Displaying help on console\n");
69 showHelp(argc, (const char **)argv);
70 exit(EXIT_SUCCESS);
71 }
72
73 // Check the precision (checked against the device capability later)
74 try {
75 char *value;
76
77 if (getCmdLineArgumentString(argc, (const char **)argv, "precision",
78 &value)) {
79 // Check requested precision is valid
80 string prec(value);
81
82 if (prec.compare("single") == 0 || prec.compare("\"single\"") == 0) {
83 runTest<float>(argc, (const char **)argv);
84 } else if (prec.compare("double") == 0 ||
85 prec.compare("\"double\"") == 0) {
86 runTest<double>(argc, (const char **)argv);
87 } else {
88 printf(
89 "specified precision (%s) is invalid, must be \"single\" or "
90 "\"double\".\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 (%s).\n",
124 cudaGetErrorString(cudaResult));
125 throw runtime_error("cudaGetDeviceCount");
126 }
127
128 // (default parameters)
129 test.numSims = k_sims_qa;
130 test.threadBlockSize = k_bsize_qa;
131 test.seed = k_seed_def;
132
133 {
134 char *value = 0;
135
136 if (getCmdLineArgumentString(argc, argv, "device", &value)) {
137 test.device = (int)atoi(value);
138
139 if (test.device >= deviceCount) {
140 printf(
141 "invalid target device specified on command line (device %d does "
142 "not exist).\n",
143 test.device);
144 throw invalid_argument("device");
145 }
146 } else {
147 test.device = gpuGetMaxGflopsDeviceId();
148 }
149
150 if (getCmdLineArgumentString(argc, argv, "sims", &value)) {
151 test.numSims = (unsigned int)atoi(value);
152 printf("number of simulations = %d\n", test.numSims);
153
154 if (test.numSims < k_sims_min || test.numSims > k_sims_max) {
155 printf(
156 "specified number of simulations (%d) is invalid, must be "
157 "between %d and %d.\n",
158 test.numSims, k_sims_min, k_sims_max);
159 throw invalid_argument("sims");
160 }
161 } else {
162 test.numSims = k_sims_def;
163 }
164
165 if (getCmdLineArgumentString(argc, argv, "block-size", &value)) {
166 // Determine max threads per block
167 cudaDeviceProp deviceProperties;
168 cudaResult = cudaGetDeviceProperties(&deviceProperties, test.device);
169
170 if (cudaResult != cudaSuccess) {
171 printf("unable to get device properties for device %d.\n",
172 test.device);
173 throw runtime_error("cudaGetDeviceProperties");
174 }
175
176 // Check requested size is valid
177 test.threadBlockSize = (unsigned int)atoi(value);
178 printf("block size = %d\n", test.threadBlockSize);
179
180 if (test.threadBlockSize < k_bsize_min ||
181 test.threadBlockSize > static_cast<unsigned int>(
182 deviceProperties.maxThreadsPerBlock)) {
183 printf(
184 "specified block size (%d) is invalid, must be between %d and %d "
185 "for device %d.\n",
186 test.threadBlockSize, k_bsize_min,
187 deviceProperties.maxThreadsPerBlock, test.device);
188 throw invalid_argument("block-size");
189 }
190
191 if (test.threadBlockSize & test.threadBlockSize - 1) {
192 printf(
193 "specified block size (%d) is invalid, must be a power of two "
194 "(see reduction function).\n",
195 test.threadBlockSize);
196 throw invalid_argument("block-size");
197 }
198 } else {
199 test.threadBlockSize = k_bsize_def;
200 }
201
202 if (getCmdLineArgumentString(argc, argv, "seed", &value)) {
203 // Check requested seed is valid
204 test.seed = (unsigned int)atoi(value);
205 printf("seed = %d\n", test.seed);
206
207 if (test.seed == 0) {
208 printf("specified seed (%d) is invalid, must be non-zero.\n",
209 test.seed);
210 throw invalid_argument("seed");
211 }
212 } else {
213 test.seed = k_seed_def;
214 }
215 }
216
217 // Execute
218 test();
219 } catch (invalid_argument &e) {
220 printf("invalid command line argument (%s)\n", e.what());
221 exit(EXIT_FAILURE);
222 } catch (runtime_error &e) {
223 printf("runtime error (%s)\n", e.what());
224 exit(EXIT_FAILURE);
225 }
226 }
227
228 void showHelp(int argc, const char **argv) {
229 using std::cout;
230 using std::endl;
231 using std::left;
232 using std::setw;
233
234 if (argc > 0) {
235 cout << endl << argv[0] << endl;
236 }
237
238 cout << endl << "Syntax:" << endl;
239 cout << left;
240 cout << " " << setw(20) << "--device=<device>"
241 << "Specify device to use for execution" << endl;
242 cout << " " << setw(20) << "--sims=<N>"
243 << "Specify number of Monte Carlo simulations" << endl;
244 cout << " " << setw(20) << "--block-size=<N>"
245 << "Specify number of threads per block" << endl;
246 cout << " " << setw(20) << "--seed=<N>"
247 << "Specify the seed to use for the random number generator" << endl;
248 cout << " " << setw(20) << "--precision=<P>"
249 << "Specify the precision (\"single\" or \"double\")" << endl;
250 cout << endl;
251 cout << " " << setw(20) << "--noprompt"
252 << "Skip prompt before exit" << endl;
253 cout << endl;
254 }