Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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) pof2 *= 2;
47
48         /* counter_mem = (int *) calloc(pof2*2, sizeof(int)); */
49
50         i = MPI_Alloc_mem(pof2*2*sizeof(int), MPI_INFO_NULL, &counter_mem);
51         if (i) {
52             printf("Can't allocate memory in test program\n");
53             MPI_Abort(MPI_COMM_WORLD, 1);
54         }
55
56         for (i=0; i<(pof2*2); i++) counter_mem[i] = 0;
57
58         MPI_Win_create(counter_mem, pof2*2*sizeof(int), sizeof(int),
59                        MPI_INFO_NULL, MPI_COMM_WORLD, &win);
60
61         MPI_Win_free(&win); 
62
63         /* free(counter_mem) */
64         MPI_Free_mem(counter_mem);
65
66         /* gather the results from other processes, sort them, and check 
67            whether they represent a counter being incremented by 1 */
68
69         results = (int *) malloc(NTIMES*nprocs*sizeof(int));
70         for (i=0; i<NTIMES*nprocs; i++)
71             results[i] = -1;
72
73         MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 
74                    0, MPI_COMM_WORLD);
75
76         qsort(results+NTIMES, NTIMES*(nprocs-1), sizeof(int), compar);
77
78         for (i=NTIMES+1; i<(NTIMES*nprocs); i++)
79             if (results[i] != results[i-1] + 1)
80                 errs++;
81         
82         free(results);
83     }
84     else {
85         /* Get the largest power of two smaller than nprocs */ 
86         mask = 1; 
87         nlevels = 0;
88         while (mask < nprocs) {
89             mask <<= 1; 
90             nlevels++;
91         }
92         mask >>= 1;
93
94         get_array = (int *) malloc(nlevels * sizeof(int));
95         get_idx = (int *) malloc(nlevels * sizeof(int));
96         acc_idx = (int *) malloc(nlevels * sizeof(int));
97
98         level = 0; 
99         idx   = 0; 
100         tmp_rank = rank;
101         while (mask >= 1) { 
102             if (tmp_rank < mask) { 
103                 /* go to left for acc_idx, go to right for
104                    get_idx. set idx=acc_idx for next iteration */ 
105                 acc_idx[level] = idx + 1; 
106                 get_idx[level] = idx + mask*2; 
107                 idx            = idx + 1; 
108             } 
109             else { 
110                 /* go to right for acc_idx, go to left for
111                    get_idx. set idx=acc_idx for next iteration */ 
112                 acc_idx[level] = idx + mask*2; 
113                 get_idx[level] = idx + 1; 
114                 idx            = idx + mask*2; 
115             } 
116             level++;
117             tmp_rank = tmp_rank % mask;
118             mask >>= 1; 
119         } 
120
121 /*        for (i=0; i<nlevels; i++)
122             printf("Rank %d, acc_idx[%d]=%d, get_idx[%d]=%d\n", rank,
123                    i, acc_idx[i], i, get_idx[i]);
124 */
125
126         MPI_Type_create_indexed_block(nlevels, 1, get_idx, MPI_INT, &get_type);
127         MPI_Type_create_indexed_block(nlevels, 1, acc_idx, MPI_INT, &acc_type);
128         MPI_Type_commit(&get_type);
129         MPI_Type_commit(&acc_type);
130
131         /* allocate array to store the values obtained from the 
132            fetch-and-add counter */
133         counter_vals = (int *) malloc(NTIMES * sizeof(int));
134
135         MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win); 
136
137         for (i=0; i<NTIMES; i++) {
138             Get_nextval_tree(win, get_array, get_type, acc_type,
139                              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, 
152                    0, MPI_COMM_WORLD);
153         free(counter_vals);
154    }
155
156     MTest_Finalize(errs);
157     MPI_Finalize(); 
158     return MTestReturnValue( errs );
159
160
161
162 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
163                       MPI_Datatype acc_type, int nlevels, int *value)
164 {
165     int *one, i;
166
167     one = (int *) malloc(nlevels*sizeof(int));
168     for (i=0; i<nlevels; i++) 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,
172                    MPI_SUM, win);
173     MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
174     MPI_Win_unlock(0, win);
175
176     *value = localvalue;
177     for (i=0; i<nlevels; i++)
178         *value = *value + get_array[i];
179
180     localvalue++;
181
182     free(one);
183 }
184
185 int compar(const void *a, const void *b)
186 {
187     return (*((int *)a) - *((int *)b));
188 }