OpenMPI  0.1.1
pml_ob1_sendreq.h
1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  * University Research and Technology
4  * Corporation. All rights reserved.
5  * Copyright (c) 2004-2013 The University of Tennessee and The University
6  * of Tennessee Research Foundation. All rights
7  * reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  * University of Stuttgart. All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  * All rights reserved.
12  * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.
13  * Copyright (c) 2011-2012 NVIDIA Corporation. All rights reserved.
14  * $COPYRIGHT$
15  *
16  * Additional copyrights may follow
17  *
18  * $HEADER$
19  */
20 
21 #ifndef OMPI_PML_OB1_SEND_REQUEST_H
22 #define OMPI_PML_OB1_SEND_REQUEST_H
23 
24 #include "ompi/mca/btl/btl.h"
27 #include "pml_ob1_comm.h"
28 #include "pml_ob1_hdr.h"
29 #include "pml_ob1_rdma.h"
30 #include "pml_ob1_rdmafrag.h"
31 #include "opal/datatype/opal_convertor.h"
32 #include "ompi/mca/bml/bml.h"
33 
34 BEGIN_C_DECLS
35 
36 typedef enum {
37  MCA_PML_OB1_SEND_PENDING_NONE,
38  MCA_PML_OB1_SEND_PENDING_SCHEDULE,
39  MCA_PML_OB1_SEND_PENDING_START
40 } mca_pml_ob1_send_pending_t;
41 
44  mca_bml_base_endpoint_t* req_endpoint;
45  ompi_ptr_t req_recv;
46  int32_t req_state;
47  int32_t req_lock;
48  bool req_throttle_sends;
49  size_t req_pipeline_depth;
50  size_t req_bytes_delivered;
51  uint32_t req_rdma_cnt;
52  mca_pml_ob1_send_pending_t req_pending;
53  opal_mutex_t req_send_range_lock;
54  opal_list_t req_send_ranges;
55  mca_pml_ob1_com_btl_t req_rdma[1];
56 };
58 
60 
63  uint64_t range_send_offset;
64  uint64_t range_send_length;
65  int range_btl_idx;
66  int range_btl_cnt;
67  mca_pml_ob1_com_btl_t range_btls[1];
68 };
71 
72 static inline bool lock_send_request(mca_pml_ob1_send_request_t *sendreq)
73 {
74  return OPAL_THREAD_ADD32(&sendreq->req_lock, 1) == 1;
75 }
76 
77 static inline bool unlock_send_request(mca_pml_ob1_send_request_t *sendreq)
78 {
79  return OPAL_THREAD_ADD32(&sendreq->req_lock, -1) == 0;
80 }
81 
82 static inline void
83 add_request_to_send_pending(mca_pml_ob1_send_request_t* sendreq,
84  const mca_pml_ob1_send_pending_t type,
85  const bool append)
86 {
87  opal_list_item_t *item = (opal_list_item_t*)sendreq;
88 
89  OPAL_THREAD_LOCK(&mca_pml_ob1.lock);
90  sendreq->req_pending = type;
91  if(append)
92  opal_list_append(&mca_pml_ob1.send_pending, item);
93  else
94  opal_list_prepend(&mca_pml_ob1.send_pending, item);
95 
96  OPAL_THREAD_UNLOCK(&mca_pml_ob1.lock);
97 }
98 
99 static inline mca_pml_ob1_send_request_t*
100 get_request_from_send_pending(mca_pml_ob1_send_pending_t *type)
101 {
103 
104  OPAL_THREAD_LOCK(&mca_pml_ob1.lock);
105  sendreq = (mca_pml_ob1_send_request_t*)
106  opal_list_remove_first(&mca_pml_ob1.send_pending);
107  if(sendreq) {
108  *type = sendreq->req_pending;
109  sendreq->req_pending = MCA_PML_OB1_SEND_PENDING_NONE;
110  }
111  OPAL_THREAD_UNLOCK(&mca_pml_ob1.lock);
112 
113  return sendreq;
114 }
115 
116 #define MCA_PML_OB1_SEND_REQUEST_ALLOC( comm, \
117  dst, \
118  sendreq, \
119  rc) \
120  { \
121  ompi_proc_t *proc = ompi_comm_peer_lookup( comm, dst ); \
122  ompi_free_list_item_t* item; \
123  \
124  rc = OMPI_ERR_OUT_OF_RESOURCE; \
125  if( OPAL_LIKELY(NULL != proc) ) { \
126  rc = OMPI_SUCCESS; \
127  OMPI_FREE_LIST_WAIT(&mca_pml_base_send_requests, item, rc); \
128  sendreq = (mca_pml_ob1_send_request_t*)item; \
129  sendreq->req_send.req_base.req_proc = proc; \
130  } \
131  }
132 
133 
134 #define MCA_PML_OB1_SEND_REQUEST_INIT( sendreq, \
135  buf, \
136  count, \
137  datatype, \
138  dst, \
139  tag, \
140  comm, \
141  sendmode, \
142  persistent) \
143  { \
144  MCA_PML_BASE_SEND_REQUEST_INIT(&sendreq->req_send, \
145  buf, \
146  count, \
147  datatype, \
148  dst, \
149  tag, \
150  comm, \
151  sendmode, \
152  persistent, \
153  0); /* convertor_flags */ \
154  (sendreq)->req_recv.pval = NULL; \
155  }
156 
157 
158 static inline void mca_pml_ob1_free_rdma_resources(mca_pml_ob1_send_request_t* sendreq)
159 {
160  size_t r;
161 
162  /* return mpool resources */
163  for(r = 0; r < sendreq->req_rdma_cnt; r++) {
164  mca_mpool_base_registration_t* reg = sendreq->req_rdma[r].btl_reg;
165  if( NULL != reg && reg->mpool != NULL ) {
166  reg->mpool->mpool_deregister(reg->mpool, reg);
167  }
168  }
169  sendreq->req_rdma_cnt = 0;
170 }
171 
172 
173 /**
174  * Start a send request.
175  */
176 
177 #define MCA_PML_OB1_SEND_REQUEST_START(sendreq, rc) \
178  do { \
179  rc = mca_pml_ob1_send_request_start(sendreq); \
180  } while (0)
181 
182 
183 /*
184  * Mark a send request as completed at the MPI level.
185  */
186 
187 #define MCA_PML_OB1_SEND_REQUEST_MPI_COMPLETE(sendreq, with_signal) \
188 do { \
189  (sendreq)->req_send.req_base.req_ompi.req_status.MPI_SOURCE = \
190  (sendreq)->req_send.req_base.req_comm->c_my_rank; \
191  (sendreq)->req_send.req_base.req_ompi.req_status.MPI_TAG = \
192  (sendreq)->req_send.req_base.req_tag; \
193  (sendreq)->req_send.req_base.req_ompi.req_status._ucount = \
194  (sendreq)->req_send.req_bytes_packed; \
195  ompi_request_complete( &((sendreq)->req_send.req_base.req_ompi), (with_signal) ); \
196  \
197  PERUSE_TRACE_COMM_EVENT( PERUSE_COMM_REQ_COMPLETE, \
198  &(sendreq->req_send.req_base), PERUSE_SEND); \
199 } while(0)
200 
201 /*
202  * Release resources associated with a request
203  */
204 
205 #define MCA_PML_OB1_SEND_REQUEST_RETURN(sendreq) \
206  do { \
207  /* Let the base handle the reference counts */ \
208  MCA_PML_BASE_SEND_REQUEST_FINI((&(sendreq)->req_send)); \
209  OMPI_FREE_LIST_RETURN( &mca_pml_base_send_requests, \
210  (ompi_free_list_item_t*)sendreq); \
211  } while(0)
212 
213 
214 /*
215  * The PML has completed a send request. Note that this request
216  * may have been orphaned by the user or have already completed
217  * at the MPI level.
218  * This function will never be called directly from the upper level, as it
219  * should only be an internal call to the PML.
220  *
221  */
222 static inline void
223 send_request_pml_complete(mca_pml_ob1_send_request_t *sendreq)
224 {
225  assert(false == sendreq->req_send.req_base.req_pml_complete);
226 
227  if(sendreq->req_send.req_bytes_packed > 0) {
228  PERUSE_TRACE_COMM_EVENT( PERUSE_COMM_REQ_XFER_END,
229  &(sendreq->req_send.req_base), PERUSE_SEND);
230  }
231 
232  /* return mpool resources */
233  mca_pml_ob1_free_rdma_resources(sendreq);
234 
235  if (sendreq->req_send.req_send_mode == MCA_PML_BASE_SEND_BUFFERED &&
236  sendreq->req_send.req_addr != sendreq->req_send.req_base.req_addr) {
237  mca_pml_base_bsend_request_fini((ompi_request_t*)sendreq);
238  }
239 
240  OPAL_THREAD_LOCK(&ompi_request_lock);
241  if(false == sendreq->req_send.req_base.req_ompi.req_complete) {
242  /* Should only be called for long messages (maybe synchronous) */
243  MCA_PML_OB1_SEND_REQUEST_MPI_COMPLETE(sendreq, true);
244  }
245  sendreq->req_send.req_base.req_pml_complete = true;
246 
247  if(sendreq->req_send.req_base.req_free_called) {
248  MCA_PML_OB1_SEND_REQUEST_RETURN(sendreq);
249  }
250  OPAL_THREAD_UNLOCK(&ompi_request_lock);
251 }
252 
253 /* returns true if request was completed on PML level */
254 static inline bool
255 send_request_pml_complete_check(mca_pml_ob1_send_request_t *sendreq)
256 {
257 #if OPAL_ENABLE_MULTI_THREADS
258  opal_atomic_rmb();
259 #endif
260  /* if no more events are expected for the request and the whole message is
261  * already sent and send fragment scheduling isn't running in another
262  * thread then complete the request on PML level. From now on, if user
263  * called free on this request, the request structure can be reused for
264  * another request or if the request is persistent it can be restarted */
265  if(sendreq->req_state == 0 &&
266  sendreq->req_bytes_delivered >= sendreq->req_send.req_bytes_packed
267  && lock_send_request(sendreq)) {
268  send_request_pml_complete(sendreq);
269  return true;
270  }
271 
272  return false;
273 }
274 
275 /**
276  * Schedule additional fragments
277  */
278 int
279 mca_pml_ob1_send_request_schedule_once(mca_pml_ob1_send_request_t*);
280 
281 static inline int
282 mca_pml_ob1_send_request_schedule_exclusive(mca_pml_ob1_send_request_t* sendreq)
283 {
284  int rc;
285  do {
286  rc = mca_pml_ob1_send_request_schedule_once(sendreq);
287  if(OPAL_SOS_GET_ERROR_CODE(rc) == OMPI_ERR_OUT_OF_RESOURCE)
288  break;
289  } while(!unlock_send_request(sendreq));
290 
291  if(OMPI_SUCCESS == rc)
292  send_request_pml_complete_check(sendreq);
293 
294  return rc;
295 }
296 
297 static inline void
298 mca_pml_ob1_send_request_schedule(mca_pml_ob1_send_request_t* sendreq)
299 {
300  /*
301  * Only allow one thread in this routine for a given request.
302  * However, we cannot block callers on a mutex, so simply keep track
303  * of the number of times the routine has been called and run through
304  * the scheduling logic once for every call.
305  */
306 
307  if(!lock_send_request(sendreq))
308  return;
309 
310  mca_pml_ob1_send_request_schedule_exclusive(sendreq);
311 }
312 
313 #if OMPI_CUDA_SUPPORT
314 int mca_pml_ob1_send_request_start_cuda(
315  mca_pml_ob1_send_request_t* sendreq,
316  mca_bml_base_btl_t* bml_btl,
317  size_t size);
318 #endif /* OMPI_CUDA_SUPPORT */
319 
320 /**
321  * Start the specified request
322  */
323 
324 int mca_pml_ob1_send_request_start_buffered(
326  mca_bml_base_btl_t* bml_btl,
327  size_t size);
328 
329 int mca_pml_ob1_send_request_start_copy(
331  mca_bml_base_btl_t* bml_btl,
332  size_t size);
333 
334 int mca_pml_ob1_send_request_start_prepare(
336  mca_bml_base_btl_t* bml_btl,
337  size_t size);
338 
339 int mca_pml_ob1_send_request_start_rdma(
341  mca_bml_base_btl_t* bml_btl,
342  size_t size);
343 
344 int mca_pml_ob1_send_request_start_rndv(
346  mca_bml_base_btl_t* bml_btl,
347  size_t size,
348  int flags);
349 
350 static inline int
351 mca_pml_ob1_send_request_start_btl( mca_pml_ob1_send_request_t* sendreq,
352  mca_bml_base_btl_t* bml_btl )
353 {
354  size_t size = sendreq->req_send.req_bytes_packed;
355  mca_btl_base_module_t* btl = bml_btl->btl;
356  size_t eager_limit = btl->btl_eager_limit - sizeof(mca_pml_ob1_hdr_t);
357  int rc;
358 
359  if( OPAL_LIKELY(size <= eager_limit) ) {
360  switch(sendreq->req_send.req_send_mode) {
361  case MCA_PML_BASE_SEND_SYNCHRONOUS:
362  rc = mca_pml_ob1_send_request_start_rndv(sendreq, bml_btl, size, 0);
363  break;
364  case MCA_PML_BASE_SEND_BUFFERED:
365  rc = mca_pml_ob1_send_request_start_copy(sendreq, bml_btl, size);
366  break;
367  case MCA_PML_BASE_SEND_COMPLETE:
368  rc = mca_pml_ob1_send_request_start_prepare(sendreq, bml_btl, size);
369  break;
370  default:
371  if (size != 0 && bml_btl->btl_flags & MCA_BTL_FLAGS_SEND_INPLACE) {
372  rc = mca_pml_ob1_send_request_start_prepare(sendreq, bml_btl, size);
373  } else {
374  rc = mca_pml_ob1_send_request_start_copy(sendreq, bml_btl, size);
375  }
376  break;
377  }
378  } else {
379  size = eager_limit;
380  if(OPAL_UNLIKELY(btl->btl_rndv_eager_limit < eager_limit))
381  size = btl->btl_rndv_eager_limit;
382  if(sendreq->req_send.req_send_mode == MCA_PML_BASE_SEND_BUFFERED) {
383  rc = mca_pml_ob1_send_request_start_buffered(sendreq, bml_btl, size);
384  } else if
385  (opal_convertor_need_buffers(&sendreq->req_send.req_base.req_convertor) == false) {
386  unsigned char *base;
387  opal_convertor_get_current_pointer( &sendreq->req_send.req_base.req_convertor, (void**)&base );
388 
389  if( 0 != (sendreq->req_rdma_cnt = (uint32_t)mca_pml_ob1_rdma_btls(
390  sendreq->req_endpoint,
391  base,
392  sendreq->req_send.req_bytes_packed,
393  sendreq->req_rdma))) {
394  rc = mca_pml_ob1_send_request_start_rdma(sendreq, bml_btl,
395  sendreq->req_send.req_bytes_packed);
396  if( OPAL_UNLIKELY(OMPI_SUCCESS != rc) ) {
397  mca_pml_ob1_free_rdma_resources(sendreq);
398  }
399  } else {
400  rc = mca_pml_ob1_send_request_start_rndv(sendreq, bml_btl, size,
401  MCA_PML_OB1_HDR_FLAGS_CONTIG);
402  }
403  } else {
404 #if OMPI_CUDA_SUPPORT
405  if (sendreq->req_send.req_base.req_convertor.flags & CONVERTOR_CUDA) {
406  return mca_pml_ob1_send_request_start_cuda(sendreq, bml_btl, size);
407  }
408 #endif /* OMPI_CUDA_SUPPORT */
409  rc = mca_pml_ob1_send_request_start_rndv(sendreq, bml_btl, size, 0);
410  }
411  }
412 
413  return rc;
414 }
415 
416 static inline int
417 mca_pml_ob1_send_request_start( mca_pml_ob1_send_request_t* sendreq )
418 {
419  mca_pml_ob1_comm_t* comm = sendreq->req_send.req_base.req_comm->c_pml_comm;
421  sendreq->req_send.req_base.req_proc->proc_bml;
422  size_t i;
423 
424  if( OPAL_UNLIKELY(endpoint == NULL) ) {
425  return OMPI_ERR_UNREACH;
426  }
427 
428  sendreq->req_endpoint = endpoint;
429  sendreq->req_state = 0;
430  sendreq->req_lock = 0;
431  sendreq->req_pipeline_depth = 0;
432  sendreq->req_bytes_delivered = 0;
433  sendreq->req_pending = MCA_PML_OB1_SEND_PENDING_NONE;
434  sendreq->req_send.req_base.req_sequence = OPAL_THREAD_ADD32(
435  &comm->procs[sendreq->req_send.req_base.req_peer].send_sequence,1);
436 
437  MCA_PML_BASE_SEND_START( &sendreq->req_send.req_base );
438 
439  for(i = 0; i < mca_bml_base_btl_array_get_size(&endpoint->btl_eager); i++) {
440  mca_bml_base_btl_t* bml_btl;
441  int rc;
442 
443  /* select a btl */
444  bml_btl = mca_bml_base_btl_array_get_next(&endpoint->btl_eager);
445  rc = mca_pml_ob1_send_request_start_btl(sendreq, bml_btl);
446  if( OPAL_LIKELY(OMPI_ERR_OUT_OF_RESOURCE != OPAL_SOS_GET_ERROR_CODE(rc)) )
447  return rc;
448  }
449  add_request_to_send_pending(sendreq, MCA_PML_OB1_SEND_PENDING_START, true);
450 
451  return OMPI_SUCCESS;
452 }
453 
454 /**
455  * Initiate a put scheduled by the receiver.
456  */
457 
458 void mca_pml_ob1_send_request_put( mca_pml_ob1_send_request_t* sendreq,
460  mca_pml_ob1_rdma_hdr_t* hdr );
461 
462 int mca_pml_ob1_send_request_put_frag(mca_pml_ob1_rdma_frag_t* frag);
463 
464 /* This function tries to continue sendreq that was stuck because of resource
465  * unavailability. A sendreq may be added to send_pending list if there is no
466  * resource to send initial packet or there is not resource to schedule data
467  * for sending. The reason the sendreq was added to the list is stored inside
468  * sendreq struct and appropriate operation is retried when resource became
469  * available. bml_btl passed to the function doesn't represents sendreq
470  * destination, it represents BTL on which resource was freed, so only this BTL
471  * should be considered for sending packets */
472 void mca_pml_ob1_send_request_process_pending(mca_bml_base_btl_t *bml_btl);
473 
474 void mca_pml_ob1_send_request_copy_in_out(mca_pml_ob1_send_request_t *sendreq,
475  uint64_t send_offset, uint64_t send_length);
476 
477 END_C_DECLS
478 
479 #endif /* OMPI_PML_OB1_SEND_REQUEST_H */
#define OPAL_THREAD_ADD32(x, y)
Use an atomic operation for increment/decrement if opal_using_threads() indicates that threads are in...
Definition: mutex.h:367
struct mca_bml_base_endpoint_t * proc_bml
BML specific proc data.
Definition: proc.h:64
Header used to initiate an RDMA operation.
Definition: pml_ob1_hdr.h:283
uint32_t btl_flags
support for put/get?
Definition: bml.h:59
void opal_atomic_rmb(void)
Read memory barrier.
struct ompi_communicator_t * req_comm
communicator pointer
Definition: pml_base_request.h:63
Definition: types.h:52
Cached on ompi_communicator_t to hold queues/state used by the PML<->PTL interface for matching logic...
Definition: pml_bfo_comm.h:51
Definition: mutex_unix.h:53
Definition: pml_ob1.h:296
size_t btl_rndv_eager_limit
the size of a data sent in a first fragment of rendezvous protocol
Definition: btl.h:791
int32_t send_sequence
send side sequence number
Definition: pml_bfo_comm.h:38
int32_t req_peer
peer process - rank w/in this communicator
Definition: pml_base_request.h:71
Definition: mpool.h:44
BML Management Layer (BML)
Definition: opal_list.h:98
#define OPAL_THREAD_LOCK(mutex)
Lock a mutex if opal_using_threads() says that multiple threads may be active in the process...
Definition: mutex.h:223
void * req_addr
pointer to send buffer - may not be application buffer
Definition: pml_base_sendreq.h:39
Definition: pml_ob1_rdmafrag.h:35
#define OPAL_THREAD_UNLOCK(mutex)
Unlock a mutex if opal_using_threads() says that multiple threads may be active in the process...
Definition: mutex.h:309
Union of defined hdr types.
Definition: pml_ob1_hdr.h:359
opal_convertor_t req_convertor
always need the convertor
Definition: pml_base_request.h:66
mca_pml_base_request_t req_base
base request type - common data structure for use by wait/test
Definition: pml_base_sendreq.h:38
Definition: pml_ob1_sendreq.h:42
#define opal_list_append(l, i)
Append an item to the end of the list.
Definition: opal_list.h:410
volatile bool req_pml_complete
flag indicating if the pt-2-pt layer is done with this request
Definition: pml_base_request.h:61
Byte Transfer Layer (BTL)
struct ompi_proc_t * req_proc
peer process
Definition: pml_base_request.h:73
mca_bml_base_btl_array_t btl_eager
array of btls to use for first fragments
Definition: bml.h:228
volatile bool req_complete
Flag indicating wether request has completed.
Definition: request.h:104
mca_mpool_base_module_deregister_fn_t mpool_deregister
deregister memory
Definition: mpool.h:181
Base type for send requests.
Definition: pml_base_sendreq.h:37
Definition: ompi_free_list.h:62
Structure associated w/ ompi_proc_t that contains the set of BTLs used to reach a destination...
Definition: bml.h:222
void * req_addr
pointer to application buffer
Definition: pml_base_request.h:69
#define MCA_PML_BASE_SEND_START(request)
Mark the request as started from the PML base point of view.
Definition: pml_base_sendreq.h:120
Definition: bml.h:58
Definition: opal_list.h:147
static opal_list_item_t * opal_list_remove_first(opal_list_t *list)
Remove the first item from the list and return it.
Definition: opal_list.h:522
static void opal_list_prepend(opal_list_t *list, opal_list_item_t *item)
Prepend an item to the beginning of the list.
Definition: opal_list.h:469
struct mca_btl_base_module_t * btl
BTL module.
Definition: bml.h:61
ompi_request_t req_ompi
base request
Definition: pml_base_request.h:60
Definition: pml_ob1_sendreq.h:61
static size_t mca_bml_base_btl_array_get_size(mca_bml_base_btl_array_t *array)
If required, reallocate (grow) the array to the indicate size.
Definition: bml.h:91
BTL module interface functions and attributes.
Definition: btl.h:786
size_t req_bytes_packed
packed size of a message given the datatype and count
Definition: pml_base_sendreq.h:40
uint32_t flags
the properties of this convertor
Definition: opal_convertor.h:93
mca_pml_base_send_mode_t req_send_mode
type of send
Definition: pml_base_sendreq.h:41
Main top-level request struct definition.
Definition: request.h:100
size_t btl_eager_limit
maximum size of first fragment – eager send
Definition: btl.h:790
#define OBJ_CLASS_DECLARATION(NAME)
Declaration for class descriptor.
Definition: opal_object.h:236
uint64_t req_sequence
sequence number for MPI pt-2-pt ordering
Definition: pml_base_request.h:74
volatile bool req_free_called
flag indicating if the user has freed this request
Definition: pml_base_request.h:65
static mca_bml_base_btl_t * mca_bml_base_btl_array_get_next(mca_bml_base_btl_array_t *array)
Return the next LRU index in the array.
Definition: bml.h:179