-
Notifications
You must be signed in to change notification settings - Fork 1
/
mat_nn_openmp2.hpp
168 lines (159 loc) · 4.49 KB
/
mat_nn_openmp2.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
// OpenMP target offload implementation
#include <omp.h>
#include <unistd.h>
#ifndef USE_VERSION
#define USE_VERSION 1
#endif
void first_touch(site *a, su3_matrix *b, site *c,
size_t total_sites)
{
#if USE_VERSION == 0
# pragma omp parallel for collapse(4)
#elif USE_VERSION == 1
# pragma omp parallel for
#elif USE_VERSION == 2
# pragma omp target teams loop collapse(4)
#elif USE_VERSION == 3
# pragma omp target teams loop
#elif USE_VERSION == 4
# pragma omp target teams distribute parallel for collapse(4)
#elif USE_VERSION == 5
# pragma omp target teams distribute parallel for
#else
// Nothing
#endif
for(int i=0;i<total_sites;++i) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for (int j=0; j<4; ++j) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for(int k=0;k<3;k++) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for(int l=0;l<3;l++){
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
# if USE_VERSION == 2 || USE_VERSION == 3
# pragma omp loop bind(thread)
# endif
for(int m=0;m<3;m++) {
a[i].link[j].e[k][m] = cc;
b[j].e[m][l] = cc;
}
c[i].link[j].e[k][l] = cc;
#else
# if USE_VERSION == 2 || USE_VERSION == 3
# pragma omp loop bind(thread)
# endif
for(int m=0;m<3;m++) {
a[i].link[j].e[k][m].real = cc.real;
a[i].link[j].e[k][m].imag = cc.imag;
b[j].e[m][l].real = cc.real;
b[j].e[m][l].imag = cc.imag;
}
c[i].link[j].e[k][l].real = cc.real;
c[i].link[j].e[k][l].imag = cc.imag;
#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)
{
if (verbose > 0)
std::cout << "Number of threads = " << omp_get_max_threads() << std::endl;
// benchmark loop
double ttotal;
auto tstart = Clock::now();
for (int iters=0; iters<iterations+warmups; ++iters) {
if (iters == warmups)
tstart = Clock::now();
#if USE_VERSION == 0
# pragma omp parallel for collapse(4)
#elif USE_VERSION == 1
# pragma omp parallel for
#elif USE_VERSION == 2
# pragma omp target teams loop collapse(4)
#elif USE_VERSION == 3
# pragma omp target teams loop
#elif USE_VERSION == 4
# pragma omp target teams distribute parallel for collapse(4)
#elif USE_VERSION == 5
# pragma omp target teams distribute parallel for
#else
// Nothing
#endif
for(int i=0;i<total_sites;++i) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for (int j=0; j<4; ++j) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for(int k=0;k<3;k++) {
#if USE_VERSION == 3
# pragma omp loop bind(thread)
#endif
for(int l=0;l<3;l++){
Complx cc = {0.0, 0.0};
#ifndef MILC_COMPLEX
# if USE_VERSION == 2 || USE_VERSION == 3
# pragma omp loop bind(thread)
# endif
for(int m=0;m<3;m++) {
cc += a[i].link[j].e[k][m] * b[j].e[m][l];
}
c[i].link[j].e[k][l] = cc;
#else
# if USE_VERSION == 2 || USE_VERSION == 3
# pragma omp loop bind(thread)
# endif
for(int m=0;m<3;m++) {
CMULSUM(a[i].link[j].e[k][m], b[j].e[m][l], cc);
}
c[i].link[j].e[k][l].real = cc.real;
c[i].link[j].e[k][l].imag = cc.imag;
#endif
}
}
}
}
}
ttotal = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-tstart).count();
// 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);
}