root/ompi/mca/io/romio321/romio/test/noncontig_coll.c

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

DEFINITIONS

This source file includes following definitions.
  1. 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 /* tests noncontiguous reads/writes using collective I/O */
  12 
  13 #define SIZE 5000
  14 
  15 #define VERBOSE 0
  16 int main(int argc, char **argv)
  17 {
  18     int *buf, i, mynod, nprocs, len, b[3];
  19     int errs=0, toterrs;
  20     MPI_Aint d[3];
  21     MPI_File fh;
  22     MPI_Status status;
  23     char *filename;
  24     MPI_Datatype typevec, newtype, t[3];
  25 
  26     MPI_Init(&argc,&argv);
  27     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  28     MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
  29 
  30     if (nprocs != 2) {
  31         fprintf(stderr, "Run this program on two processes\n");
  32         MPI_Abort(MPI_COMM_WORLD, 1);
  33     }
  34 
  35 /* process 0 takes the file name as a command-line argument and 
  36    broadcasts it to other processes */
  37     if (!mynod) {
  38         i = 1;
  39         while ((i < argc) && strcmp("-fname", *argv)) {
  40             i++;
  41             argv++;
  42         }
  43         if (i >= argc) {
  44             fprintf(stderr, "\n*#  Usage: noncontig_coll -fname filename\n\n");
  45             MPI_Abort(MPI_COMM_WORLD, 1);
  46         }
  47         argv++;
  48         len = strlen(*argv);
  49         filename = (char *) malloc(len+1);
  50         strcpy(filename, *argv);
  51         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  52         MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  53     }
  54     else {
  55         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  56         filename = (char *) malloc(len+1);
  57         MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  58     }
  59 
  60     buf = (int *) malloc(SIZE*sizeof(int));
  61 
  62     MPI_Type_vector(SIZE/2, 1, 2, MPI_INT, &typevec);
  63 
  64     b[0] = b[1] = b[2] = 1;
  65     d[0] = 0;
  66     d[1] = mynod*sizeof(int);
  67     d[2] = SIZE*sizeof(int);
  68     t[0] = MPI_LB;
  69     t[1] = typevec;
  70     t[2] = MPI_UB;
  71 
  72     MPI_Type_struct(3, b, d, t, &newtype);
  73     MPI_Type_commit(&newtype);
  74     MPI_Type_free(&typevec);
  75 
  76     if (!mynod) {
  77 #if VERBOSE
  78         fprintf(stderr, "\ntesting noncontiguous in memory, noncontiguous in file using collective I/O\n");
  79 #endif
  80         MPI_File_delete(filename, MPI_INFO_NULL);
  81     }
  82     MPI_Barrier(MPI_COMM_WORLD);
  83 
  84     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
  85                   MPI_INFO_NULL, &fh);
  86 
  87     MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
  88 
  89     for (i=0; i<SIZE; i++) buf[i] = i + mynod*SIZE;
  90     MPI_File_write_all(fh, buf, 1, newtype, &status);
  91 
  92     MPI_Barrier(MPI_COMM_WORLD);
  93 
  94     for (i=0; i<SIZE; i++) buf[i] = -1;
  95 
  96     MPI_File_read_at_all(fh, 0, buf, 1, newtype, &status);
  97 
  98     for (i=0; i<SIZE; i++) {
  99         if (!mynod) {
 100             if ((i%2) && (buf[i] != -1)) {
 101                 errs++;
 102                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", 
 103                         mynod, i, buf[i]);
 104             }
 105             if (!(i%2) && (buf[i] != i)) {
 106                 errs++;
 107                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 108                         mynod, i, buf[i], i);
 109             }
 110         }
 111         else {
 112             if ((i%2) && (buf[i] != i + mynod*SIZE)) {
 113                 errs++;
 114                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 115                         mynod, i, buf[i], i + mynod*SIZE);
 116             }
 117             if (!(i%2) && (buf[i] != -1)) {
 118                 errs++;
 119                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", 
 120                         mynod, i, buf[i]);
 121             }
 122         }
 123     }
 124 
 125     MPI_File_close(&fh);
 126 
 127     MPI_Barrier(MPI_COMM_WORLD);
 128 
 129     if (!mynod) {
 130 #if VERBOSE
 131         fprintf(stderr, "\ntesting noncontiguous in memory, contiguous in file using collective I/O\n");
 132 #endif
 133         MPI_File_delete(filename, MPI_INFO_NULL);
 134     }
 135     MPI_Barrier(MPI_COMM_WORLD);
 136 
 137     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
 138                   MPI_INFO_NULL, &fh);
 139 
 140     for (i=0; i<SIZE; i++) buf[i] = i + mynod*SIZE;
 141     MPI_File_write_at_all(fh, mynod*(SIZE/2)*sizeof(int), buf, 1, newtype, &status);
 142 
 143     MPI_Barrier(MPI_COMM_WORLD);
 144 
 145     for (i=0; i<SIZE; i++) buf[i] = -1;
 146 
 147     MPI_File_read_at_all(fh, mynod*(SIZE/2)*sizeof(int), buf, 1, newtype, &status);
 148 
 149     for (i=0; i<SIZE; i++) {
 150         if (!mynod) {
 151             if ((i%2) && (buf[i] != -1)) {
 152                 errs++;
 153                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", 
 154                         mynod, i, buf[i]);
 155             }
 156             if (!(i%2) && (buf[i] != i)) {
 157                 errs++;
 158                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 159                         mynod, i, buf[i], i);
 160             }
 161         }
 162         else {
 163             if ((i%2) && (buf[i] != i + mynod*SIZE)) {
 164                 errs++;
 165                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 166                         mynod, i, buf[i], i + mynod*SIZE);
 167             }
 168             if (!(i%2) && (buf[i] != -1)) {
 169                 errs++;
 170                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", 
 171                         mynod, i, buf[i]);
 172             }
 173         }
 174     }
 175 
 176     MPI_File_close(&fh);
 177 
 178     MPI_Barrier(MPI_COMM_WORLD);
 179 
 180     if (!mynod) {
 181 #if VERBOSE
 182         fprintf(stderr, "\ntesting contiguous in memory, noncontiguous in file using collective I/O\n");
 183 #endif
 184         MPI_File_delete(filename, MPI_INFO_NULL);
 185     }
 186     MPI_Barrier(MPI_COMM_WORLD);
 187 
 188     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
 189                   MPI_INFO_NULL, &fh);
 190 
 191     MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
 192 
 193     for (i=0; i<SIZE; i++) buf[i] = i + mynod*SIZE;
 194     MPI_File_write_all(fh, buf, SIZE, MPI_INT, &status);
 195 
 196     MPI_Barrier(MPI_COMM_WORLD);
 197 
 198     for (i=0; i<SIZE; i++) buf[i] = -1;
 199 
 200     MPI_File_read_at_all(fh, 0, buf, SIZE, MPI_INT, &status);
 201 
 202     for (i=0; i<SIZE; i++) {
 203         if (!mynod) {
 204             if (buf[i] != i) {
 205                 errs++;
 206                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 207                         mynod, i, buf[i], i);
 208             }
 209         }
 210         else {
 211             if (buf[i] != i + mynod*SIZE) {
 212                 errs++;
 213                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", 
 214                         mynod, i, buf[i], i + mynod*SIZE);
 215             }
 216         }
 217     }
 218 
 219     MPI_File_close(&fh);
 220 
 221     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 222     if (mynod == 0) {
 223         if( toterrs > 0) {
 224             fprintf( stderr, "Found %d errors\n", toterrs );
 225         }
 226         else {
 227             fprintf( stdout, " No Errors\n" );
 228         }
 229     }
 230 
 231     MPI_Type_free(&newtype);
 232     free(buf);
 233     free(filename);
 234     MPI_Finalize();
 235     return 0;
 236 }

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