Skip to content

Commit 9c947f1

Browse files
committed
test open with multiple procs
1 parent d897bde commit 9c947f1

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

open_multi.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <sys/stat.h>
5+
#include <unistd.h>
6+
#include <fcntl.h>
7+
#include <errno.h>
8+
9+
#include "mpi.h"
10+
#include "unifyfs.h"
11+
12+
int mpi_sum(int val)
13+
{
14+
int sum;
15+
MPI_Allreduce(&val, &sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16+
return sum;
17+
}
18+
19+
off_t getsize(char* file)
20+
{
21+
off_t size = (off_t)-1;
22+
struct stat buf;
23+
int rc = stat(file, &buf);
24+
if (rc == 0) {
25+
size = buf.st_size;
26+
}
27+
return size;
28+
}
29+
30+
int main(int argc, char* argv[])
31+
{
32+
char mountpoint[] = "/unifyfs";
33+
34+
MPI_Init(&argc, &argv);
35+
36+
int rank, ranks;
37+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
38+
MPI_Comm_size(MPI_COMM_WORLD, &ranks);
39+
40+
int ret = unifyfs_mount(mountpoint, rank, ranks, 0);
41+
42+
char file[256];
43+
sprintf(file, "%s/testfile", mountpoint);
44+
45+
size_t bufsize = 1024*1024;
46+
char* buf = (char*) malloc(bufsize);
47+
memset(buf, rank, bufsize);
48+
49+
MPI_Barrier(MPI_COMM_WORLD);
50+
51+
/* create a file in exclusive mode,
52+
* one rank should win, all others should get EEXIST */
53+
errno = 0;
54+
int success = 0;
55+
int eexist = 0;
56+
int fd = open(file, O_WRONLY | O_CREAT | O_EXCL);
57+
if (fd >= 0) {
58+
success = 1;
59+
close(fd);
60+
} else if (errno == EEXIST) {
61+
eexist = 1;
62+
}
63+
64+
/* one rank should win */
65+
int sum = mpi_sum(success);
66+
if (sum != 1) {
67+
}
68+
69+
/* all others should get EEXIST */
70+
sum = mpi_sum(eexist);
71+
if (sum != (ranks - 1)) {
72+
}
73+
74+
MPI_Barrier(MPI_COMM_WORLD);
75+
76+
/* all delete,
77+
* one rank should win, all others should get ENOENT */
78+
errno = 0;
79+
success = 0;
80+
int enoent = 0;
81+
int rc = unlink(file);
82+
if (rc == 0) {
83+
success = 1;
84+
} else if (errno == ENOENT) {
85+
enoent = 1;
86+
}
87+
88+
/* one winner */
89+
sum = mpi_sum(success);
90+
if (sum != 1) {
91+
}
92+
93+
/* everyone else should get ENOENT */
94+
sum = mpi_sum(enoent);
95+
if (sum != (ranks - 1)) {
96+
}
97+
98+
MPI_Barrier(MPI_COMM_WORLD);
99+
100+
/* all create, this time not exclusive */
101+
errno = 0;
102+
success = 0;
103+
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC);
104+
if (fd >= 0) {
105+
success = 1;
106+
close(fd);
107+
}
108+
109+
/* all should succeed */
110+
sum = mpi_sum(success);
111+
if (sum != ranks) {
112+
}
113+
114+
MPI_Barrier(MPI_COMM_WORLD);
115+
116+
/* open file for writing */
117+
errno = 0;
118+
success = 0;
119+
fd = open(file, O_WRONLY);
120+
if (fd >= 0) {
121+
success = 1;
122+
close(fd);
123+
}
124+
125+
/* all should succeeed */
126+
sum = mpi_sum(success);
127+
if (sum != ranks) {
128+
}
129+
130+
MPI_Barrier(MPI_COMM_WORLD);
131+
132+
unlink(file);
133+
134+
MPI_Barrier(MPI_COMM_WORLD);
135+
136+
/* have all ranks write to a different section of the file,
137+
* then open file with truncate on one rank to verify size change */
138+
fd = open(file, O_WRONLY | O_CREAT);
139+
if (fd >= 0) {
140+
off_t offset = (off_t) (rank * bufsize);
141+
pwrite(fd, buf, bufsize, offset);
142+
fsync(fd);
143+
close(fd);
144+
}
145+
MPI_Barrier(MPI_COMM_WORLD);
146+
if (rank == 0) {
147+
/* all ranks should have written some data */
148+
off_t size = getsize(file);
149+
if (size != bufsize * ranks) {
150+
}
151+
152+
/* create file with truncate */
153+
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC);
154+
if (fd >= 0) {
155+
close(fd);
156+
}
157+
158+
/* now file should be 0 length again */
159+
size = getsize(file);
160+
if (size != 0) {
161+
}
162+
}
163+
MPI_Barrier(MPI_COMM_WORLD);
164+
165+
unlink(file);
166+
167+
MPI_Barrier(MPI_COMM_WORLD);
168+
169+
free(buf);
170+
171+
unifyfs_unmount();
172+
173+
MPI_Finalize();
174+
175+
return 0;
176+
}

0 commit comments

Comments
 (0)