Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c52208e2ac177f17846bdd944c25c8be08c135c4
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / linked_list_bench_lock_excl.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2003 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /*            MPI-3 distributed linked list construction example
8  *            --------------------------------------------------
9  *
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.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <mpi.h>
19 #include <assert.h>
20 #include "mpitest.h"
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #define NUM_ELEMS 1000
27 #define MAX_NPROBE nproc
28 #define MIN_NPROBE 1
29 #define ELEM_PER_ROW 16
30
31 #define MIN(X,Y) ((X < Y) ? (X) : (Y))
32 #define MAX(X,Y) ((X > Y) ? (X) : (Y))
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 static const int print_perf = 0;
50
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;
55
56 /* Allocate a new shared linked list element */
57 MPI_Aint alloc_elem(int value, MPI_Win win)
58 {
59     MPI_Aint disp;
60     llist_elem_t *elem_ptr;
61
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;
65     elem_ptr->next = nil;
66     MPI_Win_attach(win, elem_ptr, sizeof(llist_elem_t));
67
68     /* Add the element to the list of local elements so we can free it later. */
69     if (my_elems_size == my_elems_count) {
70         my_elems_size += 100;
71         my_elems = realloc(my_elems, my_elems_size * sizeof(void *));
72     }
73     my_elems[my_elems_count] = elem_ptr;
74     my_elems_count++;
75
76     MPI_Get_address(elem_ptr, &disp);
77     return disp;
78 }
79
80 int main(int argc, char **argv)
81 {
82     int procid, nproc, i, j, my_nelem;
83     int pollint = 0;
84     double time;
85     MPI_Win llist_win;
86     llist_ptr_t head_ptr, tail_ptr;
87
88     MPI_Init(&argc, &argv);
89
90     MPI_Comm_rank(MPI_COMM_WORLD, &procid);
91     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
92
93     MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &llist_win);
94
95     /* Process 0 creates the head node */
96     if (procid == 0)
97         head_ptr.disp = alloc_elem(procid, llist_win);
98
99     /* Broadcast the head pointer to everyone */
100     head_ptr.rank = 0;
101     MPI_Bcast(&head_ptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
102     tail_ptr = head_ptr;
103
104     /* All processes append NUM_ELEMS elements to the list; rank 0 has already
105      * appended an element. */
106     if (procid == 0)
107         i = 1;
108     else
109         i = 0;
110     my_nelem = NUM_ELEMS / nproc;
111     if (procid < NUM_ELEMS % nproc)
112         my_nelem++;
113
114     MPI_Barrier(MPI_COMM_WORLD);
115     time = MPI_Wtime();
116
117     for (; i < my_nelem; i++) {
118         llist_ptr_t new_elem_ptr;
119         int success = 0;
120
121         MTEST_VG_MEM_INIT(&new_elem_ptr, sizeof(llist_ptr_t));
122
123         /* Create a new list element and register it with the window */
124         new_elem_ptr.rank = procid;
125         new_elem_ptr.disp = alloc_elem(procid, llist_win);
126
127         /* Append the new node to the list.  This might take multiple attempts if
128          * others have already appended and our tail pointer is stale. */
129         do {
130             int flag;
131
132             /* The tail is at my left neighbor, append my element. */
133             if (tail_ptr.rank == (procid + nproc - 1) % nproc) {
134                 if (verbose)
135                     printf("%d: Appending to <%d, %p>\n", procid, tail_ptr.rank,
136                            (void *) tail_ptr.disp);
137
138                 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
139 #if USE_ACC
140                 MPI_Accumulate(&new_elem_ptr, sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
141                                (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next),
142                                sizeof(llist_ptr_t), MPI_BYTE, MPI_REPLACE, llist_win);
143 #else
144                 MPI_Put(&new_elem_ptr, sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
145                         (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next), sizeof(llist_ptr_t),
146                         MPI_BYTE, llist_win);
147 #endif
148                 MPI_Win_unlock(tail_ptr.rank, llist_win);
149
150                 success = 1;
151                 tail_ptr = new_elem_ptr;
152             }
153
154             /* Otherwise, chase the tail. */
155             else {
156                 llist_ptr_t next_tail_ptr;
157
158                 MPI_Win_lock(MPI_LOCK_EXCLUSIVE, tail_ptr.rank, 0, llist_win);
159 #if USE_ACC
160                 MPI_Get_accumulate(NULL, 0, MPI_DATATYPE_NULL, &next_tail_ptr,
161                                    sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
162                                    (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next),
163                                    sizeof(llist_ptr_t), MPI_BYTE, MPI_NO_OP, llist_win);
164 #else
165                 MPI_Get(&next_tail_ptr, sizeof(llist_ptr_t), MPI_BYTE, tail_ptr.rank,
166                         (MPI_Aint) & (((llist_elem_t *) tail_ptr.disp)->next),
167                         sizeof(llist_ptr_t), MPI_BYTE, llist_win);
168 #endif
169                 MPI_Win_unlock(tail_ptr.rank, llist_win);
170
171                 if (next_tail_ptr.rank != nil.rank) {
172                     if (verbose)
173                         printf("%d: Chasing to <%d, %p>\n", procid, next_tail_ptr.rank,
174                                (void *) next_tail_ptr.disp);
175                     tail_ptr = next_tail_ptr;
176                     pollint = MAX(MIN_NPROBE, pollint / 2);
177                 }
178                 else {
179                     for (j = 0; j < pollint; j++)
180                         MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag,
181                                    MPI_STATUS_IGNORE);
182
183                     pollint = MIN(MAX_NPROBE, pollint * 2);
184                 }
185             }
186         } while (!success);
187     }
188
189     MPI_Barrier(MPI_COMM_WORLD);
190     time = MPI_Wtime() - time;
191
192     /* Traverse the list and verify that all processes inserted exactly the correct
193      * number of elements. */
194     if (procid == 0) {
195         int errors = 0;
196         int *counts, count = 0;
197
198         counts = (int *) malloc(sizeof(int) * nproc);
199         assert(counts != NULL);
200
201         for (i = 0; i < nproc; i++)
202             counts[i] = 0;
203
204         tail_ptr = head_ptr;
205
206         MPI_Win_lock_all(0, llist_win);
207
208         /* Walk the list and tally up the number of elements inserted by each rank */
209         while (tail_ptr.disp != nil.disp) {
210             llist_elem_t elem;
211
212             MPI_Get(&elem, sizeof(llist_elem_t), MPI_BYTE,
213                     tail_ptr.rank, tail_ptr.disp, sizeof(llist_elem_t), MPI_BYTE, llist_win);
214
215             MPI_Win_flush(tail_ptr.rank, llist_win);
216
217             tail_ptr = elem.next;
218
219             assert(elem.value >= 0 && elem.value < nproc);
220             counts[elem.value]++;
221             count++;
222
223             if (verbose) {
224                 int last_elem = tail_ptr.disp == nil.disp;
225                 printf("%2d%s", elem.value, last_elem ? "" : " -> ");
226                 if (count % ELEM_PER_ROW == 0 && !last_elem)
227                     printf("\n");
228             }
229         }
230
231         MPI_Win_unlock_all(llist_win);
232
233         if (verbose)
234             printf("\n\n");
235
236         /* Verify the counts we collected */
237         for (i = 0; i < nproc; i++) {
238             int expected;
239
240             expected = NUM_ELEMS / nproc;
241             if (i < NUM_ELEMS % nproc)
242                 expected++;
243
244             if (counts[i] != expected) {
245                 printf("Error: Rank %d inserted %d elements, expected %d\n", i, counts[i],
246                        expected);
247                 errors++;
248             }
249         }
250
251         printf("%s\n", errors == 0 ? " No Errors" : "FAIL");
252         free(counts);
253     }
254
255     if (print_perf) {
256         double max_time;
257
258         MPI_Reduce(&time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
259
260         if (procid == 0) {
261             printf("Total time = %0.2f sec, elem/sec = %0.2f, sec/elem = %0.2f usec\n", max_time,
262                    NUM_ELEMS / max_time, max_time / NUM_ELEMS * 1.0e6);
263         }
264     }
265
266     MPI_Win_free(&llist_win);
267
268     /* Free all the elements in the list */
269     for (; my_elems_count > 0; my_elems_count--)
270         MPI_Free_mem(my_elems[my_elems_count - 1]);
271
272     MPI_Finalize();
273     return 0;
274 }