1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2003 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
7 /* MPI-3 distributed linked list construction example
8 * --------------------------------------------------
10 * Construct a distributed shared linked list using proposed MPI-3 dynamic
11 * windows. Initially process 0 creates the head of the list, attaches it to
12 * the window, and broadcasts the pointer to all processes. Each process p then
13 * appends N new elements to the list when the tail reaches process p-1.
26 #define NUM_ELEMS 1000
27 #define MAX_NPROBE nproc
29 #define ELEM_PER_ROW 16
31 #define MIN(X,Y) ((X < Y) ? (X) : (Y))
32 #define MAX(X,Y) ((X > Y) ? (X) : (Y))
34 /* Linked list pointer */
40 /* Linked list element */
46 static const llist_ptr_t nil = { -1, (MPI_Aint) MPI_BOTTOM };
48 static const int verbose = 0;
49 static const int print_perf = 0;
51 /* List of locally allocated list elements. */
52 static llist_elem_t **my_elems = NULL;
53 static int my_elems_size = 0;
54 static int my_elems_count = 0;
56 /* Allocate a new shared linked list element */
57 MPI_Aint alloc_elem(int value, MPI_Win win)
60 llist_elem_t *elem_ptr;
62 /* Allocate the new element and register it with the window */
63 MPI_Alloc_mem(sizeof(llist_elem_t), MPI_INFO_NULL, &elem_ptr);
64 elem_ptr->value = value;
66 MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
68 /* Add the element to the list of local elements so we can free it later. */
69 if (my_elems_size == my_elems_count) {
71 my_elems = realloc(my_elems, my_elems_size * sizeof(void *));
73 my_elems[my_elems_count] = elem_ptr;
76 MPI_Get_address(elem_ptr, &disp);
80 int main(int argc, char **argv)
82 int procid, nproc, i, j, my_nelem;
86 llist_ptr_t head_ptr, tail_ptr;
88 MPI_Init(&argc, &argv);
90 MPI_Comm_rank(MPI_COMM_WORLD, &procid);
91 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
93 MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
95 /* Process 0 creates the head node */
97 head_ptr.disp = alloc_elem(procid, llist_win);
99 /* Broadcast the head pointer to everyone */
101 MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
104 /* All processes append NUM_ELEMS elements to the list; rank 0 has already
105 * appended an element. */
111 my_nelem = NUM_ELEMS / nproc;
112 if (procid < NUM_ELEMS % nproc)
115 MPI_Barrier(MPI_COMM_WORLD);
118 for (; i < my_nelem; i++) {
119 llist_ptr_t new_elem_ptr;
122 MTEST_VG_MEM_INIT(&new_elem_ptr, sizeof(llist_ptr_t));
124 /* Create a new list element and register it with the window */
125 new_elem_ptr.rank = procid;
126 new_elem_ptr.disp = alloc_elem(procid, llist_win);
128 /* Append the new node to the list. This might take multiple attempts if
129 * others have already appended and our tail pointer is stale. */
133 /* The tail is at my left neighbor, append my element. */
134 if (tail_ptr.rank == (procid + nproc - 1) % nproc) {
136 printf("%d: Appending to <%d, %p>\n", procid, tail_ptr.rank,
137 (void *) tail_ptr.disp);
139 #ifdef USE_MODE_NOCHECK
140 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
142 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, 0, llist_win);
144 MPI_Accumulate(&new_elem_ptr, sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
145 (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next),
146 sizeof(llist_ptr_t), MPI_BYTE, MPI_REPLACE, llist_win);
147 MPI_Win_unlock(tail_ptr.rank, llist_win);
150 tail_ptr = new_elem_ptr;
153 /* Otherwise, chase the tail. */
155 llist_ptr_t next_tail_ptr;
157 #ifdef USE_MODE_NOCHECK
158 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
160 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, 0, llist_win);
162 MPI_Get_accumulate(NULL, 0, MPI_DATATYPE_NULL, &next_tail_ptr,
163 sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
164 (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next),
165 sizeof(llist_ptr_t), MPI_BYTE, MPI_NO_OP, llist_win);
166 MPI_Win_unlock(tail_ptr.rank, llist_win);
168 if (next_tail_ptr.rank != nil.rank) {
170 printf("%d: Chasing to <%d, %p>\n", procid, next_tail_ptr.rank,
171 (void *) next_tail_ptr.disp);
172 tail_ptr = next_tail_ptr;
173 pollint = MAX(MIN_NPROBE, pollint / 2);
176 for (j = 0; j < pollint; j++)
177 MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag,
180 pollint = MIN(MAX_NPROBE, pollint * 2);
186 MPI_Barrier(MPI_COMM_WORLD);
187 time = MPI_Wtime() - time;
189 /* Traverse the list and verify that all processes inserted exactly the correct
190 * number of elements. */
193 int *counts, count = 0;
195 counts = (int *) malloc(sizeof(int) * nproc);
196 assert(counts != NULL);
198 for (i = 0; i < nproc; i++)
203 MPI_Win_lock_all(0, llist_win);
205 /* Walk the list and tally up the number of elements inserted by each rank */
206 while (tail_ptr.disp != nil.disp) {
209 MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
210 tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
212 MPI_Win_flush(tail_ptr.rank, llist_win);
214 tail_ptr = elem.next;
216 assert(elem.value >= 0 && elem.value < nproc);
217 counts[elem.value]++;
221 int last_elem = tail_ptr.disp == nil.disp;
222 printf("%2d%s", elem.value, last_elem ? "" : " -> ");
223 if (count % ELEM_PER_ROW == 0 && !last_elem)
228 MPI_Win_unlock_all(llist_win);
233 /* Verify the counts we collected */
234 for (i = 0; i < nproc; i++) {
237 expected = NUM_ELEMS / nproc;
238 if (i < NUM_ELEMS % nproc)
241 if (counts[i] != expected) {
242 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i],
248 printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
255 MPI_Reduce(&time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
258 printf("Total time = %0.2f sec, elem/sec = %0.2f, sec/elem = %0.2f usec\n", max_time,
259 NUM_ELEMS / max_time, max_time / NUM_ELEMS * 1.0e6);
263 MPI_Win_free(&llist_win);
265 /* Free all the elements in the list */
266 for (; my_elems_count > 0; my_elems_count--)
267 MPI_Free_mem(my_elems[my_elems_count - 1]);