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

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