Debian Codegraph @

Samples/2_Concepts_and_Techniques/MC_EstimatePiInlineQ/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 <math.h>
50 #include "../inc/test.h"
51
52 // Forward declarations
53 void showHelp(const int argc, const char **argv);
54 template <typename Real>
55 void runTest(int argc, const char **argv);
56
57 int main(int argc, char **argv) {
58 using std::invalid_argument;
59 using std::string;
60
61 // Open the log file
62 printf("Monte Carlo Estimate Pi (with inline QRNG)\n");
63 printf("==========================================\n\n");
64
65 // If help flag is set, display help and exit immediately
66 if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
67 printf("Displaying help on console\n");
68 showHelp(argc, (const char **)argv);
69 exit(EXIT_SUCCESS);
70 }
71
72 // Check the precision (checked against the device capability later)
73 try {
74 char *value;
75
76 if (getCmdLineArgumentString(argc, (const char **)argv, "precision",
77 &value)) {
78 // Check requested precision is valid
79 string prec(value);
80
81 if (prec.compare("single") == 0 || prec.compare("\"single\"") == 0) {
82 runTest<float>(argc, (const char **)argv);
83 } else if (prec.compare("double") == 0 ||
84 prec.compare("\"double\"") == 0) {
85 runTest<double>(argc, (const char **)argv);
86 } else {
87 printf(
88 "specified precision (%s) is invalid, must be \"single\" or "
89 "\"double\".\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 StopWatchInterface *timer = NULL;
111
112 try {
113 Test<Real> test;
114 int deviceCount = 0;
115 cudaError_t cudaResult = cudaSuccess;
116
117 // by default specify GPU Device == 0
118 test.device = 0;
119
120 // Get number of available devices
121 cudaResult = cudaGetDeviceCount(&deviceCount);
122
123 if (cudaResult != cudaSuccess) {
124 printf("could not get device count.\n");
125 throw runtime_error("cudaGetDeviceCount");
126 }
127
128 // (default parameters)
129 test.numSims = k_sims_qa;
130 test.threadBlockSize = k_bsize_qa;
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 // Execute
200 test();
201 } catch (invalid_argument &e) {
202 printf("invalid command line argument (%s)\n", e.what());
203 exit(EXIT_FAILURE);
204 } catch (runtime_error &e) {
205 printf("runtime error (%s)\n", e.what());
206 exit(EXIT_FAILURE);
207 }
208 }
209
210 void showHelp(int argc, const char **argv) {
211 using std::cout;
212 using std::endl;
213 using std::left;
214 using std::setw;
215
216 if (argc > 0) {
217 cout << endl << argv[0] << endl;
218 }
219
220 cout << endl << "Syntax:" << endl;
221 cout << left;
222 cout << " " << setw(20) << "--device=<device>"
223 << "Specify device to use for execution" << endl;
224 cout << " " << setw(20) << "--sims=<N>"
225 << "Specify number of Monte Carlo simulations" << endl;
226 cout << " " << setw(20) << "--block-size=<N>"
227 << "Specify number of threads per block" << endl;
228 cout << " " << setw(20) << "--precision=<P>"
229 << "Specify the precision (\"single\" or \"double\")" << endl;
230 cout << endl;
231 cout << " " << setw(20) << "--noprompt"
232 << "Skip prompt before exit" << endl;
233 cout << endl;
234 }