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. */
15 /* same as fetchandadd_tree.c but uses alloc_mem */
17 #define NTIMES 20 /* no of times each process calls the counter
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. */
24 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
25 MPI_Datatype acc_type, int nlevels, int *value);
27 int compar(const void *a, const void *b);
29 int main(int argc, char *argv[])
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;
35 int errs = 0, *results, *counter_vals;
37 MTest_Init(&argc, &argv);
38 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
39 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
42 /* allocate counter memory and initialize to 0 */
44 /* find the next power-of-two >= nprocs */
49 /* counter_mem = (int *) calloc(pof2*2, sizeof(int)); */
51 i = MPI_Alloc_mem(pof2 * 2 * sizeof(int), MPI_INFO_NULL, &counter_mem);
53 printf("Can't allocate memory in test program\n");
54 MPI_Abort(MPI_COMM_WORLD, 1);
57 for (i = 0; i < (pof2 * 2); i++)
60 MPI_Win_create(counter_mem, pof2 * 2 * sizeof(int), sizeof(int),
61 MPI_INFO_NULL, MPI_COMM_WORLD, &win);
65 /* free(counter_mem) */
66 MPI_Free_mem(counter_mem);
68 /* gather the results from other processes, sort them, and check
69 * whether they represent a counter being incremented by 1 */
71 results = (int *) malloc(NTIMES * nprocs * sizeof(int));
72 for (i = 0; i < NTIMES * nprocs; i++)
75 MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 0, MPI_COMM_WORLD);
77 qsort(results + NTIMES, NTIMES * (nprocs - 1), sizeof(int), compar);
79 for (i = NTIMES + 1; i < (NTIMES * nprocs); i++)
80 if (results[i] != results[i - 1] + 1)
86 /* Get the largest power of two smaller than nprocs */
89 while (mask < nprocs) {
95 get_array = (int *) malloc(nlevels * sizeof(int));
96 get_idx = (int *) malloc(nlevels * sizeof(int));
97 acc_idx = (int *) malloc(nlevels * sizeof(int));
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;
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;
118 tmp_rank = tmp_rank % mask;
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]);
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);
132 /* allocate array to store the values obtained from the
133 * fetch-and-add counter */
134 counter_vals = (int *) malloc(NTIMES * sizeof(int));
136 MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
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); */
147 MPI_Type_free(&get_type);
148 MPI_Type_free(&acc_type);
150 /* gather the results to the root */
151 MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
155 MTest_Finalize(errs);
157 return MTestReturnValue(errs);
161 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
162 MPI_Datatype acc_type, int nlevels, int *value)
166 one = (int *) malloc(nlevels * sizeof(int));
167 for (i = 0; i < nlevels; i++)
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);
176 for (i = 0; i < nlevels; i++)
177 *value = *value + get_array[i];
184 int compar(const void *a, const void *b)
186 return (*((int *) a) - *((int *) b));