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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#pragma once
#include <memory>
#include <vector>
#ifdef ENABLE_AVX512
#include "simd/512.h"
#else
#include "simd/256.h"
#endif
#include <omp.h>
#include "cuboid.h"
#include "population.h"
#include "propagation.h"
#include "operator.h"
#include "export.h"
#include "LLBM/initialize.h"
#include "LLBM/collect_moments.h"
template <typename T>
class LatticeMask {
private:
const std::size_t _size;
std::unique_ptr<typename simd::Mask<T>::storage_t[]> _simd_mask;
std::unique_ptr<bool[]> _base_mask;
public:
LatticeMask(std::size_t nCells):
_size(((nCells + simd::Pack<T>::size - 1) / simd::Mask<T>::storage_size + 1) * simd::Mask<T>::storage_size),
_simd_mask(new typename simd::Mask<T>::storage_t[_size / simd::Mask<T>::storage_size] { }),
_base_mask(new bool[_size] { })
{ }
void set(std::size_t iCell, bool state) {
_base_mask[iCell] = state;
}
bool get(std::size_t iCell) const {
return _base_mask[iCell];
}
void serialize() {
for (std::size_t i=0; i < _size / simd::Mask<T>::storage_size; ++i) {
_simd_mask[i] = simd::Mask<T>::encode(_base_mask.get() + i*simd::Mask<T>::storage_size);
}
}
typename simd::Mask<T>::storage_t* data() {
return _simd_mask.get();
}
};
template <concepts::PropagatablePopulationBuffer Buffer>
class Lattice {
private:
using value_t = typename Buffer::value_t;
using pack_t = simd::Pack<value_t>;
using mask_t = simd::Mask<value_t>;
Cuboid _cuboid;
Buffer _population;
template <typename COP>
void apply(COP cop,
std::size_t iCell,
pack_t f_curr[population::q],
pack_t f_next[population::q]) {
if (mask_t m = {cop.mask.data(), iCell}) {
cop(f_curr, f_next);
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
simd::maskstore(get(iPop, stage::post_collision()) + iCell, m, f_next[iPop]);
}
}
}
public:
Lattice(Cuboid cuboid):
_cuboid(cuboid),
_population(cuboid)
{
apply<InitializeO>();
}
template <typename STAGE>
value_t* get(unsigned iPop, STAGE stage) {
return _population.get(iPop, stage);
}
std::size_t volume() const {
return _cuboid.volume();
}
void stream() {
_population.stream();
}
template <typename F, typename... ARGS>
requires concepts::Operator<F,value_t>
void apply(std::size_t iCell, ARGS&&... args) {
value_t f_curr[population::q];
value_t f_next[population::q];
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
f_curr[iPop] = get(iPop, stage::pre_collision())[iCell];
}
F::apply(f_curr, f_next, std::forward<ARGS>(args)...);
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
get(iPop, stage::post_collision())[iCell] = f_next[iPop];
}
}
template <typename F>
requires concepts::Operator<F,pack_t>
void apply() {
pack_t f_curr[population::q];
pack_t f_next[population::q];
#pragma omp parallel for private(f_curr, f_next) schedule(static)
for (std::size_t i=0; i < volume() / pack_t::size; ++i) {
const std::size_t iCell = pack_t::size * i;
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
f_curr[iPop] = pack_t(get(iPop, stage::pre_collision()) + iCell);
}
F::apply(f_curr, f_next);
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
simd::store(get(iPop, stage::post_collision()) + iCell, f_next[iPop]);
}
}
for (std::size_t iCell = pack_t::size * (volume() / pack_t::size);
iCell < volume();
++iCell) {
apply<F>(iCell);
}
}
template <typename F, typename... ARGS>
requires concepts::Operator<F,pack_t,ARGS...>
void apply(LatticeMask<value_t>& mask, ARGS&&... args) {
pack_t f_curr[population::q];
pack_t f_next[population::q];
#pragma omp parallel for private(f_curr, f_next) schedule(static)
for (std::size_t iCell=0; iCell < volume(); iCell += pack_t::size) {
mask_t m(mask.data(), iCell);
if (m) {
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
f_curr[iPop] = pack_t(get(iPop, stage::pre_collision()) + iCell);
}
F::apply(f_curr, f_next, std::forward<ARGS>(args)...);
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
simd::maskstore(get(iPop, stage::post_collision()) + iCell, m, f_next[iPop]);
}
}
}
}
template <typename... COPs>
void apply(COPs&&... cops) {
pack_t f_curr[population::q];
pack_t f_next[population::q];
#pragma omp parallel for private(f_curr, f_next) schedule(static)
for (std::size_t iCell=0; iCell < volume(); iCell += pack_t::size) {
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
f_curr[iPop] = pack_t(get(iPop, stage::pre_collision()) + iCell);
}
(apply(cops, iCell, f_curr, f_next), ...);
}
}
template <typename F, typename... ARGS>
void apply(std::vector<typename pack_t::index_t>& cells, ARGS&&... args) {
#ifdef SIMD_CELL_LIST
pack_t f_curr[population::q];
pack_t f_next[population::q];
#pragma omp parallel for private(f_curr, f_next) schedule(static)
for (std::size_t i=0; i < cells.size() / pack_t::size; ++i) {
const std::size_t iIdx = pack_t::size * i;
pack_t::index_t* locs = cells.data() + iIdx;
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
f_curr[iPop] = pack_t(get(iPop, stage::pre_collision()), locs);
}
F::apply(f_curr, f_next, std::forward<ARGS>(args)...);
#pragma GCC unroll population::q
for (unsigned iPop=0; iPop < population::q; ++iPop) {
simd::store(get(iPop, stage::post_collision()), f_next[iPop], locs);
}
}
for (std::size_t i = pack_t::size * (cells.size() / pack_t::size);
i < cells.size();
++i) {
apply<F>(cells[i], std::forward<ARGS>(args
|