OSDN Git Service

new file: Integration/Tomography/Makefile.recent
[eos/hostdependX86LINUX64.git] / hostdepend / X86MAC64 / util / X86MAC64 / cuda / samples / 7_CUDALibraries / MC_EstimatePiInlineP / src / test.cpp
1 /*
2  * Copyright 1993-2013 NVIDIA Corporation.  All rights reserved.
3  *
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.
9  *
10  */
11
12 #include "../inc/test.h"
13
14 #include <sstream>
15 #include <iomanip>
16 #include <stdexcept>
17 #include <memory>
18 #include <iostream>
19 #include <cassert>
20 #include <typeinfo>
21 #include <stdio.h>
22 #include <helper_timer.h>
23 #include <math.h>
24 #include <cuda_runtime.h>
25
26 #include "../inc/piestimator.h"
27
28 template <typename Real>
29 bool Test<Real>::operator()()
30 {
31     using std::stringstream;
32     using std::endl;
33     using std::setw;
34
35     StopWatchInterface *timer = NULL;
36     sdkCreateTimer(&timer);
37
38     // Get device properties
39     struct cudaDeviceProp deviceProperties;
40     cudaError_t cudaResult = cudaGetDeviceProperties(&deviceProperties, device);
41
42     if (cudaResult != cudaSuccess)
43     {
44         std::string msg("Could not get device properties: ");
45         msg += cudaGetErrorString(cudaResult);
46         throw std::runtime_error(msg);
47     }
48
49     // Evaluate on GPU
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();
54     sdkStopTimer(&timer);
55     elapsedTime = sdkGetAverageTimerValue(&timer)/1000.0f;
56
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
60     // Monte Carlo trials
61     const Real tolerance = static_cast<Real>(0.01);
62
63     // Display results
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);
73
74     // Check result
75     if (relerror > tolerance)
76     {
77         printf("computed result (%e) does not match expected result (%e).\n", result, PI);
78         pass = false;
79     }
80     else
81     {
82         pass = true;
83     }
84
85     // Print results
86     printf("MonteCarloEstimatePiInlineP, Performance = %.2f sims/s, Time = %.2f(ms), NumDevsUsed = %u, Blocksize = %u\n",
87            numSims / elapsedTime, elapsedTime*1000.0f, 1, threadBlockSize);
88
89     sdkDeleteTimer(&timer);
90
91     return pass;
92 }
93
94 // Explicit template instantiation
95 template struct Test<float>;
96 template struct Test<double>;