2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
12 #include "../inc/test.h"
22 #include <helper_timer.h>
24 #include <cuda_runtime.h>
26 #include "../inc/piestimator.h"
28 template <typename Real>
29 bool Test<Real>::operator()()
31 using std::stringstream;
35 StopWatchInterface *timer = NULL;
36 sdkCreateTimer(&timer);
38 // Get device properties
39 struct cudaDeviceProp deviceProperties;
40 cudaError_t cudaResult = cudaGetDeviceProperties(&deviceProperties, device);
42 if (cudaResult != cudaSuccess)
44 std::string msg("Could not get device properties: ");
45 msg += cudaGetErrorString(cudaResult);
46 throw std::runtime_error(msg);
50 printf("Estimating Pi on GPU (%s)\n\n", deviceProperties.name);
51 PiEstimator<Real> estimator(numSims, device, threadBlockSize, seed);
52 sdkStartTimer(&timer);
53 Real result = estimator();
55 elapsedTime = sdkGetAverageTimerValue(&timer)/1000.0f;
57 // Tolerance to compare result with expected
58 // This is just to check that nothing has gone very wrong with the
59 // test, the actual accuracy of the result depends on the number of
61 const Real tolerance = static_cast<Real>(0.01);
64 Real abserror = fabs(result - static_cast<Real>(PI));
65 Real relerror = abserror / static_cast<float>(PI);
66 printf("Precision: %s\n", (typeid(Real) == typeid(double)) ? "double" : "single");
67 printf("Number of sims: %d\n", numSims);
68 printf("Tolerance: %e\n", tolerance);
69 printf("GPU result: %e\n", result);
70 printf("Expected: %e\n", PI);
71 printf("Absolute error: %e\n", abserror);
72 printf("Relative error: %e\n\n", relerror);
75 if (relerror > tolerance)
77 printf("computed result (%e) does not match expected result (%e).\n", result, PI);
86 printf("MonteCarloEstimatePiInlineP, Performance = %.2f sims/s, Time = %.2f(ms), NumDevsUsed = %u, Blocksize = %u\n",
87 numSims / elapsedTime, elapsedTime*1000.0f, 1, threadBlockSize);
89 sdkDeleteTimer(&timer);
94 // Explicit template instantiation
95 template struct Test<float>;
96 template struct Test<double>;