4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/tests/write_disjoint.c
36 * Each loop does 3 things:
37 * - rank 0 truncates to 0
38 * - all ranks agree on a random chunk size
39 * - all ranks race to write their pattern to their chunk of the file
40 * - rank 0 makes sure that the resulting file size is ranks * chunk size
41 * - rank 0 makes sure that everyone's patterns went to the right place
43 * compile: mpicc -g -Wall -o write_disjoint write_disjoint.c
44 * run: mpirun -np N -machlist <hostlist file> write_disjoint
45 * or: pdsh -w <N hosts> write_disjoint
46 * or: prun -n N [-N M] write_disjoint
51 #include <sys/types.h>
60 #define CHUNK_MAX_SIZE 123456
62 void rprintf(int rank, int loop, const char *fmt, ...)
66 printf("rank %d, loop %d: ", rank, loop);
72 MPI_Abort(MPI_COMM_WORLD, -1); /* This will exit() according to man */
75 #define CHUNK_SIZE(n) chunk_size[(n) % 2]
77 int main (int argc, char *argv[]) {
79 unsigned long chunk_size[2];
80 int rank, noProcessors, done;
87 char *filename = "/mnt/lustre/write_disjoint";
91 error = MPI_Init(&argc, &argv);
92 if (error != MPI_SUCCESS)
93 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
94 /* Parse command line options */
95 while ((c = getopt(argc, argv, "f:n:")) != EOF) {
101 numloops = strtoul(optarg, NULL, 0);
106 MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
107 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
109 chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
110 for (i=0; i < noProcessors; i++) {
111 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
112 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
114 read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
117 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
119 rprintf(rank, -1, "open() returned %s\n",
122 MPI_Barrier(MPI_COMM_WORLD);
124 fd = open(filename, O_RDWR);
126 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
128 for (n = 0; n < numloops; n++) {
129 /* reset the environment */
131 ret = truncate(filename, 0);
133 rprintf(rank, n, "truncate() returned %s\n",
137 MPI_Bcast(&random, 1, MPI_INT, 0, MPI_COMM_WORLD);
138 CHUNK_SIZE(n) = random % CHUNK_MAX_SIZE;
140 if (n % 1000 == 0 && rank == 0)
141 printf("loop %d: chunk_size %lu\n", n, CHUNK_SIZE(n));
143 if (stat(filename, &stat_buf) < 0)
144 rprintf(rank, n, "error stating %s: %s\n",
145 filename, strerror(errno));
147 if (stat_buf.st_size != 0)
148 rprintf(rank, n, "filesize = %lu. "
149 "Should be zero after truncate\n",
152 MPI_Barrier(MPI_COMM_WORLD);
155 offset = rank * CHUNK_SIZE(n);
156 lseek(fd, offset, SEEK_SET);
160 ret = write(fd, chunk_buf[rank] + done,
161 CHUNK_SIZE(n) - done);
162 if (ret < 0 && errno != EINTR)
163 rprintf(rank, n, "write() returned %s\n",
167 } while (done != CHUNK_SIZE(n));
169 MPI_Barrier(MPI_COMM_WORLD);
171 /* Check the result */
172 if (stat(filename, &stat_buf) < 0)
173 rprintf(rank, n, "error stating %s: %s\n",
174 filename, strerror(errno));
176 if (stat_buf.st_size != CHUNK_SIZE(n) * noProcessors) {
178 printf("loop %d: chunk_size %lu, "
179 "file size was %lu\n",
180 n - 1, CHUNK_SIZE(n - 1),
181 CHUNK_SIZE(n - 1) *noProcessors);
182 rprintf(rank, n, "invalid file size %lu"
183 " instead of %lu = %lu * %u\n",
184 (unsigned long)stat_buf.st_size,
185 CHUNK_SIZE(n) * noProcessors,
186 CHUNK_SIZE(n), noProcessors);
190 if (lseek(fd, 0, SEEK_SET) < 0)
191 rprintf(rank, n, "error seeking to 0: %s\n",
196 ret = read(fd, read_buf + done,
197 CHUNK_SIZE(n) * noProcessors - done);
199 rprintf(rank, n, "read returned %s\n",
203 } while (done != CHUNK_SIZE(n) * noProcessors);
205 for (i = 0; i < noProcessors; i++) {
208 if (!memcmp(read_buf + (i * CHUNK_SIZE(n)),
209 chunk_buf[i], CHUNK_SIZE(n)))
212 /* print out previous chunk sizes */
214 printf("loop %d: chunk_size %lu\n",
215 n - 1, CHUNK_SIZE(n - 1));
217 printf("loop %d: chunk %d corrupted "
218 "with chunk_size %lu, page_size %d\n",
219 n, i, CHUNK_SIZE(n), getpagesize());
220 printf("ranks:\tpage boundry\tchunk boundry\t"
222 for (j = 1 ; j < noProcessors; j++) {
223 int b = j * CHUNK_SIZE(n);
224 printf("%c -> %c:\t%d\t%d\t%d\n",
225 'A' + j - 1, 'A' + j,
226 b & ~(getpagesize()-1), b,
227 (b + getpagesize()) &
231 sprintf(command, "od -Ad -a %s", filename);
232 rc = system(command);
233 rprintf(0, n, "data check error - exiting\n");
236 MPI_Barrier(MPI_COMM_WORLD);
239 printf("Finished after %d loops\n", n);