Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[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)
26             printf("Error: must be run with two or more processes\n");
27         MPI_Abort(MPI_COMM_WORLD, 1);
28     }
29
30     /** Create using MPI_Win_create() **/
31
32     if (rank == 0) {
33         MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &buf);
34         *buf = nproc - 1;
35     }
36     else
37         buf = NULL;
38
39     MPI_Win_create(buf, sizeof(int) * (rank == 0), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &window);
40
41     /* Test flush of an empty epoch */
42     MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
43     MPI_Win_flush_all(window);
44     MPI_Win_unlock(0, window);
45
46     MPI_Barrier(MPI_COMM_WORLD);
47
48     /* Test third-party communication, through rank 0. */
49     MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
50
51     for (i = 0; i < ITER; i++) {
52         int val = -1, exp = -1;
53
54         /* Processes form a ring.  Process 0 starts first, then passes a token
55          * to the right.  Each process, in turn, performs third-party
56          * communication via process 0's window. */
57         if (rank > 0) {
58             MPI_Recv(NULL, 0, MPI_BYTE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
59         }
60
61         MPI_Get_accumulate(&rank, 1, MPI_INT, &val, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE,
62                            window);
63         MPI_Win_flush(0, window);
64
65         exp = (rank + nproc - 1) % nproc;
66
67         if (val != exp) {
68             printf("%d - Got %d, expected %d\n", rank, val, exp);
69             errors++;
70         }
71
72         if (rank < nproc - 1) {
73             MPI_Send(NULL, 0, MPI_BYTE, rank + 1, 0, MPI_COMM_WORLD);
74         }
75
76         MPI_Barrier(MPI_COMM_WORLD);
77     }
78
79     MPI_Win_unlock(0, window);
80
81     MPI_Win_free(&window);
82     if (buf)
83         MPI_Free_mem(buf);
84
85     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
86
87     if (rank == 0 && all_errors == 0)
88         printf(" No Errors\n");
89
90     MPI_Finalize();
91
92     return 0;
93 }