00001 
00002 
00003 
00004 
00005 
00006 #include "mpi.h"
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 
00011 
00012 
00013 
00014 struct options {
00015     char *fname;
00016     int verbose;
00017     int do_aggregation;
00018 };
00019 typedef struct options options;
00020 
00021 
00022 void handle_error(int errcode, char *str) 
00023 {
00024     char msg[MPI_MAX_ERROR_STRING];
00025     int resultlen;
00026     MPI_Error_string(errcode, msg, &resultlen);
00027     fprintf(stderr, "%s: %s\n", str, msg);
00028     MPI_Abort(MPI_COMM_WORLD, 1);
00029 }
00030 
00031 void parse_args(int argc, char ** argv, int rank, options *opts) 
00032 {
00033     int i, len=0;
00034     if (rank == 0) {
00035         i = 1;
00036         while (i < argc) {
00037             if (strcmp("-fname", argv[i]) == 0) {
00038                 len = strlen(argv[i+1]);
00039                 opts->fname = (char *) malloc(len + 1);
00040                 strcpy(opts->fname, argv[i+1]);
00041                 i+=2;
00042                 continue;
00043             }
00044             if (strcmp("-aggregate", argv[i]) == 0) {
00045                 opts->do_aggregation = 1;
00046                 i++;
00047                 continue;
00048             }
00049             if (strcmp("-verbose", argv[i]) == 0) {
00050                 opts->verbose = 1;
00051                 i++;
00052                 continue;
00053             }
00054         }
00055         if (opts->fname == NULL) { 
00056             fprintf(stderr, "Usage: %s -fname filename [-aggregate] [-verbose]\n", argv[0]);
00057             MPI_Abort(MPI_COMM_WORLD, 1);
00058         }
00059         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00060         MPI_Bcast(opts->fname, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00061         MPI_Bcast(&(opts->do_aggregation), 1, MPI_INT, 0, MPI_COMM_WORLD);
00062         MPI_Bcast(&(opts->verbose), 1, MPI_INT, 0, MPI_COMM_WORLD);
00063     } else {
00064         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00065         opts->fname = (char *) malloc(len + 1);
00066         MPI_Bcast(opts->fname, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00067         MPI_Bcast(&(opts->do_aggregation), 1, MPI_INT, 0, MPI_COMM_WORLD);
00068         MPI_Bcast(&(opts->verbose), 1, MPI_INT, 0, MPI_COMM_WORLD);
00069     }
00070 
00071 }
00072 
00073 int main(int argc, char ** argv) {
00074     int nprocs, mynod, errcode;
00075     options my_options = {NULL, 0, 0};
00076     MPI_File fh;
00077     MPI_Status status;
00078     MPI_Info  info;
00079 
00080     MPI_Init(&argc, &argv);
00081     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00082     MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
00083 
00084     parse_args(argc, argv, mynod, &my_options);
00085 
00086     if (my_options.do_aggregation) {
00087         MPI_Info_create(&info);
00088         MPI_Info_set(info, "romio_no_indep_rw", "true");
00089         MPI_Info_set(info, "cb_config_list", "leela.mcs.anl.gov:1");
00090     } else {
00091         info = MPI_INFO_NULL;
00092     }
00093 
00094     
00095     errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
00096             MPI_MODE_CREATE|MPI_MODE_RDWR, info, &fh);
00097     if (errcode != MPI_SUCCESS) {
00098         handle_error(errcode, "MPI_File_open");
00099     }
00100 
00101     errcode = MPI_File_close(&fh);
00102     if (errcode != MPI_SUCCESS) {
00103         handle_error(errcode, "MPI_File_close");
00104     }
00105 
00106     
00107     errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
00108             MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_RDWR, info, &fh);
00109     if (errcode == MPI_SUCCESS) {
00110         handle_error(errcode, "MPI_File_open: expected an error: got");
00111     }
00112 
00113     
00114     MPI_File_delete(my_options.fname, info);
00115 
00116     
00117     errcode = MPI_File_open(MPI_COMM_WORLD, my_options.fname,
00118             MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_RDWR, info, &fh);
00119     if (errcode != MPI_SUCCESS) {
00120         handle_error(errcode, "MPI_File_open");
00121     }
00122 
00123     errcode = MPI_File_close(&fh);
00124     if (errcode != MPI_SUCCESS) {
00125         handle_error(errcode, "MPI_File_close");
00126     }
00127 
00128     if (mynod == 0) {
00129         printf(" No Errors\n");
00130     }
00131 
00132     MPI_Finalize();
00133     return 0;
00134 }