-
Notifications
You must be signed in to change notification settings - Fork 1
/
mat_nn_openmp.hpp
287 lines (260 loc) · 8.89 KB
/
mat_nn_openmp.hpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// OpenMP target offload implementation
#include <omp.h>
#include <unistd.h>
#define THREADS_PER_SITE 36
#define NUM_TEAMS 1600
#ifndef USE_VERSION
#define USE_VERSION 2
#endif
double su3_mat_nn(std::vector<site> &a, std::vector<su3_matrix> &b, std::vector<site> &c,
size_t total_sites, size_t iterations, size_t threads_per_team, int use_device, Profile* profile)
{
size_t num_teams = NUM_TEAMS;
// Set num_teams from the command line
int opt;
optind = 1;
while ((opt=getopt(g_argc, g_argv, ":n:")) != -1) {
switch (opt) {
case 'n':
num_teams = atoi(optarg);
break;
}
}
if (threads_per_team == 0)
threads_per_team = THREADS_PER_SITE;
site *d_a, *d_c;
su3_matrix *d_b;
size_t len_a, len_b, len_c;
d_a = a.data(); len_a = a.size();
d_b = b.data(); len_b = b.size();
d_c = c.data(); len_c = c.size();
// Move A and B data to the device, Allocate C data
double ttotal;
auto tprofiling = Clock::now();
#pragma omp target enter data map(to: d_a[0:len_a], d_b[0:len_b]) map(alloc: d_c[0:len_c])
profile->host_to_device_time = (std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-tprofiling).count())/1.0e6;
// benchmark loop
auto tstart = Clock::now();
tprofiling = tstart;
#if USE_VERSION == 0
// Baseline implementation
// Original intent is to have teams process whole sites,
// hence sites are distributed across the teams
// However, for the Clang 10.0 OpenMP compiler this has issues in that memory gets
// flushed after each parallel region causing excessive global memory traffic
// See USE_VERSION
if (verbose >= 1) {
std::cout << "Number of teams = " << num_teams << std::endl;
std::cout << "Threads per team = " << threads_per_team << std::endl;
}
for (int iters=0; iters<iterations+warmups; ++iters) {
if (iters == warmups) {
tstart = Clock::now();
tprofiling = tstart;
}
#pragma omp target teams distribute
for(int i=0;i<total_sites;++i) {
#pragma omp parallel for collapse(3)
for (int j=0; j<4; ++j) {
for(int k=0;k<3;k++) {
for(int l=0;l<3;l++){
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
for(int m=0;m<3;m++) {
cc += d_a[i].link[j].e[k][m] * d_b[j].e[m][l];
}
d_c[i].link[j].e[k][l] = cc;
#else
for(int m=0;m<3;m++) {
CMULSUM(d_a[i].link[j].e[k][m], d_b[j].e[m][l], cc);
}
d_c[i].link[j].e[k][l].real = cc.real;
d_c[i].link[j].e[k][l].imag = cc.imag;
#endif
}
}
}
}
}
#elif USE_VERSION == 1
// This version improves performance over the baseline
// Contributed by Chris Daley, NERSC
if (verbose >= 1) {
std::cout << "Number of teams = " << num_teams << std::endl;
std::cout << "Threads per team = " << threads_per_team << std::endl;
}
for (int iters=0; iters<iterations+warmups; ++iters) {
if (iters == warmups) {
tstart = Clock::now();
tprofiling = tstart;
}
#pragma omp target teams
{
#pragma omp parallel
{
int total_teams = omp_get_num_teams();
int team_id = omp_get_team_num();
int sites_per_team = (total_sites + total_teams - 1) / total_teams;
int istart = team_id * sites_per_team;
if (istart > total_sites) istart = total_sites;
int iend = istart + sites_per_team;
if (iend > total_sites) iend = total_sites;
for (int i = istart; i < iend; ++i) {
#pragma omp for collapse(3)
for (int j=0; j<4; ++j) {
for(int k=0;k<3;k++) {
for(int l=0;l<3;l++){
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
for(int m=0;m<3;m++) {
cc += d_a[i].link[j].e[k][m] * d_b[j].e[m][l];
}
d_c[i].link[j].e[k][l] = cc;
#else
for(int m=0;m<3;m++) {
CMULSUM(d_a[i].link[j].e[k][m], d_b[j].e[m][l], cc);
}
d_c[i].link[j].e[k][l].real = cc.real;
d_c[i].link[j].e[k][l].imag = cc.imag;
#endif
}
}
}
} // end of i loop
} // end of parallel region
} // end of teams region
}
#elif USE_VERSION == 2
// This code improves performance over above baseline
// Similar to Cuda and OpenCL work item approach
// Initial contribution by Xinmin Tian, Intel
size_t num_work_items = total_sites * THREADS_PER_SITE;
if (verbose >= 1) {
std::cout << "Number of teams = " << num_teams << std::endl;
std::cout << "Threads per team = " << threads_per_team << std::endl;
std::cout << "Number of work items = " << num_work_items << std::endl;
}
for (int iters=0; iters<iterations+warmups; ++iters) {
if (iters == warmups) {
tstart = Clock::now();
tprofiling = tstart;
}
#pragma omp target teams distribute parallel for
for (int id =0; id < num_work_items; id++) {
int i = id/36;
if (i < total_sites) {
int j = (id%36)/9;
int k = (id%9)/3;
int l = id%3;
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
for(int m=0;m<3;m++) {
cc += d_a[i].link[j].e[k][m] * d_b[j].e[m][l];
}
d_c[i].link[j].e[k][l] = cc;
#else
for(int m=0;m<3;m++) {
CMULSUM(d_a[i].link[j].e[k][m], d_b[j].e[m][l], cc);
}
d_c[i].link[j].e[k][l] = cc;
#endif
}
}
}
#else // VERSION == 3 || VERSION == 4
// Baseline implementation
// Uses the purest intent of OpenMP
// Version 3 is a prescriptive approach using OpenMP-4.5 constructs
// Version 4 is a descriptive approach using the OpenMP-5.0 loop construct and
// giving the compiler the freedom to choose the number of teams and threads per team
if (verbose >= 1) {
#if USE_VERSION == 3
#ifdef NOTARGET
std::cout << "Number of threads = " << omp_get_max_threads() << std::endl;
#else
std::cout << "Number of teams = " << num_teams << std::endl;
std::cout << "Threads per team = " << threads_per_team << std::endl;
#endif
#elif USE_VERSION == 4
std::cout << "Number of teams = " << "Compiler selected" << std::endl;
std::cout << "Threads per team = " << "Compiler selected" << std::endl;
#endif
}
for (int iters=0; iters<iterations+warmups; ++iters) {
if (iters == warmups) {
tstart = Clock::now();
tprofiling = tstart;
}
#if USE_VERSION == 3
#ifdef NOTARGET
#pragma omp parallel for schedule(static)
#else
#pragma omp target teams distribute parallel for collapse(4) num_teams(num_teams) thread_limit(threads_per_team)
#endif
#elif USE_VERSION == 4
#pragma omp target teams loop collapse(4)
#endif
for(int i=0;i<total_sites;++i) {
for (int j=0; j<4; ++j) {
for(int k=0;k<3;k++) {
for(int l=0;l<3;l++){
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
#if USE_VERSION == 4
#pragma omp loop bind(thread)
#endif
for(int m=0;m<3;m++) {
cc += d_a[i].link[j].e[k][m] * d_b[j].e[m][l];
}
d_c[i].link[j].e[k][l] = cc;
#else
#if USE_VERSION == 4
#pragma omp loop bind(thread)
#endif
for(int m=0;m<3;m++) {
CMULSUM(d_a[i].link[j].e[k][m], d_b[j].e[m][l], cc);
}
d_c[i].link[j].e[k][l] = cc;
#endif
}
}
}
}
}
#endif
profile->kernel_time = (std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-tprofiling).count())/1.0e6;
ttotal = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-tstart).count();
// C gets moved back to the host
tprofiling = Clock::now();
#pragma omp target exit data map(from: d_c[0:len_c])
profile->device_to_host_time = (std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-tprofiling).count())/1.0e6;
// It is not possible to check for NaNs when the application is compiled with -ffast-math
// Therefore we print out the calculated checksum as a manual check for the user.
// This is helpful when using LLVM/Clang-10.0 to compile the OpenMP target offload
// implementation without MILC_COMPLEX (i.e. using std::complex).
double sum = 0.0;
for (int i=0;i<total_sites;++i) for(int j=0;j<4;++j) for(int k=0;k<3;++k) for(int l=0;l<3;++l) {
Complx cc = {0.0, 0.0};
for(int m=0;m<3;m++) {
#ifdef MILC_COMPLEX
CMULSUM( a[i].link[j].e[k][m], b[j].e[m][l], cc)
#else
cc += a[i].link[j].e[k][m] * b[j].e[m][l];
#endif
}
#ifdef MILC_COMPLEX
sum += c[i].link[j].e[k][l].real;
#else
sum += std::real(c[i].link[j].e[k][l]);
#endif
}
sum /= (double)total_sites;
if (almost_equal(sum, 4.0*sizeof(su3_matrix)/(sizeof(Complx)), 1E-6)) {
if (verbose > 0)
printf("Checksum SUCCESS... though please be diligent and check the "
"following value is not NaN: checksum=%.0lf\n", sum);
} else {
printf("Checksum FAILURE\n");
}
return (ttotal /= 1.0e6);
}