Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / flush.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include <mpi.h>
8 #include <stdio.h>
9 #include "mpitest.h"
10
11 #define ITER 100
12
13 int main( int argc, char *argv[] )
14 {
15     int rank, nproc, i;
16     int errors = 0, all_errors = 0;
17     int *buf;
18     MPI_Win window;
19
20     MPI_Init(&argc, &argv);
21     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
23
24     if (nproc < 2) {
25         if (rank == 0) printf("Error: must be run with two or more processes\n");
26         MPI_Abort(MPI_COMM_WORLD, 1);
27     }
28
29     /** Create using MPI_Win_create() **/
30
31     if (rank == 0) {
32       MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &buf);
33       *buf = nproc-1;
34     } else
35       buf = NULL;
36
37     MPI_Win_create(buf, sizeof(int)*(rank == 0), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &window);
38
39     /* Test flush of an empty epoch */
40     MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
41     MPI_Win_flush_all(window);
42     MPI_Win_unlock(0, window);
43
44     MPI_Barrier(MPI_COMM_WORLD);
45
46     /* Test third-party communication, through rank 0. */
47     MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
48
49     for (i = 0; i < ITER; i++) {
50         int val = -1, exp = -1;
51
52         /* Processes form a ring.  Process 0 starts first, then passes a token
53          * to the right.  Each process, in turn, performs third-party
54          * communication via process 0's window. */
55         if (rank > 0) {
56             MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
57         }
58
59         MPI_Get_accumulate(&rank, 1, MPI_INT, &val, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE, window);
60         MPI_Win_flush(0, window);
61
62         exp = (rank + nproc-1) % nproc;
63
64         if (val != exp) {
65             printf("%d - Got %d, expected %d\n", rank, val, exp);
66             errors++;
67         }
68
69         if (rank < nproc-1) {
70             MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
71         }
72
73         MPI_Barrier(MPI_COMM_WORLD);
74     }
75
76     MPI_Win_unlock(0, window);
77
78     MPI_Win_free(&window);
79     if (buf) MPI_Free_mem(buf);
80
81     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
82
83     if (rank == 0 && all_errors == 0)
84         printf(" No Errors\n");
85
86     MPI_Finalize();
87
88     return 0;
89 }