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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/write_disjoint.c
38 * Each loop does 3 things:
39 * - rank 0 truncates to 0
40 * - all ranks agree on a random chunk size
41 * - all ranks race to write their pattern to their chunk of the file
42 * - rank 0 makes sure that the resulting file size is ranks * chunk size
43 * - rank 0 makes sure that everyone's patterns went to the right place
45 * compile: mpicc -g -Wall -o write_disjoint write_disjoint.c
46 * run: mpirun -np N -machlist <hostlist file> write_disjoint
47 * or: pdsh -w <N hosts> write_disjoint
48 * or: prun -n N [-N M] write_disjoint
53 #include <sys/types.h>
62 #define CHUNK_MAX_SIZE 123456
64 void rprintf(int rank, int loop, const char *fmt, ...)
68 printf("rank %d, loop %d: ", rank, loop);
74 MPI_Abort(MPI_COMM_WORLD, -1); /* This will exit() according to man */
77 #define CHUNK_SIZE(n) chunk_size[(n) % 2]
79 int main (int argc, char *argv[]) {
81 unsigned long chunk_size[2];
82 int rank, noProcessors, done;
89 char *filename = "/mnt/lustre/write_disjoint";
93 error = MPI_Init(&argc, &argv);
94 if (error != MPI_SUCCESS)
95 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
96 /* Parse command line options */
97 while ((c = getopt(argc, argv, "f:n:")) != EOF) {
103 numloops = strtoul(optarg, NULL, 0);
108 MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
109 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
111 chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
112 for (i=0; i < noProcessors; i++) {
113 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
114 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
116 read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
119 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
121 rprintf(rank, -1, "open() returned %s\n",
124 MPI_Barrier(MPI_COMM_WORLD);
126 fd = open(filename, O_RDWR);
128 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
130 for (n = 0; n < numloops; n++) {
131 /* reset the environment */
133 ret = truncate(filename, 0);
135 rprintf(rank, n, "truncate() returned %s\n",
139 MPI_Bcast(&random, 1, MPI_INT, 0, MPI_COMM_WORLD);
140 CHUNK_SIZE(n) = random % CHUNK_MAX_SIZE;
142 if (n % 1000 == 0 && rank == 0)
143 printf("loop %d: chunk_size %lu\n", n, CHUNK_SIZE(n));
145 if (stat(filename, &stat_buf) < 0)
146 rprintf(rank, n, "error stating %s: %s\n",
147 filename, strerror(errno));
149 if (stat_buf.st_size != 0)
150 rprintf(rank, n, "filesize = %lu. "
151 "Should be zero after truncate\n",
154 MPI_Barrier(MPI_COMM_WORLD);
157 offset = rank * CHUNK_SIZE(n);
158 lseek(fd, offset, SEEK_SET);
162 ret = write(fd, chunk_buf[rank] + done,
163 CHUNK_SIZE(n) - done);
164 if (ret < 0 && errno != EINTR)
165 rprintf(rank, n, "write() returned %s\n",
169 } while (done != CHUNK_SIZE(n));
171 MPI_Barrier(MPI_COMM_WORLD);
173 /* Check the result */
174 if (stat(filename, &stat_buf) < 0)
175 rprintf(rank, n, "error stating %s: %s\n",
176 filename, strerror(errno));
178 if (stat_buf.st_size != CHUNK_SIZE(n) * noProcessors) {
180 printf("loop %d: chunk_size %lu, "
181 "file size was %lu\n",
182 n - 1, CHUNK_SIZE(n - 1),
183 CHUNK_SIZE(n - 1) *noProcessors);
184 rprintf(rank, n, "invalid file size %lu"
185 " instead of %lu = %lu * %u\n",
186 (unsigned long)stat_buf.st_size,
187 CHUNK_SIZE(n) * noProcessors,
188 CHUNK_SIZE(n), noProcessors);
192 if (lseek(fd, 0, SEEK_SET) < 0)
193 rprintf(rank, n, "error seeking to 0: %s\n",
198 ret = read(fd, read_buf + done,
199 CHUNK_SIZE(n) * noProcessors - done);
201 rprintf(rank, n, "read returned %s\n",
205 } while (done != CHUNK_SIZE(n) * noProcessors);
207 for (i = 0; i < noProcessors; i++) {
211 if (!memcmp(read_buf + (i * CHUNK_SIZE(n)),
212 chunk_buf[i], CHUNK_SIZE(n)))
215 /* print out previous chunk sizes */
217 printf("loop %d: chunk_size %lu\n",
218 n - 1, CHUNK_SIZE(n - 1));
220 printf("loop %d: chunk %d corrupted "
221 "with chunk_size %lu, page_size %d\n",
222 n, i, CHUNK_SIZE(n), getpagesize());
223 printf("ranks:\tpage boundry\tchunk boundry\t"
225 for (j = 1 ; j < noProcessors; j++) {
226 int b = j * CHUNK_SIZE(n);
227 printf("%c -> %c:\t%d\t%d\t%d\n",
228 'A' + j - 1, 'A' + j,
229 b & ~(getpagesize()-1), b,
230 (b + getpagesize()) &
234 sprintf(command, "od -Ad -a %s", filename);
235 ret = system(command);
236 rprintf(0, n, "data check error - exiting\n");
239 MPI_Barrier(MPI_COMM_WORLD);
242 printf("Finished after %d loops\n", n);