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