Debian Codegraph @

src/mpid/ch3/channels/nemesis/include/mpid_nem_inline.h

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #ifndef MPID_NEM_INLINE_H_INCLUDED
7 #define MPID_NEM_INLINE_H_INCLUDED
8
9 #include "my_papi_defs.h"
10 #include "mpl.h"
11 #include "mpidi_nem_statistics.h"
12 #include "mpit.h"
13
14 extern int MPID_nem_lmt_shm_pending;
15 extern MPID_nem_cell_ptr_t MPID_nem_prefetched_cell;
16
17 static inline int MPID_nem_mpich_send_header (void* buf, int size, MPIDI_VC_t *vc, int *again);
18 static inline int MPID_nem_mpich_sendv (struct iovec **iov, int *n_iov, MPIDI_VC_t *vc, int *again);
19 static inline void MPID_nem_mpich_dequeue_fastbox (int local_rank);
20 static inline void MPID_nem_mpich_enqueue_fastbox (int local_rank);
21 static inline int MPID_nem_mpich_sendv_header (struct iovec **iov, int *n_iov, MPIDI_VC_t *vc, int *again);
22 static inline int MPID_nem_recv_seqno_matches (MPID_nem_queue_ptr_t qhead);
23 static inline int MPID_nem_mpich_test_recv (MPID_nem_cell_ptr_t *cell, int *in_fbox, int in_blocking_progress);
24 static inline int MPID_nem_mpich_blocking_recv (MPID_nem_cell_ptr_t *cell, int *in_fbox, int completions);
25 static inline int MPID_nem_mpich_test_recv_wait (MPID_nem_cell_ptr_t *cell, int *in_fbox, int timeout);
26 static inline int MPID_nem_mpich_release_cell (MPID_nem_cell_ptr_t cell, MPIDI_VC_t *vc);
27 static inline void MPID_nem_mpich_send_seg_header (void *buf, MPI_Aint count, MPI_Datatype datatype,
28 intptr_t *msg_offset,
29 intptr_t msgsize, void *header, intptr_t header_sz,
30 MPIDI_VC_t *vc, int *again);
31 static inline void MPID_nem_mpich_send_seg (void *buf, MPI_Aint count, MPI_Datatype datatype,
32 intptr_t *msg_offset, intptr_t msgsize,
33 MPIDI_VC_t *vc, int *again);
34
35
36 /*
37 === BEGIN_MPI_T_CVAR_INFO_BLOCK ===
38
39 cvars:
40 - name : MPIR_CVAR_POLLS_BEFORE_YIELD
41 category : NEMESIS
42 type : int
43 default : 1000
44 class : none
45 verbosity : MPI_T_VERBOSITY_USER_BASIC
46 scope : MPI_T_SCOPE_ALL_EQ
47 description : >-
48 When MPICH is in a busy waiting loop, it will periodically
49 call a function to yield the processor. This cvar sets
50 the number of loops before the yield function is called. A
51 value of 0 disables yielding.
52
53 === END_MPI_T_CVAR_INFO_BLOCK ===
54 */
55
56 /* -------------------------------------------------------------------------- */
57
58 /*
59 * MPIU_Busy_wait()
60 *
61 * Call this in every busy wait loop to periodically yield the processor. The
62 * MPIR_CVAR_POLLS_BEFORE_YIELD parameter can be used to adjust the number of
63 * times MPIU_Busy_wait is called before the yield function is called.
64 */
65 #ifdef USE_NOTHING_FOR_YIELD
66 /* optimize if we're not yielding the processor */
67 #define MPIU_Busy_wait() do {} while (0)
68 #else
69 /* MT: Updating the static int poll_count variable isn't thread safe and will
70 need to be changed for fine-grained multithreading. A possible alternative
71 is to make it a global thread-local variable. */
72 #define MPIU_Busy_wait() do { \
73 if (MPIR_CVAR_POLLS_BEFORE_YIELD) { \
74 static int poll_count_ = 0; \
75 if (poll_count_ >= MPIR_CVAR_POLLS_BEFORE_YIELD) { \
76 poll_count_ = 0; \
77 MPID_Thread_yield(); \
78 } else { \
79 ++poll_count_; \
80 } \
81 } \
82 } while (0)
83 #endif
84
85 /* evaluates to TRUE if it is safe to block on recv operations in the progress
86 * loop, FALSE otherwise */
87 #define MPID_nem_safe_to_block_recv() \
88 (!MPID_nem_local_lmt_pending && \
89 !MPIDI_CH3I_shm_active_send && \
90 !MPIDI_CH3I_Sendq_head(MPIDI_CH3I_shm_sendq) && \
91 !MPIDU_Sched_are_pending() && \
92 MPIR_Coll_safe_to_block() && \
93 !MPIDI_RMA_Win_active_list_head)
94
95 static inline int
96 MPID_nem_mpich_send_header (void* buf, int size, MPIDI_VC_t *vc, int *again)
97 {
98 int mpi_errno = MPI_SUCCESS;
99 MPID_nem_cell_ptr_t el;
100 int my_rank;
101 MPIDI_CH3I_VC *vc_ch = &vc->ch;
102
103 MPIR_FUNC_ENTER;
104
105 /*DO_PAPI (PAPI_reset (PAPI_EventSet)); */
106
107 MPIR_Assert (size == sizeof(MPIDI_CH3_Pkt_t));
108 MPIR_Assert (vc_ch->is_local);
109
110 my_rank = MPID_nem_mem_region.rank;
111
112 #ifdef USE_FASTBOX
113 {
114 MPID_nem_fastbox_t *pbox = vc_ch->fbox_out;
115 MPID_nem_cell_t *cell_ptr = MPID_NEM_FBOX_TO_CELL(pbox);
116
117 /* _is_full contains acquire barrier */
118 if (MPID_nem_fbox_is_full(pbox))
119 goto usequeue_l;
120
121 cell_ptr->header.source = MPID_nem_mem_region.local_rank;
122 cell_ptr->header.datalen = size;
123 cell_ptr->header.seqno = vc_ch->send_seqno++;
124
125 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, cell_ptr->header.type = MPID_NEM_PKT_MPICH_HEAD);
126
127 MPIR_Memcpy((void *)cell_ptr->payload, buf, size);
128
129 MPL_atomic_release_store_int(&pbox->flag, 1);
130
131 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent fbox ");
132 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (cell_ptr));
133
134 goto return_success;
135 }
136 usequeue_l:
137 MPIR_T_PVAR_COUNTER_INC_VAR(NEM, &MPID_nem_fbox_fall_back_to_queue_count[MPID_nem_mem_region.local_ranks[vc->lpid]], 1);
138
139 #endif /*USE_FASTBOX */
140
141 #ifdef PREFETCH_CELL
142 DO_PAPI (PAPI_reset (PAPI_EventSet));
143 el = MPID_nem_prefetched_cell;
144
145 if (!el)
146 {
147 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
148 goto return_again;
149
150 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
151 }
152 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues14));
153 #else /* PREFETCH_CELL */
154 DO_PAPI (PAPI_reset (PAPI_EventSet));
155 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
156 {
157 goto return_again;
158 }
159 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues14));
160
161 DO_PAPI (PAPI_reset (PAPI_EventSet));
162 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ , &el);
163 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues10));
164 #endif /* PREFETCH_CELL */
165
166 DO_PAPI (PAPI_reset (PAPI_EventSet));
167 el->header.source = my_rank;
168 el->header.dest = vc->lpid;
169 el->header.datalen = size;
170 el->header.seqno = vc_ch->send_seqno++;
171 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, el->header.type = MPID_NEM_PKT_MPICH_HEAD);
172
173 MPIR_Memcpy((void *)el->payload, buf, size);
174 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues11));
175
176 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent queue");
177 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (el));
178
179 DO_PAPI (PAPI_reset (PAPI_EventSet));
180 MPID_nem_queue_enqueue (vc_ch->recv_queue, el);
181 /*MPID_nem_rel_dump_queue( vc_ch->recv_queue ); */
182 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues12));
183 DO_PAPI (PAPI_reset (PAPI_EventSet));
184
185 #ifdef PREFETCH_CELL
186 DO_PAPI (PAPI_reset (PAPI_EventSet));
187 if (!MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
188 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &MPID_nem_prefetched_cell);
189 else
190 MPID_nem_prefetched_cell = 0;
191 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues10));
192 #endif /*PREFETCH_CELL */
193
194 /*DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues14)); */
195
196 return_success:
197 *again = 0;
198 goto fn_exit;
199 return_again:
200 *again = 1;
201 goto fn_exit;
202 fn_exit:
203 MPIR_FUNC_EXIT;
204 return mpi_errno;
205 }
206
207
208 /*
209 int MPID_nem_mpich_sendv (struct iovec **iov, int *n_iov, MPIDI_VC_t *vc);
210
211 sends iov to vc
212 Non-blocking
213 if iov specifies more than MPID_NEM_MPICH_DATA_LEN of data, the iov will be truncated, so that after MPID_nem_mpich_sendv returns,
214 iov will describe unsent data
215 sets again to 1 if it can't get a free cell, 0 otherwise
216 */
217 static inline int
218 MPID_nem_mpich_sendv (struct iovec **iov, int *n_iov, MPIDI_VC_t *vc, int *again)
219 {
220 int mpi_errno = MPI_SUCCESS;
221 MPID_nem_cell_ptr_t el;
222 char *cell_buf;
223 intptr_t payload_len;
224 int my_rank;
225 MPIDI_CH3I_VC *vc_ch = &vc->ch;
226
227 MPIR_FUNC_ENTER;
228
229 MPIR_Assert (*n_iov > 0 && (*iov)->iov_len > 0);
230 MPIR_Assert(vc_ch->is_local);
231
232 DO_PAPI (PAPI_reset (PAPI_EventSet));
233
234 my_rank = MPID_nem_mem_region.rank;
235
236 #ifdef PREFETCH_CELL
237 el = MPID_nem_prefetched_cell;
238
239 if (!el)
240 {
241 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
242 {
243 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
244 goto return_again;
245 }
246
247 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
248 }
249 #else /*PREFETCH_CELL */
250 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
251 {
252 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
253 goto return_again;
254 }
255
256 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ , &el);
257 #endif /*PREFETCH_CELL */
258
259 payload_len = MPID_NEM_MPICH_DATA_LEN;
260 cell_buf = (char *) el->payload; /* cast away volatile */
261
262 while (*n_iov && payload_len >= (*iov)->iov_len)
263 {
264 size_t _iov_len = (*iov)->iov_len;
265 MPIR_Memcpy (cell_buf, (*iov)->iov_base, _iov_len);
266 payload_len -= _iov_len;
267 cell_buf += _iov_len;
268 --(*n_iov);
269 ++(*iov);
270 }
271
272 if (*n_iov && payload_len > 0)
273 {
274 MPIR_Memcpy (cell_buf, (*iov)->iov_base, payload_len);
275 (*iov)->iov_base = (char *)(*iov)->iov_base + payload_len;
276 (*iov)->iov_len -= payload_len;
277 payload_len = 0;
278 }
279
280 el->header.source = my_rank;
281 el->header.dest = vc->lpid;
282 el->header.datalen = MPID_NEM_MPICH_DATA_LEN - payload_len;
283 el->header.seqno = vc_ch->send_seqno++;
284 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, el->header.type = MPID_NEM_PKT_MPICH);
285
286 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent queue");
287 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (el));
288
289 MPID_nem_queue_enqueue (vc_ch->recv_queue, el);
290 /*MPID_nem_rel_dump_queue( vc_ch->recv_queue ); */
291
292 #ifdef PREFETCH_CELL
293 if (!MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
294 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &MPID_nem_prefetched_cell);
295 else
296 MPID_nem_prefetched_cell = 0;
297 #endif /*PREFETCH_CELL */
298 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
299
300 *again = 0;
301 goto fn_exit;
302 return_again:
303 *again = 1;
304 goto fn_exit;
305 fn_exit:
306 MPIR_FUNC_EXIT;
307 return mpi_errno;
308 }
309
310 /* MPID_nem_mpich_sendv_header (struct iovec **iov, int *n_iov, int dest)
311 same as above but first iov element is an MPICH header */
312 static inline int
313 MPID_nem_mpich_sendv_header (struct iovec **iov, int *n_iov, MPIDI_VC_t *vc, int *again)
314 {
315 int mpi_errno = MPI_SUCCESS;
316 MPID_nem_cell_ptr_t el;
317 char *cell_buf;
318 intptr_t payload_len;
319 int my_rank;
320 MPIDI_CH3I_VC *vc_ch = &vc->ch;
321 MPI_Aint buf_offset = 0;
322
323 MPIR_FUNC_ENTER;
324
325 MPIR_Assert(vc_ch->is_local);
326
327 DO_PAPI (PAPI_reset (PAPI_EventSet));
328 MPIR_Assert (*n_iov > 0 && (*iov)->iov_len == sizeof(MPIDI_CH3_Pkt_t));
329
330 my_rank = MPID_nem_mem_region.rank;
331
332 #ifdef USE_FASTBOX
333 if (*n_iov == 2 && (*iov)[1].iov_len + sizeof(MPIDI_CH3_Pkt_t) <= MPID_NEM_FBOX_DATALEN)
334 {
335 MPID_nem_fastbox_t *pbox = vc_ch->fbox_out;
336 MPID_nem_cell_t *cell_ptr = MPID_NEM_FBOX_TO_CELL(pbox);
337
338 if (MPID_nem_fbox_is_full(pbox))
339 goto usequeue_l;
340
341 cell_ptr->header.source = MPID_nem_mem_region.local_rank;
342 cell_ptr->header.datalen = (*iov)[1].iov_len + sizeof(MPIDI_CH3_Pkt_t);
343 cell_ptr->header.seqno = vc_ch->send_seqno++;
344 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, cell_ptr->header.type = MPID_NEM_PKT_MPICH_HEAD);
345
346 MPIR_Memcpy((void *)cell_ptr->payload, (*iov)[0].iov_base, (*iov)[0].iov_len);
347 MPIR_Memcpy ((char *)cell_ptr->payload + (*iov)[0].iov_len, (*iov)[1].iov_base, (*iov)[1].iov_len);
348
349 MPL_atomic_release_store_int(&pbox->flag, 1);
350 *n_iov = 0;
351
352 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent fbox ");
353 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (cell_ptr));
354
355 goto return_success;
356 }
357 usequeue_l:
358 MPIR_T_PVAR_COUNTER_INC_VAR(NEM, &MPID_nem_fbox_fall_back_to_queue_count[MPID_nem_mem_region.local_ranks[vc->lpid]], 1);
359
360 #endif /*USE_FASTBOX */
361
362 #ifdef PREFETCH_CELL
363 el = MPID_nem_prefetched_cell;
364
365 if (!el)
366 {
367 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
368 {
369 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
370 goto return_again;
371 }
372
373 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
374 }
375 #else /*PREFETCH_CELL */
376 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
377 {
378 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
379 goto return_again;
380 }
381
382 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
383 #endif /*PREFETCH_CELL */
384
385 MPIR_Memcpy((void *)el->payload, (*iov)->iov_base, sizeof(MPIDI_CH3_Pkt_t));
386 buf_offset += sizeof(MPIDI_CH3_Pkt_t);
387
388 cell_buf = (char *)(el->payload) + buf_offset;
389 ++(*iov);
390 --(*n_iov);
391
392 payload_len = MPID_NEM_MPICH_DATA_LEN - buf_offset;
393 while (*n_iov && payload_len >= (*iov)->iov_len)
394 {
395 size_t _iov_len = (*iov)->iov_len;
396 MPIR_Memcpy (cell_buf, (*iov)->iov_base, _iov_len);
397 payload_len -= _iov_len;
398 cell_buf += _iov_len;
399 --(*n_iov);
400 ++(*iov);
401 }
402
403 if (*n_iov && payload_len > 0)
404 {
405 MPIR_Memcpy (cell_buf, (*iov)->iov_base, payload_len);
406 (*iov)->iov_base = (char *)(*iov)->iov_base + payload_len;
407 (*iov)->iov_len -= payload_len;
408 payload_len = 0;
409 }
410
411 el->header.source = my_rank;
412 el->header.dest = vc->lpid;
413 el->header.datalen = MPID_NEM_MPICH_DATA_LEN - payload_len;
414 el->header.seqno = vc_ch->send_seqno++;
415 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, el->header.type = MPID_NEM_PKT_MPICH_HEAD);
416
417 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent queue");
418 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (el));
419
420 MPID_nem_queue_enqueue (vc_ch->recv_queue, el);
421 /*MPID_nem_rel_dump_queue( vc_ch->recv_queue ); */
422
423 #ifdef PREFETCH_CELL
424 if (!MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
425 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &MPID_nem_prefetched_cell);
426 else
427 MPID_nem_prefetched_cell = 0;
428 #endif /*PREFETCH_CELL */
429 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
430
431 return_success:
432 *again = 0;
433 goto fn_exit;
434 return_again:
435 *again = 1;
436 goto fn_exit;
437 fn_exit:
438 MPIR_FUNC_EXIT;
439 return mpi_errno;
440 }
441
442 /* send the header and data described by the segment in one cell. If
443 there is no cell available, *again is set to 1. If all of the data
444 cannot be sent, *msg_offset is set to the index of the first
445 unsent byte.
446 Pre condition: This must be the first packet of a message (i.e.,
447 *segment first == 0)
448 The destination process is local
449 Post conditions: the header has been sent iff *again == 0
450 if there is data to send (msgsize > 0) then
451 (the header has been sent iff any data has
452 been sent (i.e., *msg_offset > 0) )
453 i.e.: we will never send only the header
454 */
455 static inline void
456 MPID_nem_mpich_send_seg_header (void *buf, MPI_Aint count, MPI_Datatype datatype,
457 intptr_t *msg_offset, intptr_t msgsize,
458 void *header, intptr_t header_sz, MPIDI_VC_t *vc, int *again)
459 {
460 MPIR_FUNC_ENTER;
461
462 MPID_nem_cell_ptr_t el;
463 intptr_t datalen;
464 int my_rank;
465 MPIDI_CH3I_VC *vc_ch = &vc->ch;
466 MPI_Aint buf_offset = 0;
467
468 MPIR_Assert(vc_ch->is_local); /* netmods will have their own implementation */
469 MPIR_Assert(header_sz <= sizeof(MPIDI_CH3_Pkt_t));
470
471
472 DO_PAPI (PAPI_reset (PAPI_EventSet));
473
474 my_rank = MPID_nem_mem_region.rank;
475
476 #ifdef USE_FASTBOX
477 if (sizeof(MPIDI_CH3_Pkt_t) + msgsize <= MPID_NEM_FBOX_DATALEN)
478 {
479 MPID_nem_fastbox_t *pbox = vc_ch->fbox_out;
480 MPID_nem_cell_t *cell_ptr = MPID_NEM_FBOX_TO_CELL(pbox);
481
482 /* Add a compiler time check on streaming unit size and FASTBOX size */
483 MPIR_Static_assert((MPIDI_CH3U_Acc_stream_size > MPID_NEM_FBOX_DATALEN),
484 "RMA ACC Streaming unit size <= FASTBOX size in Nemesis.");
485
486 /* NOTE: when FASTBOX is being used, streaming optimization is never triggered,
487 * because streaming unit size is larger than FASTBOX size. In such case,
488 * first offset (*msg_offset) is zero, and last offset (msgsize)
489 * is the data size */
490 MPIR_Assert(*msg_offset == 0);
491
492 if (MPID_nem_fbox_is_full(pbox))
493 goto usequeue_l;
494
495 {
496 cell_ptr->header.source = MPID_nem_mem_region.local_rank;
497 cell_ptr->header.datalen = sizeof(MPIDI_CH3_Pkt_t) + msgsize;
498 cell_ptr->header.seqno = vc_ch->send_seqno++;
499 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, cell_ptr->header.type = MPID_NEM_PKT_MPICH_HEAD);
500
501 /* copy header */
502 MPIR_Memcpy((void *)cell_ptr->payload, header, header_sz);
503
504 /* copy data */
505 MPI_Aint actual_pack_bytes;
506 MPIR_Typerep_pack(buf, count, datatype, *msg_offset,
507 (char *)cell_ptr->payload + sizeof(MPIDI_CH3_Pkt_t),
508 msgsize - *msg_offset, &actual_pack_bytes, MPIR_TYPEREP_FLAG_NONE);
509 MPIR_Assert(actual_pack_bytes == msgsize - *msg_offset);
510
511 MPL_atomic_release_store_int(&pbox->flag, 1);
512
513 *msg_offset += actual_pack_bytes;;
514
515 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent fbox ");
516 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (cell_ptr));
517
518 goto return_success;
519 }
520 }
521 usequeue_l:
522 MPIR_T_PVAR_COUNTER_INC_VAR(NEM, &MPID_nem_fbox_fall_back_to_queue_count[MPID_nem_mem_region.local_ranks[vc->lpid]], 1);
523
524 #endif /*USE_FASTBOX */
525
526 #ifdef PREFETCH_CELL
527 el = MPID_nem_prefetched_cell;
528
529 if (!el)
530 {
531 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
532 {
533 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
534 goto return_again;
535 }
536
537 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
538 }
539 #else /*PREFETCH_CELL */
540 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
541 {
542 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
543 goto return_again;
544 }
545
546 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
547 #endif /*PREFETCH_CELL */
548
549 /* copy header */
550 MPIR_Memcpy((void *)el->payload, header, header_sz);
551
552 buf_offset += sizeof(MPIDI_CH3_Pkt_t);
553
554 /* copy data */
555 MPI_Aint max_pack_bytes;
556 if (msgsize - *msg_offset <= MPID_NEM_MPICH_DATA_LEN - buf_offset)
557 max_pack_bytes = msgsize - *msg_offset;
558 else
559 max_pack_bytes = MPID_NEM_MPICH_DATA_LEN - buf_offset;
560
561 MPI_Aint actual_pack_bytes;
562 MPIR_Typerep_pack(buf, count, datatype, *msg_offset, (char *)el->payload + buf_offset,
563 max_pack_bytes, &actual_pack_bytes, MPIR_TYPEREP_FLAG_NONE);
564 datalen = buf_offset + actual_pack_bytes;
565 *msg_offset += actual_pack_bytes;
566
567 el->header.source = my_rank;
568 el->header.dest = vc->lpid;
569 el->header.datalen = datalen;
570 el->header.seqno = vc_ch->send_seqno++;
571 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, el->header.type = MPID_NEM_PKT_MPICH_HEAD);
572
573 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent queue");
574 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (el));
575
576 MPID_nem_queue_enqueue (vc_ch->recv_queue, el);
577
578 #ifdef PREFETCH_CELL
579 if (!MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
580 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &MPID_nem_prefetched_cell);
581 else
582 MPID_nem_prefetched_cell = 0;
583 #endif /*PREFETCH_CELL */
584 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
585
586 return_success:
587 *again = 0;
588 goto fn_exit;
589 return_again:
590 *again = 1;
591 goto fn_exit;
592 fn_exit:
593 MPIR_FUNC_EXIT;
594 return;
595 }
596
597 /* similar to MPID_nem_mpich_send_seg_header, except there is no
598 header to send. This need not be the first packet of a message. */
599 static inline void
600 MPID_nem_mpich_send_seg (void *buf, MPI_Aint count, MPI_Datatype datatype,
601 intptr_t *msg_offset, intptr_t msgsize, MPIDI_VC_t *vc, int *again)
602 {
603 MPIR_FUNC_ENTER;
604
605 MPID_nem_cell_ptr_t el;
606 intptr_t datalen;
607 int my_rank;
608 MPIDI_CH3I_VC *vc_ch = &vc->ch;
609
610 MPIR_Assert(vc_ch->is_local); /* netmods will have their own implementation */
611
612 DO_PAPI (PAPI_reset (PAPI_EventSet));
613
614 my_rank = MPID_nem_mem_region.rank;
615
616 #ifdef PREFETCH_CELL
617 el = MPID_nem_prefetched_cell;
618
619 if (!el)
620 {
621 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
622 {
623 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
624 goto return_again;
625 }
626
627 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
628 }
629 #else /*PREFETCH_CELL */
630 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
631 {
632 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
633 goto return_again;
634 }
635
636 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &el);
637 #endif /*PREFETCH_CELL */
638
639 /* copy data */
640 MPI_Aint max_pack_bytes;
641 if (msgsize - *msg_offset <= MPID_NEM_MPICH_DATA_LEN)
642 max_pack_bytes = msgsize - *msg_offset;
643 else
644 max_pack_bytes = MPID_NEM_MPICH_DATA_LEN;
645
646 MPI_Aint actual_pack_bytes;
647 MPIR_Typerep_pack(buf, count, datatype, *msg_offset, (char *)el->payload,
648 max_pack_bytes, &actual_pack_bytes, MPIR_TYPEREP_FLAG_NONE);
649 datalen = actual_pack_bytes;
650 *msg_offset += actual_pack_bytes;
651
652 el->header.source = my_rank;
653 el->header.dest = vc->lpid;
654 el->header.datalen = datalen;
655 el->header.seqno = vc_ch->send_seqno++;
656 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, el->header.type = MPID_NEM_PKT_MPICH_HEAD);
657
658 MPL_DBG_MSG (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "--> Sent queue");
659 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (el));
660
661 MPID_nem_queue_enqueue (vc_ch->recv_queue, el);
662
663 #ifdef PREFETCH_CELL
664 if (!MPID_nem_queue_empty (MPID_nem_mem_region.my_freeQ))
665 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_freeQ, &MPID_nem_prefetched_cell);
666 else
667 MPID_nem_prefetched_cell = 0;
668 #endif /*PREFETCH_CELL */
669 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues5));
670
671 *again = 0;
672 goto fn_exit;
673 return_again:
674 *again = 1;
675 goto fn_exit;
676 fn_exit:
677 MPIR_FUNC_EXIT;
678 return;
679 }
680
681 /*
682 MPID_nem_mpich_dequeue_fastbox (int local_rank)
683 decrements usage count on fastbox for process with local rank local_rank and
684 dequeues it from fbox queue if usage is 0.
685 This function is called whenever a receive for a process on this node is matched.
686 Fastboxes on fbox queue are polled regularly for incoming messages.
687 */
688 static inline void MPID_nem_mpich_dequeue_fastbox(int local_rank)
689 {
690 MPID_nem_fboxq_elem_t *el;
691 MPIR_FUNC_ENTER;
692
693 MPIR_Assert(local_rank < MPID_nem_mem_region.num_local);
694
695 el = &MPID_nem_fboxq_elem_list[local_rank];
696 MPIR_Assert(el->fbox != NULL);
697
698 MPIR_Assert(el->usage);
699
700 --el->usage;
701 if (el->usage == 0)
702 {
703 if (el->prev == NULL)
704 MPID_nem_fboxq_head = el->next;
705 else
706 el->prev->next = el->next;
707
708 if (el->next == NULL)
709 MPID_nem_fboxq_tail = el->prev;
710 else
711 el->next->prev = el->prev;
712
713 if (el == MPID_nem_curr_fboxq_elem)
714 {
715 if (el->next == NULL)
716 MPID_nem_curr_fboxq_elem = MPID_nem_fboxq_head;
717 else
718 MPID_nem_curr_fboxq_elem = el->next;
719 }
720 }
721 MPIR_FUNC_EXIT;
722 }
723
724 /*
725 MPID_nem_mpich_enqueue_fastbox (int local_rank)
726 enqueues fastbox for process with local rank local_rank on fbox queue
727 This function is called whenever a receive is posted for a process on this node.
728 Fastboxes on fbox queue are polled regularly for incoming messages.
729 */
730 static inline void MPID_nem_mpich_enqueue_fastbox(int local_rank)
731 {
732 MPID_nem_fboxq_elem_t *el;
733 MPIR_FUNC_ENTER;
734
735 MPIR_Assert(local_rank < MPID_nem_mem_region.num_local);
736
737 el = &MPID_nem_fboxq_elem_list[local_rank];
738 MPIR_Assert(el->fbox != NULL);
739
740 if (el->usage)
741 {
742 ++el->usage;
743 }
744 else
745 {
746 el->usage = 1;
747 if (MPID_nem_fboxq_tail == NULL)
748 {
749 el->prev = NULL;
750 MPID_nem_curr_fboxq_elem = MPID_nem_fboxq_head = el;
751 }
752 else
753 {
754 el->prev = MPID_nem_fboxq_tail;
755 MPID_nem_fboxq_tail->next = el;
756 }
757
758 el->next = NULL;
759 MPID_nem_fboxq_tail = el;
760 }
761 MPIR_FUNC_EXIT;
762 }
763 /*
764 MPID_nem_recv_seqno_matches (MPID_nem_queue_ptr_t qhead)
765 check whether the sequence number for the cell at the head of qhead is the one
766 expected from the sender of that cell
767 We only check these for processes in COMM_WORLD (i.e. the ones initially allocated)
768 */
769 static inline int
770 MPID_nem_recv_seqno_matches (MPID_nem_queue_ptr_t qhead)
771 {
772 int source;
773 MPIR_FUNC_ENTER;
774
775 MPID_nem_cell_ptr_t cell = MPID_nem_queue_head(qhead);
776 source = cell->header.source;
777
778 MPIR_FUNC_EXIT;
779 return (cell->header.seqno == MPID_nem_recv_seqno[source]);
780 }
781
782 /*
783 int MPID_nem_mpich_test_recv (MPID_nem_cell_ptr_t *cell, int *in_fbox);
784
785 non-blocking receive
786 sets cell to the received cell, or NULL if there is nothing to receive. in_fbox is true iff the cell was found in a fbox
787 the cell must be released back to the subsystem with MPID_nem_mpich_release_cell() once the packet has been copied out
788 */
789 static inline int
790 MPID_nem_mpich_test_recv(MPID_nem_cell_ptr_t *cell, int *in_fbox, int in_blocking_progress)
791 {
792 int mpi_errno = MPI_SUCCESS;
793 MPIR_FUNC_ENTER;
794
795 DO_PAPI (PAPI_reset (PAPI_EventSet));
796
797 #ifdef USE_FASTBOX
798 if (poll_active_fboxes(cell)) goto fbox_l;
799 #endif/* USE_FASTBOX */
800
801 if (MPID_nem_num_netmods)
802 {
803 mpi_errno = MPID_nem_network_poll(in_blocking_progress);
804 if (mpi_errno) MPIR_ERR_POP (mpi_errno);
805 }
806
807 if (MPID_nem_queue_empty (MPID_nem_mem_region.my_recvQ) || !MPID_nem_recv_seqno_matches (MPID_nem_mem_region.my_recvQ))
808 {
809 #ifdef USE_FASTBOX
810 /* check for messages from any process (even those from which
811 we don't expect messages). If we're nonblocking, check all
812 fboxes at once, if we're in a blocking loop we'll keep
813 iterating, so just check them one at a time. */
814 if (!in_blocking_progress) {
815 int found;
816 found = poll_every_fbox(cell);
817 if (found)
818 goto fbox_l;
819 } else {
820 poll_next_fbox (cell, goto fbox_l);
821 }
822 #endif/* USE_FASTBOX */
823 *cell = NULL;
824 goto fn_exit;
825 }
826
827 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_recvQ, cell);
828
829 ++MPID_nem_recv_seqno[(*cell)->header.source];
830 *in_fbox = 0;
831
832 fn_exit:
833 DO_PAPI (PAPI_accum_var (PAPI_EventSet, PAPI_vvalues6));
834
835 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, {
836 if (*cell)
837 {
838 MPL_DBG_MSG_S (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "<-- Recv %s", (*in_fbox) ? "fbox " : "queue");
839 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (*cell));
840 }
841 });
842
843 fn_fail:
844 MPIR_FUNC_EXIT;
845 return mpi_errno;
846
847 fbox_l:
848 *in_fbox = 1;
849 goto fn_exit;
850
851 }
852
853 /*
854 int MPID_nem_mpich_test_recv_wait (MPID_nem_cell_ptr_t *cell, int *in_fbox, int timeout);
855
856 blocking receive with timeout
857 waits up to timeout iterations to receive a cell
858 sets cell to the received cell, or NULL if there is nothing to receive. in_fbox is true iff the cell was found in a fbox
859 the cell must be released back to the subsystem with MPID_nem_mpich_release_cell() once the packet has been copied out
860 */
861 static inline int
862 MPID_nem_mpich_test_recv_wait (MPID_nem_cell_ptr_t *cell, int *in_fbox, int timeout)
863 {
864 int mpi_errno = MPI_SUCCESS;
865 MPIR_FUNC_ENTER;
866 #ifdef USE_FASTBOX
867 if (poll_active_fboxes(cell)) goto fbox_l;
868 #endif/* USE_FASTBOX */
869
870 if (MPID_nem_num_netmods)
871 {
872 mpi_errno = MPID_nem_network_poll(TRUE /* blocking */);
873 if (mpi_errno) MPIR_ERR_POP (mpi_errno);
874 }
875
876 while ((--timeout > 0) && (MPID_nem_queue_empty (MPID_nem_mem_region.my_recvQ) || !MPID_nem_recv_seqno_matches (MPID_nem_mem_region.my_recvQ)))
877 {
878 #ifdef USE_FASTBOX
879 poll_next_fbox (cell, goto fbox_l);
880 #endif/* USE_FASTBOX */
881 *cell = NULL;
882 goto exit_l;
883 }
884
885 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_recvQ, cell);
886
887 ++MPID_nem_recv_seqno[(*cell)->header.source];
888 *in_fbox = 0;
889 exit_l:
890
891 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, {
892 if (*cell)
893 {
894 MPL_DBG_MSG_S (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "<-- Recv %s", (*in_fbox) ? "fbox " : "queue");
895 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell (*cell));
896 }
897 });
898
899 fn_fail:
900 MPIR_FUNC_EXIT;
901 return mpi_errno;
902
903 fbox_l:
904 *in_fbox = 1;
905 goto exit_l;
906 }
907
908 /*
909 int MPID_nem_mpich_blocking_recv (MPID_nem_cell_ptr_t *cell, int *in_fbox);
910
911 blocking receive waits until there is something to receive, or then
912 sets cell to the received cell. in_fbox is true iff the cell was
913 found in a fbox the cell must be released back to the subsystem with
914 MPID_nem_mpich_release_cell() once the packet has been copied out
915 */
916 static inline int
917 MPID_nem_mpich_blocking_recv(MPID_nem_cell_ptr_t *cell, int *in_fbox, int completions)
918 {
919 int mpi_errno = MPI_SUCCESS;
920 MPIR_FUNC_ENTER;
921 DO_PAPI (PAPI_reset (PAPI_EventSet));
922
923 /* We should never enter this function in a multithreaded app */
924 MPIR_Assert(!MPIR_IS_THREADED);
925
926 #ifdef USE_FASTBOX
927 if (poll_active_fboxes(cell)) goto fbox_l;
928 #endif /*USE_FASTBOX */
929
930 if (MPID_nem_num_netmods)
931 {
932 mpi_errno = MPID_nem_network_poll(TRUE /* blocking */);
933 if (mpi_errno) MPIR_ERR_POP (mpi_errno);
934 }
935
936 while (MPID_nem_queue_empty (MPID_nem_mem_region.my_recvQ) || !MPID_nem_recv_seqno_matches (MPID_nem_mem_region.my_recvQ))
937 {
938 DO_PAPI (PAPI_reset (PAPI_EventSet));
939
940 #ifdef USE_FASTBOX
941 poll_next_fbox (cell, goto fbox_l);
942 if (poll_active_fboxes(cell)) goto fbox_l;
943 #endif /*USE_FASTBOX */
944
945 if (MPID_nem_num_netmods)
946 {
947 mpi_errno = MPID_nem_network_poll(TRUE /* blocking */);
948 if (mpi_errno) MPIR_ERR_POP (mpi_errno);
949
950 if (!MPID_nem_safe_to_block_recv())
951 {
952 *cell = NULL;
953 *in_fbox = 0;
954 goto exit_l;
955 }
956 }
957
958 if (completions != MPL_atomic_relaxed_load_int(&MPIDI_CH3I_progress_completion_count)) {
959 *cell = NULL;
960 *in_fbox = 0;
961 goto exit_l;
962 }
963 MPIU_Busy_wait();
964 }
965
966 MPID_nem_queue_dequeue (MPID_nem_mem_region.my_recvQ, cell);
967
968 ++MPID_nem_recv_seqno[(*cell)->header.source];
969 *in_fbox = 0;
970
971 exit_l:
972
973 DO_PAPI (PAPI_accum_var (PAPI_EventSet,PAPI_vvalues8));
974
975 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, {
976 if (*cell)
977 {
978 MPL_DBG_MSG_S (MPIDI_CH3_DBG_CHANNEL, VERBOSE, "<-- Recv %s", (*in_fbox) ? "fbox " : "queue");
979 MPL_DBG_STMT (MPIDI_CH3_DBG_CHANNEL, VERBOSE, MPID_nem_dbg_dump_cell(*cell));
980 }
981 });
982
983 fn_fail:
984 MPIR_FUNC_EXIT;
985 return mpi_errno;
986
987 fbox_l:
988 *in_fbox = 1;
989 goto exit_l;
990 }
991
992 /*
993 int MPID_nem_mpich_release_cell (MPID_nem_cell_ptr_t cell, MPIDI_VC_t *vc);
994
995 releases the cell back to the subsystem to be used for subsequent receives
996 */
997 static inline int
998 MPID_nem_mpich_release_cell (MPID_nem_cell_ptr_t cell, MPIDI_VC_t *vc)
999 {
1000 int mpi_errno = MPI_SUCCESS;
1001 MPIR_FUNC_ENTER;
1002
1003 MPIDI_CH3I_VC *vc_ch = &vc->ch;
1004 DO_PAPI (PAPI_reset (PAPI_EventSet));
1005 MPID_nem_queue_enqueue (vc_ch->free_queue, cell);
1006 DO_PAPI (PAPI_accum_var (PAPI_EventSet,PAPI_vvalues9));
1007
1008 MPIR_FUNC_EXIT;
1009 return mpi_errno;
1010 }
1011
1012 #endif /* MPID_NEM_INLINE_H_INCLUDED */
1013