00001
00002
00003
00004
00005
00006 #include "mpi.h"
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <stdio.h>
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 void handle_error(int errcode, char *str);
00022
00023 void handle_error(int errcode, char *str)
00024 {
00025 char msg[MPI_MAX_ERROR_STRING];
00026 int resultlen;
00027 MPI_Error_string(errcode, msg, &resultlen);
00028 fprintf(stderr, "%s: %s\n", str, msg);
00029 MPI_Abort(MPI_COMM_WORLD, 1);
00030 }
00031
00032 int main(int argc, char **argv)
00033 {
00034 MPI_Datatype newtype;
00035 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
00036 int order, nprocs, j, len;
00037 int array_of_dargs[3], array_of_psizes[3];
00038 int *readbuf, *writebuf, bufcount, mynod, *tmpbuf, array_size;
00039 char *filename;
00040 int errs=0, toterrs;
00041 MPI_File fh;
00042 MPI_Status status;
00043 MPI_Request request;
00044 MPI_Info info = MPI_INFO_NULL;
00045 int errcode;
00046
00047 MPI_Init(&argc,&argv);
00048 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
00049 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00050
00051
00052
00053 if (!mynod) {
00054 i = 1;
00055 while ((i < argc) && strcmp("-fname", *argv)) {
00056 i++;
00057 argv++;
00058 }
00059 if (i >= argc) {
00060 fprintf(stderr, "\n*# Usage: coll_test -fname filename\n\n");
00061 MPI_Abort(MPI_COMM_WORLD, 1);
00062 }
00063 argv++;
00064 len = strlen(*argv);
00065 filename = (char *) malloc(len+1);
00066 strcpy(filename, *argv);
00067 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00068 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00069 }
00070 else {
00071 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00072 filename = (char *) malloc(len+1);
00073 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00074 }
00075
00076
00077
00078 ndims = 3;
00079 order = MPI_ORDER_C;
00080
00081 array_of_gsizes[0] = 32;
00082 array_of_gsizes[1] = 32;
00083 array_of_gsizes[2] = 32;
00084
00085 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
00086 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
00087 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
00088
00089 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
00090 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
00091 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
00092
00093 for (i=0; i<ndims; i++) array_of_psizes[i] = 0;
00094 MPI_Dims_create(nprocs, ndims, array_of_psizes);
00095
00096 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
00097 array_of_distribs, array_of_dargs,
00098 array_of_psizes, order, MPI_INT, &newtype);
00099 MPI_Type_commit(&newtype);
00100
00101
00102
00103 MPI_Type_size(newtype, &bufcount);
00104 bufcount = bufcount/sizeof(int);
00105 writebuf = (int *) malloc(bufcount * sizeof(int));
00106 for (i=0; i<bufcount; i++) writebuf[i] = 1;
00107
00108 array_size = array_of_gsizes[0]*array_of_gsizes[1]*array_of_gsizes[2];
00109 tmpbuf = (int *) calloc(array_size, sizeof(int));
00110 MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
00111 MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
00112 MPI_Wait(&request, &status);
00113
00114 j = 0;
00115 for (i=0; i<array_size; i++)
00116 if (tmpbuf[i]) {
00117 writebuf[j] = i;
00118 j++;
00119 }
00120 free(tmpbuf);
00121
00122 if (j != bufcount) {
00123 fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
00124 MPI_Abort(MPI_COMM_WORLD, 1);
00125 }
00126
00127
00128
00129 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
00130 MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
00131 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
00132
00133 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
00134 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_set_view");
00135
00136 errcode = MPI_File_write_all(fh, writebuf, bufcount, MPI_INT, &status);
00137 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_write_all");
00138 errcode = MPI_File_close(&fh);
00139 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
00140
00141 if (!mynod) {
00142
00143
00144
00145 errcode = MPI_File_open(MPI_COMM_SELF, filename,
00146 MPI_MODE_RDONLY, info, &fh);
00147 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
00148
00149 readbuf = (int *) malloc(array_size * sizeof(int));
00150 errcode = MPI_File_read(fh, readbuf, array_size, MPI_INT, &status);
00151 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read");
00152
00153 errcode = MPI_File_close(&fh);
00154 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
00155
00156 for (i=0; i<array_size; i++)
00157 if (readbuf[i] != i) {
00158 errs++;
00159 fprintf(stderr, "Error: write integer %d but read %d\n",
00160 i,readbuf[i]);
00161 break;
00162 }
00163 free(readbuf);
00164 }
00165 MPI_Barrier(MPI_COMM_WORLD);
00166
00167
00168 readbuf = (int *) malloc(bufcount * sizeof(int));
00169 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
00170 MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
00171 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
00172
00173 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
00174 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_set_view");
00175 errcode = MPI_File_read_all(fh, readbuf, bufcount, MPI_INT, &status);
00176 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read_all");
00177 errcode = MPI_File_close(&fh);
00178 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
00179
00180
00181 for (i=0; i<bufcount; i++) {
00182 if (readbuf[i] != writebuf[i]) {
00183 errs++;
00184 fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n", mynod, readbuf[i], writebuf[i], i);
00185 }
00186 }
00187
00188 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00189 if (mynod == 0) {
00190 if( toterrs > 0) {
00191 fprintf( stderr, "Found %d errors\n", toterrs );
00192 }
00193 else {
00194 fprintf( stdout, " No Errors\n" );
00195 }
00196 }
00197
00198 MPI_Type_free(&newtype);
00199 free(readbuf);
00200 free(writebuf);
00201 free(filename);
00202
00203 MPI_Finalize();
00204 return 0;
00205 }