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 };
47 static const int verbose = 0;
48 static const int print_perf = 0;
50 /* List of locally allocated list elements. */
51 static llist_elem_t **my_elems = NULL;
52 static int my_elems_size = 0;
53 static int my_elems_count = 0;
55 /* Allocate a new shared linked list element */
56 MPI_Aint alloc_elem(int value, MPI_Win win) {
58 llist_elem_t *elem_ptr;
60 /* Allocate the new element and register it with the window */
61 MPI_Alloc_mem(sizeof(llist_elem_t), MPI_INFO_NULL, &elem_ptr);
62 elem_ptr->value = value;
64 MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
66 /* Add the element to the list of local elements so we can free it later. */
67 if (my_elems_size == my_elems_count) {
69 my_elems = realloc(my_elems, my_elems_size*sizeof(void*));
71 my_elems[my_elems_count] = elem_ptr;
74 MPI_Get_address(elem_ptr, &disp);
78 int main(int argc, char **argv) {
79 int procid, nproc, i, j, my_nelem;
83 llist_ptr_t head_ptr, tail_ptr;
85 MPI_Init(&argc, &argv);
87 MPI_Comm_rank(MPI_COMM_WORLD, &procid);
88 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
90 MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
92 /* Process 0 creates the head node */
94 head_ptr.disp = alloc_elem(procid, llist_win);
96 /* Broadcast the head pointer to everyone */
98 MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
101 /* All processes append NUM_ELEMS elements to the list; rank 0 has already
102 * appended an element. */
108 my_nelem = NUM_ELEMS/nproc;
109 if (procid < NUM_ELEMS % nproc)
112 MPI_Barrier(MPI_COMM_WORLD);
115 for ( ; i < my_nelem; i++) {
116 llist_ptr_t new_elem_ptr;
119 /* Create a new list element and register it with the window */
120 new_elem_ptr.rank = procid;
121 new_elem_ptr.disp = alloc_elem(procid, llist_win);
123 /* Append the new node to the list. This might take multiple attempts if
124 others have already appended and our tail pointer is stale. */
128 /* The tail is at my left neighbor, append my element. */
129 if (tail_ptr.rank == (procid + nproc-1) % nproc)
132 printf("%d: Appending to <%d, %p>\n", procid, tail_ptr.rank, (void*) tail_ptr.disp);
134 #ifdef USE_MODE_NOCHECK
135 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
137 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, 0, llist_win);
139 MPI_Accumulate(&new_elem_ptr, sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
140 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next), sizeof(llist_ptr_t),
141 MPI_BYTE, MPI_REPLACE, llist_win);
142 MPI_Win_unlock(tail_ptr.rank, llist_win);
145 tail_ptr = new_elem_ptr;
148 /* Otherwise, chase the tail. */
151 llist_ptr_t next_tail_ptr;
153 #ifdef USE_MODE_NOCHECK
154 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, MPI_MODE_NOCHECK, llist_win);
156 MPI_Win_lock(MPI_LOCK_SHARED, tail_ptr.rank, 0, llist_win);
158 MPI_Get_accumulate( NULL, 0, MPI_DATATYPE_NULL, &next_tail_ptr,
159 sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
160 (MPI_Aint) &(((llist_elem_t*)tail_ptr.disp)->next),
161 sizeof(llist_ptr_t), MPI_BYTE, MPI_NO_OP, llist_win);
162 MPI_Win_unlock(tail_ptr.rank, llist_win);
164 if (next_tail_ptr.rank != nil.rank) {
166 printf("%d: Chasing to <%d, %p>\n", procid, next_tail_ptr.rank, (void*) next_tail_ptr.disp);
167 tail_ptr = next_tail_ptr;
168 pollint = MAX(MIN_NPROBE, pollint/2);
171 for (j = 0; j < pollint; j++)
172 MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
174 pollint = MIN(MAX_NPROBE, pollint*2);
180 MPI_Barrier(MPI_COMM_WORLD);
181 time = MPI_Wtime() - time;
183 /* Traverse the list and verify that all processes inserted exactly the correct
184 number of elements. */
187 int *counts, count = 0;
189 counts = (int*) malloc(sizeof(int) * nproc);
190 assert(counts != NULL);
192 for (i = 0; i < nproc; i++)
197 MPI_Win_lock_all(0, llist_win);
199 /* Walk the list and tally up the number of elements inserted by each rank */
200 while (tail_ptr.disp != nil.disp) {
203 MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
204 tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
206 MPI_Win_flush(tail_ptr.rank, llist_win);
208 tail_ptr = elem.next;
210 assert(elem.value >= 0 && elem.value < nproc);
211 counts[elem.value]++;
215 int last_elem = tail_ptr.disp == nil.disp;
216 printf("%2d%s", elem.value, last_elem ? "" : " -> ");
217 if (count % ELEM_PER_ROW == 0 && !last_elem)
222 MPI_Win_unlock_all(llist_win);
227 /* Verify the counts we collected */
228 for (i = 0; i < nproc; i++) {
231 expected = NUM_ELEMS/nproc;
232 if (i < NUM_ELEMS % nproc)
235 if (counts[i] != expected) {
236 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i], expected);
241 printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
248 MPI_Reduce(&time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
251 printf("Total time = %0.2f sec, elem/sec = %0.2f, sec/elem = %0.2f usec\n", max_time, NUM_ELEMS/max_time, max_time/NUM_ELEMS*1.0e6);
255 MPI_Win_free(&llist_win);
257 /* Free all the elements in the list */
258 for ( ; my_elems_count > 0; my_elems_count--)
259 MPI_Free_mem(my_elems[my_elems_count-1]);