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 int main(int argc, char **argv)
00022 {
00023 MPI_Datatype newtype;
00024 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
00025 int order, nprocs, j, len;
00026 int array_of_dargs[3], array_of_psizes[3];
00027 int *readbuf, *writebuf, bufcount, mynod, *tmpbuf, array_size;
00028 int errs=0, toterrs;
00029 char *filename;
00030 MPI_File fh;
00031 MPI_Status status;
00032 MPI_Request request;
00033
00034 MPI_Init(&argc,&argv);
00035 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
00036 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00037
00038
00039
00040 if (!mynod) {
00041 i = 1;
00042 while ((i < argc) && strcmp("-fname", *argv)) {
00043 i++;
00044 argv++;
00045 }
00046 if (i >= argc) {
00047 fprintf(stderr, "\n*# Usage: coll_test -fname filename\n\n");
00048 MPI_Abort(MPI_COMM_WORLD, 1);
00049 }
00050 argv++;
00051 len = strlen(*argv);
00052 filename = (char *) malloc(len+1);
00053 strcpy(filename, *argv);
00054 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00055 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00056 }
00057 else {
00058 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00059 filename = (char *) malloc(len+1);
00060 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00061 }
00062
00063
00064
00065 ndims = 3;
00066 order = MPI_ORDER_C;
00067
00068 array_of_gsizes[0] = 32;
00069 array_of_gsizes[1] = 32;
00070 array_of_gsizes[2] = 32;
00071
00072 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
00073 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
00074 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
00075
00076 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
00077 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
00078 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
00079
00080 for (i=0; i<ndims; i++) array_of_psizes[i] = 0;
00081 MPI_Dims_create(nprocs, ndims, array_of_psizes);
00082
00083 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
00084 array_of_distribs, array_of_dargs,
00085 array_of_psizes, order, MPI_INT, &newtype);
00086 MPI_Type_commit(&newtype);
00087
00088
00089
00090 MPI_Type_size(newtype, &bufcount);
00091 bufcount = bufcount/sizeof(int);
00092 writebuf = (int *) malloc(bufcount * sizeof(int));
00093 for (i=0; i<bufcount; i++) writebuf[i] = 1;
00094
00095 array_size = array_of_gsizes[0]*array_of_gsizes[1]*array_of_gsizes[2];
00096 tmpbuf = (int *) calloc(array_size, sizeof(int));
00097 MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
00098 MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
00099 MPI_Wait(&request, &status);
00100
00101 j = 0;
00102 for (i=0; i<array_size; i++)
00103 if (tmpbuf[i]) {
00104 writebuf[j] = i;
00105 j++;
00106 }
00107 free(tmpbuf);
00108
00109 if (j != bufcount) {
00110 fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
00111 MPI_Abort(MPI_COMM_WORLD, 1);
00112 }
00113
00114
00115
00116 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
00117 MPI_INFO_NULL, &fh);
00118 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
00119 MPI_File_write_all_begin(fh, writebuf, bufcount, MPI_INT);
00120 MPI_File_write_all_end(fh, writebuf, &status);
00121 MPI_File_close(&fh);
00122
00123
00124
00125 readbuf = (int *) malloc(bufcount * sizeof(int));
00126 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
00127 MPI_INFO_NULL, &fh);
00128 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
00129 MPI_File_read_all_begin(fh, readbuf, bufcount, MPI_INT);
00130 MPI_File_read_all_end(fh, readbuf, &status);
00131 MPI_File_close(&fh);
00132
00133
00134 for (i=0; i<bufcount; i++) {
00135 if (readbuf[i] != writebuf[i]) {
00136 errs++;
00137 fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n",
00138 mynod, readbuf[i], writebuf[i], i);
00139 }
00140 }
00141
00142 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
00143 if (mynod == 0) {
00144 if( toterrs > 0) {
00145 fprintf( stderr, "Found %d errors\n", toterrs );
00146 }
00147 else {
00148 fprintf( stdout, " No Errors\n" );
00149 }
00150 }
00151
00152 MPI_Type_free(&newtype);
00153 free(readbuf);
00154 free(writebuf);
00155 free(filename);
00156
00157 MPI_Finalize();
00158 return 0;
00159 }