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 */
46 while (pof2 < nprocs) pof2 *= 2;
48 /* counter_mem = (int *) calloc(pof2*2, sizeof(int)); */
50 i = MPI_Alloc_mem(pof2*2*sizeof(int), MPI_INFO_NULL, &counter_mem);
52 printf("Can't allocate memory in test program\n");
53 MPI_Abort(MPI_COMM_WORLD, 1);
56 for (i=0; i<(pof2*2); i++) counter_mem[i] = 0;
58 MPI_Win_create(counter_mem, pof2*2*sizeof(int), sizeof(int),
59 MPI_INFO_NULL, MPI_COMM_WORLD, &win);
63 /* free(counter_mem) */
64 MPI_Free_mem(counter_mem);
66 /* gather the results from other processes, sort them, and check
67 whether they represent a counter being incremented by 1 */
69 results = (int *) malloc(NTIMES*nprocs*sizeof(int));
70 for (i=0; i<NTIMES*nprocs; i++)
73 MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT,
76 qsort(results+NTIMES, NTIMES*(nprocs-1), sizeof(int), compar);
78 for (i=NTIMES+1; i<(NTIMES*nprocs); i++)
79 if (results[i] != results[i-1] + 1)
85 /* Get the largest power of two smaller than nprocs */
88 while (mask < nprocs) {
94 get_array = (int *) malloc(nlevels * sizeof(int));
95 get_idx = (int *) malloc(nlevels * sizeof(int));
96 acc_idx = (int *) malloc(nlevels * sizeof(int));
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;
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;
117 tmp_rank = tmp_rank % mask;
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]);
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);
131 /* allocate array to store the values obtained from the
132 fetch-and-add counter */
133 counter_vals = (int *) malloc(NTIMES * sizeof(int));
135 MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
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); */
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,
156 MTest_Finalize(errs);
158 return MTestReturnValue( errs );
162 void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
163 MPI_Datatype acc_type, int nlevels, int *value)
167 one = (int *) malloc(nlevels*sizeof(int));
168 for (i=0; i<nlevels; i++) one[i] = 1;
170 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
171 MPI_Accumulate(one, nlevels, MPI_INT, 0, 0, 1, acc_type,
173 MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
174 MPI_Win_unlock(0, win);
177 for (i=0; i<nlevels; i++)
178 *value = *value + get_array[i];
185 int compar(const void *a, const void *b)
187 return (*((int *)a) - *((int *)b));