root/ompi/mca/io/romio321/romio/test/atomicity.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 whether atomicity semantics are satisfied for overlapping accesses
  12    in atomic mode. The probability of detecting errors is higher if you run 
  13    it on 8 or more processes. */
  14 
  15 /* The file name is taken as a command-line argument. */
  16 
  17 #define BUFSIZE 10000    /* no. of integers */
  18 #define VERBOSE 0
  19 int main(int argc, char **argv)
  20 {
  21     int *writebuf, *readbuf, i, mynod, nprocs, len, err;
  22     char *filename;
  23     int errs=0, toterrs;
  24     MPI_Datatype newtype;
  25     MPI_File fh;
  26     MPI_Status status;
  27     MPI_Info info;
  28 
  29     MPI_Init(&argc,&argv);
  30     MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
  31     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  32 
  33 /* process 0 takes the file name as a command-line argument and 
  34    broadcasts it to other processes */
  35     if (!mynod) {
  36         i = 1;
  37         while ((i < argc) && strcmp("-fname", *argv)) {
  38             i++;
  39             argv++;
  40         }
  41         if (i >= argc) {
  42             fprintf(stderr, "\n*#  Usage: coll_test -fname filename\n\n");
  43             MPI_Abort(MPI_COMM_WORLD, 1);
  44         }
  45         argv++;
  46         len = strlen(*argv);
  47         filename = (char *) malloc(len+1);
  48         strcpy(filename, *argv);
  49         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  50         MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  51     }
  52     else {
  53         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  54         filename = (char *) malloc(len+1);
  55         MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  56     }
  57 
  58     writebuf = (int *) malloc(BUFSIZE*sizeof(int));
  59     readbuf = (int *) malloc(BUFSIZE*sizeof(int));
  60 
  61 /* test atomicity of contiguous accesses */
  62 
  63 /* initialize file to all zeros */
  64     if (!mynod) {
  65         MPI_File_delete(filename, MPI_INFO_NULL);
  66         MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | 
  67              MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  68         for (i=0; i<BUFSIZE; i++) writebuf[i] = 0;
  69         MPI_File_write(fh, writebuf, BUFSIZE, MPI_INT, &status);
  70         MPI_File_close(&fh);
  71 #if VERBOSE
  72         fprintf(stderr, "\ntesting contiguous accesses\n");
  73 #endif
  74     }
  75     MPI_Barrier(MPI_COMM_WORLD);
  76 
  77     for (i=0; i<BUFSIZE; i++) writebuf[i] = 10;
  78     for (i=0; i<BUFSIZE; i++) readbuf[i] = 20;
  79 
  80     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | 
  81              MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
  82 
  83 /* set atomicity to true */
  84     err = MPI_File_set_atomicity(fh, 1);
  85     if (err != MPI_SUCCESS) {
  86         fprintf(stderr, "Atomic mode not supported on this file system.\n");fflush(stderr);
  87         MPI_Abort(MPI_COMM_WORLD, 1);
  88     }
  89 
  90     MPI_Barrier(MPI_COMM_WORLD);
  91     
  92 /* process 0 writes and others concurrently read. In atomic mode, 
  93    the data read must be either all old values or all new values; nothing
  94    in between. */ 
  95 
  96     if (!mynod) MPI_File_write(fh, writebuf, BUFSIZE, MPI_INT, &status);
  97     else {
  98         err = MPI_File_read(fh, readbuf, BUFSIZE, MPI_INT, &status);
  99         if (err == MPI_SUCCESS) {
 100             if (readbuf[0] == 0) { /* the rest must also be 0 */
 101                 for (i=1; i<BUFSIZE; i++) 
 102                     if (readbuf[i] != 0) {
 103                         errs++;
 104                         fprintf(stderr, "Process %d: readbuf[%d] is %d, should be 0\n", mynod, i, readbuf[i]);
 105                         MPI_Abort(MPI_COMM_WORLD, 1);
 106                     }
 107             }
 108             else if (readbuf[0] == 10) { /* the rest must also be 10 */
 109                 for (i=1; i<BUFSIZE; i++) 
 110                     if (readbuf[i] != 10) {
 111                         errs++;
 112                         fprintf(stderr, "Process %d: readbuf[%d] is %d, should be 10\n", mynod, i, readbuf[i]);
 113                         MPI_Abort(MPI_COMM_WORLD, 1);
 114                     }
 115             }
 116             else {
 117                 errs++;
 118                 fprintf(stderr, "Process %d: readbuf[0] is %d, should be either 0 or 10\n", mynod, readbuf[0]);         
 119             }    
 120         }
 121     }
 122 
 123     MPI_File_close(&fh);
 124         
 125     MPI_Barrier(MPI_COMM_WORLD);
 126 
 127 
 128 /* repeat the same test with a noncontiguous filetype */
 129 
 130     MPI_Type_vector(BUFSIZE, 1, 2, MPI_INT, &newtype);
 131     MPI_Type_commit(&newtype);
 132 
 133     MPI_Info_create(&info);
 134     /* I am setting these info values for testing purposes only. It is
 135        better to use the default values in practice. */
 136     MPI_Info_set(info, "ind_rd_buffer_size", "1209");
 137     MPI_Info_set(info, "ind_wr_buffer_size", "1107");
 138     
 139     if (!mynod) {
 140         MPI_File_delete(filename, MPI_INFO_NULL);
 141         MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | 
 142              MPI_MODE_RDWR, info, &fh);
 143         for (i=0; i<BUFSIZE; i++) writebuf[i] = 0;
 144         MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
 145         MPI_File_write(fh, writebuf, BUFSIZE, MPI_INT, &status);
 146         MPI_File_close(&fh);
 147 #if VERBOSE
 148         fprintf(stderr, "\ntesting noncontiguous accesses\n");
 149 #endif
 150     }
 151     MPI_Barrier(MPI_COMM_WORLD);
 152 
 153     for (i=0; i<BUFSIZE; i++) writebuf[i] = 10;
 154     for (i=0; i<BUFSIZE; i++) readbuf[i] = 20;
 155 
 156     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | 
 157              MPI_MODE_RDWR, info, &fh);
 158     MPI_File_set_atomicity(fh, 1);
 159     MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
 160     MPI_Barrier(MPI_COMM_WORLD);
 161     
 162     if (!mynod) MPI_File_write(fh, writebuf, BUFSIZE, MPI_INT, &status);
 163     else {
 164         err = MPI_File_read(fh, readbuf, BUFSIZE, MPI_INT, &status);
 165         if (err == MPI_SUCCESS) {
 166             if (readbuf[0] == 0) {
 167                 for (i=1; i<BUFSIZE; i++) 
 168                     if (readbuf[i] != 0) {
 169                         errs++;
 170                         fprintf(stderr, "Process %d: readbuf[%d] is %d, should be 0\n", mynod, i, readbuf[i]);
 171                         MPI_Abort(MPI_COMM_WORLD, 1);
 172                     }
 173             }
 174             else if (readbuf[0] == 10) {
 175                 for (i=1; i<BUFSIZE; i++) 
 176                     if (readbuf[i] != 10) {
 177                         errs++;
 178                         fprintf(stderr, "Process %d: readbuf[%d] is %d, should be 10\n", mynod, i, readbuf[i]);
 179                         MPI_Abort(MPI_COMM_WORLD, 1);
 180                     }
 181             }
 182             else {
 183                 errs++;
 184                 fprintf(stderr, "Process %d: readbuf[0] is %d, should be either 0 or 10\n", mynod, readbuf[0]);             
 185             }
 186         }
 187     }
 188 
 189     MPI_File_close(&fh);
 190         
 191     MPI_Barrier(MPI_COMM_WORLD);
 192 
 193     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
 194     if (mynod == 0) {
 195         if( toterrs > 0) {
 196             fprintf( stderr, "Found %d errors\n", toterrs );
 197         }
 198         else {
 199             fprintf( stdout, " No Errors\n" );
 200         }
 201     }
 202     MPI_Type_free(&newtype);
 203     MPI_Info_free(&info);
 204     free(writebuf);
 205     free(readbuf);
 206     free(filename);
 207 
 208     MPI_Finalize();
 209     return 0;
 210 }

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