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

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

DEFINITIONS

This source file includes following definitions.
  1. handle_error
  2. parse_args
  3. 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  * the new defered open code made some changes to the way we manage CREAT|EXCL,
  12  * so test out that code path */
  13 
  14 struct options {
  15         char *fname;
  16         int verbose;
  17         int do_aggregation;
  18 };
  19 typedef struct options options;
  20 
  21 
  22 void handle_error(int errcode, char *str) 
  23 {
  24         char msg[MPI_MAX_ERROR_STRING];
  25         int resultlen;
  26         MPI_Error_string(errcode, msg, &resultlen);
  27         fprintf(stderr, "%s: %s\n", str, msg);
  28         MPI_Abort(MPI_COMM_WORLD, 1);
  29 }
  30 
  31 void parse_args(int argc, char ** argv, int rank, options *opts) 
  32 {
  33         int i, len=0;
  34         if (rank == 0) {
  35                 i = 1;
  36                 while (i < argc) {
  37                         if (strcmp("-fname", argv[i]) == 0) {
  38                                 len = strlen(argv[i+1]);
  39                                 opts->fname = (char *) malloc(len + 1);
  40                                 strcpy(opts->fname, argv[i+1]);
  41                                 i+=2;/* option and argument */
  42                                 continue;
  43                         }
  44                         if (strcmp("-aggregate", argv[i]) == 0) {
  45                                 opts->do_aggregation = 1;
  46                                 i++;
  47                                 continue;
  48                         }
  49                         if (strcmp("-verbose", argv[i]) == 0) {
  50                                 opts->verbose = 1;
  51                                 i++;
  52                                 continue;
  53                         }
  54                 }
  55                 if (opts->fname == NULL) { /* didn't get a filename */
  56                         fprintf(stderr, "Usage: %s -fname filename [-aggregate] [-verbose]\n", argv[0]);
  57                         MPI_Abort(MPI_COMM_WORLD, 1);
  58                 }
  59                 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  60                 MPI_Bcast(opts->fname, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  61                 MPI_Bcast(&(opts->do_aggregation), 1, MPI_INT, 0, MPI_COMM_WORLD);
  62                 MPI_Bcast(&(opts->verbose), 1, MPI_INT, 0, MPI_COMM_WORLD);
  63         } else {
  64                 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
  65                 opts->fname = (char *) malloc(len + 1);
  66                 MPI_Bcast(opts->fname, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
  67                 MPI_Bcast(&(opts->do_aggregation), 1, MPI_INT, 0, MPI_COMM_WORLD);
  68                 MPI_Bcast(&(opts->verbose), 1, MPI_INT, 0, MPI_COMM_WORLD);
  69         }
  70 
  71 }
  72 
  73 int main(int argc, char ** argv) {
  74         int nprocs, mynod, errcode;
  75         options my_options = {NULL, 0, 0};
  76         MPI_File fh;
  77         MPI_Status status;
  78         MPI_Info  info;
  79 
  80         MPI_Init(&argc, &argv);
  81         MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  82         MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
  83 
  84         parse_args(argc, argv, mynod, &my_options);
  85 
  86         if (my_options.do_aggregation) {
  87                 MPI_Info_create(&info);
  88                 MPI_Info_set(info, "romio_no_indep_rw", "true");
  89                 MPI_Info_set(info, "cb_config_list", "leela.mcs.anl.gov:1");
  90         } else {
  91                 info = MPI_INFO_NULL;
  92         }
  93 
  94         /* create the file w/o EXCL: this must not fail */
  95         errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
  96                         MPI_MODE_CREATE|MPI_MODE_RDWR, info, &fh);
  97         if (errcode != MPI_SUCCESS) {
  98                 handle_error(errcode, "MPI_File_open");
  99         }
 100 
 101         errcode = MPI_File_close(&fh);
 102         if (errcode != MPI_SUCCESS) {
 103                 handle_error(errcode, "MPI_File_close");
 104         }
 105 
 106         /* now try to open w/ CREAT|EXCL: this must fail */
 107         errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
 108                         MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_RDWR, info, &fh);
 109         if (errcode == MPI_SUCCESS) {
 110                 handle_error(errcode, "MPI_File_open: expected an error: got");
 111         }
 112 
 113         /* ignore the error: File_delete is not aggregator-aware */
 114         MPI_File_delete(my_options.fname, info);
 115 
 116         /* this must succeed: the file no longer exists */
 117         errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
 118                         MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_RDWR, info, &fh);
 119         if (errcode != MPI_SUCCESS) {
 120                 handle_error(errcode, "MPI_File_open");
 121         }
 122 
 123         errcode = MPI_File_close(&fh);
 124         if (errcode != MPI_SUCCESS) {
 125                 handle_error(errcode, "MPI_File_close");
 126         }
 127 
 128         if (mynod == 0) {
 129                 printf(" No Errors\n");
 130         }
 131 
 132         MPI_Finalize();
 133         return 0;
 134 }

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