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 1024
00012
00013 void handle_error(int errcode, char *str);
00014
00015 void handle_error(int errcode, char *str)
00016 {
00017 char msg[MPI_MAX_ERROR_STRING];
00018 int resultlen;
00019 MPI_Error_string(errcode, msg, &resultlen);
00020 fprintf(stderr, "%s: %s\n", str, msg);
00021 MPI_Abort(MPI_COMM_WORLD, 1);
00022 }
00023
00024
00025
00026 int main(int argc, char **argv)
00027 {
00028 int *buf, i, rank, nprocs, len, sum, global_sum;
00029 int errs=0, toterrs, errcode;
00030 char *filename;
00031 MPI_File fh;
00032 MPI_Status status;
00033
00034 MPI_Init(&argc,&argv);
00035 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00036
00037
00038
00039 if (!rank) {
00040 i = 1;
00041 while ((i < argc) && strcmp("-fname", *argv)) {
00042 i++;
00043 argv++;
00044 }
00045 if (i >= argc) {
00046 fprintf(stderr, "\n*# Usage: shared_fp -fname filename\n\n");
00047 MPI_Abort(MPI_COMM_WORLD, 1);
00048 }
00049 argv++;
00050 len = strlen(*argv);
00051 filename = (char *) malloc(len+10);
00052 strcpy(filename, *argv);
00053 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00054 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00055 }
00056 else {
00057 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00058 filename = (char *) malloc(len+10);
00059 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
00060 }
00061
00062 buf = (int *) malloc(COUNT * sizeof(int));
00063
00064 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00065 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00066
00067 for (i=0; i<COUNT; i++) buf[i] = COUNT*rank + i;
00068
00069 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
00070 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00071 if (errcode != MPI_SUCCESS) {
00072 handle_error(errcode, "MPI_File_open");
00073 }
00074
00075 errcode = MPI_File_write_shared(fh, buf, COUNT, MPI_INT, &status);
00076 if (errcode != MPI_SUCCESS) {
00077 handle_error(errcode, "MPI_File_write_shared");
00078 }
00079
00080 for (i=0; i<COUNT; i++) buf[i] = 0;
00081
00082 MPI_Barrier(MPI_COMM_WORLD);
00083
00084 errcode = MPI_File_seek_shared(fh, 0, MPI_SEEK_SET);
00085 if (errcode != MPI_SUCCESS) {
00086 handle_error(errcode, "MPI_File_seek_shared");
00087 }
00088
00089 errcode = MPI_File_read_shared(fh, buf, COUNT, MPI_INT, &status);
00090 if (errcode != MPI_SUCCESS) {
00091 handle_error(errcode, "MPI_File_read_shared");
00092 }
00093
00094 MPI_File_close(&fh);
00095
00096 sum = 0;
00097 for (i=0; i<COUNT; i++) sum += buf[i];
00098
00099 MPI_Allreduce(&sum, &global_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
00100
00101 if (global_sum != (((COUNT*nprocs - 1)*(COUNT*nprocs))/2)) {
00102 errs++;
00103 fprintf(stderr, "Error: sum %d, global_sum %d, %d\n",
00104 sum, global_sum,(((COUNT*nprocs - 1)*(COUNT*nprocs))/2));
00105 }
00106
00107 free(buf);
00108 free(filename);
00109
00110 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00111 if (rank == 0) {
00112 if( toterrs > 0) {
00113 fprintf( stderr, "Found %d errors\n", toterrs );
00114 }
00115 else {
00116 fprintf( stdout, " No Errors\n" );
00117 }
00118 }
00119
00120 MPI_Finalize();
00121 return 0;
00122 }