1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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";
92 error = MPI_Init(&argc, &argv);
93 if (error != MPI_SUCCESS)
94 rprintf(-1, -1, "MPI_Init failed: %d\n", error);
95 /* Parse command line options */
96 while ((c = getopt(argc, argv, "f:n:")) != EOF) {
102 numloops = strtoul(optarg, NULL, 0);
107 MPI_Comm_size(MPI_COMM_WORLD, &noProcessors);
108 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
110 chunk_buf = malloc(noProcessors * sizeof(chunk_buf[0]));
111 for (i=0; i < noProcessors; i++) {
112 chunk_buf[i] = malloc(CHUNK_MAX_SIZE);
113 memset(chunk_buf[i], 'A'+ i, CHUNK_MAX_SIZE);
115 read_buf = malloc(noProcessors * CHUNK_MAX_SIZE);
118 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
120 rprintf(rank, -1, "open() returned %s\n",
123 MPI_Barrier(MPI_COMM_WORLD);
125 fd = open(filename, O_RDWR);
127 rprintf(rank, -1, "open() returned %s\n", strerror(errno));
129 for (n = 0; n < numloops; n++) {
130 /* reset the environment */
132 ret = truncate(filename, 0);
134 rprintf(rank, n, "truncate() returned %s\n",
137 CHUNK_SIZE(n) = rand() % CHUNK_MAX_SIZE;
139 if (n % 1000 == 0 && rank == 0)
140 printf("loop %d: chunk_size %lu\n", n, CHUNK_SIZE(n));
142 MPI_Barrier(MPI_COMM_WORLD);
145 offset = rank * CHUNK_SIZE(n);
146 lseek(fd, offset, SEEK_SET);
150 ret = write(fd, chunk_buf[rank] + done,
151 CHUNK_SIZE(n) - done);
153 rprintf(rank, n, "write() returned %s\n",
156 } while (done != CHUNK_SIZE(n));
158 MPI_Barrier(MPI_COMM_WORLD);
160 /* Check the result */
161 if (stat(filename, &stat_buf) < 0)
162 rprintf(rank, n, "error stating %s: %s\n",
163 filename, strerror(errno));
165 if (stat_buf.st_size != CHUNK_SIZE(n) * noProcessors) {
167 printf("loop %d: chunk_size %lu, "
168 "file size was %lu\n",
169 n - 1, CHUNK_SIZE(n - 1),
170 CHUNK_SIZE(n - 1) *noProcessors);
171 rprintf(rank, n, "invalid file size %lu"
172 " instead of %lu = %lu * %u\n",
173 (unsigned long)stat_buf.st_size,
174 CHUNK_SIZE(n) * noProcessors,
175 CHUNK_SIZE(n), noProcessors);
179 if (lseek(fd, 0, SEEK_SET) < 0)
180 rprintf(rank, n, "error seeking to 0: %s\n",
185 ret = read(fd, read_buf + done,
186 CHUNK_SIZE(n) * noProcessors - done);
188 rprintf(rank, n, "read returned %s\n",
192 } while (done != CHUNK_SIZE(n) * noProcessors);
194 for (i = 0; i < noProcessors; i++) {
197 if (!memcmp(read_buf + (i * CHUNK_SIZE(n)),
198 chunk_buf[i], CHUNK_SIZE(n)))
201 /* print out previous chunk sizes */
203 printf("loop %d: chunk_size %lu\n",
204 n - 1, CHUNK_SIZE(n - 1));
206 printf("loop %d: chunk %d corrupted "
207 "with chunk_size %lu, page_size %d\n",
208 n, i, CHUNK_SIZE(n), getpagesize());
209 printf("ranks:\tpage boundry\tchunk boundry\t"
211 for (j = 1 ; j < noProcessors; j++) {
212 int b = j * CHUNK_SIZE(n);
213 printf("%c -> %c:\t%d\t%d\t%d\n",
214 'A' + j - 1, 'A' + j,
215 b & ~(getpagesize()-1), b,
216 (b + getpagesize()) &
220 sprintf(command, "od -Ad -a %s", filename);
222 rprintf(0, n, "data check error - exiting\n");
227 printf("Finished after %d loops\n", n);