root/ompi/mca/io/romio321/romio/test/simple.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 static void handle_error(int errcode, const char *str)
  14 {
  15         char msg[MPI_MAX_ERROR_STRING];
  16         int resultlen;
  17         MPI_Error_string(errcode, msg, &resultlen);
  18         fprintf(stderr, "%s: %s\n", str, msg);
  19         MPI_Abort(MPI_COMM_WORLD, 1);
  20 }
  21 /* Each process writes to separate files and reads them back. 
  22    The file name is taken as a command-line argument, and the process rank 
  23    is appended to it. */ 
  24 
  25 int main(int argc, char **argv)
  26 {
  27     int *buf, i, rank, nints, len;
  28     char *filename, *tmp;
  29     int  errs = 0, toterrs, errcode;
  30     MPI_File fh;
  31     MPI_Status status;
  32 
  33     MPI_Init(&argc,&argv);
  34     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  35 
  36 /* process 0 takes the file name as a command-line argument and 
  37    broadcasts it to other processes */
  38     if (!rank) {
  39         i = 1;
  40         while ((i < argc) && strcmp("-fname", *argv)) {
  41             i++;
  42             argv++;
  43         }
  44         if (i >= argc) {
  45             fprintf(stderr, "\n*#  Usage: simple -fname filename\n\n");
  46             MPI_Abort(MPI_COMM_WORLD, 1);
  47         }
  48         argv++;
  49         len = strlen(*argv);
  50         filename = (char *) malloc(len+10);
  51         strcpy(filename, *argv);
  52         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  53         MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
  54     }
  55     else {
  56         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  57         filename = (char *) malloc(len+10);
  58         MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
  59     }
  60     
  61 
  62     buf = (int *) malloc(SIZE);
  63     nints = SIZE/sizeof(int);
  64     for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
  65 
  66     /* each process opens a separate file called filename.'myrank' */
  67     tmp = (char *) malloc(len+10);
  68     strcpy(tmp, filename);
  69     sprintf(filename, "%s.%d", tmp, rank);
  70 
  71     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
  72                     MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  73     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(1)");
  74 
  75     errcode = MPI_File_write(fh, buf, nints, MPI_INT, &status);
  76     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_write");
  77 
  78     errcode = MPI_File_close(&fh);
  79     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_clode (1)");
  80 
  81     /* reopen the file and read the data back */
  82 
  83     for (i=0; i<nints; i++) buf[i] = 0;
  84     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
  85                     MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  86     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(2)");
  87 
  88     errcode = MPI_File_read(fh, buf, nints, MPI_INT, &status);
  89     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read");
  90 
  91     errcode = MPI_File_close(&fh);
  92     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close(2)");
  93 
  94     /* check if the data read is correct */
  95     for (i=0; i<nints; i++) {
  96         if (buf[i] != (rank*100000 + i)) {
  97             errs++;
  98             fprintf(stderr, "Process %d: error, read %d, should be %d\n", 
  99                     rank, buf[i], rank*100000+i);
 100         }
 101     }
 102 
 103     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 104     if (rank == 0) {
 105         if( toterrs > 0) {
 106             fprintf( stderr, "Found %d errors\n", toterrs );
 107         }
 108         else {
 109             fprintf( stdout, " No Errors\n" );
 110         }
 111     }
 112 
 113     free(buf);
 114     free(filename);
 115     free(tmp);
 116 
 117     MPI_Finalize();
 118     return 0; 
 119 }

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