1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Each loop does 3 things:
5 * - rank 0 truncates to 0
6 * - all ranks agree on a random chunk size
7 * - all ranks race to write their pattern to their chunk of the file
8 * - rank 0 makes sure that the resulting file size is ranks * chunk size
9 * - rank 0 makes sure that everyone's patterns went to the right place
11 * compile: mpicc -g -Wall -o write_disjoint write_disjoint.c
12 * run: mpirun -np N -machlist <hostlist file> write_disjoint
13 * or: pdsh -w <N hosts> write_disjoint
14 * or: prun -n N [-N M] write_disjoint
18 #include <sys/types.h>
28 #define FILENAME "/mnt/lustre/write_disjoint"
29 #define CHUNK_MAX_SIZE 123456
31 int rprintf(int rank, int loop, const char *fmt, ...)
35 printf("rank %d, loop %d: ", rank, loop);
45 int main (int argc, char *argv[]) {
46 int i, n, fd, chunk_size, file_size;
47 int rank, noProcessors, done;
54 MPI_Init(&argc, &argv);
55 MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
56 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
58 chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
59 for (i=0; i < noProcessors; i++) {
60 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
61 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
63 read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
66 fd = open(FILENAME, O_WRONLY|O_CREAT|O_TRUNC, 0666);
68 rprintf(rank, -1, "open() returned %s\n",
71 MPI_Barrier(MPI_COMM_WORLD);
73 fd = open(FILENAME, O_RDWR);
75 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
77 for (n=0; n < 1000 ; n++) {
78 /* reset the environment */
80 ret = truncate(FILENAME, 0);
82 rprintf(rank, n, "truncate() returned %s\n",
85 chunk_size = rand() % CHUNK_MAX_SIZE;
87 if (n % 1000 == 0 && rank == 0)
88 printf("loop %d: chunk_size %d\n", n, chunk_size);
90 MPI_Barrier(MPI_COMM_WORLD);
93 offset = rank * chunk_size;
94 lseek(fd, offset, SEEK_SET);
98 ret = write(fd, chunk_buf[rank]+done, chunk_size-done);
100 rprintf(rank, n, "write() returned %s\n",
103 } while (done != chunk_size);
105 MPI_Barrier(MPI_COMM_WORLD);
107 /* Check the result */
109 lseek(fd, 0, SEEK_SET);
112 stat(FILENAME, &stat_buf);
113 file_size = stat_buf.st_size;
114 if (file_size != chunk_size * noProcessors)
115 rprintf(rank, n, "invalid file size %d"
116 " instead of %d\n", file_size,
117 chunk_size * noProcessors);
121 ret = read(fd, read_buf + done,
122 (chunk_size * noProcessors) - done);
124 rprintf(rank, n, "read returned %s\n",
128 } while (done != chunk_size * noProcessors);
130 for (i = 0; i < noProcessors; i++) {
133 if (!memcmp(read_buf + (i * chunk_size),
134 chunk_buf[i], chunk_size))
137 printf("rank %d, loop %d: chunk %d corrupted "
138 "with chunk_size %d\n", rank, n, i,
140 printf("(ranks: page boundry, chunk boundry, "
142 for (j = 1 ; j < noProcessors; j++) {
143 int b = j * chunk_size;
144 printf("\t%c -> %c: %d %d %d\n",
145 'A' + j - 1, 'A' + j,
147 (b + 4096) & ~(4096-1));
150 sprintf(command, "od -Ad -a %s", FILENAME);
158 printf("Finished after %d loops\n", n);