1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2015 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
8 /* This test is going to test the atomicity for "read-modify-write" in CAS
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:
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.
20 * All other results are not correct. */
25 #define LOOP_SIZE 10000
28 int main(int argc, char *argv[])
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;
38 MPI_Init(&argc, &argv);
40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
41 MPI_Comm_size(MPI_COMM_WORLD, &size);
43 /* run this test with three processes */
47 /* this works when MPIR_PARAM_CH3_ODD_EVEN_CLIQUES is set */
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);
58 MPI_Win_allocate(sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &target_buf, &win);
60 for (k = 0; k < LOOP_SIZE; k++) {
63 if (rank == origin_shm) {
68 else if (rank == origin_am) {
74 MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
76 MPI_Win_unlock(rank, win);
79 MPI_Barrier(MPI_COMM_WORLD);
82 MPI_Win_lock_all(0, win);
84 MPI_Compare_and_swap(orig_buf, compare_buf, result_buf, MPI_INT, dest, 0, win);
85 MPI_Win_flush(dest, win);
87 MPI_Win_unlock_all(win);
89 MPI_Barrier(MPI_COMM_WORLD);
93 MPI_Gather(result_buf, 1, MPI_INT, check_buf, 1, MPI_INT, dest, MPI_COMM_WORLD);
96 MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
97 target_value = target_buf[0];
98 MPI_Win_unlock(rank, win);
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);
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)) {
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]);
112 ("Expected results (1): target result = 1, origin_shm result = 0, origin_am result = 0\n");
114 ("Expected results (2): target result = 0, origin_shm result = 0, origin_am result = 1\n");
119 MPI_Free_mem(check_buf);
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);
132 if (rank == dest && errors == 0)
133 printf(" No Errors\n");