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 };
48 static const int verbose = 0;
50 /* Allocate a new shared linked list element */
51 static MPI_Aint alloc_elem(int value, MPI_Win win, llist_elem_t ***my_elems, int* my_elems_size, int* my_elems_count)
54 llist_elem_t *elem_ptr;
56 /* Allocate the new element and register it with the window */
57 MPI_Alloc_mem(sizeof(llist_elem_t), MPI_INFO_NULL, &elem_ptr);
58 elem_ptr->value = value;
60 MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
62 /* Add the element to the list of local elements so we can free it later. */
63 if (*my_elems_size == *my_elems_count) {
64 *my_elems_size += 100;
65 *my_elems = realloc(*my_elems, *my_elems_size * sizeof(void *));
67 (*my_elems)[*my_elems_count] = elem_ptr;
70 MPI_Get_address(elem_ptr, &disp);
74 int main(int argc, char **argv)
78 llist_ptr_t head_ptr, tail_ptr;
79 /* List of locally allocated list elements. */
80 llist_elem_t **my_elems = NULL;
81 int my_elems_size = 0;
82 int my_elems_count = 0;
84 MPI_Init(&argc, &argv);
86 MPI_Comm_rank(MPI_COMM_WORLD, &procid);
87 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
89 MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
91 /* Process 0 creates the head node */
93 head_ptr.disp = alloc_elem(-1, llist_win, &my_elems, &my_elems_size, &my_elems_count);
95 /* Broadcast the head pointer to everyone */
97 MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
100 /* All processes concurrently append NUM_ELEMS elements to the list */
101 for (i = 0; i < NUM_ELEMS; i++) {
102 llist_ptr_t new_elem_ptr;
105 /* Create a new list element and register it with the window */
106 new_elem_ptr.rank = procid;
107 new_elem_ptr.disp = alloc_elem(procid, llist_win, &my_elems, &my_elems_size, &my_elems_count);
109 /* Append the new node to the list. This might take multiple attempts if
110 * others have already appended and our tail pointer is stale. */
112 llist_ptr_t next_tail_ptr = nil;
114 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
116 MPI_Compare_and_swap((void *) &new_elem_ptr.rank, (void *) &nil.rank,
117 (void *) &next_tail_ptr.rank, MPI_INT, tail_ptr.rank,
118 (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.rank),
121 MPI_Win_unlock(tail_ptr.rank, llist_win);
122 success = (next_tail_ptr.rank == nil.rank);
127 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
129 MPI_Put(&new_elem_ptr.disp, 1, MPI_AINT, tail_ptr.rank,
130 (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.disp), 1,
131 MPI_AINT, llist_win);
133 MPI_Win_unlock(tail_ptr.rank, llist_win);
134 tail_ptr = new_elem_ptr;
136 /* For implementations that use pt-to-pt messaging, force progress for other threads'
138 for (i = 0; i < NPROBE; i++)
139 MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag,
144 /* Tail pointer is stale, fetch the displacement. May take multiple tries
145 * if it is being updated. */
147 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
149 MPI_Get(&next_tail_ptr.disp, 1, MPI_AINT, tail_ptr.rank,
150 (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.disp),
151 1, MPI_AINT, llist_win);
153 MPI_Win_unlock(tail_ptr.rank, llist_win);
154 } while (next_tail_ptr.disp == nil.disp);
155 tail_ptr = next_tail_ptr;
160 MPI_Barrier(MPI_COMM_WORLD);
162 /* Traverse the list and verify that all processes inserted exactly the correct
163 * number of elements. */
167 int *counts, count = 0;
169 counts = (int *) malloc(sizeof(int) * nproc);
170 assert(counts != NULL);
172 for (i = 0; i < nproc; i++)
177 /* Walk the list and tally up the number of elements inserted by each rank */
178 while (tail_ptr.disp != nil.disp) {
181 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
183 MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
184 tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
186 MPI_Win_unlock(tail_ptr.rank, llist_win);
188 tail_ptr = elem.next;
190 /* This is not the root */
192 assert(elem.value >= 0 && elem.value < nproc);
193 counts[elem.value]++;
197 int last_elem = tail_ptr.disp == nil.disp;
198 printf("%2d%s", elem.value, last_elem ? "" : " -> ");
199 if (count % ELEM_PER_ROW == 0 && !last_elem)
204 /* This is the root */
206 assert(elem.value == -1);
214 /* Verify the counts we collected */
215 for (i = 0; i < nproc; i++) {
216 int expected = NUM_ELEMS;
218 if (counts[i] != expected) {
219 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i],
225 printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
229 MPI_Win_free(&llist_win);
231 /* Free all the elements in the list */
232 for (; my_elems_count > 0; my_elems_count--)
233 MPI_Free_mem(my_elems[my_elems_count - 1]);