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 / compare_and_swap.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2012 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <mpi.h>
12 #include "mpitest.h"
13 #include "squelch.h"
14
15 #define ITER 100
16
17 int main(int argc, char **argv)
18 {
19     int i, rank, nproc;
20     int errors = 0, all_errors = 0;
21     int *val_ptr;
22     MPI_Win win;
23
24     MPI_Init(&argc, &argv);
25
26     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
27     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
28
29     val_ptr = malloc(sizeof(int));
30
31     *val_ptr = 0;
32
33     MPI_Win_create(val_ptr, sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
34
35     /* Test self communication */
36
37     for (i = 0; i < ITER; i++) {
38         int next = i + 1, result = -1;
39         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, win);
40         MPI_Compare_and_swap(&next, &i, &result, MPI_INT, rank, 0, win);
41         MPI_Win_unlock(rank, win);
42         if (result != i) {
43             SQUELCH(printf("%d->%d -- Error: next=%d compare=%d result=%d val=%d\n", rank,
44                            rank, next, i, result, *val_ptr););
45             errors++;
46         }
47     }
48
49     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, win);
50     *val_ptr = 0;
51     MPI_Win_unlock(rank, win);
52
53     MPI_Barrier(MPI_COMM_WORLD);
54
55     /* Test neighbor communication */
56
57     for (i = 0; i < ITER; i++) {
58         int next = i + 1, result = -1;
59         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, (rank + 1) % nproc, 0, win);
60         MPI_Compare_and_swap(&next, &i, &result, MPI_INT, (rank + 1) % nproc, 0, win);
61         MPI_Win_unlock((rank + 1) % nproc, win);
62         if (result != i) {
63             SQUELCH(printf("%d->%d -- Error: next=%d compare=%d result=%d val=%d\n", rank,
64                            (rank + 1) % nproc, next, i, result, *val_ptr););
65             errors++;
66         }
67     }
68
69     fflush(NULL);
70
71     MPI_Barrier(MPI_COMM_WORLD);
72     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, win);
73     *val_ptr = 0;
74     MPI_Win_unlock(rank, win);
75     MPI_Barrier(MPI_COMM_WORLD);
76
77
78     /* Test contention */
79
80     if (rank != 0) {
81         for (i = 0; i < ITER; i++) {
82             int next = i + 1, result = -1;
83             MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
84             MPI_Compare_and_swap(&next, &i, &result, MPI_INT, 0, 0, win);
85             MPI_Win_unlock(0, win);
86         }
87     }
88
89     MPI_Barrier(MPI_COMM_WORLD);
90
91     if (rank == 0 && nproc > 1) {
92         if (*val_ptr != ITER) {
93             SQUELCH(printf("%d - Error: expected=%d val=%d\n", rank, ITER, *val_ptr););
94             errors++;
95         }
96     }
97
98     MPI_Win_free(&win);
99
100     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
101
102     if (rank == 0 && all_errors == 0)
103         printf(" No Errors\n");
104
105     free(val_ptr);
106     MPI_Finalize();
107
108     return 0;
109 }