1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
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. */
16 #define NTIMES 20 /* no of times each process calls the counter
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. */
23 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
24 MPI_Datatype acc_type, int nlevels, int *value);
26 int compar(const void *a, const void *b);
28 int main(int argc, char *argv[])
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;
34 int errs = 0, *results, *counter_vals;
36 MTest_Init(&argc, &argv);
37 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
38 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
41 /* allocate counter memory and initialize to 0 */
43 /* find the next power-of-two >= nprocs */
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);
55 /* gather the results from other processes, sort them, and check
56 * whether they represent a counter being incremented by 1 */
58 results = (int *) malloc(NTIMES * nprocs * sizeof(int));
59 for (i = 0; i < NTIMES * nprocs; i++)
62 MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 0, MPI_COMM_WORLD);
64 qsort(results + NTIMES, NTIMES * (nprocs - 1), sizeof(int), compar);
66 for (i = NTIMES + 1; i < (NTIMES * nprocs); i++)
67 if (results[i] != results[i - 1] + 1)
73 /* Get the largest power of two smaller than nprocs */
76 while (mask < nprocs) {
82 get_array = (int *) malloc(nlevels * sizeof(int));
83 get_idx = (int *) malloc(nlevels * sizeof(int));
84 acc_idx = (int *) malloc(nlevels * sizeof(int));
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;
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;
105 tmp_rank = tmp_rank % mask;
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]);
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);
119 /* allocate array to store the values obtained from the
120 * fetch-and-add counter */
121 counter_vals = (int *) malloc(NTIMES * sizeof(int));
123 MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
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); */
134 MPI_Type_free(&get_type);
135 MPI_Type_free(&acc_type);
137 /* gather the results to the root */
138 MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
142 MTest_Finalize(errs);
144 return MTestReturnValue(errs);
148 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
149 MPI_Datatype acc_type, int nlevels, int *value)
153 one = (int *) malloc(nlevels * sizeof(int));
154 for (i = 0; i < nlevels; i++)
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);
163 for (i = 0; i < nlevels; i++)
164 *value = *value + get_array[i];
171 int compar(const void *a, const void *b)
173 return (*((int *) a) - *((int *) b));