This source file includes following definitions.
- 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 SIZE (65536)
12
13
14
15 int main(int argc, char **argv)
16 {
17 int *buf, i, rank, nints, len, count, elements;
18 char *filename, *tmp;
19 int errs=0, toterrs;
20 MPI_File fh;
21 MPI_Status status;
22
23 MPI_Init(&argc,&argv);
24 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25
26
27
28 if (!rank) {
29 i = 1;
30 while ((i < argc) && strcmp("-fname", *argv)) {
31 i++;
32 argv++;
33 }
34 if (i >= argc) {
35 fprintf(stderr, "\n*# Usage: simple -fname filename\n\n");
36 MPI_Abort(MPI_COMM_WORLD, 1);
37 }
38 argv++;
39 len = strlen(*argv);
40 filename = (char *) malloc(len+10);
41 strcpy(filename, *argv);
42 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
43 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
44 }
45 else {
46 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
47 filename = (char *) malloc(len+10);
48 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
49 }
50
51 buf = (int *) malloc(SIZE);
52 nints = SIZE/sizeof(int);
53
54
55 tmp = (char *) malloc(len+10);
56 strcpy(tmp, filename);
57 sprintf(filename, "%s.%d", tmp, rank);
58
59 MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
60 MPI_INFO_NULL, &fh);
61 MPI_File_write(fh, buf, nints, MPI_INT, &status);
62
63 MPI_Get_count(&status, MPI_INT, &count);
64 MPI_Get_elements(&status, MPI_INT, &elements);
65 if (!rank) {
66 if (count != nints) {
67 errs++;
68 printf("count = %d, should be %d\n", count, nints);
69 }
70 if (elements != nints) {
71 errs++;
72 printf("elements = %d, should be %d\n", elements, nints);
73 }
74 }
75
76 MPI_File_close(&fh);
77
78 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
79 if (rank == 0) {
80 if( toterrs > 0) {
81 fprintf( stderr, "Found %d errors\n", toterrs );
82 }
83 else {
84 fprintf( stdout, " No Errors\n" );
85 }
86 }
87 free(buf);
88 free(filename);
89 free(tmp);
90
91 MPI_Finalize();
92 return 0;
93 }