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