-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi.cu
165 lines (124 loc) · 3.94 KB
/
pi.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
using namespace std;
#include <iomanip>
#include <cassert>
#include <helper_cuda.h>
#include "type.h"
#include "fort.h"
__device__ double atomicAdd(double *address, double val)
{
unsigned long long int* address_as_ull = (unsigned long long int*)address;
unsigned long long int old = *address_as_ull, assumed;
do {
assumed = old;
old = atomicCAS(address_as_ull, assumed,
__double_as_longlong(val + __longlong_as_double(assumed)));
} while (assumed != old);
return __longlong_as_double(old);
}
inline bool is_pow_2(int x) { return ((x&(x-1)) == 0); }
__constant__ __device__ Real step;
__host__ __device__ Real myf(const Real &x)
{
return _one_/(_one_ + x*x);
}
template<int block_size> __device__ void _calculate_pi_with_device_(Real *block_sum, int n_grids)
{
extern __shared__ Real s_data [];
int n = n_grids/gridDim.x;
const Real x_left = blockIdx.x*n*step;
// number of grid points of the last block
if(blockIdx.x == gridDim.x-1)
n = n_grids - n*(gridDim.x-1);
Real s = _zero_;
for(int i = 0; i < n; i += blockDim.x) {
const int j = i + threadIdx.x;
if(j < n) {
const Real x = x_left + j*step;
s += myf(x);
}
}
s_data[threadIdx.x] = s;
__syncthreads();
// do reduction in shared memory
for(int i = blockDim.x/2; i > 0; i /= 2) {
if(threadIdx.x < i)
s_data[threadIdx.x] += s_data[threadIdx.x + i];
__syncthreads();
}
if(threadIdx.x == 0) {
atomicAdd(block_sum, s_data[0]);
//printf("Block %d, sum = %f\n", blockIdx.x, s_data[0]*step);
}
}
__global__ void calculate_pi_with_device_wrapper(const int block_dim, Real *block_sum, int n_grids)
{
switch (block_dim) {
case 2048:
_calculate_pi_with_device_<2048>(block_sum, n_grids);
break;
case 1024:
_calculate_pi_with_device_<1024>(block_sum, n_grids);
break;
case 512:
_calculate_pi_with_device_<512>(block_sum, n_grids);
break;
case 256:
_calculate_pi_with_device_<256>(block_sum, n_grids);
break;
case 128:
_calculate_pi_with_device_<128>(block_sum, n_grids);
break;
case 64:
_calculate_pi_with_device_<64>(block_sum, n_grids);
break;
case 32:
_calculate_pi_with_device_<32>(block_sum, n_grids);
break;
case 16:
_calculate_pi_with_device_<16>(block_sum, n_grids);
break;
case 8:
_calculate_pi_with_device_<8>(block_sum, n_grids);
break;
case 4:
_calculate_pi_with_device_<4>(block_sum, n_grids);
break;
case 2:
_calculate_pi_with_device_<2>(block_sum, n_grids);
break;
case 1:
_calculate_pi_with_device_<1>(block_sum, n_grids);
break;
default:
break;
}
}
void calculate_pi_with_device(const int block_dim, const int grid_dim)
{
assert(is_pow_2(block_dim));
cout << " sizeof(Real) = " << sizeof(Real) << endl;
const int n = _NGrids_;
cout << " n = " << n << ", block_dim = " << block_dim
<< ", grid_dim = " << grid_dim << endl;
const Real dx = _one_/(n-1);
checkCudaErrors(cudaMemcpyToSymbol(step, &dx, sizeof(Real)));
Real *block_sum = 0;
checkCudaErrors(cudaMalloc((void **) &block_sum, sizeof(Real)));
checkCudaErrors(cudaMemset(block_sum, 0, sizeof(Real)));
const int share_memory_size = sizeof(Real)*block_dim;
calculate_pi_with_device_wrapper<<<grid_dim, block_dim, share_memory_size>>>(block_dim, block_sum, n);
Real block_sum_host = 0.0;
checkCudaErrors(cudaMemcpy(&block_sum_host, block_sum, sizeof(Real), cudaMemcpyDeviceToHost));
if(block_sum) { checkCudaErrors(cudaFree(block_sum)); block_sum = 0; }
Real pi = block_sum_host;
pi -= _pt5_ * (myf(_zero_) + myf(_one_));
pi *= 4*dx;
cout << " Pi = " << setprecision(16) << pi << "\n"
<< " analytical: " << setprecision(16) << 2.0*asin(1.0) << endl;
}
// Fotran version: CalculatePiWithDevice
extern "C" void FORT(calculatepiwithdevice)(const int &block_dim, const int &grid_dim)
{
calculate_pi_with_device(block_dim, grid_dim);
}