00001
00002
00003
00004
00005
00006 #include "mpi.h"
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 int main(int argc, char **argv)
00021 {
00022 MPI_Datatype newtype;
00023 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
00024 int order, nprocs, len, flag, err;
00025 int array_of_dargs[3], array_of_psizes[3];
00026 int *readbuf, *writebuf, bufcount, mynod;
00027 char filename[1024];
00028 MPI_File fh;
00029 MPI_Status status;
00030 MPI_Aint size_with_aint;
00031 MPI_Offset size_with_offset;
00032
00033 MPI_Init(&argc,&argv);
00034 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
00035 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00036
00037
00038
00039 if (!mynod) {
00040 i = 1;
00041 while ((i < argc) && strcmp("-fname", *argv)) {
00042 i++;
00043 argv++;
00044 }
00045 if (i >= argc) {
00046 fprintf(stderr, "\n*# Usage: large_array -fname filename\n\n");
00047 MPI_Abort(MPI_COMM_WORLD, 1);
00048 }
00049 argv++;
00050 len = strlen(*argv);
00051 strcpy(filename, *argv);
00052 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00053 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00054 fprintf(stderr, "This program creates a 4 Gbyte file. Don't run it if you don't have that much disk space!\n");
00055 }
00056 else {
00057 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
00058 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
00059 }
00060
00061
00062 ndims = 3;
00063 order = MPI_ORDER_C;
00064
00065 array_of_gsizes[0] = 1024;
00066 array_of_gsizes[1] = 1024;
00067 array_of_gsizes[2] = 4*1024/sizeof(int);
00068
00069 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
00070 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
00071 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
00072
00073 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
00074 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
00075 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
00076
00077 for (i=0; i<ndims; i++) array_of_psizes[i] = 0;
00078 MPI_Dims_create(nprocs, ndims, array_of_psizes);
00079
00080
00081
00082
00083 size_with_aint = sizeof(int);
00084 for (i=0; i<ndims; i++) size_with_aint *= array_of_gsizes[i];
00085 size_with_offset = sizeof(int);
00086 for (i=0; i<ndims; i++) size_with_offset *= array_of_gsizes[i];
00087 if (size_with_aint != size_with_offset) {
00088 fprintf(stderr, "Can't use an array of this size unless the MPI implementation defines a 64-bit MPI_Aint\n");
00089 MPI_Abort(MPI_COMM_WORLD, 1);
00090 }
00091
00092 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
00093 array_of_distribs, array_of_dargs,
00094 array_of_psizes, order, MPI_INT, &newtype);
00095 MPI_Type_commit(&newtype);
00096
00097
00098
00099 MPI_Type_size(newtype, &bufcount);
00100 bufcount = bufcount/sizeof(int);
00101 writebuf = (int *) malloc(bufcount * sizeof(int));
00102 if (!writebuf) fprintf(stderr, "Process %d, not enough memory for writebuf\n", mynod);
00103 for (i=0; i<bufcount; i++) writebuf[i] = mynod*1024 + i;
00104
00105
00106 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
00107 MPI_INFO_NULL, &fh);
00108 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
00109 MPI_File_write_all(fh, writebuf, bufcount, MPI_INT, &status);
00110 MPI_File_close(&fh);
00111
00112 free(writebuf);
00113
00114
00115 readbuf = (int *) calloc(bufcount, sizeof(int));
00116 if (!readbuf) fprintf(stderr, "Process %d, not enough memory for readbuf\n", mynod);
00117
00118 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
00119 MPI_INFO_NULL, &fh);
00120 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
00121 MPI_File_read_all(fh, readbuf, bufcount, MPI_INT, &status);
00122 MPI_File_close(&fh);
00123
00124
00125 flag = 0;
00126 for (i=0; i<bufcount; i++)
00127 if (readbuf[i] != mynod*1024 + i) {
00128 fprintf(stderr, "Process %d, readbuf=%d, writebuf=%d\n", mynod, readbuf[i], mynod*1024 + i);
00129 flag = 1;
00130 }
00131 if (!flag) fprintf(stderr, "Process %d: data read back is correct\n", mynod);
00132
00133 MPI_Type_free(&newtype);
00134 free(readbuf);
00135
00136 MPI_Barrier(MPI_COMM_WORLD);
00137 if (!mynod) {
00138 err = MPI_File_delete(filename, MPI_INFO_NULL);
00139 if (err == MPI_SUCCESS) fprintf(stderr, "file deleted\n");
00140 }
00141
00142 MPI_Finalize();
00143 return 0;
00144 }