This source file includes following definitions.
- main
1
2
3
4
5
6 #include "mpi.h"
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10
11
12
13
14
15
16
17
18
19
20
21 int main(int argc, char **argv)
22 {
23 MPI_Datatype newtype;
24 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
25 int order, nprocs, j, len;
26 int array_of_dargs[3], array_of_psizes[3];
27 int *readbuf, *writebuf, mynod, *tmpbuf, array_size;
28 MPI_Count bufcount;
29 int errs=0, toterrs;
30 char *filename;
31 MPI_File fh;
32 MPI_Status status;
33 MPI_Request request;
34
35 MPI_Init(&argc,&argv);
36 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
37 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
38
39
40
41 if (!mynod) {
42 i = 1;
43 while ((i < argc) && strcmp("-fname", *argv)) {
44 i++;
45 argv++;
46 }
47 if (i >= argc) {
48 fprintf(stderr, "\n*# Usage: coll_test -fname filename\n\n");
49 MPI_Abort(MPI_COMM_WORLD, 1);
50 }
51 argv++;
52 len = strlen(*argv);
53 filename = (char *) malloc(len+1);
54 strcpy(filename, *argv);
55 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
56 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
57 }
58 else {
59 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
60 filename = (char *) malloc(len+1);
61 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
62 }
63
64
65
66 ndims = 3;
67 order = MPI_ORDER_C;
68
69 array_of_gsizes[0] = 32;
70 array_of_gsizes[1] = 32;
71 array_of_gsizes[2] = 32;
72
73 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
74 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
75 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
76
77 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
78 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
79 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
80
81 for (i=0; i<ndims; i++) array_of_psizes[i] = 0;
82 MPI_Dims_create(nprocs, ndims, array_of_psizes);
83
84 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
85 array_of_distribs, array_of_dargs,
86 array_of_psizes, order, MPI_INT, &newtype);
87 MPI_Type_commit(&newtype);
88
89
90
91 MPI_Type_size_x(newtype, &bufcount);
92 bufcount = bufcount/sizeof(int);
93 writebuf = (int *) malloc(bufcount * sizeof(int));
94 for (i=0; i<bufcount; i++) writebuf[i] = 1;
95
96 array_size = array_of_gsizes[0]*array_of_gsizes[1]*array_of_gsizes[2];
97 tmpbuf = (int *) calloc(array_size, sizeof(int));
98 MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
99 MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
100 MPI_Wait(&request, &status);
101
102 j = 0;
103 for (i=0; i<array_size; i++)
104 if (tmpbuf[i]) {
105 writebuf[j] = i;
106 j++;
107 }
108 free(tmpbuf);
109
110 if (j != bufcount) {
111 fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
112 MPI_Abort(MPI_COMM_WORLD, 1);
113 }
114
115
116
117 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
118 MPI_INFO_NULL, &fh);
119 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
120 MPI_File_write_all_begin(fh, writebuf, bufcount, MPI_INT);
121 MPI_File_write_all_end(fh, writebuf, &status);
122 MPI_File_close(&fh);
123
124
125
126 readbuf = (int *) malloc(bufcount * sizeof(int));
127 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
128 MPI_INFO_NULL, &fh);
129 MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", MPI_INFO_NULL);
130 MPI_File_read_all_begin(fh, readbuf, bufcount, MPI_INT);
131 MPI_File_read_all_end(fh, readbuf, &status);
132 MPI_File_close(&fh);
133
134
135 for (i=0; i<bufcount; i++) {
136 if (readbuf[i] != writebuf[i]) {
137 errs++;
138 fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n",
139 mynod, readbuf[i], writebuf[i], i);
140 }
141 }
142
143 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
144 if (mynod == 0) {
145 if( toterrs > 0) {
146 fprintf( stderr, "Found %d errors\n", toterrs );
147 }
148 else {
149 fprintf( stdout, " No Errors\n" );
150 }
151 }
152
153 MPI_Type_free(&newtype);
154 free(readbuf);
155 free(writebuf);
156 free(filename);
157
158 MPI_Finalize();
159 return 0;
160 }