Debian Codegraph @

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