|
Packit Service |
c5cf8c |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*-
|
|
Packit Service |
c5cf8c |
* vim: ts=8 sts=4 sw=4 noexpandtab
|
|
Packit Service |
c5cf8c |
*
|
|
Packit Service |
c5cf8c |
* (C) 2001 by Argonne National Laboratory.
|
|
Packit Service |
c5cf8c |
* See COPYRIGHT in top-level directory.
|
|
Packit Service |
c5cf8c |
*/
|
|
Packit Service |
c5cf8c |
#include "mpi.h"
|
|
Packit Service |
c5cf8c |
#include <stdio.h>
|
|
Packit Service |
c5cf8c |
#include <string.h>
|
|
Packit Service |
c5cf8c |
#include <stdlib.h>
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
#define SIZE (65536)
|
|
Packit Service |
c5cf8c |
#define NR_NBOPS (32)
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* Uses asynchronous I/O. Each process writes to separate files and
|
|
Packit Service |
c5cf8c |
reads them back. The file name is taken as a command-line argument,
|
|
Packit Service |
c5cf8c |
and the process rank is appended to it.*/
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
void handle_error(int errcode, const char *str);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
void handle_error(int errcode, const char *str)
|
|
Packit Service |
c5cf8c |
{
|
|
Packit Service |
c5cf8c |
char msg[MPI_MAX_ERROR_STRING];
|
|
Packit Service |
c5cf8c |
int resultlen;
|
|
Packit Service |
c5cf8c |
MPI_Error_string(errcode, msg, &resultlen);
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "%s: %s\n", str, msg);
|
|
Packit Service |
c5cf8c |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
int main(int argc, char **argv)
|
|
Packit Service |
c5cf8c |
{
|
|
Packit Service |
c5cf8c |
int *buf, i, rank, nints, len;
|
|
Packit Service |
c5cf8c |
char *filename, *tmp;
|
|
Packit Service |
c5cf8c |
int errs = 0, toterrs;
|
|
Packit Service |
c5cf8c |
MPI_File fh;
|
|
Packit Service |
c5cf8c |
MPI_Status status[NR_NBOPS];
|
|
Packit Service |
c5cf8c |
MPI_Request request[NR_NBOPS];
|
|
Packit Service |
c5cf8c |
int errcode = 0;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Init(&argc, &argv);
|
|
Packit Service |
c5cf8c |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* process 0 takes the file name as a command-line argument and
|
|
Packit Service |
c5cf8c |
broadcasts it to other processes */
|
|
Packit Service |
c5cf8c |
if (!rank) {
|
|
Packit Service |
c5cf8c |
i = 1;
|
|
Packit Service |
c5cf8c |
while ((i < argc) && strcmp("-fname", *argv)) {
|
|
Packit Service |
c5cf8c |
i++;
|
|
Packit Service |
c5cf8c |
argv++;
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
if (i >= argc) {
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "\n*# Usage: async -fname filename\n\n");
|
|
Packit Service |
c5cf8c |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
argv++;
|
|
Packit Service |
c5cf8c |
len = strlen(*argv);
|
|
Packit Service |
c5cf8c |
filename = (char *) malloc(len + 10);
|
|
Packit Service |
c5cf8c |
strcpy(filename, *argv);
|
|
Packit Service |
c5cf8c |
MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
} else {
|
|
Packit Service |
c5cf8c |
MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
filename = (char *) malloc(len + 10);
|
|
Packit Service |
c5cf8c |
MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
buf = (int *) malloc(SIZE);
|
|
Packit Service |
c5cf8c |
nints = SIZE / sizeof(int);
|
|
Packit Service |
c5cf8c |
for (i = 0; i < nints; i++)
|
|
Packit Service |
c5cf8c |
buf[i] = rank * 100000 + i;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* each process opens a separate file called filename.'myrank' */
|
|
Packit Service |
c5cf8c |
tmp = (char *) malloc(len + 10);
|
|
Packit Service |
c5cf8c |
strcpy(tmp, filename);
|
|
Packit Service |
c5cf8c |
sprintf(filename, "%s.%d", tmp, rank);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_open(MPI_COMM_SELF, filename,
|
|
Packit Service |
c5cf8c |
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh;;
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_open");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_set_view");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
for (i = 0; i < NR_NBOPS; i++) {
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_iwrite_at(fh, nints / NR_NBOPS * i,
|
|
Packit Service |
c5cf8c |
buf + (nints / NR_NBOPS * i), nints / NR_NBOPS, MPI_INT,
|
|
Packit Service |
c5cf8c |
&(request[i]));
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_iwrite");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
MPI_Waitall(NR_NBOPS, request, status);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_File_close(&fh;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* reopen the file and read the data back */
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
for (i = 0; i < nints; i++)
|
|
Packit Service |
c5cf8c |
buf[i] = 0;
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_open(MPI_COMM_SELF, filename,
|
|
Packit Service |
c5cf8c |
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh;;
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_open");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_set_view");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
for (i = 0; i < NR_NBOPS; i++) {
|
|
Packit Service |
c5cf8c |
errcode = MPI_File_iread_at(fh, nints / NR_NBOPS * i,
|
|
Packit Service |
c5cf8c |
buf + (nints / NR_NBOPS * i), nints / NR_NBOPS, MPI_INT,
|
|
Packit Service |
c5cf8c |
&(request[i]));
|
|
Packit Service |
c5cf8c |
if (errcode != MPI_SUCCESS) {
|
|
Packit Service |
c5cf8c |
handle_error(errcode, "MPI_File_open");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
MPI_Waitall(NR_NBOPS, request, status);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_File_close(&fh;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* check if the data read is correct */
|
|
Packit Service |
c5cf8c |
for (i = 0; i < nints; i++) {
|
|
Packit Service |
c5cf8c |
if (buf[i] != (rank * 100000 + i)) {
|
|
Packit Service |
c5cf8c |
errs++;
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],
|
|
Packit Service |
c5cf8c |
rank * 100000 + i);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Allreduce(&errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
if (rank == 0) {
|
|
Packit Service |
c5cf8c |
if (toterrs > 0) {
|
|
Packit Service |
c5cf8c |
fprintf(stderr, "Found %d errors\n", toterrs);
|
|
Packit Service |
c5cf8c |
} else {
|
|
Packit Service |
c5cf8c |
fprintf(stdout, " No Errors\n");
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
free(buf);
|
|
Packit Service |
c5cf8c |
free(filename);
|
|
Packit Service |
c5cf8c |
free(tmp);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Finalize();
|
|
Packit Service |
c5cf8c |
return 0;
|
|
Packit Service |
c5cf8c |
}
|