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.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) pof2 *= 2;
46
47         counter_mem = (int *) calloc(pof2*2, sizeof(int));
48         MPI_Win_create(counter_mem, pof2*2*sizeof(int), sizeof(int),
49                        MPI_INFO_NULL, MPI_COMM_WORLD, &win);
50
51         MPI_Win_free(&win); 
52         free(counter_mem);
53
54         /* gather the results from other processes, sort them, and check 
55            whether they represent a counter being incremented by 1 */
56
57         results = (int *) malloc(NTIMES*nprocs*sizeof(int));
58         for (i=0; i<NTIMES*nprocs; i++)
59             results[i] = -1;
60
61         MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 
62                    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,
127                              nlevels, counter_vals+i); 
128             /* printf("Rank %d, counter %d\n", rank, value); */
129         }
130
131         MPI_Win_free(&win);
132         free(get_array);
133         free(get_idx);
134         free(acc_idx);
135         MPI_Type_free(&get_type);
136         MPI_Type_free(&acc_type);
137
138          /* gather the results to the root */
139         MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 
140                    0, MPI_COMM_WORLD);
141         free(counter_vals);
142    }
143
144     MTest_Finalize(errs);
145     MPI_Finalize(); 
146     return MTestReturnValue( errs );
147
148
149
150 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
151                       MPI_Datatype acc_type, int nlevels, int *value)
152 {
153     int *one, i;
154
155     one = (int *) malloc(nlevels*sizeof(int));
156     for (i=0; i<nlevels; i++) one[i] = 1;
157
158     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
159     MPI_Accumulate(one, nlevels, MPI_INT, 0, 0, 1, acc_type,
160                    MPI_SUM, win);
161     MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
162     MPI_Win_unlock(0, win);
163
164     *value = localvalue;
165     for (i=0; i<nlevels; i++)
166         *value = *value + get_array[i];
167
168     localvalue++;
169
170     free(one);
171 }
172
173 int compar(const void *a, const void *b)
174 {
175     return (*((int *)a) - *((int *)b));
176 }