Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / fetchandadd_tree.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 /* This is the tree-based scalable version of the fetch-and-add
12    example from Using MPI-2, pg 206-207. The code in the book (Fig
13    6.16) has bugs that are fixed below. */
14
15
16 #define NTIMES 20       /* no of times each process calls the counter
17                          * routine */
18
19 int localvalue = 0;             /* contribution of this process to the counter. We
20                                  * define it as a global variable because attribute
21                                  * caching on the window is not enabled yet. */
22
23 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
24                       MPI_Datatype acc_type, int nlevels, int *value);
25
26 int compar(const void *a, const void *b);
27
28 int main(int argc, char *argv[])
29 {
30     int rank, nprocs, i, *counter_mem, *get_array, *get_idx, *acc_idx,
31         mask, nlevels, level, idx, tmp_rank, pof2;
32     MPI_Datatype get_type, acc_type;
33     MPI_Win win;
34     int errs = 0, *results, *counter_vals;
35
36     MTest_Init(&argc, &argv);
37     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
38     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
39
40     if (rank == 0) {
41         /* allocate counter memory and initialize to 0 */
42
43         /* find the next power-of-two >= nprocs */
44         pof2 = 1;
45         while (pof2 < nprocs)
46             pof2 *= 2;
47
48         counter_mem = (int *) calloc(pof2 * 2, sizeof(int));
49         MPI_Win_create(counter_mem, pof2 * 2 * sizeof(int), sizeof(int),
50                        MPI_INFO_NULL, MPI_COMM_WORLD, &win);
51
52         MPI_Win_free(&win);
53         free(counter_mem);
54
55         /* gather the results from other processes, sort them, and check
56          * whether they represent a counter being incremented by 1 */
57
58         results = (int *) malloc(NTIMES * nprocs * sizeof(int));
59         for (i = 0; i < NTIMES * nprocs; i++)
60             results[i] = -1;
61
62         MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 0, MPI_COMM_WORLD);
63
64         qsort(results + NTIMES, NTIMES * (nprocs - 1), sizeof(int), compar);
65
66         for (i = NTIMES + 1; i < (NTIMES * nprocs); i++)
67             if (results[i] != results[i - 1] + 1)
68                 errs++;
69
70         free(results);
71     }
72     else {
73         /* Get the largest power of two smaller than nprocs */
74         mask = 1;
75         nlevels = 0;
76         while (mask < nprocs) {
77             mask <<= 1;
78             nlevels++;
79         }
80         mask >>= 1;
81
82         get_array = (int *) malloc(nlevels * sizeof(int));
83         get_idx = (int *) malloc(nlevels * sizeof(int));
84         acc_idx = (int *) malloc(nlevels * sizeof(int));
85
86         level = 0;
87         idx = 0;
88         tmp_rank = rank;
89         while (mask >= 1) {
90             if (tmp_rank < mask) {
91                 /* go to left for acc_idx, go to right for
92                  * get_idx. set idx=acc_idx for next iteration */
93                 acc_idx[level] = idx + 1;
94                 get_idx[level] = idx + mask * 2;
95                 idx = idx + 1;
96             }
97             else {
98                 /* go to right for acc_idx, go to left for
99                  * get_idx. set idx=acc_idx for next iteration */
100                 acc_idx[level] = idx + mask * 2;
101                 get_idx[level] = idx + 1;
102                 idx = idx + mask * 2;
103             }
104             level++;
105             tmp_rank = tmp_rank % mask;
106             mask >>= 1;
107         }
108
109 /*        for (i=0; i<nlevels; i++)
110             printf("Rank %d, acc_idx[%d]=%d, get_idx[%d]=%d\n", rank,
111                    i, acc_idx[i], i, get_idx[i]);
112 */
113
114         MPI_Type_create_indexed_block(nlevels, 1, get_idx, MPI_INT, &get_type);
115         MPI_Type_create_indexed_block(nlevels, 1, acc_idx, MPI_INT, &acc_type);
116         MPI_Type_commit(&get_type);
117         MPI_Type_commit(&acc_type);
118
119         /* allocate array to store the values obtained from the
120          * fetch-and-add counter */
121         counter_vals = (int *) malloc(NTIMES * sizeof(int));
122
123         MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
124
125         for (i = 0; i < NTIMES; i++) {
126             Get_nextval_tree(win, get_array, get_type, acc_type, nlevels, counter_vals + i);
127             /* printf("Rank %d, counter %d\n", rank, value); */
128         }
129
130         MPI_Win_free(&win);
131         free(get_array);
132         free(get_idx);
133         free(acc_idx);
134         MPI_Type_free(&get_type);
135         MPI_Type_free(&acc_type);
136
137         /* gather the results to the root */
138         MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
139         free(counter_vals);
140     }
141
142     MTest_Finalize(errs);
143     MPI_Finalize();
144     return MTestReturnValue(errs);
145 }
146
147
148 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
149                       MPI_Datatype acc_type, int nlevels, int *value)
150 {
151     int *one, i;
152
153     one = (int *) malloc(nlevels * sizeof(int));
154     for (i = 0; i < nlevels; i++)
155         one[i] = 1;
156
157     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
158     MPI_Accumulate(one, nlevels, MPI_INT, 0, 0, 1, acc_type, MPI_SUM, win);
159     MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
160     MPI_Win_unlock(0, win);
161
162     *value = localvalue;
163     for (i = 0; i < nlevels; i++)
164         *value = *value + get_array[i];
165
166     localvalue++;
167
168     free(one);
169 }
170
171 int compar(const void *a, const void *b)
172 {
173     return (*((int *) a) - *((int *) b));
174 }