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_fop.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 FOP
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 multiple FOP with MPI_SUM and integer (value 1)
13  * to P2 via SHM and AM respectively. The correct results should be that the
14  * results on P0 and P1 never be the same. */
15
16 #include "mpi.h"
17 #include <stdio.h>
18
19 #define AM_BUF_SIZE  10
20 #define SHM_BUF_SIZE 1000
21 #define WIN_BUF_SIZE 1
22
23 #define LOOP_SIZE 15
24 #define CHECK_TAG 123
25
26 int main(int argc, char *argv[])
27 {
28     int rank, size, i, j, k;
29     int errors = 0, all_errors = 0;
30     int origin_shm, origin_am, dest;
31     int my_buf_size;
32     int *orig_buf = NULL, *result_buf = NULL, *target_buf = NULL, *check_buf = NULL;
33     MPI_Win win;
34     MPI_Status status;
35
36     MPI_Init(&argc, &argv);
37
38     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
39     MPI_Comm_size(MPI_COMM_WORLD, &size);
40     if (size != 3) {
41         /* run this test with three processes */
42         goto exit_test;
43     }
44
45     /* this works when MPIR_PARAM_CH3_ODD_EVEN_CLIQUES is set */
46     dest = 2;
47     origin_shm = 0;
48     origin_am = 1;
49
50     if (rank == origin_am)
51         my_buf_size = AM_BUF_SIZE;
52     else if (rank == origin_shm)
53         my_buf_size = SHM_BUF_SIZE;
54
55     if (rank != dest) {
56         MPI_Alloc_mem(sizeof(int) * my_buf_size, MPI_INFO_NULL, &orig_buf);
57         MPI_Alloc_mem(sizeof(int) * my_buf_size, MPI_INFO_NULL, &result_buf);
58     }
59
60     MPI_Win_allocate(sizeof(int) * WIN_BUF_SIZE, sizeof(int), MPI_INFO_NULL,
61                      MPI_COMM_WORLD, &target_buf, &win);
62
63     for (k = 0; k < LOOP_SIZE; k++) {
64
65         /* init buffers */
66         if (rank != dest) {
67             for (i = 0; i < my_buf_size; i++) {
68                 orig_buf[i] = 1;
69                 result_buf[i] = 0;
70             }
71         }
72         else {
73             MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
74             for (i = 0; i < WIN_BUF_SIZE; i++) {
75                 target_buf[i] = 0;
76             }
77             MPI_Win_unlock(rank, win);
78         }
79
80         MPI_Barrier(MPI_COMM_WORLD);
81
82         /* perform FOP */
83         MPI_Win_lock_all(0, win);
84         if (rank != dest) {
85             for (i = 0; i < my_buf_size; i++) {
86                 MPI_Fetch_and_op(&(orig_buf[i]), &(result_buf[i]), MPI_INT, dest, 0, MPI_SUM, win);
87                 MPI_Win_flush(dest, win);
88             }
89         }
90         MPI_Win_unlock_all(win);
91
92         MPI_Barrier(MPI_COMM_WORLD);
93
94         if (rank != dest) {
95             /* check results on P0 and P2 (origin) */
96             if (rank == origin_am) {
97                 MPI_Send(result_buf, AM_BUF_SIZE, MPI_INT, origin_shm, CHECK_TAG, MPI_COMM_WORLD);
98             }
99             else if (rank == origin_shm) {
100                 MPI_Alloc_mem(sizeof(int) * AM_BUF_SIZE, MPI_INFO_NULL, &check_buf);
101                 MPI_Recv(check_buf, AM_BUF_SIZE, MPI_INT, origin_am, CHECK_TAG, MPI_COMM_WORLD,
102                          &status);
103                 for (i = 0; i < AM_BUF_SIZE; i++) {
104                     for (j = 0; j < SHM_BUF_SIZE; j++) {
105                         if (check_buf[i] == result_buf[j]) {
106                             printf
107                                 ("LOOP=%d, rank=%d, FOP, both check_buf[%d] and result_buf[%d] equal to %d, expected to be different. \n",
108                                  k, rank, i, j, check_buf[i]);
109                             errors++;
110                         }
111                     }
112                 }
113                 MPI_Free_mem(check_buf);
114             }
115         }
116         else {
117             MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
118             /* check results on P1 */
119             if (target_buf[0] != AM_BUF_SIZE + SHM_BUF_SIZE) {
120                 printf("LOOP=%d, rank=%d, FOP, target_buf[0] = %d, expected %d. \n",
121                        k, rank, target_buf[0], AM_BUF_SIZE + SHM_BUF_SIZE);
122                 errors++;
123             }
124             MPI_Win_unlock(rank, win);
125         }
126     }
127
128     MPI_Win_free(&win);
129
130     if (rank == origin_am || rank == origin_shm) {
131         MPI_Free_mem(orig_buf);
132         MPI_Free_mem(result_buf);
133     }
134
135   exit_test:
136     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
137
138     if (rank == 0 && all_errors == 0)
139         printf(" No Errors\n");
140
141     MPI_Finalize();
142     return 0;
143 }