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
12
13 int main(int argc, char **argv)
14 {
15 MPI_File fh;
16 int rank, len, err, i;
17 int errs=0, toterrs;
18 char *filename;
19
20 MPI_Init(&argc,&argv);
21 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22
23
24
25 if (!rank) {
26 i = 1;
27 while ((i < argc) && strcmp("-fname", *argv)) {
28 i++;
29 argv++;
30 }
31 if (i >= argc) {
32 fprintf(stderr, "\n*# Usage: excl -fname filename\n\n");
33 MPI_Abort(MPI_COMM_WORLD, 1);
34 }
35 argv++;
36 len = strlen(*argv);
37 filename = (char *) malloc(len+10);
38 strcpy(filename, *argv);
39 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
40 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
41 }
42 else {
43 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
44 filename = (char *) malloc(len+10);
45 MPI_Bcast(filename, len+10, MPI_CHAR, 0, MPI_COMM_WORLD);
46 }
47
48
49 if (!rank) MPI_File_delete(filename, MPI_INFO_NULL);
50 MPI_Barrier(MPI_COMM_WORLD);
51
52
53 err = MPI_File_open(MPI_COMM_WORLD, filename,
54 MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_RDWR, MPI_INFO_NULL , &fh);
55 if (err != MPI_SUCCESS) {
56 errs++;
57 fprintf(stderr, "Process %d: open failed when it should have succeeded\n", rank);
58 }
59 else MPI_File_close(&fh);
60
61 MPI_Barrier(MPI_COMM_WORLD);
62
63
64 err = MPI_File_open(MPI_COMM_WORLD, filename,
65 MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_RDWR, MPI_INFO_NULL , &fh);
66 if (err == MPI_SUCCESS) {
67 errs++;
68 fprintf(stderr, "Process %d: open succeeded when it should have failed\n", rank);
69 }
70
71 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
72 if (rank == 0) {
73 if( toterrs > 0) {
74 fprintf( stderr, "Found %d errors\n", toterrs );
75 }
76 else {
77 fprintf( stdout, " No Errors\n" );
78 }
79 }
80
81 free(filename);
82 MPI_Finalize();
83 return 0;
84 }