-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathArbLatticeLauncher.hpp.Rt
More file actions
192 lines (172 loc) · 6.06 KB
/
ArbLatticeLauncher.hpp.Rt
File metadata and controls
192 lines (172 loc) · 6.06 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
<?R
source("conf.R")
c_header();
?>
#ifndef ARBLATTICELAUNCHER_HPP
#define ARBLATTICELAUNCHER_HPP
#include "ArbLatticeAccess.hpp"
#include "ArbLatticeLauncher.h"
#include "GetThreads.h"
#include "LatticeData.hpp"
#include "Node.hpp"
#include "CudaUtils.hpp"
template <eOperationType I, eCalculateGlobals G, eStage S>
struct ArbLatticeExecutor : public LinearExecutor {
ArbLatticeContainer container;
LatticeData data;
unsigned offset; /// Starting offset for the iteration space, allows the reuse of the executor for both the border and interior
CudaDeviceFunction void Execute() const {
using LA = ArbLatticeAccess;
using N = Node<LA, I, G, S>;
const int i = threadID(CudaThread, CudaBlock, CudaNumberOfThreads);
if (inRange(i)) {
const unsigned node_lid = offset + i;
ArbLatticeAccess acc(node_lid, container);
N now(acc, data);
now.RunElement();
}
}
};
template <eOperationType I, eCalculateGlobals G, eStage S>
void ArbLatticeLauncher::RunBorder(CudaStream_t stream, const LatticeData& data) const {
const ArbLatticeExecutor<I, G, S> executor{{container.num_border_nodes}, container, data, 0};
LaunchExecutorAsync(executor, stream);
}
template <eOperationType I, eCalculateGlobals G, eStage S>
void ArbLatticeLauncher::RunInterior(CudaStream_t stream, const LatticeData& data) const {
const ArbLatticeExecutor<I, G, S> executor{{container.num_interior_nodes}, container, data, container.num_border_nodes};
LaunchExecutorAsync(executor, stream);
}
struct ArbLatticePacker : public LinearExecutor {
const storage_t* src;
storage_t* dest;
const size_t* src_inds;
const size_t* dest_inds;
CudaDeviceFunction void Execute() const {
const int i = threadID(CudaThread, CudaBlock, CudaNumberOfThreads);
if(inRange(i)) {
const size_t si = src_inds ? src_inds[i] : i;
const size_t di = dest_inds ? dest_inds[i] : i;
dest[di] = src[si];
}
}
};
void ArbLatticeLauncher::pack(CudaStream_t stream) const {
const ArbLatticePacker executor{{container.pack_sz}, container.snap_out, container.pack_buf, container.pack_inds, nullptr};
LaunchExecutorAsync(executor, stream);
}
void ArbLatticeLauncher::unpack(CudaStream_t stream) const {
const ArbLatticePacker executor{{container.unpack_sz}, container.unpack_buf, container.snap_out, nullptr, container.unpack_inds};
LaunchExecutorAsync(executor, stream);
}
void ArbLatticeLauncher::getQuantity(int quant, real_t* host_tab, real_t scale, const LatticeData& data) const {
const size_t num_nodes = container.num_border_nodes + container.num_interior_nodes;
switch(quant) { <?R
for (q in rows(Quantities)) { ifdef(q$adjoint);
?>
case <?%s q$Index ?>: {
const auto gpu_tab = cudaMakeUnique<<?%s q$type ?>>(num_nodes);
getQuantity<?%s q$name ?>(gpu_tab.get(), scale, data);
CudaMemcpy(host_tab, gpu_tab.get(), num_nodes * sizeof(<?%s q$type ?>), CudaMemcpyDeviceToHost);
break;
} <?R
}
ifdef();
?>
}
}
void ArbLatticeLauncher::SampleQuantity(int quant, int lid, real_t* host_tab, real_t scale, const LatticeData &data) {
switch(quant) { <?R
for (q in rows(Quantities)) { ifdef(q$adjoint);
?>
case <?%s q$Index ?>: {
const auto gpu_tab = cudaMakeUnique<<?%s q$type ?>>(1);
getSample<?%s q$name ?>(lid, gpu_tab.get(), scale, data);
CudaMemcpy(host_tab, gpu_tab.get(), sizeof(<?%s q$type ?>), CudaMemcpyDeviceToHost);
break;
} <?R
}
ifdef();
?>
}
}
<?R for (q in rows(Quantities)) { ifdef(q$adjoint);
if (q$adjoint) {
node = "Node_Adj"
} else {
node = "Node"
}
?>
struct GetQuantityArbExecutor<?%s q$name?> : public LinearExecutor {
ArbLatticeContainer container;
LatticeData data;
<?%s q$type ?>* buf;
real_t scale;
CudaDeviceFunction void Execute() const {
using LA = ArbLatticeAccess; <?R
if (q$adjoint) { ?>
using N = Node< LA, Adjoint, NoGlobals, Get >; <?R
} else { ?>
using N = Node< LA, Primal, NoGlobals, Get >; <?R
}?>
const int i = threadID(CudaThread, CudaBlock, CudaNumberOfThreads);
if (inRange(i)) {
LA acc(i, container);
N now(acc, data);
acc.pop(now); <?R
if (q$adjoint) { ?>
acc.pop_adj(now); <?R
} ?>
auto val = now.get<?%s q$name ?>(); <?R
if (q$type == "vector_t") {
for (coef in c("x","y","z")) { ?>
val.<?%s coef ?> *= scale; <?R
}
} else { ?>
val *= scale; <?R
} ?>
buf[i] = val;
}
}
};
struct GetSampleArbExecutor<?%s q$name?> : public LinearExecutor {
ArbLatticeContainer container;
LatticeData data;
int lid;
<?%s q$type ?>* buf;
real_t scale;
CudaDeviceFunction void Execute() const {
using LA = ArbLatticeAccess; <?R
if (q$adjoint) { ?>
using N = Node< LA, Adjoint, NoGlobals, Get >; <?R
} else { ?>
using N = Node< LA, Primal, NoGlobals, Get >; <?R
}?>
LA acc(lid, container);
N now(acc, data);
acc.pop(now); <?R
if (q$adjoint) { ?>
acc.pop_adj(now); <?R
} ?>
auto val = now.get<?%s q$name ?>(); <?R
if (q$type == "vector_t") {
for (coef in c("x","y","z")) { ?>
val.<?%s coef ?> *= scale; <?R
}
} else { ?>
val *= scale; <?R
} ?>
buf[0] = val;
}
};
void ArbLatticeLauncher::getQuantity<?%s q$name ?>(<?%s q$type ?>* tab, real_t scale, const LatticeData& data) const {
const GetQuantityArbExecutor<?%s q$name?> executor{{container.num_border_nodes + container.num_interior_nodes}, container, data, tab, scale};
LaunchExecutor(executor);
}
void ArbLatticeLauncher::getSample<?%s q$name ?>(int lid, <?%s q$type ?>* tab, real_t scale, const LatticeData& data) const {
const GetSampleArbExecutor<?%s q$name?> executor{{1}, container, data, lid, tab, scale};
LaunchExecutor(executor);
}
<?R }
ifdef() ?>
#endif // ARBLATTICELAUNCHER_HPP