-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_omp_write.c
225 lines (190 loc) · 5.93 KB
/
test_omp_write.c
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
/* -----------------------------------------------------------------------------
MIT License
Copyright (c) 2022 CSC HPC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------ */
#include "shmem_tests.h"
#include <omp.h>
#ifdef USE_INTRINSICS
#include <immintrin.h>
#endif
/* Borrowed from util-linux-2.13-pre7/schedutils/taskset.c */
static char *cpuset_to_cstr(cpu_set_t *mask, char *str)
{
char *ptr = str;
int i, j, entry_made = 0;
for (i = 0; i < CPU_SETSIZE; i++) {
if (CPU_ISSET(i, mask)) {
int run = 0;
entry_made = 1;
for (j = i + 1; j < CPU_SETSIZE; j++) {
if (CPU_ISSET(j, mask)) run++;
else break;
}
if (!run)
sprintf(ptr, "%d,", i);
else if (run == 1) {
sprintf(ptr, "%d,%d,", i, i + 1);
i++;
} else {
sprintf(ptr, "%d-%d,", i, i + run);
i += run;
}
while (*ptr != 0) ptr++;
}
}
ptr -= entry_made;
*ptr = 0;
return(str);
}
double mysecond()
{
struct timeval tp;
struct timezone tzp;
int i;
i = gettimeofday(&tp,&tzp);
return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 );
}
int main(int argc, char** argv)
{
double times[NTIMES];
double avgtime = 0;
double mintime = FLT_MAX;
double maxtime = 0;
int nthreads;
#pragma omp parallel
nthreads = omp_get_num_threads();
if (nthreads != 2) {
printf("Only two threads can be used\n");
return -1;
}
cpu_set_t coremask;
char clbuf[7 * CPU_SETSIZE];
sched_getaffinity(0, sizeof(coremask), &coremask);
memset(clbuf, 0, sizeof(clbuf));
cpuset_to_cstr(&coremask, clbuf);
printf("Original affinity: %s\n", clbuf);
fflush(stdout);
double *a, *b;
printf("Array size: %d MB\n", BUF_SIZE / 1000000);
#ifdef USE_AVX
printf("Using omp SIMD (note: compiler might still optimize for memcpy)\n");
#elif defined USE_INTRINSICS
#ifdef USE_STREAMING
printf("Using intrinsics with non-temporal stores\n");
#else
printf("Using intrinsics\n");
#endif
#else
printf("Using memcpy\n");
#endif
#pragma omp parallel
{
int tid = omp_get_thread_num();
// child
if (1 == tid) {
char clbuf[7 * CPU_SETSIZE];
int ncpus = CPU_COUNT(&coremask);
CPU_ZERO(&coremask);
CPU_SET(ncpus-1, &coremask);
sched_setaffinity(0, sizeof(coremask), &coremask);
memset(clbuf, 0, sizeof(clbuf));
cpuset_to_cstr(&coremask, clbuf);
printf("Affinity in child: %s\n", clbuf);
fflush(stdout);
// a = (double *) malloc(BUF_SIZE * sizeof(double));
posix_memalign((void *)&a, 32, BUF_SIZE * sizeof(double));
// invalidate data
for (int i=0; i < BUF_SIZE; i++) {
a[i] = i;
}
// synchronize with parent
#pragma omp barrier
// write data
for (int k=0; k < NTIMES; k++)
{
times[k] = mysecond();
// write data
#ifdef USE_AVX
#pragma omp simd aligned(a, b : 32)
for (int i=0; i < BUF_SIZE; i++)
b[i] = a[i];
#elif defined USE_INTRINSICS
for (int i=0; i < BUF_SIZE; i += 4) {
__m256d src = _mm256_load_pd(&a[i]);
#ifdef USE_STREAMING
_mm256_stream_pd(&b[i], src);
#else
_mm256_store_pd(&b[i], src);
#endif
}
#else
memcpy(b, a, sizeof(double) * BUF_SIZE);
#endif
times[k] = mysecond() - times[k];
}
for (int k=1; k < NTIMES; k++) /* note -- skip first iteration */
{
avgtime = avgtime + times[k];
mintime = MIN(mintime, times[k]);
maxtime = MAX(maxtime, times[k]);
}
size_t bytes = BW_CONVENTION * sizeof(double) * BUF_SIZE;
printf("Function Best Rate MB/s Avg time Min time Max time\n");
avgtime = avgtime / (double)(NTIMES-1);
printf("%s%12.1f %11.6f %11.6f %11.6f\n", "omp write",
1.0E-06 * bytes/mintime,
avgtime,
mintime,
maxtime);
#pragma omp barrier
// parent
} else {
char clbuf[7 * CPU_SETSIZE];
cpu_set_t coremask;
CPU_ZERO(&coremask);
CPU_SET(0, &coremask);
sched_setaffinity(0, sizeof(coremask), &coremask);
memset(clbuf, 0, sizeof(clbuf));
cpuset_to_cstr(&coremask, clbuf);
printf("Affinity in parent: %s\n", clbuf);
fflush(stdout);
// b = (double *) malloc(BUF_SIZE * sizeof(double));
posix_memalign((void *)&b, 32, BUF_SIZE * sizeof(double));
// invalidate data
for (int i=0; i < BUF_SIZE; i++) {
b[i] = -1;
}
// synchronize with child
#pragma omp barrier
// synchronize after write
#pragma omp barrier
// Use Kahan's algorithm for summation
double checksum = b[0];
double tmp_c = 0.0;
for (size_t i=1; i < BUF_SIZE; i++) {
double y = b[i] - tmp_c;
double t = checksum + y;
tmp_c = (t - checksum) - y;
checksum = t;
}
checksum /= (double) BUF_SIZE;
printf(HLINE);
printf("check: %f %f\n", checksum, 0.5*(BUF_SIZE-1));
}
} // end omp parallel
}