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 int main(int argc, char **argv)
00014 {
00015 MPI_File fh;
00016 int rank, len, err, i;
00017 int errs=0, toterrs;
00018 char *filename;
00019
00020 MPI_Init(&argc,&argv);
00021 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00022
00023
00024
00025 if (!rank) {
00026 i = 1;
00027 while ((i < argc) && strcmp("-fname", *argv)) {
00028 i++;
00029 argv++;
00030 }
00031 if (i >= argc) {
00032 fprintf(stderr, "\n*# Usage: excl -fname filename\n\n");
00033 MPI_Abort(MPI_COMM_WORLD, 1);
00034 }
00035 argv++;
00036 len = strlen(*argv);
00037 filename = (char *) malloc(len+10);
00038 strcpy(filename, *argv);
00039 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00040 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00041 }
00042 else {
00043 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00044 filename = (char *) malloc(len+10);
00045 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00046 }
00047
00048
00049 if (!rank) MPI_File_delete(filename, MPI_INFO_NULL);
00050 MPI_Barrier(MPI_COMM_WORLD);
00051
00052
00053 err = MPI_File_open(MPI_COMM_WORLD, filename,
00054 MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_RDWR, MPI_INFO_NULL , &fh);
00055 if (err != MPI_SUCCESS) {
00056 errs++;
00057 fprintf(stderr, "Process %d: open failed when it should have succeeded\n", rank);
00058 }
00059 else MPI_File_close(&fh);
00060
00061 MPI_Barrier(MPI_COMM_WORLD);
00062
00063
00064 err = MPI_File_open(MPI_COMM_WORLD, filename,
00065 MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_RDWR, MPI_INFO_NULL , &fh);
00066 if (err == MPI_SUCCESS) {
00067 errs++;
00068 fprintf(stderr, "Process %d: open succeeded when it should have failed\n", rank);
00069 }
00070
00071 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00072 if (rank == 0) {
00073 if( toterrs > 0) {
00074 fprintf( stderr, "Found %d errors\n", toterrs );
00075 }
00076 else {
00077 fprintf( stdout, " No Errors\n" );
00078 }
00079 }
00080
00081 free(filename);
00082 MPI_Finalize();
00083 return 0;
00084 }