-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_posix_write.c
More file actions
199 lines (162 loc) · 5.31 KB
/
Copy pathtest_posix_write.c
File metadata and controls
199 lines (162 loc) · 5.31 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
193
194
195
196
197
198
199
/* -----------------------------------------------------------------------------
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"
/* 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 pipefd[2];
const char* name = "MSGBUF";
if (-1 == pipe(pipefd)) {
perror("pipe");
exit(EXIT_FAILURE);
}
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);
int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, BUF_SIZE * sizeof(double));
double *shm_ptr = (double *) mmap(0, BUF_SIZE * sizeof(double), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
pid_t cpid = fork();
// child
if (0 == cpid) {
close(pipefd[1]);
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);
double *a = (double *) malloc(BUF_SIZE * sizeof(double));
for (int i=0; i < BUF_SIZE; i++)
a[i] = i;
// synchronize with parent
char dummy;
if (read(pipefd[0], &dummy, 1) != 0)
printf("child didn't get EOF\n");
for (int k=0; k < NTIMES; k++)
{
times[k] = mysecond();
// write data
#ifdef USE_AVX
#pragma omp simd
for (int i=0; i < BUF_SIZE; i++)
shm_ptr[i] = a[i];
#else
memcpy(shm_ptr, 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", "POSIX write",
1.0E-06 * bytes/mintime,
avgtime,
mintime,
maxtime);
// parent
} else {
close(pipefd[0]);
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);
double *b = (double *) malloc(BUF_SIZE * sizeof(double));
for (int i=0; i < BUF_SIZE; i++) {
b[i] = -1;
}
// synchronize with child
close(pipefd[1]);
wait(NULL);
// read data
for (int i=0; i < BUF_SIZE; i++)
b[i] = shm_ptr[i];
// 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));
}
}