Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug some leak by delete last message before leaving
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / linked_list.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 /*            MPI-3 distributed linked list construction example
9  *            --------------------------------------------------
10  *
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
17  * attached.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <mpi.h>
23 #include <assert.h>
24 #include "mpitest.h"
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #define NUM_ELEMS 32
31 #define NPROBE    100
32 #define ELEM_PER_ROW 16
33
34 /* Linked list pointer */
35 typedef struct {
36     int rank;
37     MPI_Aint disp;
38 } llist_ptr_t;
39
40 /* Linked list element */
41 typedef struct {
42     int value;
43     llist_ptr_t next;
44 } llist_elem_t;
45
46 static const llist_ptr_t nil = { -1, (MPI_Aint) MPI_BOTTOM };
47
48 static const int verbose = 0;
49
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;
54
55 /* Allocate a new shared linked list element */
56 MPI_Aint alloc_elem(int value, MPI_Win win)
57 {
58     MPI_Aint disp;
59     llist_elem_t *elem_ptr;
60
61     /* Allocate the new element and register it with the window */
62     MPI_Alloc_mem(sizeof(llist_elem_t), MPI_INFO_NULL, &elem_ptr);
63     elem_ptr->value = value;
64     elem_ptr->next = nil;
65     MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
66
67     /* Add the element to the list of local elements so we can free it later. */
68     if (my_elems_size == my_elems_count) {
69         my_elems_size += 100;
70         my_elems = realloc(my_elems, my_elems_size * sizeof(void *));
71     }
72     my_elems[my_elems_count] = elem_ptr;
73     my_elems_count++;
74
75     MPI_Get_address(elem_ptr, &disp);
76     return disp;
77 }
78
79 int main(int argc, char **argv)
80 {
81     int procid, nproc, i;
82     MPI_Win llist_win;
83     llist_ptr_t head_ptr, tail_ptr;
84
85     MPI_Init(&argc, &argv);
86
87     MPI_Comm_rank(MPI_COMM_WORLD, &procid);
88     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
89
90     MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
91
92     /* Process 0 creates the head node */
93     if (procid == 0)
94         head_ptr.disp = alloc_elem(-1, llist_win);
95
96     /* Broadcast the head pointer to everyone */
97     head_ptr.rank = 0;
98     MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
99     tail_ptr = head_ptr;
100
101     /* All processes concurrently append NUM_ELEMS elements to the list */
102     for (i = 0; i < NUM_ELEMS; i++) {
103         llist_ptr_t new_elem_ptr;
104         int success;
105
106         /* Create a new list element and register it with the window */
107         new_elem_ptr.rank = procid;
108         new_elem_ptr.disp = alloc_elem(procid, llist_win);
109
110         /* Append the new node to the list.  This might take multiple attempts if
111          * others have already appended and our tail pointer is stale. */
112         do {
113             llist_ptr_t next_tail_ptr = nil;
114
115             MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
116
117             MPI_Compare_and_swap((void *) &new_elem_ptr.rank, (void *) &nil.rank,
118                                  (void *) &next_tail_ptr.rank, MPI_INT, tail_ptr.rank,
119                                  (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.rank),
120                                  llist_win);
121
122             MPI_Win_unlock(tail_ptr.rank, llist_win);
123             success = (next_tail_ptr.rank == nil.rank);
124
125             if (success) {
126                 int i, flag;
127
128                 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
129
130                 MPI_Put(&new_elem_ptr.disp, 1, MPI_AINT, tail_ptr.rank,
131                         (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.disp), 1,
132                         MPI_AINT, llist_win);
133
134                 MPI_Win_unlock(tail_ptr.rank, llist_win);
135                 tail_ptr = new_elem_ptr;
136
137                 /* For implementations that use pt-to-pt messaging, force progress for other threads'
138                  * RMA operations. */
139                 for (i = 0; i < NPROBE; i++)
140                     MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag,
141                                MPI_STATUS_IGNORE);
142
143             }
144             else {
145                 /* Tail pointer is stale, fetch the displacement.  May take multiple tries
146                  * if it is being updated. */
147                 do {
148                     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
149
150                     MPI_Get(&next_tail_ptr.disp, 1, MPI_AINT, tail_ptr.rank,
151                             (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next.disp),
152                             1, MPI_AINT, llist_win);
153
154                     MPI_Win_unlock(tail_ptr.rank, llist_win);
155                 } while (next_tail_ptr.disp == nil.disp);
156                 tail_ptr = next_tail_ptr;
157             }
158         } while (!success);
159     }
160
161     MPI_Barrier(MPI_COMM_WORLD);
162
163     /* Traverse the list and verify that all processes inserted exactly the correct
164      * number of elements. */
165     if (procid == 0) {
166         int have_root = 0;
167         int errors = 0;
168         int *counts, count = 0;
169
170         counts = (int *) malloc(sizeof(int) * nproc);
171         assert(counts != NULL);
172
173         for (i = 0; i < nproc; i++)
174             counts[i] = 0;
175
176         tail_ptr = head_ptr;
177
178         /* Walk the list and tally up the number of elements inserted by each rank */
179         while (tail_ptr.disp != nil.disp) {
180             llist_elem_t elem;
181
182             MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
183
184             MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
185                     tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
186
187             MPI_Win_unlock(tail_ptr.rank, llist_win);
188
189             tail_ptr = elem.next;
190
191             /* This is not the root */
192             if (have_root) {
193                 assert(elem.value >= 0 && elem.value < nproc);
194                 counts[elem.value]++;
195                 count++;
196
197                 if (verbose) {
198                     int last_elem = tail_ptr.disp == nil.disp;
199                     printf("%2d%s", elem.value, last_elem ? "" : " -> ");
200                     if (count % ELEM_PER_ROW == 0 && !last_elem)
201                         printf("\n");
202                 }
203             }
204
205             /* This is the root */
206             else {
207                 assert(elem.value == -1);
208                 have_root = 1;
209             }
210         }
211
212         if (verbose)
213             printf("\n\n");
214
215         /* Verify the counts we collected */
216         for (i = 0; i < nproc; i++) {
217             int expected = NUM_ELEMS;
218
219             if (counts[i] != expected) {
220                 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i],
221                        expected);
222                 errors++;
223             }
224         }
225
226         printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
227         free(counts);
228     }
229
230     MPI_Win_free(&llist_win);
231
232     /* Free all the elements in the list */
233     for (; my_elems_count > 0; my_elems_count--)
234         MPI_Free_mem(my_elems[my_elems_count - 1]);
235
236     MPI_Finalize();
237     return 0;
238 }