1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
8 /* MPI-3 distributed linked list construction example
9 * --------------------------------------------------
11 * Construct a distributed shared linked list using proposed MPI-3 dynamic
12 * windows. Initially process 0 creates the head of the list, attaches it to
13 * the window, and broadcasts the pointer to all processes. All processes then
14 * concurrently append N new elements to the list. When a process attempts to
15 * attach its element to the tail of list it may discover that its tail pointer
16 * is stale and it must chase ahead to the new tail before the element can be
32 #define ELEM_PER_ROW 16
34 /* Linked list pointer */
40 /* Linked list element */
46 static const llist_ptr_t nil = { -1, (MPI_Aint) MPI_BOTTOM };
47 static const int verbose = 0;
49 /* List of locally allocated list elements. */
50 static llist_elem_t **my_elems = NULL;
51 static int my_elems_size = 0;
52 static int my_elems_count = 0;
54 /* Allocate a new shared linked list element */
55 MPI_Aint alloc_elem(int value, MPI_Win win) {
57 llist_elem_t *elem_ptr;
59 /* Allocate the new element and register it with the window */
60 MPI_Alloc_mem(sizeof(llist_elem_t), MPI_INFO_NULL, &elem_ptr);
61 elem_ptr->value = value;
63 MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
65 /* Add the element to the list of local elements so we can free it later. */
66 if (my_elems_size == my_elems_count) {
68 my_elems = realloc(my_elems, my_elems_size*sizeof(void*));
70 my_elems[my_elems_count] = elem_ptr;
73 MPI_Get_address(elem_ptr, &disp);
77 int main(int argc, char **argv) {
80 llist_ptr_t head_ptr, tail_ptr;
82 MPI_Init(&argc, &argv);
84 MPI_Comm_rank(MPI_COMM_WORLD, &procid);
85 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
87 MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
89 /* Process 0 creates the head node */
91 head_ptr.disp = alloc_elem(-1, llist_win);
93 /* Broadcast the head pointer to everyone */
95 MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
98 /* All processes concurrently append NUM_ELEMS elements to the list */
99 for (i = 0; i < NUM_ELEMS; i++) {
100 llist_ptr_t new_elem_ptr;
103 /* Create a new list element and register it with the window */
104 new_elem_ptr.rank = procid;
105 new_elem_ptr.disp = alloc_elem(procid, llist_win);
107 /* Append the new node to the list. This might take multiple attempts if
108 others have already appended and our tail pointer is stale. */
110 llist_ptr_t next_tail_ptr = nil;
112 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
114 MPI_Compare_and_swap((void*) &new_elem_ptr.rank, (void*) &nil.rank,
115 (void*) &next_tail_ptr.rank, MPI_INT, tail_ptr.rank,
116 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next.rank), llist_win);
118 MPI_Win_unlock(tail_ptr.rank, llist_win);
119 success = (next_tail_ptr.rank == nil.rank);
125 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
127 MPI_Fetch_and_op(&new_elem_ptr.disp, &result, MPI_AINT, tail_ptr.rank,
128 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next.disp),
129 MPI_REPLACE, llist_win);
131 /* Note: accumulate is faster, since we don't need the result. Replacing with
132 Fetch_and_op to create a more complete test case. */
134 MPI_Accumulate(&new_elem_ptr.disp, 1, MPI_AINT, tail_ptr.rank,
135 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next.disp), 1,
136 MPI_AINT, MPI_REPLACE, llist_win);
139 MPI_Win_unlock(tail_ptr.rank, llist_win);
140 tail_ptr = new_elem_ptr;
142 /* For implementations that use pt-to-pt messaging, force progress for other threads'
144 for (i = 0; i < NPROBE; i++)
145 MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
148 /* Tail pointer is stale, fetch the displacement. May take multiple tries
149 if it is being updated. */
153 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
155 MPI_Fetch_and_op(NULL, &next_tail_ptr.disp, MPI_AINT, tail_ptr.rank,
156 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next.disp),
157 MPI_NO_OP, llist_win);
159 MPI_Win_unlock(tail_ptr.rank, llist_win);
160 } while (next_tail_ptr.disp == nil.disp);
161 tail_ptr = next_tail_ptr;
166 MPI_Barrier(MPI_COMM_WORLD);
168 /* Traverse the list and verify that all processes inserted exactly the correct
169 number of elements. */
173 int *counts, count = 0;
175 counts = (int*) malloc(sizeof(int) * nproc);
176 assert(counts != NULL);
178 for (i = 0; i < nproc; i++)
183 /* Walk the list and tally up the number of elements inserted by each rank */
184 while (tail_ptr.disp != nil.disp) {
187 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
189 MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
190 tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
192 MPI_Win_unlock(tail_ptr.rank, llist_win);
194 tail_ptr = elem.next;
196 /* This is not the root */
198 assert(elem.value >= 0 && elem.value < nproc);
199 counts[elem.value]++;
203 int last_elem = tail_ptr.disp == nil.disp;
204 printf("%2d%s", elem.value, last_elem ? "" : " -> ");
205 if (count % ELEM_PER_ROW == 0 && !last_elem)
210 /* This is the root */
212 assert(elem.value == -1);
220 /* Verify the counts we collected */
221 for (i = 0; i < nproc; i++) {
222 int expected = NUM_ELEMS;
224 if (counts[i] != expected) {
225 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i], expected);
230 printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
234 MPI_Win_free(&llist_win);
236 /* Free all the elements in the list */
237 for ( ; my_elems_count > 0; my_elems_count--)
238 MPI_Free_mem(my_elems[my_elems_count-1]);