Repository navigation
Expand file tree
/
Copy pathlattice_loop.hpp
More file actions
214 lines (188 loc) · 6.48 KB
/
Copy pathlattice_loop.hpp
File metadata and controls
214 lines (188 loc) · 6.48 KB
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
211
212
213
214
#ifndef LATTICE_LOOP_HPP
#define LATTICE_LOOP_HPP
#include <cuda/atomic>
using namespace LATfield2;
// generic loop for lattice
template <typename ForEachFunct, int noutput = 0>
__global__ void lattice_for_each(ForEachFunct funct, int numpts, Field<Real> ** fields, int nfields, double * params, double * output, int * reduce_type)
{
int coord1 = blockIdx.x;
int coord2 = blockIdx.y;
int thread_id = threadIdx.x;
alignas(8) double output_thread[(noutput > 0) ? noutput : 1];
alignas(8) double output_site [(noutput > 0) ? noutput : 1];
Site * sites = nullptr;
if (nfields > 0)
{
sites = (Site *)alloca(nfields * sizeof(Site));
}
if constexpr (noutput > 0)
{
#pragma unroll
for (int i = 0; i < noutput; i++)
{
if (reduce_type[i] & (SUM | SUM_LOCAL))
{
output_thread[i] = 0.;
}
else if (reduce_type[i] & (MAX | MAX_LOCAL))
{
output_thread[i] = -1.e30;
}
else if (reduce_type[i] & (MIN | MIN_LOCAL))
{
output_thread[i] = 1.e30;
}
}
}
for (int idx = thread_id; idx < numpts; idx += 128)
{
for (int i = 0; i < nfields; i++)
{
sites[i] = Site(fields[i]->lattice(), fields[i]->lattice().siteFirst()
+ idx*fields[i]->lattice().jump(0)
+ coord1*fields[i]->lattice().jump(1)
+ coord2*fields[i]->lattice().jump(2));
}
funct(fields, sites, nfields, params, output_site);
if constexpr (noutput > 0)
{
#pragma unroll
for (int i = 0; i < noutput; i++)
{
if (reduce_type[i] & (SUM | SUM_LOCAL))
{
output_thread[i] += output_site[i];
}
else if (reduce_type[i] & (MAX | MAX_LOCAL) && output_site[i] > output_thread[i])
{
output_thread[i] = output_site[i];
}
else if (reduce_type[i] & (MIN | MIN_LOCAL) && output_site[i] < output_thread[i])
{
output_thread[i] = output_site[i];
}
}
}
}
if constexpr (noutput > 0)
{
__shared__ double smem[4 * noutput];
constexpr int W = 32;
const int lane = threadIdx.x & (W-1);
const int warpId = threadIdx.x / W;
constexpr int nWarps = 4; // blockDim.x = 128
unsigned mask = __activemask();
double warp_val[noutput];
#pragma unroll
for (int i = 0; i < noutput; i++)
{
double v = output_thread[i];
if (reduce_type[i] & SUM)
{
for (int ofs = 16; ofs > 0; ofs >>= 1) // 16,8,4,2,1 = log2(32) steps
v += __shfl_down_sync(mask, v, ofs);
}
else if (reduce_type[i] & MAX)
{
for (int ofs = 16; ofs > 0; ofs >>= 1) // 16,8,4,2,1 = log2(32) steps
v = fmax(v, __shfl_down_sync(mask, v, ofs));
}
else if (reduce_type[i] & MIN)
{
for (int ofs = 16; ofs > 0; ofs >>= 1) // 16,8,4,2,1 = log2(32) steps
v = fmin(v, __shfl_down_sync(mask, v, ofs));
}
warp_val[i] = v;
}
if (lane == 0) {
#pragma unroll
for (int i = 0; i < noutput; ++i)
smem[warpId * noutput + i] = warp_val[i];
}
__syncthreads();
if (warpId == 0)
{
#pragma unroll
for (int i = 0; i < noutput; i++)
{
double v = (lane < nWarps) ? smem[lane * noutput + i] : 0.0;
if (reduce_type[i] & SUM)
{
#pragma unroll
for (int ofs = 2; ofs > 0; ofs >>= 1) // 2,1 = log2(4) steps, blockDim.x = 128 => nWarps = 4
v += __shfl_down_sync(mask, v, ofs);
}
else if (reduce_type[i] & MAX)
{
#pragma unroll
for (int ofs = 2; ofs > 0; ofs >>= 1) // 2,1 = log2(4) steps, blockDim.x = 128 => nWarps = 4
v = fmax(v, __shfl_down_sync(mask, v, ofs));
}
else if (reduce_type[i] & MIN)
{
#pragma unroll
for (int ofs = 2; ofs > 0; ofs >>= 1) // 2,1 = log2(4) steps, blockDim.x = 128 => nWarps = 4
v = fmin(v, __shfl_down_sync(mask, v, ofs));
}
if (lane == 0)
{
cuda::atomic_ref<double, cuda::thread_scope_device> output_ref(output[i]);
if (reduce_type[i] & SUM)
{
output_ref.fetch_add(v);
}
else if (reduce_type[i] & MAX)
{
output_ref.fetch_max(v);
}
else if (reduce_type[i] & MIN)
{
output_ref.fetch_min(v);
}
}
}
}
}
}
template <int components = 1>
__host__ __device__ void lattice_add(Field<Real> * fields[], Site * sites, int nfields, double * params, double * outputs)
{
if constexpr (components == 1)
(*fields[0])(sites[0]) += (*params);
else
{
#pragma unroll
for (int i = 0; i < components; i++)
(*fields[0])(sites[0], i) += (*params);
}
}
template <int components = 1>
struct lattice_add_functor
{
__host__ __device__ void operator()(Field<Real> * fields[], Site * sites, int nfields, double * params, double * outputs)
{
lattice_add<components>(fields, sites, nfields, params, outputs);
}
};
template <int components = 1>
__host__ __device__ void lattice_multiply(Field<Real> * fields[], Site * sites, int nfields, double * params, double * outputs)
{
if constexpr (components == 1)
(*fields[0])(sites[0]) *= (*params);
else
{
#pragma unroll
for (int i = 0; i < components; i++)
(*fields[0])(sites[0], i) *= (*params);
}
}
template <int components = 1>
struct lattice_multiply_functor
{
__host__ __device__ void operator()(Field<Real> * fields[], Site * sites, int nfields, double * params, double * outputs)
{
lattice_multiply<components>(fields, sites, nfields, params, outputs);
}
};
#endif // LATTICE_LOOP_HPP