root/ompi/mca/io/romio321/romio/test/async-multiple.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. handle_error
  2. main

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- 
   2  * vim: ts=8 sts=4 sw=4 noexpandtab
   3  *  
   4  *  (C) 2001 by Argonne National Laboratory.
   5  *      See COPYRIGHT in top-level directory.
   6  */
   7 #include "mpi.h"
   8 #include <stdio.h>
   9 #include <string.h>
  10 #include <stdlib.h>
  11 
  12 #define SIZE (65536)
  13 #define NR_NBOPS (32)
  14 
  15 /* Uses asynchronous I/O. Each process writes to separate files and
  16    reads them back. The file name is taken as a command-line argument,
  17    and the process rank is appended to it.*/ 
  18 
  19 void handle_error(int errcode, const char *str);
  20 
  21 void handle_error(int errcode, const char *str) 
  22 {
  23         char msg[MPI_MAX_ERROR_STRING];
  24         int resultlen;
  25         MPI_Error_string(errcode, msg, &resultlen);
  26         fprintf(stderr, "%s: %s\n", str, msg);
  27         MPI_Abort(MPI_COMM_WORLD, 1);
  28 }
  29 int main(int argc, char **argv)
  30 {
  31     int *buf, i, rank, nints, len;
  32     char *filename, *tmp;
  33     int errs=0, toterrs;
  34     MPI_File fh;
  35     MPI_Status status[NR_NBOPS];
  36     MPI_Request request[NR_NBOPS];
  37     int errcode = 0;
  38 
  39     MPI_Init(&argc,&argv);
  40     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  41 
  42 /* process 0 takes the file name as a command-line argument and 
  43    broadcasts it to other processes */
  44     if (!rank) {
  45         i = 1;
  46         while ((i < argc) && strcmp("-fname", *argv)) {
  47             i++;
  48             argv++;
  49         }
  50         if (i >= argc) {
  51             fprintf(stderr, "\n*#  Usage: async -fname filename\n\n");
  52             MPI_Abort(MPI_COMM_WORLD, 1);
  53         }
  54         argv++;
  55         len = strlen(*argv);
  56         filename = (char *) malloc(len+10);
  57         strcpy(filename, *argv);
  58         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  59         MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
  60     }
  61     else {
  62         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  63         filename = (char *) malloc(len+10);
  64         MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
  65     }
  66 
  67 
  68     buf = (int *) malloc(SIZE);
  69     nints = SIZE/sizeof(int);
  70     for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
  71 
  72     /* each process opens a separate file called filename.'myrank' */
  73     tmp = (char *) malloc(len+10);
  74     strcpy(tmp, filename);
  75     sprintf(filename, "%s.%d", tmp, rank);
  76 
  77     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
  78                     MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  79     if (errcode != MPI_SUCCESS) {
  80             handle_error(errcode, "MPI_File_open");
  81     }
  82     MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
  83     for (i=0; i<NR_NBOPS; i++) { 
  84         errcode = MPI_File_iwrite_at(fh, nints/NR_NBOPS*i, 
  85                 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
  86         if (errcode != MPI_SUCCESS) {
  87             handle_error(errcode, "MPI_File_iwrite");
  88         }
  89     }
  90     MPI_Waitall(NR_NBOPS, request, status);
  91 
  92     MPI_File_close(&fh);
  93 
  94     /* reopen the file and read the data back */
  95 
  96     for (i=0; i<nints; i++) buf[i] = 0;
  97     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
  98                     MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  99     if (errcode != MPI_SUCCESS) {
 100             handle_error(errcode, "MPI_File_open");
 101     }
 102 
 103     MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
 104     for (i=0; i<NR_NBOPS; i++) {
 105         errcode = MPI_File_iread_at(fh, nints/NR_NBOPS*i, 
 106                 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
 107         if (errcode != MPI_SUCCESS) {
 108             handle_error(errcode, "MPI_File_open");
 109         }
 110     }
 111     MPI_Waitall(NR_NBOPS, request, status);
 112 
 113     MPI_File_close(&fh);
 114 
 115     /* check if the data read is correct */
 116     for (i=0; i<nints; i++) {
 117         if (buf[i] != (rank*100000 + i)) {
 118             errs++;
 119             fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i], rank*100000+i);
 120         }
 121     }
 122 
 123     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 124     if (rank == 0) {
 125         if( toterrs > 0) {
 126             fprintf( stderr, "Found %d errors\n", toterrs );
 127         }
 128         else {
 129             fprintf( stdout, " No Errors\n" );
 130         }
 131     }
 132 
 133     free(buf);
 134     free(filename);
 135     free(tmp);
 136 
 137     MPI_Finalize();
 138     return 0; 
 139 }

/* [<][>][^][v][top][bottom][index][help] */