-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyVegasCallFilla.cu
210 lines (170 loc) · 5.24 KB
/
myVegasCallFilla.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "vegasconst.h"
#include "vegas.h"
__device__ float d[ndim_max][nd_max];
__device__ float dti, dtsi;
__device__ double doubleti, doubletsi;
__global__
void initzero(void){
/*
for (int dim = 0; dim < g_ndim; dim++){
for (int box = 0; box < g_nd; box++){
d[dim][box] = 0.0f;
}
}
*/
// Dos alternativas, cudamemset o armar un 0 para cada hilo y llamar bien al kernel
d[threadIdx.x][threadIdx.y] = 0.0f;
dti = 0.0f;
dtsi = 0.0f;
}
__global__
void myVegasCallFilla(int mds)
{
//--------------------
// Check the thread ID
//--------------------
const unsigned int tIdx = threadIdx.x;
const unsigned int bDimx = blockDim.x;
const unsigned int bIdx = blockIdx.x;
const unsigned int gDimx = gridDim.x;
const unsigned int bIdy = blockIdx.y;
// const unsigned int gDimy = gridDim.y;
unsigned int bid = bIdy*gDimx+bIdx;
const unsigned int tid = bid*bDimx+tIdx;
//Using float for now, atomicAdd doesn't support double yet...
__shared__ float block_fb;
__shared__ float block_f2b;
__shared__ float block_d[ndim_max][nd_max];
block_fb = 0.0f;
block_f2b = 0.0f;
/*
for (int idim = 0; idim < g_ndim; idim++){
for (int ind = 0; ind < g_nd; ind ++)
block_d[idim][ind] = 0.0f;
}
*/
/* Alternative for above */
for (int i = 0; i < (g_ndim * g_nd - 1) / bDimx + 1; i++){
int xdim = (i * bDimx + tIdx) / g_nd;
int xind = (i * bDimx + tIdx) % g_nd;
if (xdim < g_ndim){
block_d[xdim][xind] = 0.0f;
}
}
//int ig = tid;
int lane = tIdx % warpSize;
//d[tid] = 0.0;
int kg[ndim_max];
unsigned ia[ndim_max];
//fb and f2b will be the accumulations of f and the "error", these values
//will be reduced later and stored in dti and dtsi.
float f, f2;
float fb = 0.0f;
float f2b = 0.0f;
if (tid<g_nCubes) {
for (int point = 0; point < g_npg; point++){
unsigned int tidRndm = tid * g_npg + point;
unsigned igg = tid;
for (int j=0;j<g_ndim;j++) {
kg[j] = igg%g_ng+1;
igg /= g_ng;
}
//Generate a random point in [0,1]^ndim.
float randm[ndim_max];
fxorshift128(tidRndm, g_ndim, randm);
float x[ndim_max];
float wgt = g_xjac;
/*
This piece of code places the random point in the domain of integration,
g_xi will change at every iteration as a result of the refining step, so
the weight will change as well.
*/
for (int j=0;j<g_ndim;j++) {
float xo,xn,rc;
xn = (kg[j]-randm[j])*g_dxg+1.f;
ia[j] = (int)xn-1;
if (ia[j]<=0) {
xo = g_xi[j][ia[j]];
rc = (xn-(float)(ia[j]+1))*xo;
} else {
xo = g_xi[j][ia[j]]-g_xi[j][ia[j]-1];
rc = g_xi[j][ia[j]-1]+(xn-(float)(ia[j]+1))*xo;
}
x[j] = g_xl[j]+rc*g_dx[j];
wgt *= xo*(float)g_nd;
}
/* Different calls for different functions */
f = wgt * sum(x, g_ndim);
// f = wgt * sqsum(x, g_ndim);
// f = wgt * sumsqroot(x, g_ndim);
// f = wgt * prodones(x, g_ndim);
// f = wgt * prodexp(x, g_ndim);
// f = wgt * prodcub(x, g_ndim);
// f = wgt * prodx(x, g_ndim);
// f = wgt * sumfifj(x, g_ndim);
// f = wgt * sumfonefj(x, g_ndim);
// f = wgt * hellekalek(x, g_ndim);
// f = wgt * roosarnoldone(x, g_ndim);
// f = wgt * roosarnoldtwo(x, g_ndim);
// f = wgt * roosarnoldthree(x, g_ndim);
// f = wgt * rst(x, g_ndim);
// f = wgt * sobolprod(x, g_ndim);
// f = wgt * oscill(x, g_ndim);
// f = wgt * prpeak(x, g_ndim);
fb += f;
f2 = f*f;
f2b += f2;
//If mds = 1, we just have to add f^2 to the corresponding space in d.
if (mds > 0){
for (int idim = 0; idim < g_ndim; idim++) {
atomicAdd(&block_d[idim][ia[idim]], f2);
}
}
}
/*When mds = -1, original code uses the data of the first element of the
cube to store f2b in d, that won't change much if I use the last element.
If it does, maybe we can go for a decreasing loop in npg...*/
f2b = sqrt(f2b * g_npg);
f2b = (f2b - fb) * (f2b - fb);
if (mds < 0){
for (int idim = 0; idim < g_ndim; idim++){
atomicAdd(&block_d[idim][ia[idim]], f2b);
}
}
__syncthreads();
//REDUCE TIME!!!
#pragma unroll
for (int offset = warpSize/2; offset > 0; offset /= 2){
fb += __shfl_down(fb, offset);
f2b += __shfl_down(f2b, offset);
}
if (0 == lane){
atomicAdd(&block_fb, fb);
atomicAdd(&block_f2b, f2b);
}
__syncthreads();
if (0 == tIdx){
atomicAdd(&dti, block_fb);
doubleti = (double)dti;
}
if (32 == tIdx){
atomicAdd(&dtsi, block_f2b);
doubletsi = (double)dtsi;
}
/* Threaded binning, much better performance */
for (int i = 0; i < (g_ndim * g_nd - 1) / bDimx + 1; i++){
int xdim = (i * bDimx + tIdx) / g_nd;
int xind = (i * bDimx + tIdx) % g_nd;
if (xdim < g_ndim){
atomicAdd(&d[xdim][xind], block_d[xdim][xind]);
}
}
/* Sequential binning, low performance :(
for (int idim = 0; idim < g_ndim; idim++){
for (int ind = 0; ind < g_nd; ind ++){
atomicAdd(&d[idim][ind], block_d[idim][ind]);
}
}
*/
}
}