00001 
00002 
00003 
00004 
00005 
00006 #include "mpi.h"
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 
00011 #define SIZE (65536)
00012 
00013 static void handle_error(int errcode, char *str)
00014 {
00015     char msg[MPI_MAX_ERROR_STRING];
00016     int resultlen;
00017     MPI_Error_string(errcode, msg, &resultlen);
00018     fprintf(stderr, "%s: %s\n", str, msg);
00019     MPI_Abort(MPI_COMM_WORLD, 1);
00020 }
00021 
00022 
00023  
00024 
00025 int main(int argc, char **argv)
00026 {
00027     int *buf, i, rank, nints, len;
00028     char *filename, *tmp;
00029     int  errs = 0, toterrs, errcode;
00030     MPI_File fh;
00031     MPI_Status status;
00032 
00033     MPI_Init(&argc,&argv);
00034     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00035 
00036 
00037 
00038     if (!rank) {
00039     i = 1;
00040     while ((i < argc) && strcmp("-fname", *argv)) {
00041         i++;
00042         argv++;
00043     }
00044     if (i >= argc) {
00045         fprintf(stderr, "\n*#  Usage: simple -fname filename\n\n");
00046         MPI_Abort(MPI_COMM_WORLD, 1);
00047     }
00048     argv++;
00049     len = strlen(*argv);
00050     filename = (char *) malloc(len+10);
00051     strcpy(filename, *argv);
00052     MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00053     MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00054     }
00055     else {
00056     MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00057     filename = (char *) malloc(len+10);
00058     MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00059     }
00060     
00061 
00062     buf = (int *) malloc(SIZE);
00063     nints = SIZE/sizeof(int);
00064     for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
00065 
00066     
00067     tmp = (char *) malloc(len+10);
00068     strcpy(tmp, filename);
00069     sprintf(filename, "%s.%d", tmp, rank);
00070 
00071     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
00072             MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00073     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(1)");
00074 
00075     errcode = MPI_File_write(fh, buf, nints, MPI_INT, &status);
00076     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_write");
00077 
00078     errcode = MPI_File_close(&fh);
00079     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_clode (1)");
00080 
00081     
00082 
00083     for (i=0; i<nints; i++) buf[i] = 0;
00084     errcode = MPI_File_open(MPI_COMM_SELF, filename, 
00085             MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00086     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(2)");
00087 
00088     errcode = MPI_File_read(fh, buf, nints, MPI_INT, &status);
00089     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read");
00090 
00091     errcode = MPI_File_close(&fh);
00092     if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close(2)");
00093 
00094     
00095     for (i=0; i<nints; i++) {
00096     if (buf[i] != (rank*100000 + i)) {
00097         errs++;
00098         fprintf(stderr, "Process %d: error, read %d, should be %d\n", 
00099             rank, buf[i], rank*100000+i);
00100     }
00101     }
00102 
00103     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00104     if (rank == 0) {
00105     if( toterrs > 0) {
00106         fprintf( stderr, "Found %d errors\n", toterrs );
00107     }
00108     else {
00109         fprintf( stdout, " No Errors\n" );
00110     }
00111     }
00112 
00113     free(buf);
00114     free(filename);
00115     free(tmp);
00116 
00117     MPI_Finalize();
00118     return 0; 
00119 }