00001
00002
00003
00004
00005
00006
00007 #include "mpi.h"
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <stdlib.h>
00011
00012 #define SIZE (65536)
00013 #define NR_NBOPS (32)
00014
00015
00016
00017
00018
00019 void handle_error(int errcode, char *str);
00020
00021 void handle_error(int errcode, char *str)
00022 {
00023 char msg[MPI_MAX_ERROR_STRING];
00024 int resultlen;
00025 MPI_Error_string(errcode, msg, &resultlen);
00026 fprintf(stderr, "%s: %s\n", str, msg);
00027 MPI_Abort(MPI_COMM_WORLD, 1);
00028 }
00029 int main(int argc, char **argv)
00030 {
00031 int *buf, i, rank, nints, len;
00032 char *filename, *tmp;
00033 int errs=0, toterrs;
00034 MPI_File fh;
00035 MPI_Status status[NR_NBOPS];
00036 MPI_Request request[NR_NBOPS];
00037 int errcode = 0;
00038
00039 MPI_Init(&argc,&argv);
00040 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
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: async -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
00068 buf = (int *) malloc(SIZE);
00069 nints = SIZE/sizeof(int);
00070 for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
00071
00072
00073 tmp = (char *) malloc(len+10);
00074 strcpy(tmp, filename);
00075 sprintf(filename, "%s.%d", tmp, rank);
00076
00077 errcode = MPI_File_open(MPI_COMM_SELF, filename,
00078 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00079 if (errcode != MPI_SUCCESS) {
00080 handle_error(errcode, "MPI_File_open");
00081 }
00082 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
00083 for (i=0; i<NR_NBOPS; i++) {
00084 errcode = MPI_File_iwrite_at(fh, nints/NR_NBOPS*i,
00085 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
00086 if (errcode != MPI_SUCCESS) {
00087 handle_error(errcode, "MPI_File_iwrite");
00088 }
00089 }
00090 MPI_Waitall(NR_NBOPS, request, status);
00091
00092 MPI_File_close(&fh);
00093
00094
00095
00096 for (i=0; i<nints; i++) buf[i] = 0;
00097 errcode = MPI_File_open(MPI_COMM_SELF, filename,
00098 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
00099 if (errcode != MPI_SUCCESS) {
00100 handle_error(errcode, "MPI_File_open");
00101 }
00102
00103 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
00104 for (i=0; i<NR_NBOPS; i++) {
00105 errcode = MPI_File_iread_at(fh, nints/NR_NBOPS*i,
00106 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
00107 if (errcode != MPI_SUCCESS) {
00108 handle_error(errcode, "MPI_File_open");
00109 }
00110 }
00111 MPI_Waitall(NR_NBOPS, request, status);
00112
00113 MPI_File_close(&fh);
00114
00115
00116 for (i=0; i<nints; i++) {
00117 if (buf[i] != (rank*100000 + i)) {
00118 errs++;
00119 fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i], rank*100000+i);
00120 }
00121 }
00122
00123 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00124 if (rank == 0) {
00125 if( toterrs > 0) {
00126 fprintf( stderr, "Found %d errors\n", toterrs );
00127 }
00128 else {
00129 fprintf( stdout, " No Errors\n" );
00130 }
00131 }
00132
00133 free(buf);
00134 free(filename);
00135 free(tmp);
00136
00137 MPI_Finalize();
00138 return 0;
00139 }