This source file includes following definitions.
- handle_error
- main
1
2
3
4
5
6
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #define SIZE (65536)
13 #define NR_NBOPS (32)
14
15
16
17
18
19 void handle_error(int errcode, const char *str);
20
21 void handle_error(int errcode, const char *str)
22 {
23 char msg[MPI_MAX_ERROR_STRING];
24 int resultlen;
25 MPI_Error_string(errcode, msg, &resultlen);
26 fprintf(stderr, "%s: %s\n", str, msg);
27 MPI_Abort(MPI_COMM_WORLD, 1);
28 }
29 int main(int argc, char **argv)
30 {
31 int *buf, i, rank, nints, len;
32 char *filename, *tmp;
33 int errs=0, toterrs;
34 MPI_File fh;
35 MPI_Status status[NR_NBOPS];
36 MPI_Request request[NR_NBOPS];
37 int errcode = 0;
38
39 MPI_Init(&argc,&argv);
40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
41
42
43
44 if (!rank) {
45 i = 1;
46 while ((i < argc) && strcmp("-fname", *argv)) {
47 i++;
48 argv++;
49 }
50 if (i >= argc) {
51 fprintf(stderr, "\n*# Usage: async -fname filename\n\n");
52 MPI_Abort(MPI_COMM_WORLD, 1);
53 }
54 argv++;
55 len = strlen(*argv);
56 filename = (char *) malloc(len+10);
57 strcpy(filename, *argv);
58 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
59 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
60 }
61 else {
62 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
63 filename = (char *) malloc(len+10);
64 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
65 }
66
67
68 buf = (int *) malloc(SIZE);
69 nints = SIZE/sizeof(int);
70 for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
71
72
73 tmp = (char *) malloc(len+10);
74 strcpy(tmp, filename);
75 sprintf(filename, "%s.%d", tmp, rank);
76
77 errcode = MPI_File_open(MPI_COMM_SELF, filename,
78 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
79 if (errcode != MPI_SUCCESS) {
80 handle_error(errcode, "MPI_File_open");
81 }
82 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
83 for (i=0; i<NR_NBOPS; i++) {
84 errcode = MPI_File_iwrite_at(fh, nints/NR_NBOPS*i,
85 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
86 if (errcode != MPI_SUCCESS) {
87 handle_error(errcode, "MPI_File_iwrite");
88 }
89 }
90 MPI_Waitall(NR_NBOPS, request, status);
91
92 MPI_File_close(&fh);
93
94
95
96 for (i=0; i<nints; i++) buf[i] = 0;
97 errcode = MPI_File_open(MPI_COMM_SELF, filename,
98 MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
99 if (errcode != MPI_SUCCESS) {
100 handle_error(errcode, "MPI_File_open");
101 }
102
103 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
104 for (i=0; i<NR_NBOPS; i++) {
105 errcode = MPI_File_iread_at(fh, nints/NR_NBOPS*i,
106 buf+(nints/NR_NBOPS*i), nints/NR_NBOPS, MPI_INT, &(request[i]));
107 if (errcode != MPI_SUCCESS) {
108 handle_error(errcode, "MPI_File_open");
109 }
110 }
111 MPI_Waitall(NR_NBOPS, request, status);
112
113 MPI_File_close(&fh);
114
115
116 for (i=0; i<nints; i++) {
117 if (buf[i] != (rank*100000 + i)) {
118 errs++;
119 fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i], rank*100000+i);
120 }
121 }
122
123 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
124 if (rank == 0) {
125 if( toterrs > 0) {
126 fprintf( stderr, "Found %d errors\n", toterrs );
127 }
128 else {
129 fprintf( stdout, " No Errors\n" );
130 }
131 }
132
133 free(buf);
134 free(filename);
135 free(tmp);
136
137 MPI_Finalize();
138 return 0;
139 }