00001
00002
00003
00004
00005
00006 #include "mpi.h"
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010
00011 #define COUNT (200)
00012 #undef TIMING
00013
00014 void handle_error(int errcode, char *str);
00015
00016 void handle_error(int errcode, char *str)
00017 {
00018 char msg[MPI_MAX_ERROR_STRING];
00019 int resultlen;
00020 MPI_Error_string(errcode, msg, &resultlen);
00021 fprintf(stderr, "%s: %s\n", str, msg);
00022 MPI_Abort(MPI_COMM_WORLD, 1);
00023 }
00024
00025
00026
00027 int main(int argc, char **argv)
00028 {
00029 int *buf, i, rank, nprocs, len, sum;
00030 int global_sum;
00031 int errs=0, toterrs, errcode;
00032 char *filename;
00033 MPI_File fh;
00034 MPI_Status status;
00035
00036 MPI_Init(&argc,&argv);
00037 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00038
00039 double wr_stime, wr_etime, wr_time, wr_sumtime;
00040 double rd_stime, rd_etime, rd_time, rd_sumtime;
00041
00042
00043
00044 if (!rank) {
00045 i = 1;
00046 while ((i < argc) && strcmp("-fname", *argv)) {
00047 i++;
00048 argv++;
00049 }
00050 if (i >= argc) {
00051 fprintf(stderr, "\n*# Usage: shared_fp -fname filename\n\n");
00052 MPI_Abort(MPI_COMM_WORLD, 1);
00053 }
00054 argv++;
00055 len = strlen(*argv);
00056 filename = (char *) malloc(len+10);
00057 strcpy(filename, *argv);
00058 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00059 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00060 }
00061 else {
00062 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00063 filename = (char *) malloc(len+10);
00064 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00065 }
00066
00067 buf = (int *) malloc(COUNT * sizeof(int));
00068
00069 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00070 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00071
00072 for (i=0; i<COUNT; i++) buf[i] = COUNT*rank + i;
00073
00074 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
00075 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00076 if (errcode != MPI_SUCCESS) {
00077 handle_error(errcode, "MPI_File_open");
00078 }
00079
00080 wr_stime = MPI_Wtime();
00081
00082 errcode = MPI_File_write_ordered(fh, buf, COUNT, MPI_INT, &status);
00083 if (errcode != MPI_SUCCESS) {
00084 handle_error(errcode, "MPI_File_write_shared");
00085 }
00086 wr_etime = MPI_Wtime();
00087
00088 for (i=0; i<COUNT; i++) buf[i] = 0;
00089
00090 MPI_Barrier(MPI_COMM_WORLD);
00091
00092 rd_stime = MPI_Wtime();
00093 errcode = MPI_File_seek_shared(fh, 0, MPI_SEEK_SET);
00094 if (errcode != MPI_SUCCESS) {
00095 handle_error(errcode, "MPI_File_seek_shared");
00096 }
00097
00098 errcode = MPI_File_read_ordered(fh, buf, COUNT, MPI_INT, &status);
00099 if (errcode != MPI_SUCCESS) {
00100 handle_error(errcode, "MPI_File_read_shared");
00101 }
00102
00103 rd_etime = MPI_Wtime();
00104 MPI_File_close(&fh);
00105
00106 sum = 0;
00107 for (i=0; i<COUNT; i++) sum += buf[i];
00108
00109 MPI_Allreduce(&sum, &global_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
00110
00111 wr_time = wr_etime - wr_stime;
00112 rd_time = rd_etime - rd_stime;
00113
00114 MPI_Allreduce(&wr_time, &wr_sumtime, 1,
00115 MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
00116 MPI_Allreduce(&rd_time, &rd_sumtime, 1,
00117 MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
00118
00119 if (global_sum != (((COUNT*nprocs - 1)*(COUNT*nprocs))/2)) {
00120 errs++;
00121 fprintf(stderr, "Error: sum %d, global_sum %d, %d\n",
00122 sum, global_sum,(((COUNT*nprocs - 1)*(COUNT*nprocs))/2));
00123 }
00124
00125 free(buf);
00126 free(filename);
00127
00128 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00129 if (rank == 0) {
00130 if( toterrs > 0) {
00131 fprintf( stderr, "Found %d errors\n", toterrs );
00132 }
00133 else {
00134 fprintf( stdout, " No Errors\n" );
00135 #ifdef TIMING
00136 fprintf( stderr, "nprocs: %d bytes: %d write: %f read %f\n",
00137 nprocs, COUNT*sizeof(int), wr_sumtime, rd_sumtime);
00138 #endif
00139 }
00140 }
00141
00142 MPI_Finalize();
00143 return 0;
00144 }