This source file includes following definitions.
- handle_error
- 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 void handle_error(int errcode, const char *str);
22
23 void handle_error(int errcode, const char *str)
24 {
25 char msg[MPI_MAX_ERROR_STRING];
26 int resultlen;
27 MPI_Error_string(errcode, msg, &resultlen);
28 fprintf(stderr, "%s: %s\n", str, msg);
29 MPI_Abort(MPI_COMM_WORLD, 1);
30 }
31
32 int main(int argc, char **argv)
33 {
34 MPI_Datatype newtype;
35 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
36 int order, nprocs, j, len;
37 int array_of_dargs[3], array_of_psizes[3];
38 int *readbuf, *writebuf, mynod, *tmpbuf, array_size;
39 MPI_Count bufcount;
40 char *filename;
41 int errs=0, toterrs;
42 MPI_File fh;
43 MPI_Status status;
44 MPI_Request request;
45 MPI_Info info = MPI_INFO_NULL;
46 int errcode;
47
48 MPI_Init(&argc,&argv);
49 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
50 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
51
52
53
54 if (!mynod) {
55 i = 1;
56 while ((i < argc) && strcmp("-fname", *argv)) {
57 i++;
58 argv++;
59 }
60 if (i >= argc) {
61 fprintf(stderr, "\n*# Usage: coll_test -fname filename\n\n");
62 MPI_Abort(MPI_COMM_WORLD, 1);
63 }
64 argv++;
65 len = strlen(*argv);
66 filename = (char *) malloc(len+1);
67 strcpy(filename, *argv);
68 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
69 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
70 }
71 else {
72 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
73 filename = (char *) malloc(len+1);
74 MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD);
75 }
76
77
78
79 ndims = 3;
80 order = MPI_ORDER_C;
81
82 array_of_gsizes[0] = 32;
83 array_of_gsizes[1] = 32;
84 array_of_gsizes[2] = 32;
85
86 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
87 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
88 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
89
90 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
91 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
92 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
93
94 for (i=0; i<ndims; i++) array_of_psizes[i] = 0;
95 MPI_Dims_create(nprocs, ndims, array_of_psizes);
96
97 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
98 array_of_distribs, array_of_dargs,
99 array_of_psizes, order, MPI_INT, &newtype);
100 MPI_Type_commit(&newtype);
101
102
103
104 MPI_Type_size_x(newtype, &bufcount);
105 bufcount = bufcount/sizeof(int);
106 writebuf = (int *) malloc(bufcount * sizeof(int));
107 for (i=0; i<bufcount; i++) writebuf[i] = 1;
108
109 array_size = array_of_gsizes[0]*array_of_gsizes[1]*array_of_gsizes[2];
110 tmpbuf = (int *) calloc(array_size, sizeof(int));
111 MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
112 MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
113 MPI_Wait(&request, &status);
114
115 j = 0;
116 for (i=0; i<array_size; i++)
117 if (tmpbuf[i]) {
118 writebuf[j] = i;
119 j++;
120 }
121 free(tmpbuf);
122
123 if (j != bufcount) {
124 fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
125 MPI_Abort(MPI_COMM_WORLD, 1);
126 }
127
128
129
130 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
131 MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
132 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
133
134 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
135 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_set_view");
136
137 errcode = MPI_File_write_all(fh, writebuf, bufcount, MPI_INT, &status);
138 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_write_all");
139 errcode = MPI_File_close(&fh);
140 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
141
142 if (!mynod) {
143
144
145
146 errcode = MPI_File_open(MPI_COMM_SELF, filename,
147 MPI_MODE_RDONLY, info, &fh);
148 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
149
150 readbuf = (int *) malloc(array_size * sizeof(int));
151 errcode = MPI_File_read(fh, readbuf, array_size, MPI_INT, &status);
152 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read");
153
154 errcode = MPI_File_close(&fh);
155 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
156
157 for (i=0; i<array_size; i++)
158 if (readbuf[i] != i) {
159 errs++;
160 fprintf(stderr, "Error: write integer %d but read %d\n",
161 i,readbuf[i]);
162 break;
163 }
164 free(readbuf);
165 }
166 MPI_Barrier(MPI_COMM_WORLD);
167
168
169 readbuf = (int *) malloc(bufcount * sizeof(int));
170 errcode = MPI_File_open(MPI_COMM_WORLD, filename,
171 MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
172 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open");
173
174 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
175 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_set_view");
176 errcode = MPI_File_read_all(fh, readbuf, bufcount, MPI_INT, &status);
177 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_read_all");
178 errcode = MPI_File_close(&fh);
179 if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_close");
180
181
182 for (i=0; i<bufcount; i++) {
183 if (readbuf[i] != writebuf[i]) {
184 errs++;
185 fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n", mynod, readbuf[i], writebuf[i], i);
186 }
187 }
188
189 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
190 if (mynod == 0) {
191 if( toterrs > 0) {
192 fprintf( stderr, "Found %d errors\n", toterrs );
193 }
194 else {
195 fprintf( stdout, " No Errors\n" );
196 }
197 }
198
199 MPI_Type_free(&newtype);
200 free(readbuf);
201 free(writebuf);
202 free(filename);
203
204 MPI_Finalize();
205 return 0;
206 }