1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
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
main(int argc,char * argv[])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 } 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 } else {
109 /* go to right for acc_idx, go to left for
110 * get_idx. set idx=acc_idx for next iteration */
111 acc_idx[level] = idx + mask * 2;
112 get_idx[level] = idx + 1;
113 idx = idx + mask * 2;
114 }
115 level++;
116 tmp_rank = tmp_rank % mask;
117 mask >>= 1;
118 }
119
120 /* for (i=0; i<nlevels; i++)
121 printf("Rank %d, acc_idx[%d]=%d, get_idx[%d]=%d\n", rank,
122 i, acc_idx[i], i, get_idx[i]);
123 */
124
125 MPI_Type_create_indexed_block(nlevels, 1, get_idx, MPI_INT, &get_type);
126 MPI_Type_create_indexed_block(nlevels, 1, acc_idx, MPI_INT, &acc_type);
127 MPI_Type_commit(&get_type);
128 MPI_Type_commit(&acc_type);
129
130 /* allocate array to store the values obtained from the
131 * fetch-and-add counter */
132 counter_vals = (int *) malloc(NTIMES * sizeof(int));
133
134 MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
135
136 for (i = 0; i < NTIMES; i++) {
137 Get_nextval_tree(win, get_array, get_type, acc_type, nlevels, counter_vals + i);
138 /* printf("Rank %d, counter %d\n", rank, value); */
139 }
140
141 MPI_Win_free(&win);
142 free(get_array);
143 free(get_idx);
144 free(acc_idx);
145 MPI_Type_free(&get_type);
146 MPI_Type_free(&acc_type);
147
148 /* gather the results to the root */
149 MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
150 free(counter_vals);
151 }
152
153 MTest_Finalize(errs);
154 return MTestReturnValue(errs);
155 }
156
157
Get_nextval_tree(MPI_Win win,int * get_array,MPI_Datatype get_type,MPI_Datatype acc_type,int nlevels,int * value)158 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
159 MPI_Datatype acc_type, int nlevels, int *value)
160 {
161 int *one, i;
162
163 one = (int *) malloc(nlevels * sizeof(int));
164 for (i = 0; i < nlevels; i++)
165 one[i] = 1;
166
167 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
168 MPI_Accumulate(one, nlevels, MPI_INT, 0, 0, 1, acc_type, MPI_SUM, win);
169 MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
170 MPI_Win_unlock(0, win);
171
172 *value = localvalue;
173 for (i = 0; i < nlevels; i++)
174 *value = *value + get_array[i];
175
176 localvalue++;
177
178 free(one);
179 }
180
compar(const void * a,const void * b)181 int compar(const void *a, const void *b)
182 {
183 return (*((int *) a) - *((int *) b));
184 }
185