Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
activate tests
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / fetchandadd.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include "stdio.h"
8 #include "stdlib.h"
9 #include "mpitest.h"
10
11 /* Fetch and add example from Using MPI-2 (the non-scalable version,
12    Fig. 6.12). */
13
14
15 #define NTIMES 20       /* no of times each process calls the counter
16                          * routine */
17
18 int localvalue = 0;             /* contribution of this process to the counter. We
19                                  * define it as a global variable because attribute
20                                  * caching on the window is not enabled yet. */
21
22 void Get_nextval(MPI_Win win, int *val_array, MPI_Datatype get_type,
23                  int rank, int nprocs, int *value);
24
25 int compar(const void *a, const void *b);
26
27 int main(int argc, char *argv[])
28 {
29     int rank, nprocs, i, blens[2], disps[2], *counter_mem, *val_array, *results, *counter_vals;
30     MPI_Datatype get_type;
31     MPI_Win win;
32     int errs = 0;
33
34     MTest_Init(&argc, &argv);
35     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
36     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
37
38     if (rank == 0) {
39         /* allocate counter memory and initialize to 0 */
40         counter_mem = (int *) calloc(nprocs, sizeof(int));
41         MPI_Win_create(counter_mem, nprocs * sizeof(int), sizeof(int),
42                        MPI_INFO_NULL, MPI_COMM_WORLD, &win);
43
44         MPI_Win_free(&win);
45         free(counter_mem);
46
47         /* gather the results from other processes, sort them, and check
48          * whether they represent a counter being incremented by 1 */
49
50         results = (int *) malloc(NTIMES * nprocs * sizeof(int));
51         for (i = 0; i < NTIMES * nprocs; i++)
52             results[i] = -1;
53
54         MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 0, MPI_COMM_WORLD);
55
56         qsort(results + NTIMES, NTIMES * (nprocs - 1), sizeof(int), compar);
57
58         for (i = NTIMES + 1; i < (NTIMES * nprocs); i++)
59             if (results[i] != results[i - 1] + 1)
60                 errs++;
61
62         free(results);
63     }
64     else {
65         blens[0] = rank;
66         disps[0] = 0;
67         blens[1] = nprocs - rank - 1;
68         disps[1] = rank + 1;
69
70         MPI_Type_indexed(2, blens, disps, MPI_INT, &get_type);
71         MPI_Type_commit(&get_type);
72
73         val_array = (int *) malloc(nprocs * sizeof(int));
74
75         /* allocate array to store the values obtained from the
76          * fetch-and-add counter */
77         counter_vals = (int *) malloc(NTIMES * sizeof(int));
78
79         MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
80
81         for (i = 0; i < NTIMES; i++) {
82             Get_nextval(win, val_array, get_type, rank, nprocs, counter_vals + i);
83             /* printf("Rank %d, counter %d\n", rank, localvalue); */
84         }
85
86         MPI_Win_free(&win);
87
88         free(val_array);
89         MPI_Type_free(&get_type);
90
91         /* gather the results to the root */
92         MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
93         free(counter_vals);
94     }
95
96     MTest_Finalize(errs);
97     MPI_Finalize();
98     return 0;
99 }
100
101
102 void Get_nextval(MPI_Win win, int *val_array, MPI_Datatype get_type,
103                  int rank, int nprocs, int *value)
104 {
105     int one = 1, i;
106
107     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
108     MPI_Accumulate(&one, 1, MPI_INT, 0, rank, 1, MPI_INT, MPI_SUM, win);
109     MPI_Get(val_array, 1, get_type, 0, 0, 1, get_type, win);
110     MPI_Win_unlock(0, win);
111
112     *value = 0;
113     val_array[rank] = localvalue;
114     for (i = 0; i < nprocs; i++)
115         *value = *value + val_array[i];
116
117     localvalue++;
118 }
119
120 int compar(const void *a, const void *b)
121 {
122     return (*((int *) a) - *((int *) b));
123 }