root/ompi/mca/io/romio321/romio/test/darray_read.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) 2014 by Argonne National Laboratory.
   4  *      See COPYRIGHT in top-level directory.
   5  */
   6 
   7 #include <stdio.h>
   8 #include <stdlib.h>
   9 #include <mpi.h>
  10 
  11 #define NSIDE 5
  12 #define NBLOCK 3
  13 #define NPROC 2
  14 
  15 #define CHECK(fn) {int errcode; errcode = (fn); if (errcode != MPI_SUCCESS) handle_error(errcode, #fn);}
  16 
  17 static void handle_error(int errcode, const char *str)
  18 {
  19         char msg[MPI_MAX_ERROR_STRING];
  20         int resultlen;
  21         MPI_Error_string(errcode, msg, &resultlen);
  22         fprintf(stderr, "%s: %s\n", str, msg);
  23         MPI_Abort(MPI_COMM_WORLD, 1);
  24 }
  25 
  26 
  27 int main(int argc, char *argv[]) 
  28 { 
  29   int i, j, nerrors=0, total_errors=0; 
  30 
  31   int rank, size;
  32   int bpos;
  33 
  34   MPI_Datatype darray;
  35   MPI_Status status;
  36   MPI_File mpi_fh;
  37 
  38   /* Define array distribution
  39       A 2x2 block size works with ROMIO, a 3x3 block size breaks it. */
  40   int distrib[2] = { MPI_DISTRIBUTE_CYCLIC, MPI_DISTRIBUTE_CYCLIC };
  41   int bsize[2] = { NBLOCK, NBLOCK };
  42   int gsize[2] = { NSIDE, NSIDE };
  43   int psize[2] = { NPROC, NPROC };
  44 
  45   double data[NSIDE*NSIDE];
  46   double *ldata, *pdata;
  47 
  48   int tsize, nelem;
  49 
  50   MPI_File dfile;
  51  
  52   MPI_Init(&argc, &argv);
  53 
  54   MPI_Comm_size(MPI_COMM_WORLD, &size);
  55   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  56 
  57   /* Set up type */
  58   CHECK(MPI_Type_create_darray(size, rank, 2, gsize, distrib,
  59                          bsize, psize, MPI_ORDER_FORTRAN, MPI_DOUBLE, &darray));
  60   CHECK(MPI_Type_commit(&darray));
  61   CHECK(MPI_Type_size(darray, &tsize));
  62   nelem = tsize / sizeof(double);
  63 
  64   for(i = 0; i < (NSIDE*NSIDE); i++) data[i] = i;
  65 
  66   if (rank == 0) {
  67     CHECK(MPI_File_open(MPI_COMM_SELF, argv[1],
  68                 MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &dfile));
  69     CHECK(MPI_File_write(dfile, data, NSIDE*NSIDE, MPI_DOUBLE, &status));
  70     CHECK(MPI_File_close(&dfile));
  71   }
  72   MPI_Barrier(MPI_COMM_WORLD);
  73 
  74   /* Allocate buffer */
  75   ldata = (double *)malloc(tsize);
  76   pdata = (double *)malloc(tsize);
  77 
  78   /* Use Pack to pull out array */
  79   bpos = 0;
  80   CHECK(MPI_Pack(data, 1, darray, pdata, tsize, &bpos, MPI_COMM_WORLD));
  81 
  82   MPI_Barrier(MPI_COMM_WORLD);
  83 
  84   /* Read in array from file.  */
  85   CHECK(MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &mpi_fh));
  86   CHECK(MPI_File_set_view(mpi_fh, 0, MPI_DOUBLE, darray, "native", MPI_INFO_NULL));
  87   CHECK(MPI_File_read_all(mpi_fh, ldata, nelem, MPI_DOUBLE, &status));
  88   CHECK(MPI_File_close(&mpi_fh));
  89 
  90   for(i = 0; i < size; i++) {
  91 #ifdef VERBOSE
  92     MPI_Barrier(MPI_COMM_WORLD);
  93     if(rank == i) {
  94       printf("=== Rank %i === (%i elements) \nPacked: ", rank, nelem);
  95       for(j = 0; j < nelem; j++) {
  96         printf("%4.1f ", pdata[j]);
  97         fflush(stdout);
  98       }
  99       printf("\nRead:   ");
 100       for(j = 0; j < nelem; j++) {
 101         printf("%4.1f ", ldata[j]);
 102         fflush(stdout);
 103       }
 104       printf("\n\n");
 105       fflush(stdout);
 106     }
 107 #endif
 108     if(rank == i) {
 109         for (j=0; j< nelem; j++) {
 110             if (pdata[j] != ldata[j]) {
 111                 fprintf(stderr, "rank %d at index %d: packbuf %4.1f filebuf %4.1f\n",
 112                         rank, j, pdata[j], ldata[j]);
 113                 nerrors++;
 114             }
 115         }
 116     }
 117   }
 118   MPI_Allreduce(&nerrors, &total_errors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
 119   if (rank == 0 && total_errors == 0)
 120       printf(" No Errors\n");
 121 
 122   free(ldata);
 123   free(pdata);
 124   MPI_Type_free(&darray);
 125   MPI_Finalize();
 126 
 127   exit(total_errors);
 128 
 129 } 

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