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>
27 #define CHUNK_MAX_SIZE 123456
29 void rprintf(int rank, int loop, const char *fmt, ...)
33 printf("rank %d, loop %d: ", rank, loop);
39 MPI_Abort(MPI_COMM_WORLD, -1);
42 int main (int argc, char *argv[]) {
43 int i, n, fd, chunk_size, file_size;
44 int rank, noProcessors, done;
51 char *filename = "/mnt/lustre/write_disjoint";
54 error = MPI_Init(&argc, &argv);
55 if (error != MPI_SUCCESS)
56 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
57 /* Parse command line options */
58 while ((c = getopt(argc, argv, "f:n:")) != EOF) {
64 numloops = strtoul(optarg, NULL, 0);
69 MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
70 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
72 chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
73 for (i=0; i < noProcessors; i++) {
74 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
75 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
77 read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
80 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
82 rprintf(rank, -1, "open() returned %s\n",
85 MPI_Barrier(MPI_COMM_WORLD);
87 fd = open(filename, O_RDWR);
89 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
91 for (n=0; n < numloops; n++) {
92 /* reset the environment */
94 ret = truncate(filename, 0);
96 rprintf(rank, n, "truncate() returned %s\n",
99 chunk_size = rand() % CHUNK_MAX_SIZE;
101 if (n % 1000 == 0 && rank == 0)
102 printf("loop %d: chunk_size %d\n", n, chunk_size);
104 MPI_Barrier(MPI_COMM_WORLD);
107 offset = rank * chunk_size;
108 lseek(fd, offset, SEEK_SET);
112 ret = write(fd, chunk_buf[rank]+done, chunk_size-done);
114 rprintf(rank, n, "write() returned %s\n",
117 } while (done != chunk_size);
119 MPI_Barrier(MPI_COMM_WORLD);
121 /* Check the result */
123 lseek(fd, 0, SEEK_SET);
126 stat(filename, &stat_buf);
127 file_size = stat_buf.st_size;
128 if (file_size != chunk_size * noProcessors)
129 rprintf(rank, n, "invalid file size %d"
130 " instead of %d\n", file_size,
131 chunk_size * noProcessors);
135 ret = read(fd, read_buf + done,
136 (chunk_size * noProcessors) - done);
138 rprintf(rank, n, "read returned %s\n",
142 } while (done != chunk_size * noProcessors);
144 for (i = 0; i < noProcessors; i++) {
147 if (!memcmp(read_buf + (i * chunk_size),
148 chunk_buf[i], chunk_size))
151 printf("rank %d, loop %d: chunk %d corrupted "
152 "with chunk_size %d, page_size %d\n",
153 rank, n, i, chunk_size, getpagesize());
154 printf("(ranks: page boundry, chunk boundry, "
156 for (j = 1 ; j < noProcessors; j++) {
157 int b = j * chunk_size;
158 printf("\t%c -> %c: %d %d %d\n",
159 'A' + j - 1, 'A' + j,
160 b & ~(getpagesize()-1), b,
161 (b + getpagesize()) & ~(getpagesize()-1));
164 sprintf(command, "od -Ad -a %s", filename);
172 printf("Finished after %d loops\n", n);