Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This particular RMA test is filled with stupid calls... We send errors for most of...
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / atomic_rmw_cas.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2015 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 /* This test is going to test the atomicity for "read-modify-write" in CAS
9  * operations */
10
11 /* There are three processes involved in this test: P0 (origin_shm), P1 (origin_am),
12  * and P2 (dest). P0 and P1 issues one CAS to P2 via SHM and AM respectively.
13  * For P0, origin value is 1 and compare value is 0; for P1, origin value is 0 and
14  * compare value is 1; for P2, initial target value is 0. The correct results can
15  * only be one of the following cases:
16  *
17  *   (1) result value on P0: 0, result value on P1: 0, target value on P2: 1.
18  *   (2) result value on P0: 0, result value on P1: 1, target value on P2: 0.
19  *
20  * All other results are not correct. */
21
22 #include "mpi.h"
23 #include <stdio.h>
24
25 #define LOOP_SIZE 10000
26 #define CHECK_TAG 123
27
28 int main(int argc, char *argv[])
29 {
30     int rank, size, k;
31     int errors = 0;
32     int origin_shm, origin_am, dest;
33     int *orig_buf = NULL, *result_buf = NULL, *compare_buf = NULL,
34         *target_buf = NULL, *check_buf = NULL;
35     int target_value = 0;
36     MPI_Win win;
37
38     MPI_Init(&argc, &argv);
39
40     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
41     MPI_Comm_size(MPI_COMM_WORLD, &size);
42     if (size != 3) {
43         /* run this test with three processes */
44         goto exit_test;
45     }
46
47     /* this works when MPIR_PARAM_CH3_ODD_EVEN_CLIQUES is set */
48     dest = 2;
49     origin_shm = 0;
50     origin_am = 1;
51
52     if (rank != dest) {
53         MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &orig_buf);
54         MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &result_buf);
55         MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &compare_buf);
56     }
57
58     MPI_Win_allocate(sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &target_buf, &win);
59
60     for (k = 0; k < LOOP_SIZE; k++) {
61
62         /* init buffers */
63         if (rank == origin_shm) {
64             orig_buf[0] = 1;
65             compare_buf[0] = 0;
66             result_buf[0] = 0;
67         }
68         else if (rank == origin_am) {
69             orig_buf[0] = 0;
70             compare_buf[0] = 1;
71             result_buf[0] = 0;
72         }
73         else {
74             MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
75             target_buf[0] = 0;
76             MPI_Win_unlock(rank, win);
77         }
78
79         MPI_Barrier(MPI_COMM_WORLD);
80
81         /* perform FOP */
82         MPI_Win_lock_all(0, win);
83         if (rank != dest) {
84             MPI_Compare_and_swap(orig_buf, compare_buf, result_buf, MPI_INT, dest, 0, win);
85             MPI_Win_flush(dest, win);
86         }
87         MPI_Win_unlock_all(win);
88
89         MPI_Barrier(MPI_COMM_WORLD);
90
91         /* check results */
92         if (rank != dest) {
93             MPI_Gather(result_buf, 1, MPI_INT, check_buf, 1, MPI_INT, dest, MPI_COMM_WORLD);
94         }
95         else {
96             MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
97             target_value = target_buf[0];
98             MPI_Win_unlock(rank, win);
99
100             MPI_Alloc_mem(sizeof(int) * 3, MPI_INFO_NULL, &check_buf);
101             MPI_Gather(&target_value, 1, MPI_INT, check_buf, 1, MPI_INT, dest, MPI_COMM_WORLD);
102
103             if (!(check_buf[dest] == 0 && check_buf[origin_shm] == 0 && check_buf[origin_am] == 1)
104                 && !(check_buf[dest] == 1 && check_buf[origin_shm] == 0 &&
105                      check_buf[origin_am] == 0)) {
106
107                 printf
108                     ("Wrong results: target result = %d, origin_shm result = %d, origin_am result = %d\n",
109                      check_buf[dest], check_buf[origin_shm], check_buf[origin_am]);
110
111                 printf
112                     ("Expected results (1): target result = 1, origin_shm result = 0, origin_am result = 0\n");
113                 printf
114                     ("Expected results (2): target result = 0, origin_shm result = 0, origin_am result = 1\n");
115
116                 errors++;
117             }
118
119             MPI_Free_mem(check_buf);
120         }
121     }
122
123     MPI_Win_free(&win);
124
125     if (rank == origin_am || rank == origin_shm) {
126         MPI_Free_mem(orig_buf);
127         MPI_Free_mem(result_buf);
128         MPI_Free_mem(compare_buf);
129     }
130
131   exit_test:
132     if (rank == dest && errors == 0)
133         printf(" No Errors\n");
134
135     MPI_Finalize();
136     return 0;
137 }