From efc7bf494c090152da62cab7cf8289ae34215aaf Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Thu, 30 Jul 2026 16:42:02 -0400 Subject: [PATCH 01/10] osc/rdma: notified communication support Implement the notified communication interface (MPI Standard section 12.6) in the osc/rdma component, which covers the libfabric/ofi BTL path: - MPI_PUT_NOTIFY / MPI_GET_NOTIFY, incrementing the target's notification counter with a btl atomic after the data movement has completed there - MPI_WIN_GET_NOTIFY_VALUE / MPI_WIN_RESET_NOTIFY_VALUE, using a btl compare-and-swap when it is not safe to mix CPU and btl atomics - MPI_WIN_SET_NUM_NOTIFY / MPI_WIN_GET_NUM_NOTIFY, publishing each rank's attached counter count to the group with an allgather A fixed region of OMPI_OSC_RDMA_NOTIFY_MAX counters is allocated in the window state so origins can update them with btl atomics; only the first notify_counts[rank] are considered attached. Signed-off-by: Joseph Antony Co-Authored-By: Claude Opus 5 --- ompi/mca/osc/rdma/osc_rdma.h | 27 ++++++ ompi/mca/osc/rdma/osc_rdma_comm.c | 119 +++++++++++++++++++++++ ompi/mca/osc/rdma/osc_rdma_comm.h | 8 ++ ompi/mca/osc/rdma/osc_rdma_component.c | 7 ++ ompi/mca/osc/rdma/osc_rdma_module.c | 126 +++++++++++++++++++++++++ ompi/mca/osc/rdma/osc_rdma_types.h | 18 ++++ 6 files changed, 305 insertions(+) diff --git a/ompi/mca/osc/rdma/osc_rdma.h b/ompi/mca/osc/rdma/osc_rdma.h index b18d2d7c4d0..6eec94b150e 100644 --- a/ompi/mca/osc/rdma/osc_rdma.h +++ b/ompi/mca/osc/rdma/osc_rdma.h @@ -224,6 +224,13 @@ struct ompi_osc_rdma_module_t { /** memory alignment to be used for new windows */ size_t memory_alignment; + /** per-rank count of notification counters attached at each process in + * the window group, as last published by MPI_WIN_SET_NUM_NOTIFY. NULL + * until the first call to MPI_WIN_SET_NUM_NOTIFY (no counters attached + * anywhere). used by origins to validate notification indices against + * the target's attached count */ + int *notify_counts; + /* ********************* sync data ************************ */ /** global sync object (PSCW, fence, lock all) */ @@ -325,6 +332,26 @@ OMPI_DECLSPEC extern ompi_osc_rdma_component_t mca_osc_rdma_component; int ompi_osc_rdma_free (struct ompi_win_t *win); +/* notified communication (MPI Standard section 12.6) */ + +/** validate a notification index against the number of counters attached at + * {rank} (as last published by MPI_WIN_SET_NUM_NOTIFY). the standard makes + * it erroneous to reference a counter that is out of range at the target. */ +#define OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, rank) \ + if (OPAL_UNLIKELY(NULL == (module)->notify_counts || (notify) < 0 || \ + (notify) >= (module)->notify_counts[(rank)])) { \ + return MPI_ERR_NOTIFY_IDX; \ + } + +int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, + OMPI_MPI_COUNT_TYPE *value); +int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, + OMPI_MPI_COUNT_TYPE *value); +int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t *info, + int num_notifications); +int ompi_osc_rdma_win_get_num_notify (struct ompi_win_t *win, int target_rank, + int *num_notifications); + /* peer functions */ diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.c b/ompi/mca/osc/rdma/osc_rdma_comm.c index 9b519e36eaf..e13ffbb77b6 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.c +++ b/ompi/mca/osc/rdma/osc_rdma_comm.c @@ -946,3 +946,122 @@ int ompi_osc_rdma_rget (void *origin_addr, size_t origin_count, ompi_datatype_t return OMPI_SUCCESS; } + +/* ******************* notified communication ******************* */ + +static void ompi_osc_rdma_notify_atomic_complete (void *cbdata, void *cbcontext, int status) +{ + ompi_osc_rdma_sync_t *sync = (ompi_osc_rdma_sync_t *) cbdata; + + (void) cbcontext; + (void) status; + + ompi_osc_rdma_sync_rdma_dec_always (sync); +} + +/** + * @brief atomically increment notification counter {notify} at {peer} + * + * The caller must ensure the data movement of the associated notified + * operation has completed at the target before calling this function + * (see ompi_osc_rdma_sync_rdma_complete). The increment is tracked + * against the sync object so that MPI_WIN_FLUSH (which must guarantee + * the notification is visible at the target) waits for it. + */ +static int ompi_osc_rdma_notify_increment (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, int notify) +{ + ompi_osc_rdma_module_t *module = sync->module; + uint64_t address = (uint64_t) (intptr_t) peer->state + offsetof (ompi_osc_rdma_state_t, notify_counters) + + (uint64_t) notify * sizeof (osc_rdma_counter_t); + int ret; + + /* order the counter update after any local stores made on behalf of the + * data movement (local peers are handled with memcpy) */ + opal_atomic_wmb (); + + if (ompi_osc_rdma_peer_local_state (peer)) { + /* the local state flag is only set when it is safe to mix CPU and btl atomics */ + (void) ompi_osc_rdma_counter_add ((osc_rdma_atomic_counter_t *) (intptr_t) address, 1); + return OMPI_SUCCESS; + } + + ompi_osc_rdma_sync_rdma_inc_always (sync); + + ret = ompi_osc_rdma_btl_op (module, peer->state_btl_index, peer->state_endpoint, address, + peer->state_handle, MCA_BTL_ATOMIC_ADD, 1, 0, false, + ompi_osc_rdma_notify_atomic_complete, (void *) sync, NULL); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + /* the completion callback will not be called */ + ompi_osc_rdma_sync_rdma_dec_always (sync); + } + + return ret; +} + +int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, + int target_rank, ptrdiff_t target_disp, size_t target_count, + ompi_datatype_t *target_datatype, int notify, ompi_win_t *win) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + ompi_osc_rdma_peer_t *peer; + ompi_osc_rdma_sync_t *sync; + int ret; + + OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "put_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", + (unsigned long) origin_addr, origin_count, origin_datatype->name, target_rank, + (int) target_disp, target_count, target_datatype->name, notify, win->w_name); + + OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, target_rank); + + sync = ompi_osc_rdma_module_sync_lookup (module, target_rank, &peer); + if (OPAL_UNLIKELY(NULL == sync)) { + return OMPI_ERR_RMA_SYNC; + } + + ret = ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, + target_count, target_datatype, NULL); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* the notification counter may only be updated at the target after the data movement + * has completed there (MPI standard section 12.6). osc/rdma equates btl-level completion + * with remote completion (the same assumption MPI_WIN_FLUSH relies on), so completing + * all outstanding RDMA on this sync orders the counter increment after the put */ + ompi_osc_rdma_sync_rdma_complete (sync); + + return ompi_osc_rdma_notify_increment (sync, peer, notify); +} + +int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, + int source_rank, ptrdiff_t source_disp, size_t source_count, + ompi_datatype_t *source_datatype, int notify, ompi_win_t *win) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + ompi_osc_rdma_peer_t *peer; + ompi_osc_rdma_sync_t *sync; + int ret; + + OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "get_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", + (unsigned long) origin_addr, origin_count, origin_datatype->name, source_rank, + (int) source_disp, source_count, source_datatype->name, notify, win->w_name); + + OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, source_rank); + + sync = ompi_osc_rdma_module_sync_lookup (module, source_rank, &peer); + if (OPAL_UNLIKELY(NULL == sync)) { + return OMPI_ERR_RMA_SYNC; + } + + ret = ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, + source_disp, source_count, source_datatype, NULL); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* completion of the get at the origin implies the window memory at the target has + * been accessed, which is the ordering requirement for the counter update */ + ompi_osc_rdma_sync_rdma_complete (sync); + + return ompi_osc_rdma_notify_increment (sync, peer, notify); +} diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.h b/ompi/mca/osc/rdma/osc_rdma_comm.h index 153633846a0..45e9bb5a91b 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.h +++ b/ompi/mca/osc/rdma/osc_rdma_comm.h @@ -99,6 +99,14 @@ int ompi_osc_rdma_rget (void *origin_addr, size_t origin_count, ompi_datatype_t ompi_datatype_t *target_dt, ompi_win_t *win, ompi_request_t **request); +int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi_datatype_t *origin_dt, + int target, ptrdiff_t target_disp, size_t target_count, + ompi_datatype_t *target_dt, int notify, ompi_win_t *win); + +int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datatype_t *origin_dt, + int target, ptrdiff_t target_disp, size_t target_count, + ompi_datatype_t *target_dt, int notify, ompi_win_t *win); + /** * @brief read data from a remote memory region (blocking) * diff --git a/ompi/mca/osc/rdma/osc_rdma_component.c b/ompi/mca/osc/rdma/osc_rdma_component.c index 14eeb928e40..6ebf144f3e2 100644 --- a/ompi/mca/osc/rdma/osc_rdma_component.c +++ b/ompi/mca/osc/rdma/osc_rdma_component.c @@ -133,6 +133,13 @@ ompi_osc_base_module_t ompi_osc_rdma_module_rdma_template = { .osc_raccumulate = ompi_osc_rdma_raccumulate, .osc_rget_accumulate = ompi_osc_rdma_rget_accumulate, + .osc_put_notify = ompi_osc_rdma_put_notify, + .osc_get_notify = ompi_osc_rdma_get_notify, + .osc_win_get_notify_value = ompi_osc_rdma_win_get_notify_value, + .osc_win_reset_notify_value = ompi_osc_rdma_win_reset_notify_value, + .osc_win_set_num_notify = ompi_osc_rdma_win_set_num_notify, + .osc_win_get_num_notify = ompi_osc_rdma_win_get_num_notify, + .osc_fence = ompi_osc_rdma_fence_atomic, .osc_start = ompi_osc_rdma_start_atomic, diff --git a/ompi/mca/osc/rdma/osc_rdma_module.c b/ompi/mca/osc/rdma/osc_rdma_module.c index 8a080e1e4eb..80d78c78f37 100644 --- a/ompi/mca/osc/rdma/osc_rdma_module.c +++ b/ompi/mca/osc/rdma/osc_rdma_module.c @@ -142,6 +142,7 @@ int ompi_osc_rdma_free(ompi_win_t *win) free (module->peer_array); free (module->outstanding_lock_array); + free (module->notify_counts); mca_mpool_base_default_module->mpool_free(mca_mpool_base_default_module, module->free_after); if (!module->use_accelerated_btl) { @@ -154,3 +155,128 @@ int ompi_osc_rdma_free(ompi_win_t *win) return OMPI_SUCCESS; } + +/* ******************* notified communication ******************* */ + +int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + int my_rank = ompi_comm_rank (module->comm); + + OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, my_rank); + + /* the standard's usage model is polling this function until a counter + * reaches a threshold. progress the module so counter updates arrive even + * when the btl requires target-side progress (emulated atomics) */ + ompi_osc_rdma_progress (module); + + *value = (OMPI_MPI_COUNT_TYPE) ((volatile osc_rdma_counter_t *) module->state->notify_counters)[notify]; + /* ensure loads of the window data are not reordered before the counter read */ + opal_atomic_rmb (); + + return OMPI_SUCCESS; +} + +int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + ompi_osc_rdma_peer_t *my_peer = module->my_peer; + int my_rank = ompi_comm_rank (module->comm); + ompi_osc_rdma_lock_t old_value; + int ret; + + OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, my_rank); + + /* the counter is incremented by remote origins with btl atomics, so the + * read-and-zero must be atomic with respect to those. use a CPU atomic + * only when the peer flags indicate it is safe to mix CPU and btl + * atomics, otherwise loop over a btl compare-and-swap on our own state */ + if (ompi_osc_rdma_peer_local_state (my_peer)) { + old_value = module->state->notify_counters[notify]; + while (!ompi_osc_rdma_lock_compare_exchange ((osc_rdma_atomic_counter_t *) (module->state->notify_counters + notify), + &old_value, 0)); + } else { + uint64_t address = (uint64_t) (intptr_t) my_peer->state + offsetof (ompi_osc_rdma_state_t, notify_counters) + + (uint64_t) notify * sizeof (osc_rdma_counter_t); + ompi_osc_rdma_lock_t result; + + /* the local value is a guess at the current value. if another origin + * updates the counter concurrently the compare-and-swap fails and + * returns the new value to retry with */ + old_value = module->state->notify_counters[notify]; + + do { + ret = ompi_osc_rdma_lock_btl_cswap (module, my_peer, address, old_value, 0, &result); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + if (result == old_value) { + break; + } + + old_value = result; + } while (1); + } + + *value = (OMPI_MPI_COUNT_TYPE) old_value; + + return OMPI_SUCCESS; +} + +int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t *info, int num_notifications) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + int my_rank = ompi_comm_rank (module->comm); + int my_count, ret; + + (void) info; /* "mpi_assert_same_num_notifications" is an optimization hint only */ + + if (OPAL_UNLIKELY(num_notifications < 0 || num_notifications > OMPI_OSC_RDMA_NOTIFY_MAX)) { + return MPI_ERR_ARG; + } + + /* it is erroneous to call MPI_WIN_SET_NUM_NOTIFY while an access epoch is open */ + if (OPAL_UNLIKELY(ompi_osc_rdma_access_epoch_active (module))) { + return OMPI_ERR_RMA_SYNC; + } + + if (NULL == module->notify_counts) { + module->notify_counts = calloc (ompi_comm_size (module->comm), sizeof (int)); + if (OPAL_UNLIKELY(NULL == module->notify_counts)) { + return OMPI_ERR_OUT_OF_RESOURCE; + } + } + + /* the number of attached notification counters is never decreased (section 12.6.1) */ + if (num_notifications > module->notify_counts[my_rank]) { + module->notify_counts[my_rank] = num_notifications; + } + + /* all notification counters (existing and newly attached) are reset to zero by + * this call. no access epoch is open so no btl atomics can be in flight on them */ + memset ((void *) module->state->notify_counters, 0, sizeof (module->state->notify_counters)); + opal_atomic_wmb (); + + /* publish every rank's attached count to the whole group so origins can validate + * notification indices against the target's count. this allgather doubles as the + * blocking, synchronizing collective required by the standard */ + my_count = module->notify_counts[my_rank]; + ret = module->comm->c_coll->coll_allgather (&my_count, 1, MPI_INT, module->notify_counts, 1, MPI_INT, + module->comm, module->comm->c_coll->coll_allgather_module); + + return ret; +} + +int ompi_osc_rdma_win_get_num_notify (struct ompi_win_t *win, int target_rank, int *num_notifications) +{ + ompi_osc_rdma_module_t *module = GET_MODULE(win); + + if (OPAL_UNLIKELY(target_rank < 0 || target_rank >= ompi_comm_size (module->comm))) { + return MPI_ERR_RANK; + } + + *num_notifications = (NULL != module->notify_counts) ? module->notify_counts[target_rank] : 0; + + return OMPI_SUCCESS; +} diff --git a/ompi/mca/osc/rdma/osc_rdma_types.h b/ompi/mca/osc/rdma/osc_rdma_types.h index 69157f6bef0..66e017c5e86 100644 --- a/ompi/mca/osc/rdma/osc_rdma_types.h +++ b/ompi/mca/osc/rdma/osc_rdma_types.h @@ -112,6 +112,20 @@ OBJ_CLASS_DECLARATION(ompi_osc_rdma_handle_t); */ #define OMPI_OSC_RDMA_POST_PEER_MAX 32 +/** + * @brief number of notification counters allocated in the window state + * + * Notified communication (MPI Standard section 12.6) increments one of + * these counters at the target after the data movement of a put or get + * completes there. A fixed region is always allocated (and covered by the + * state registration) so that origins can update counters with btl atomics; + * only the first notify_counts[rank] counters are considered *attached* by + * MPI_WIN_SET_NUM_NOTIFY. The value is constant and not exposed as an MCA + * variable to keep the layout of the \ref ompi_osc_rdma_state_t structure + * simple. This value is the effective MPI_WIN_NOTIFICATION_NUM_UB. + */ +#define OMPI_OSC_RDMA_NOTIFY_MAX 16 + /** * @brief window state structure * @@ -138,6 +152,10 @@ struct ompi_osc_rdma_state_t { osc_rdma_counter_t num_complete_msgs; /** lock for the region state to ensure consistency */ ompi_osc_rdma_lock_t regions_lock; + /** notification counters for notified communication. incremented by + * origin processes with btl (or CPU) atomics after the data movement + * of a notified operation has completed at this process */ + osc_rdma_counter_t notify_counters[OMPI_OSC_RDMA_NOTIFY_MAX]; /** displacement unit for this process */ int64_t disp_unit; /** number of attached regions. this count will be 1 in non-dynamic regions */ From 6f77f30b0dbc1d5e7c6e420ea65a39f1fc90200b Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Thu, 6 Aug 2026 18:53:32 -0400 Subject: [PATCH 02/10] btl: hardware notification counters, with an ofi implementation Add a btl interface for counters that the network adapter increments at the target once a remote operation completes there, and use it in osc/rdma so a notified put or get costs a single network operation instead of a data transfer followed by an atomic to announce it. opal/mca/btl: - MCA_BTL_FLAGS_NOTIFIED_RMA, set by a btl that provides btl_register_notification, btl_deregister_notification, btl_notification_read and btl_notification_wait - registering a region this way yields an ordinary registration handle alongside the counter, so an origin targets that handle in a put or get and the target's adapter counts the operation as a side effect - counters are monotonic and never reset by the btl; a consumer that needs a reset records a base value and subtracts it opal/mca/btl/ofi: - a notification is an fi_cntr bound to a memory region with FI_REMOTE_WRITE | FI_REMOTE_READ, so it requires the provider to have been opened with FI_RMA_EVENT - FI_RMA_EVENT is requested as an optional capability: fi_getinfo treats requested caps as mandatory, so the query is retried without it before any other fallback, since counters are an optimization and should be the first thing given up - these registrations bypass the rcache, which keys on the address range and would hand back one registration where several distinct counters over the same buffer are wanted - btl_ofi_enable_rma_event MCA parameter to suppress the request ompi/mca/osc/rdma: - each rank registers its window base once per attached notification index and publishes the resulting handles to the group; an origin picks a counter by choosing which handle to target - the accelerated path is taken only when the transfer maps to exactly one btl operation, since the counter counts operations and the standard requires exactly one increment per notified call; local peers, non-contiguous datatypes, size mismatches, transfers over the put/get limit and unaligned gets fall back to the atomic path - MPI_WIN_RESET_NOTIFY_VALUE records the raw counter value as a base rather than resetting it, which avoids racing increments in flight Signed-off-by: Joseph Antony Co-Authored-By: Claude Opus 5 --- ompi/mca/osc/rdma/osc_rdma.h | 46 ++++ ompi/mca/osc/rdma/osc_rdma_comm.c | 95 +++++++ ompi/mca/osc/rdma/osc_rdma_module.c | 209 +++++++++++++++ opal/mca/btl/base/btl_base_frame.c | 1 + opal/mca/btl/btl.h | 112 +++++++++ opal/mca/btl/ofi/Makefile.am | 1 + opal/mca/btl/ofi/btl_ofi.h | 19 ++ opal/mca/btl/ofi/btl_ofi_component.c | 49 ++++ opal/mca/btl/ofi/btl_ofi_notification.c | 220 ++++++++++++++++ test/simple/notified_rma.c | 321 ++++++++++++++++++++++++ 10 files changed, 1073 insertions(+) create mode 100644 opal/mca/btl/ofi/btl_ofi_notification.c create mode 100644 test/simple/notified_rma.c diff --git a/ompi/mca/osc/rdma/osc_rdma.h b/ompi/mca/osc/rdma/osc_rdma.h index 6eec94b150e..295b450ceb9 100644 --- a/ompi/mca/osc/rdma/osc_rdma.h +++ b/ompi/mca/osc/rdma/osc_rdma.h @@ -231,6 +231,35 @@ struct ompi_osc_rdma_module_t { * the target's attached count */ int *notify_counts; + /** notification counters bound to the window base by the accelerated btl, + * one per attached notification index. when these are in use an origin + * notifies by targeting the matching registration handle, so the adapter + * increments the counter as a side effect of the data movement itself and + * no separate atomic (and no flush to order it) is needed. NULL entries + * are indices that are not attached. */ + struct mca_btl_base_notification_t *notify_handles[OMPI_OSC_RDMA_NOTIFY_MAX]; + + /** raw value of each hardware counter at the last reset. the btl never + * resets a counter, so MPI_WIN_RESET_NOTIFY_VALUE records a base here and + * subsequent reads report the difference. */ + uint64_t notify_reset_base[OMPI_OSC_RDMA_NOTIFY_MAX]; + + /** registration handles published by every rank for their notification + * counters, packed as [rank * notify_stride + notify] entries of + * notify_handle_size bytes each. NULL when counters are not in use. */ + unsigned char *notify_peer_handles; + + /** size in bytes of one published notification registration handle */ + size_t notify_handle_size; + + /** number of handles published per rank, i.e. the group-wide maximum + * number of attached notification counters */ + int notify_stride; + + /** true when this window notifies through btl notification counters + * rather than through atomics on the counters in the window state */ + bool use_notify_counters; + /* ********************* sync data ************************ */ /** global sync object (PSCW, fence, lock all) */ @@ -343,6 +372,23 @@ int ompi_osc_rdma_free (struct ompi_win_t *win); return MPI_ERR_NOTIFY_IDX; \ } +/** + * @brief registration handle rank {rank} published for its notification counter {notify} + * + * An origin targets this handle instead of the ordinary window handle to make + * the adapter at the target count the operation. Only valid when + * module->use_notify_counters is set. + */ +static inline mca_btl_base_registration_handle_t * +ompi_osc_rdma_peer_notify_handle (ompi_osc_rdma_module_t *module, int rank, int notify) +{ + size_t offset = ((size_t) rank * (size_t) module->notify_stride + (size_t) notify) + * module->notify_handle_size; + + return (mca_btl_base_registration_handle_t *) (module->notify_peer_handles + offset); +} + +void ompi_osc_rdma_notify_counters_destroy (ompi_osc_rdma_module_t *module); int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value); int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.c b/ompi/mca/osc/rdma/osc_rdma_comm.c index e13ffbb77b6..5af7e599b05 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.c +++ b/ompi/mca/osc/rdma/osc_rdma_comm.c @@ -968,6 +968,60 @@ static void ompi_osc_rdma_notify_atomic_complete (void *cbdata, void *cbcontext, * against the sync object so that MPI_WIN_FLUSH (which must guarantee * the notification is visible at the target) waits for it. */ +/** + * @brief can this notified operation be carried by a single btl operation? + * + * When the btl provides notification counters the notification is a side + * effect of the data movement: the origin targets the registration handle the + * target published for that notification index and the adapter increments the + * matching counter once per operation it completes. That equivalence only + * holds when the transfer maps to exactly one btl operation, since the counter + * counts operations and the standard requires exactly one increment per + * notified call, so everything that might be split or served without touching + * the adapter has to take the atomic path instead. + */ +static inline bool ompi_osc_rdma_notify_single_op (ompi_osc_rdma_module_t *module, ompi_osc_rdma_peer_t *peer, + size_t origin_count, ompi_datatype_t *origin_datatype, + size_t target_count, ompi_datatype_t *target_datatype, + size_t limit, size_t alignment, size_t *size) +{ + size_t origin_size, target_size; + + if (!module->use_notify_counters) { + return false; + } + + /* a peer we can reach with loads and stores is served by a direct copy + * that the adapter never observes */ + if (ompi_osc_rdma_peer_local_base (peer)) { + return false; + } + + if (!ompi_datatype_is_contiguous_memory_layout (origin_datatype, origin_count) + || !ompi_datatype_is_contiguous_memory_layout (target_datatype, target_count)) { + return false; + } + + ompi_datatype_type_size (origin_datatype, &origin_size); + ompi_datatype_type_size (target_datatype, &target_size); + origin_size *= origin_count; + target_size *= target_count; + + if (origin_size != target_size || 0 == origin_size || origin_size > limit) { + return false; + } + + /* a transfer that does not meet the btl's alignment requirement is broken + * into a head, a body and a tail */ + if (alignment && (origin_size & (alignment - 1))) { + return false; + } + + *size = origin_size; + + return true; +} + static int ompi_osc_rdma_notify_increment (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, int notify) { ompi_osc_rdma_module_t *module = sync->module; @@ -1005,6 +1059,7 @@ int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi ompi_osc_rdma_module_t *module = GET_MODULE(win); ompi_osc_rdma_peer_t *peer; ompi_osc_rdma_sync_t *sync; + size_t size; int ret; OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "put_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", @@ -1018,6 +1073,26 @@ int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi return OMPI_ERR_RMA_SYNC; } + /* when the btl counts operations in hardware the notification rides along + * with the put itself: no second operation to issue and nothing to order + * it against, so the origin never has to stall here */ + if (ompi_osc_rdma_notify_single_op (module, peer, origin_count, origin_datatype, target_count, + target_datatype, module->put_limit, 0, &size)) { + mca_btl_base_registration_handle_t *target_handle; + uint64_t target_address; + + ret = osc_rdma_get_remote_segment (module, peer, target_disp, size, &target_address, &target_handle); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* targeting the handle the peer published for this notification index + * is what selects which of its counters the adapter advances */ + return ompi_osc_rdma_put_contig (sync, peer, target_address, + ompi_osc_rdma_peer_notify_handle (module, target_rank, notify), + (void *) (intptr_t) origin_addr, size, NULL); + } + ret = ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, target_count, target_datatype, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { @@ -1040,6 +1115,7 @@ int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datat ompi_osc_rdma_module_t *module = GET_MODULE(win); ompi_osc_rdma_peer_t *peer; ompi_osc_rdma_sync_t *sync; + size_t size; int ret; OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "get_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", @@ -1053,6 +1129,25 @@ int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datat return OMPI_ERR_RMA_SYNC; } + if (ompi_osc_rdma_notify_single_op (module, peer, origin_count, origin_datatype, source_count, + source_datatype, module->get_limit, module->get_alignment, &size)) { + mca_btl_base_registration_handle_t *source_handle; + uint64_t source_address; + + ret = osc_rdma_get_remote_segment (module, peer, source_disp, size, &source_address, &source_handle); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* an unaligned source is fetched as a head, a body and a tail, which + * the adapter would count three times */ + if (!(source_address & ALIGNMENT_MASK(module->get_alignment))) { + return ompi_osc_rdma_get_contig (sync, peer, source_address, + ompi_osc_rdma_peer_notify_handle (module, source_rank, notify), + origin_addr, size, NULL); + } + } + ret = ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, source_disp, source_count, source_datatype, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { diff --git a/ompi/mca/osc/rdma/osc_rdma_module.c b/ompi/mca/osc/rdma/osc_rdma_module.c index 80d78c78f37..1ad8d56b61b 100644 --- a/ompi/mca/osc/rdma/osc_rdma_module.c +++ b/ompi/mca/osc/rdma/osc_rdma_module.c @@ -140,6 +140,8 @@ int ompi_osc_rdma_free(ompi_win_t *win) module->segment_base = NULL; } + ompi_osc_rdma_notify_counters_destroy (module); + free (module->peer_array); free (module->outstanding_lock_array); free (module->notify_counts); @@ -158,6 +160,126 @@ int ompi_osc_rdma_free(ompi_win_t *win) /* ******************* notified communication ******************* */ +/** + * @brief release every btl notification counter attached to this window + */ +void ompi_osc_rdma_notify_counters_destroy (ompi_osc_rdma_module_t *module) +{ + mca_btl_base_module_t *btl = module->use_accelerated_btl ? module->accelerated_btl : NULL; + + for (int i = 0 ; i < OMPI_OSC_RDMA_NOTIFY_MAX ; ++i) { + if (NULL != module->notify_handles[i]) { + if (NULL != btl) { + (void) btl->btl_deregister_notification (btl, module->notify_handles[i]); + } + module->notify_handles[i] = NULL; + } + } + + free (module->notify_peer_handles); + module->notify_peer_handles = NULL; + module->notify_stride = 0; + module->use_notify_counters = false; +} + +/** + * @brief can this window notify through btl notification counters? + * + * Dynamic windows are excluded because a counter is bound to one registration + * and the attached regions of a dynamic window come and go. + */ +static bool ompi_osc_rdma_notify_counters_available (ompi_osc_rdma_module_t *module) +{ + return module->use_accelerated_btl && MPI_WIN_FLAVOR_DYNAMIC != module->flavor + && module->use_memory_registration && module->size + && !!(module->accelerated_btl->btl_flags & MCA_BTL_FLAGS_NOTIFIED_RMA); +} + +/** + * @brief attach {my_count} notification counters to the window and publish their handles + * + * Each counter is a separate registration of the same window memory, so an + * origin picks which counter to advance purely by choosing which registration + * handle to target -- the adapter then counts the data movement itself and no + * follow-up atomic (nor a flush to order it behind the data) is required. + * + * Every rank in the group runs the same sequence of collectives here, whether + * or not its own registrations succeeded, so that a failure on one rank makes + * the whole window fall back rather than deadlocking the rest. + */ +static int ompi_osc_rdma_notify_counters_setup (ompi_osc_rdma_module_t *module, int my_count, int max_count) +{ + mca_btl_base_module_t *btl = module->accelerated_btl; + size_t handle_size = btl->btl_registration_handle_size; + ompi_osc_rdma_region_t *region; + unsigned char *my_handles = NULL; + int registered_ok = 1; + int all_ok = 0; + int ret; + + /* a non-dynamic window always has exactly one region, which describes the + * memory the window was created over */ + region = (ompi_osc_rdma_region_t *) module->state->regions; + + module->notify_handle_size = handle_size; + module->notify_stride = max_count; + + module->notify_peer_handles = calloc ((size_t) ompi_comm_size (module->comm) * (size_t) max_count, + handle_size); + my_handles = calloc ((size_t) max_count, handle_size); + + if (NULL == module->notify_peer_handles || NULL == my_handles) { + registered_ok = 0; + } + + for (int i = 0 ; registered_ok && i < my_count ; ++i) { + mca_btl_base_registration_handle_t *handle = NULL; + + module->notify_handles[i] = btl->btl_register_notification (btl, + (void *) (intptr_t) region->base, + (size_t) region->len, + MCA_BTL_REG_FLAG_ACCESS_ANY, &handle); + if (OPAL_UNLIKELY(NULL == module->notify_handles[i])) { + /* leaving some indices counted by the adapter and others not would + * be worse than not using counters at all */ + registered_ok = 0; + break; + } + + memcpy (my_handles + (size_t) i * handle_size, handle, handle_size); + } + + ret = module->comm->c_coll->coll_allreduce (®istered_ok, &all_ok, 1, MPI_INT, MPI_MIN, module->comm, + module->comm->c_coll->coll_allreduce_module); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + all_ok = 0; + } + + if (!all_ok) { + free (my_handles); + ompi_osc_rdma_notify_counters_destroy (module); + /* not an error: the caller notifies with atomics instead */ + return OMPI_SUCCESS; + } + + ret = module->comm->c_coll->coll_allgather (my_handles, (int) ((size_t) max_count * handle_size), MPI_BYTE, + module->notify_peer_handles, + (int) ((size_t) max_count * handle_size), MPI_BYTE, + module->comm, module->comm->c_coll->coll_allgather_module); + free (my_handles); + + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + ompi_osc_rdma_notify_counters_destroy (module); + return ret; + } + + module->use_notify_counters = true; + + OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_INFO, "notifying through %d btl notification counter(s)", my_count); + + return OMPI_SUCCESS; +} + int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value) { ompi_osc_rdma_module_t *module = GET_MODULE(win); @@ -165,6 +287,29 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, my_rank); + if (module->use_notify_counters) { + mca_btl_base_module_t *btl = module->accelerated_btl; + uint64_t hardware; + int ret; + + ret = btl->btl_notification_read (btl, module->notify_handles[notify], &hardware); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* origins on this node reach the window with a direct copy that the + * adapter never sees, so their notifications land in the state counter + * instead. both count operations on this index, so the value the + * standard asks for is their sum. */ + *value = (OMPI_MPI_COUNT_TYPE) (hardware - module->notify_reset_base[notify] + + (uint64_t) ((volatile osc_rdma_counter_t *) + module->state->notify_counters)[notify]); + /* ensure loads of the window data are not reordered before the counter read */ + opal_atomic_rmb (); + + return OMPI_SUCCESS; + } + /* the standard's usage model is polling this function until a counter * reaches a threshold. progress the module so counter updates arrive even * when the btl requires target-side progress (emulated atomics) */ @@ -187,6 +332,34 @@ int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OM OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, my_rank); + if (module->use_notify_counters) { + mca_btl_base_module_t *btl = module->accelerated_btl; + uint64_t hardware; + + ret = btl->btl_notification_read (btl, module->notify_handles[notify], &hardware); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* the adapter's counter is monotonic and cannot be zeroed, so record + * where this reset happened and report differences from there. any + * increment that lands between the read above and the store below is + * simply attributed to the next interval, which is what an atomic + * fetch-and-zero at the instant of the read would also have done. */ + *value = (OMPI_MPI_COUNT_TYPE) (hardware - module->notify_reset_base[notify]); + module->notify_reset_base[notify] = hardware; + + /* same-node origins notify through the state counter, which only ever + * sees CPU atomics in this mode, so it can be zeroed directly */ + old_value = module->state->notify_counters[notify]; + while (!ompi_osc_rdma_lock_compare_exchange ((osc_rdma_atomic_counter_t *) (module->state->notify_counters + notify), + &old_value, 0)); + + *value += (OMPI_MPI_COUNT_TYPE) old_value; + + return OMPI_SUCCESS; + } + /* the counter is incremented by remote origins with btl atomics, so the * read-and-zero must be atomic with respect to those. use a CPU atomic * only when the peer flags indicate it is safe to mix CPU and btl @@ -228,6 +401,7 @@ int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t { ompi_osc_rdma_module_t *module = GET_MODULE(win); int my_rank = ompi_comm_rank (module->comm); + int max_count = 0, supported, all_supported = 0; int my_count, ret; (void) info; /* "mpi_assert_same_num_notifications" is an optimization hint only */ @@ -264,6 +438,41 @@ int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t my_count = module->notify_counts[my_rank]; ret = module->comm->c_coll->coll_allgather (&my_count, 1, MPI_INT, module->notify_counts, 1, MPI_INT, module->comm, module->comm->c_coll->coll_allgather_module); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + /* A hardware counter cannot be zeroed, so rather than track a reset base + * for a counter nobody has used yet, drop the existing registrations and + * attach fresh ones. This call is collective and no access epoch is open, + * so no origin can be holding a handle we are about to invalidate. */ + ompi_osc_rdma_notify_counters_destroy (module); + memset (module->notify_reset_base, 0, sizeof (module->notify_reset_base)); + + for (int i = 0 ; i < ompi_comm_size (module->comm) ; ++i) { + if (module->notify_counts[i] > max_count) { + max_count = module->notify_counts[i]; + } + } + + if (0 == max_count) { + return OMPI_SUCCESS; + } + + /* whether counters can be used has to be agreed group-wide: an origin + * chooses how to notify, and it must make the same choice its target made */ + supported = ompi_osc_rdma_notify_counters_available (module) ? 1 : 0; + ret = module->comm->c_coll->coll_allreduce (&supported, &all_supported, 1, MPI_INT, MPI_MIN, module->comm, + module->comm->c_coll->coll_allreduce_module); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + if (all_supported) { + /* falls back silently (leaving use_notify_counters clear) if the + * adapter will not hand out counters everywhere */ + ret = ompi_osc_rdma_notify_counters_setup (module, my_count, max_count); + } return ret; } diff --git a/opal/mca/btl/base/btl_base_frame.c b/opal/mca/btl/base/btl_base_frame.c index 63fc0f7f75c..4478b2facc6 100644 --- a/opal/mca/btl/base/btl_base_frame.c +++ b/opal/mca/btl/base/btl_base_frame.c @@ -59,6 +59,7 @@ mca_base_var_enum_value_flag_t mca_btl_base_flag_enum_flags[] {MCA_BTL_FLAGS_GET_AM, "get_am", MCA_BTL_FLAGS_GET}, {MCA_BTL_FLAGS_ATOMIC_AM_FOP, "atomic-am", MCA_BTL_FLAGS_ATOMIC_FOPS}, {MCA_BTL_FLAGS_RDMA_REMOTE_COMPLETION, "rdma-remote-completion", 0}, + {MCA_BTL_FLAGS_NOTIFIED_RMA, "notified-rma", 0}, {0, NULL, 0}}; mca_base_var_enum_value_flag_t mca_btl_base_atomic_enum_flags[] diff --git a/opal/mca/btl/btl.h b/opal/mca/btl/btl.h index f6f97564cd5..016a158171c 100644 --- a/opal/mca/btl/btl.h +++ b/opal/mca/btl/btl.h @@ -273,6 +273,22 @@ typedef uint8_t mca_btl_base_tag_t; */ #define MCA_BTL_FLAGS_RDMA_REMOTE_COMPLETION 0x800000 +/** The BTL can associate a hardware notification counter with a memory + * region. The counter is incremented by the network adapter at the target + * once a remote operation on that region has completed there, without any + * involvement of the target's CPU. + * + * A BTL that sets this flag must provide btl_register_notification, + * btl_deregister_notification, btl_notification_read and + * btl_notification_wait. The registration handle returned alongside the + * counter is an ordinary handle: an origin uses it as the remote handle of + * a btl_put or btl_get, and the counter at the target counts those + * operations. This lets a consumer implement RMA-with-notification with a + * single network operation rather than a data transfer followed by a + * separate atomic update. + */ +#define MCA_BTL_FLAGS_NOTIFIED_RMA 0x1000000 + /* End of btl flags. if additional flags are added please update * mca_btl_base_flag_enum_flags in btl_base_frame.c */ @@ -1169,6 +1185,95 @@ typedef void (*mca_btl_base_module_dump_fn_t)(struct mca_btl_base_module_t *btl, typedef int (*mca_btl_base_module_flush_fn_t)(struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint); +/** + * @brief Opaque handle to a hardware notification counter. + * + * The contents are private to the BTL. A notification is always local to the + * process that created it; it cannot be passed to or read by a peer. + */ +struct mca_btl_base_notification_t; +typedef struct mca_btl_base_notification_t mca_btl_base_notification_t; + +/** + * @brief Register a memory region and bind a notification counter to it + * + * Registers [base, base + size) and associates a hardware counter with the + * registration. The counter starts at zero and is incremented by the network + * adapter once per completed remote operation on the region, after the data + * movement has completed at this process. + * + * The same memory may be registered this way more than once. Each such + * registration yields a distinct handle and a distinct counter, so a consumer + * that needs several independent counters over one buffer can obtain them by + * registering the buffer once per counter and publishing a different handle to + * origins for each. Registrations consume adapter resources, so callers should + * not create more than they need. + * + * Ownership of both the notification and the registration handle passes to the + * caller; both are released by btl_deregister_notification. + * + * @param[IN] btl BTL module + * @param[IN] base Pointer to start of region + * @param[IN] size Size of region + * @param[IN] flags Registration flags including access permissions + * @param[OUT] handle Registration handle to publish to origins + * + * @returns a notification handle, or NULL if the region could not be + * registered or no counter could be associated with it. + */ +typedef struct mca_btl_base_notification_t *(*mca_btl_base_module_register_notification_fn_t)( + struct mca_btl_base_module_t *btl, void *base, size_t size, uint32_t flags, + struct mca_btl_base_registration_handle_t **handle); + +/** + * @brief Release a notification counter and its memory registration + * + * No remote operation may target the associated region after this call. + * + * @param[IN] btl BTL module the notification was created with + * @param[IN] notification Notification to release + */ +typedef int (*mca_btl_base_module_deregister_notification_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification); + +/** + * @brief Read the current value of a notification counter + * + * This is a local operation and does not guarantee progress of outstanding + * operations. The value is monotonically non-decreasing for the lifetime of + * the notification; the BTL never resets it. A consumer that needs to reset a + * counter should record the value at reset time and subtract it from + * subsequent reads, which avoids racing against increments still in flight. + * + * @param[IN] btl BTL module + * @param[IN] notification Notification to read + * @param[OUT] value Current counter value + */ +typedef int (*mca_btl_base_module_notification_read_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, + uint64_t *value); + +/** + * @brief Wait for a notification counter to reach a threshold + * + * Blocks until the counter is greater than or equal to threshold, or until + * timeout milliseconds have elapsed. A timeout of 0 polls once and returns + * immediately; a negative timeout blocks indefinitely. + * + * Returns OPAL_SUCCESS if the threshold was reached and OPAL_ERR_TIMEOUT if it + * was not. Because this call may block inside the adapter or the kernel, a + * caller that must also guarantee MPI progress should either use a short + * timeout or poll with btl_notification_read instead. + * + * @param[IN] btl BTL module + * @param[IN] notification Notification to wait on + * @param[IN] threshold Counter value to wait for + * @param[IN] timeout Milliseconds to wait, or negative to block + */ +typedef int (*mca_btl_base_module_notification_wait_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, + uint64_t threshold, int timeout); + /** * BTL module interface functions and attributes. */ @@ -1244,6 +1349,13 @@ struct mca_btl_base_module_t { union { struct { void *btl_am_data; + + /* notification counters. only valid when the BTL sets + * MCA_BTL_FLAGS_NOTIFIED_RMA */ + mca_btl_base_module_register_notification_fn_t btl_register_notification; + mca_btl_base_module_deregister_notification_fn_t btl_deregister_notification; + mca_btl_base_module_notification_read_fn_t btl_notification_read; + mca_btl_base_module_notification_wait_fn_t btl_notification_wait; }; unsigned char padding[256]; /**< padding to future-proof the btl module */ diff --git a/opal/mca/btl/ofi/Makefile.am b/opal/mca/btl/ofi/Makefile.am index 271a93bed54..f786e783159 100644 --- a/opal/mca/btl/ofi/Makefile.am +++ b/opal/mca/btl/ofi/Makefile.am @@ -34,6 +34,7 @@ sources = \ btl_ofi_rdma.h \ btl_ofi_rdma.c \ btl_ofi_atomics.c \ + btl_ofi_notification.c \ btl_ofi_frag.c \ btl_ofi_frag.h \ btl_ofi_context.c diff --git a/opal/mca/btl/ofi/btl_ofi.h b/opal/mca/btl/ofi/btl_ofi.h index 085eee27471..eb183624a3b 100644 --- a/opal/mca/btl/ofi/btl_ofi.h +++ b/opal/mca/btl/ofi/btl_ofi.h @@ -171,6 +171,11 @@ struct mca_btl_ofi_component_t { bool disable_hmem; + /** request FI_RMA_EVENT so that notification counters can be bound to + * memory regions. optional: if no provider offers it the query is retried + * without it. */ + bool enable_rma_event; + /** All BTL OFI modules (1 per tl) */ mca_btl_ofi_module_t *modules[MCA_BTL_OFI_MAX_MODULES]; }; @@ -366,6 +371,20 @@ int mca_btl_ofi_reg_mem(void *reg_data, void *base, size_t size, mca_rcache_base_registration_t *reg); int mca_btl_ofi_dereg_mem(void *reg_data, mca_rcache_base_registration_t *reg); +/* hardware notification counters. only installed on the module when the + * selected provider reports FI_RMA_EVENT. */ +struct mca_btl_base_notification_t * +mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t size, + uint32_t flags, mca_btl_base_registration_handle_t **handle); +int mca_btl_ofi_deregister_notification(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification); +int mca_btl_ofi_notification_read(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification, + uint64_t *value); +int mca_btl_ofi_notification_wait(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification, + uint64_t threshold, int timeout); + int mca_btl_ofi_context_progress(mca_btl_ofi_context_t *context); mca_btl_ofi_module_t *mca_btl_ofi_module_alloc(int mode); diff --git a/opal/mca/btl/ofi/btl_ofi_component.c b/opal/mca/btl/ofi/btl_ofi_component.c index 744faa27fcc..44dd5f5d372 100644 --- a/opal/mca/btl/ofi/btl_ofi_component.c +++ b/opal/mca/btl/ofi/btl_ofi_component.c @@ -219,6 +219,19 @@ static int mca_btl_ofi_component_register(void) MCA_BASE_VAR_SCOPE_READONLY, &mca_btl_ofi_component.disable_hmem); + mca_btl_ofi_component.enable_rma_event = true; + mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, + "enable_rma_event", + "Request FI_RMA_EVENT so notification counters can be bound " + "to memory regions, enabling RMA with notification in a " + "single operation. Requesting this capability may restrict " + "which memory registration modes a provider offers, so the " + "provider query is retried without it if nothing matches.", + MCA_BASE_VAR_TYPE_BOOL, NULL, 0, 0, + OPAL_INFO_LVL_5, + MCA_BASE_VAR_SCOPE_READONLY, + &mca_btl_ofi_component.enable_rma_event); + /* for now we want this component to lose to the MTL. */ module->super.btl_exclusivity = MCA_BTL_EXCLUSIVITY_HIGH - 50; @@ -293,6 +306,7 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, struct fi_fabric_attr fabric_attr = {0}; struct fi_domain_attr domain_attr = {0}; uint64_t required_caps; + bool try_rma_event = mca_btl_ofi_component.enable_rma_event; switch (mca_btl_ofi_component.mode) { @@ -379,6 +393,18 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, no_hmem: #endif + /* Request the ability to bind notification counters to memory regions. + * This is never added to required_caps: a provider without it is still + * perfectly usable, it just cannot accelerate RMA with notification. It + * has to go in the hints regardless because fi_getinfo treats requested + * caps as mandatory, which is also why it has its own fallback below. */ +no_rma_event: + if (try_rma_event) { + hints.caps |= FI_RMA_EVENT; + } else { + hints.caps &= ~FI_RMA_EVENT; + } + hints.fabric_attr->fabric = opal_common_ofi.fabric; hints.domain_attr->domain = opal_common_ofi.domain; @@ -400,6 +426,17 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, && 0 == opal_common_ofi_count_providers_in_list(info_list, include_list)) || (0 == rc && !include_list && exclude_list && opal_common_ofi_providers_subset_of_list(info_list, exclude_list))) { + /* Notification counters are an optimization, so give them up before + * giving up anything else. */ + if (try_rma_event) { + BTL_VERBOSE(("no provider matched with FI_RMA_EVENT, retrying without it")); + try_rma_event = false; + if (info_list) { + (void) fi_freeinfo(info_list); + info_list = NULL; + } + goto no_rma_event; + } #if defined(FI_HMEM) /* Attempt selecting a provider without FI_HMEM hints */ if (hints.caps & FI_HMEM) { @@ -680,6 +717,18 @@ static int mca_btl_ofi_init_device(struct fi_info *info) } #endif + /* Notification counters are bound to memory regions, so they are only + * meaningful when this module does RDMA at all. btl_register_mem is set + * by mca_btl_ofi_module_alloc() for the one-sided modes only. */ + if ((ofi_info->caps & FI_RMA_EVENT) && NULL != module->super.btl_register_mem) { + module->super.btl_flags |= MCA_BTL_FLAGS_NOTIFIED_RMA; + module->super.btl_register_notification = mca_btl_ofi_register_notification; + module->super.btl_deregister_notification = mca_btl_ofi_deregister_notification; + module->super.btl_notification_read = mca_btl_ofi_notification_read; + module->super.btl_notification_wait = mca_btl_ofi_notification_wait; + BTL_VERBOSE(("%s supports notification counters", linux_device_name)); + } + if (ofi_info->domain_attr->mr_mode == MCA_BTL_OFI_MR_BASIC || ofi_info->domain_attr->mr_mode & FI_MR_VIRT_ADDR) { module->use_virt_addr = true; diff --git a/opal/mca/btl/ofi/btl_ofi_notification.c b/opal/mca/btl/ofi/btl_ofi_notification.c new file mode 100644 index 00000000000..5df0d0a4935 --- /dev/null +++ b/opal/mca/btl/ofi/btl_ofi_notification.c @@ -0,0 +1,220 @@ +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ +/* + * Copyright (c) 2026 The University of Tennessee and The University + * of Tennessee Research Foundation. All rights + * reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + */ + +#include "opal_config.h" + +#include "btl_ofi.h" + +/** + * Hardware notification counters. + * + * A notification is a libfabric counter (fi_cntr) bound to a memory region + * with FI_REMOTE_WRITE | FI_REMOTE_READ. Once a region has been registered + * this way the adapter increments the counter every time a remote operation + * on that region completes here, without any involvement of this process's + * CPU and without the origin having to issue a second operation to announce + * that the data has landed. + * + * The counter counts operations on one memory region, so a consumer that + * needs several independent counters over the same buffer obtains them by + * registering the buffer once per counter and publishing a different rkey to + * origins for each. These registrations deliberately bypass the rcache: it + * keys on the address range and would hand back the same registration every + * time, which is the opposite of what is wanted here. + * + * This requires the provider to have been opened with FI_RMA_EVENT, which is + * requested as an optional capability during component init. + */ + +struct mca_btl_base_notification_t { + struct fid_mr *mr; + struct fid_cntr *cntr; + mca_btl_base_registration_handle_t handle; +}; + +struct mca_btl_base_notification_t * +mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t size, + uint32_t flags, mca_btl_base_registration_handle_t **handle) +{ + /* the same access flags as an ordinary registration. the counter binding + * below, not the access mask, is what decides which operations count. */ + static uint64_t access_flags = FI_REMOTE_WRITE | FI_REMOTE_READ | FI_READ | FI_WRITE; + mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; + struct mca_btl_base_notification_t *notification; + struct fi_cntr_attr cntr_attr = {0}; + struct fi_mr_attr attr = {0}; + struct iovec iov = {0}; + int rc; + + (void) flags; + + notification = calloc(1, sizeof(*notification)); + if (OPAL_UNLIKELY(NULL == notification)) { + return NULL; + } + + cntr_attr.events = FI_CNTR_EVENTS_COMP; + cntr_attr.wait_obj = FI_WAIT_UNSPEC; + + rc = fi_cntr_open(ofi_btl->domain, &cntr_attr, ¬ification->cntr, NULL); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed fi_cntr_open with err=%s", ofi_btl->linux_device_name, + fi_strerror(-rc))); + notification->cntr = NULL; + goto fail; + } + + iov.iov_base = base; + iov.iov_len = size; + attr.mr_iov = &iov; + attr.iov_count = 1; + attr.access = access_flags; + attr.offset = 0; + attr.context = NULL; + attr.requested_key = (uint64_t) (uintptr_t) notification; + + rc = fi_mr_regattr(ofi_btl->domain, &attr, 0, ¬ification->mr); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed fi_mr_regattr with err=%s", ofi_btl->linux_device_name, + fi_strerror(-rc))); + notification->mr = NULL; + goto fail; + } + + /* Bind the counter for both remote writes and remote reads so that a + * consumer can notify on either. We deliberately do not fall back to + * counting writes alone: a counter that silently misses remote reads + * would leave a get-with-notification permanently unnoticed at the + * target, which is worse than reporting no support at all and letting + * the consumer use its own fallback path. */ + rc = fi_mr_bind(notification->mr, ¬ification->cntr->fid, + FI_REMOTE_WRITE | FI_REMOTE_READ); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed to bind notification counter with err=%s", + ofi_btl->linux_device_name, fi_strerror(-rc))); + goto fail; + } + + if (ofi_btl->use_fi_mr_bind) { + rc = fi_mr_bind(notification->mr, &ofi_btl->ofi_endpoint->fid, 0ULL); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed to bind notification mr to endpoint with err=%s", + ofi_btl->linux_device_name, fi_strerror(-rc))); + goto fail; + } + } + + /* Unlike an ordinary registration this is required unconditionally: an mr + * that has anything bound to it is created disabled and does not service + * remote operations until enabled. */ + rc = fi_mr_enable(notification->mr); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed fi_mr_enable with err=%s", ofi_btl->linux_device_name, + fi_strerror(-rc))); + goto fail; + } + + notification->handle.rkey = fi_mr_key(notification->mr); + notification->handle.desc = fi_mr_desc(notification->mr); + + /* In case the provider doesn't support FI_MR_VIRT_ADDR, remote addresses + * are expressed as a distance from the base registered address. */ + if (ofi_btl->use_virt_addr) { + notification->handle.base_addr = 0; + } else { + notification->handle.base_addr = base; + } + + *handle = ¬ification->handle; + + return notification; + +fail: + if (NULL != notification->mr) { + fi_close(¬ification->mr->fid); + } + if (NULL != notification->cntr) { + fi_close(¬ification->cntr->fid); + } + free(notification); + + return NULL; +} + +int mca_btl_ofi_deregister_notification(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification) +{ + int ret = OPAL_SUCCESS; + int rc; + + if (OPAL_UNLIKELY(NULL == notification)) { + return OPAL_SUCCESS; + } + + /* close the mr first, it references the counter */ + if (NULL != notification->mr) { + rc = fi_close(¬ification->mr->fid); + if (FI_SUCCESS != rc) { + BTL_ERROR(("%s: error unpinning notification memory: %s", __func__, fi_strerror(-rc))); + ret = OPAL_ERROR; + } + } + + if (NULL != notification->cntr) { + rc = fi_close(¬ification->cntr->fid); + if (FI_SUCCESS != rc) { + BTL_ERROR(("%s: error closing notification counter: %s", __func__, fi_strerror(-rc))); + ret = OPAL_ERROR; + } + } + + free(notification); + + return ret; +} + +int mca_btl_ofi_notification_read(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification, + uint64_t *value) +{ + uint64_t errors; + + errors = fi_cntr_readerr(notification->cntr); + if (OPAL_UNLIKELY(0 != errors)) { + BTL_ERROR(("notification counter reported %llu error(s)", (unsigned long long) errors)); + return OPAL_ERROR; + } + + *value = fi_cntr_read(notification->cntr); + + return OPAL_SUCCESS; +} + +int mca_btl_ofi_notification_wait(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification, + uint64_t threshold, int timeout) +{ + int rc; + + rc = fi_cntr_wait(notification->cntr, threshold, timeout); + if (FI_SUCCESS == rc) { + return OPAL_SUCCESS; + } + + if (-FI_ETIMEDOUT == rc) { + return OPAL_ERR_TIMEOUT; + } + + BTL_VERBOSE(("fi_cntr_wait failed with err=%s", fi_strerror(-rc))); + + return OPAL_ERROR; +} diff --git a/test/simple/notified_rma.c b/test/simple/notified_rma.c new file mode 100644 index 00000000000..25fdd40d76e --- /dev/null +++ b/test/simple/notified_rma.c @@ -0,0 +1,321 @@ +/* + * Copyright (c) 2026 Joseph Antony. All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + * + * Functional test for MPI Notified Communication (MPI draft section 12.6). + * + * Exercises MPI_Win_set_num_notify / MPI_Win_get_num_notify, + * MPI_Put_notify, MPI_Get_notify, MPI_Win_get_notify_value and + * MPI_Win_reset_notify_value against whichever osc component is + * selected. Run with at least 2 ranks; force a component with e.g. + * + * mpirun -np 2 --mca osc ucx ./notified_rma + * mpirun -np 2 --mca osc sm ./notified_rma + * + * The interesting property under test is the ordering guarantee: a + * notification counter must only become visible at the target *after* + * the payload of the notified operation has landed there. The payload + * is deliberately large so that it is carried by real RDMA rather than + * an inlined/eager path, which is where a missing fence shows up. + */ + +#include +#include +#include + +#include + +#define NELEM 16384 /* 64 KB payload: big enough to force real RDMA */ +#define NCOUNTERS 4 +#define NITERS 64 +#define TIMEOUT_SEC 30.0 + +static int rank, size; +static int failures = 0; + +#define CHECK(cond, ...) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "[%d] FAIL %s:%d: ", rank, __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + failures++; \ + } \ + } while (0) + +#define CHECK_MPI(call, expected) \ + do { \ + int rc_ = (call); \ + int cls_ = MPI_SUCCESS; \ + if (MPI_SUCCESS != rc_) { \ + MPI_Error_class(rc_, &cls_); \ + } \ + CHECK(cls_ == (expected), "%s returned class %d, expected %d", \ + #call, cls_, (expected)); \ + } while (0) + +/* Poll a local notification counter until it reaches "target", or give + * up after TIMEOUT_SEC. A hang here means notifications are not being + * delivered (or the counter is not being progressed). */ +static int poll_counter(MPI_Win win, int idx, MPI_Count target, const char *what) +{ + double deadline = MPI_Wtime() + TIMEOUT_SEC; + MPI_Count value = 0; + + while (value < target) { + int rc = MPI_Win_get_notify_value(win, idx, &value); + if (MPI_SUCCESS != rc) { + fprintf(stderr, "[%d] FAIL %s: get_notify_value rc=%d\n", rank, what, rc); + failures++; + return -1; + } + if (MPI_Wtime() > deadline) { + fprintf(stderr, "[%d] FAIL %s: timed out waiting for counter %d " + "to reach %lld (stuck at %lld)\n", + rank, what, idx, (long long) target, (long long) value); + fflush(stderr); + failures++; + return -1; + } + } + return 0; +} + +static int expected_get_elem(int owner, int i) +{ + return owner * 1000003 + i; +} + +static int expected_put_elem(int origin, int iter, int i) +{ + return (origin + 1) * 7919 + iter * 31 + i; +} + +int main(int argc, char *argv[]) +{ + MPI_Win win; + int *winbuf = NULL; + int *localbuf = NULL; + int left, right; + int i, iter, r; + int num; + MPI_Count value; + + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + if (size < 2) { + if (0 == rank) { + fprintf(stderr, "This test requires at least 2 ranks\n"); + } + MPI_Finalize(); + return 77; /* automake "skipped" */ + } + + left = (rank - 1 + size) % size; + right = (rank + 1) % size; + + localbuf = malloc(NELEM * sizeof(int)); + if (NULL == localbuf) { + fprintf(stderr, "[%d] malloc failed\n", rank); + MPI_Abort(MPI_COMM_WORLD, 1); + } + + MPI_Win_allocate(NELEM * sizeof(int), sizeof(int), MPI_INFO_NULL, + MPI_COMM_WORLD, &winbuf, &win); + MPI_Win_set_errhandler(win, MPI_ERRORS_RETURN); + + /* Seed each window with a rank-specific pattern for the Get test. */ + for (i = 0; i < NELEM; i++) { + winbuf[i] = expected_get_elem(rank, i); + } + + /* --------------------------------------------------------------- + * Test 1: counter attachment. Collective, must be called outside + * any access epoch, and must never decrease the count. + * --------------------------------------------------------------- */ + CHECK_MPI(MPI_Win_set_num_notify(win, MPI_INFO_NULL, NCOUNTERS), MPI_SUCCESS); + + for (r = 0; r < size; r++) { + num = -1; + CHECK_MPI(MPI_Win_get_num_notify(win, r, &num), MPI_SUCCESS); + CHECK(NCOUNTERS == num, "get_num_notify(rank %d) = %d, expected %d", + r, num, NCOUNTERS); + } + + /* Counters must read zero immediately after attachment. */ + for (i = 0; i < NCOUNTERS; i++) { + value = -1; + CHECK_MPI(MPI_Win_get_notify_value(win, i, &value), MPI_SUCCESS); + CHECK(0 == value, "counter %d = %lld after set_num_notify, expected 0", + i, (long long) value); + } + + MPI_Barrier(MPI_COMM_WORLD); + + /* Notified operations are only valid in a passive target epoch. */ + CHECK_MPI(MPI_Win_lock_all(0, win), MPI_SUCCESS); + + /* --------------------------------------------------------------- + * Test 2: MPI_Get_notify. Each rank fetches its right neighbour's + * window; the counter is incremented at the *target* (12.6), so + * every rank is notified exactly once on counter 1 by its left + * neighbour. + * --------------------------------------------------------------- */ + memset(localbuf, 0, NELEM * sizeof(int)); + CHECK_MPI(MPI_Get_notify(localbuf, NELEM, MPI_INT, right, 0, NELEM, MPI_INT, + 1, win), + MPI_SUCCESS); + CHECK_MPI(MPI_Win_flush(right, win), MPI_SUCCESS); + + for (i = 0; i < NELEM; i++) { + if (localbuf[i] != expected_get_elem(right, i)) { + CHECK(0, "Get_notify data mismatch at %d: got %d, expected %d", + i, localbuf[i], expected_get_elem(right, i)); + break; + } + } + + if (0 == poll_counter(win, 1, 1, "Get_notify")) { + value = -1; + MPI_Win_get_notify_value(win, 1, &value); + CHECK(1 == value, "counter 1 = %lld after one Get_notify, expected 1", + (long long) value); + } + + MPI_Barrier(MPI_COMM_WORLD); + + /* --------------------------------------------------------------- + * Test 3: MPI_Put_notify ordering. Each iteration writes a fresh + * pattern to the right neighbour and notifies counter 0; the target + * polls the counter and then validates the payload. If the counter + * can become visible before the data lands, this reports a mismatch. + * The barrier keeps iteration i+1 from overwriting the buffer the + * target is still checking for iteration i. + * --------------------------------------------------------------- */ + for (iter = 0; iter < NITERS; iter++) { + for (i = 0; i < NELEM; i++) { + localbuf[i] = expected_put_elem(rank, iter, i); + } + + CHECK_MPI(MPI_Put_notify(localbuf, NELEM, MPI_INT, right, 0, NELEM, + MPI_INT, 0, win), + MPI_SUCCESS); + + if (0 != poll_counter(win, 0, iter + 1, "Put_notify")) { + break; + } + + /* Data written by "left" must be fully visible now. */ + for (i = 0; i < NELEM; i++) { + if (winbuf[i] != expected_put_elem(left, iter, i)) { + CHECK(0, "Put_notify ordering violation, iter %d elem %d: " + "got %d, expected %d (counter fired before data landed)", + iter, i, winbuf[i], expected_put_elem(left, iter, i)); + iter = NITERS; /* stop after the first bad iteration */ + break; + } + } + + MPI_Barrier(MPI_COMM_WORLD); + } + + MPI_Barrier(MPI_COMM_WORLD); + + /* --------------------------------------------------------------- + * Test 4: several notifications in flight against distinct + * counters, with no per-operation synchronisation. + * --------------------------------------------------------------- */ + for (i = 1; i < NCOUNTERS; i++) { + CHECK_MPI(MPI_Put_notify(localbuf, 1, MPI_INT, right, 0, 1, MPI_INT, + i, win), + MPI_SUCCESS); + } + for (i = 1; i < NCOUNTERS; i++) { + /* counter 1 already carries the Get_notify from test 2 */ + poll_counter(win, i, (1 == i) ? 2 : 1, "burst Put_notify"); + } + + MPI_Barrier(MPI_COMM_WORLD); + + /* --------------------------------------------------------------- + * Test 5: reset is an atomic fetch-and-zero. + * --------------------------------------------------------------- */ + value = -1; + CHECK_MPI(MPI_Win_reset_notify_value(win, 0, &value), MPI_SUCCESS); + CHECK(value == NITERS, "reset_notify_value(0) returned %lld, expected %d", + (long long) value, NITERS); + + value = -1; + CHECK_MPI(MPI_Win_get_notify_value(win, 0, &value), MPI_SUCCESS); + CHECK(0 == value, "counter 0 = %lld after reset, expected 0", + (long long) value); + + CHECK_MPI(MPI_Win_unlock_all(win), MPI_SUCCESS); + + /* --------------------------------------------------------------- + * Test 6: error paths. An out-of-range notification index must + * raise MPI_ERR_RMA_NOTIFICATION rather than corrupting memory. + * --------------------------------------------------------------- */ + CHECK_MPI(MPI_Win_get_notify_value(win, NCOUNTERS, &value), + MPI_ERR_RMA_NOTIFICATION); + CHECK_MPI(MPI_Win_get_notify_value(win, -1, &value), + MPI_ERR_RMA_NOTIFICATION); + CHECK_MPI(MPI_Win_reset_notify_value(win, NCOUNTERS, &value), + MPI_ERR_RMA_NOTIFICATION); + CHECK_MPI(MPI_Win_get_num_notify(win, size, &num), MPI_ERR_RANK); + + /* An out-of-range index on the data path must be caught too. */ + CHECK_MPI(MPI_Win_lock(MPI_LOCK_SHARED, right, 0, win), MPI_SUCCESS); + CHECK_MPI(MPI_Put_notify(localbuf, 1, MPI_INT, right, 0, 1, MPI_INT, + NCOUNTERS, win), + MPI_ERR_RMA_NOTIFICATION); + CHECK_MPI(MPI_Win_unlock(right, win), MPI_SUCCESS); + + /* Requesting more counters than the implementation supports must + * fail cleanly. All ranks pass the same value, so this stays + * collectively consistent. */ + CHECK_MPI(MPI_Win_set_num_notify(win, MPI_INFO_NULL, 1 << 20), MPI_ERR_ARG); + CHECK_MPI(MPI_Win_set_num_notify(win, MPI_INFO_NULL, -1), MPI_ERR_ARG); + + /* The count never decreases (12.6.1). */ + CHECK_MPI(MPI_Win_set_num_notify(win, MPI_INFO_NULL, 1), MPI_SUCCESS); + CHECK_MPI(MPI_Win_get_num_notify(win, rank, &num), MPI_SUCCESS); + CHECK(num >= NCOUNTERS, "get_num_notify = %d after shrinking request, " + "expected >= %d", num, NCOUNTERS); + + MPI_Win_free(&win); + free(localbuf); + + /* --------------------------------------------------------------- + * Test 7: notified communication is not supported on dynamic + * windows in this implementation; it must say so rather than crash. + * --------------------------------------------------------------- */ + MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &win); + MPI_Win_set_errhandler(win, MPI_ERRORS_RETURN); + CHECK_MPI(MPI_Win_set_num_notify(win, MPI_INFO_NULL, 1), MPI_ERR_RMA_FLAVOR); + MPI_Win_free(&win); + + /* --------------------------------------------------------------- */ + { + int total = 0; + MPI_Reduce(&failures, &total, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + if (0 == rank) { + if (0 == total) { + printf("notified_rma: PASSED (%d ranks)\n", size); + } else { + printf("notified_rma: FAILED (%d errors across %d ranks)\n", + total, size); + } + fflush(stdout); + } + MPI_Finalize(); + return (0 == total) ? 0 : 1; + } +} From 6aef1293c57103d5f434fc0d91ecf000964d99b1 Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Thu, 17 Sep 2026 13:47:14 -0400 Subject: [PATCH 03/10] btl/ofi: fix notification counter setup Request FI_RMA_EVENT only from the selected provider, register with FI_RMA_EVENT, and bind FI_REMOTE_WRITE only (reads are not counted). Signed-off-by: Joseph Antony --- opal/mca/btl/btl.h | 14 +-- opal/mca/btl/ofi/btl_ofi.h | 3 + opal/mca/btl/ofi/btl_ofi_component.c | 131 ++++++++++++++++++------ opal/mca/btl/ofi/btl_ofi_notification.c | 44 ++++---- 4 files changed, 133 insertions(+), 59 deletions(-) diff --git a/opal/mca/btl/btl.h b/opal/mca/btl/btl.h index 016a158171c..c88bc0c223d 100644 --- a/opal/mca/btl/btl.h +++ b/opal/mca/btl/btl.h @@ -282,10 +282,11 @@ typedef uint8_t mca_btl_base_tag_t; * btl_deregister_notification, btl_notification_read and * btl_notification_wait. The registration handle returned alongside the * counter is an ordinary handle: an origin uses it as the remote handle of - * a btl_put or btl_get, and the counter at the target counts those - * operations. This lets a consumer implement RMA-with-notification with a - * single network operation rather than a data transfer followed by a - * separate atomic update. + * a btl_put, and the counter at the target counts those operations. Only + * operations that modify the region (puts and atomics) are counted; a + * btl_get through the handle is not. This lets a consumer implement + * RMA-with-notification for writes with a single network operation rather + * than a data transfer followed by a separate atomic update. */ #define MCA_BTL_FLAGS_NOTIFIED_RMA 0x1000000 @@ -1199,8 +1200,9 @@ typedef struct mca_btl_base_notification_t mca_btl_base_notification_t; * * Registers [base, base + size) and associates a hardware counter with the * registration. The counter starts at zero and is incremented by the network - * adapter once per completed remote operation on the region, after the data - * movement has completed at this process. + * adapter once per completed remote operation that modifies the region (a + * put or an atomic), after the data movement has completed at this process. + * Remote reads of the region are not counted. * * The same memory may be registered this way more than once. Each such * registration yields a distinct handle and a distinct counter, so a consumer diff --git a/opal/mca/btl/ofi/btl_ofi.h b/opal/mca/btl/ofi/btl_ofi.h index eb183624a3b..c63d82739b8 100644 --- a/opal/mca/btl/ofi/btl_ofi.h +++ b/opal/mca/btl/ofi/btl_ofi.h @@ -128,6 +128,9 @@ struct mca_btl_ofi_module_t { bool use_virt_addr; bool is_scalable_ep; bool use_fi_mr_bind; + /** the domain was opened with FI_MR_RMA_EVENT: memory regions registered + * for a notification counter are created disabled and need fi_mr_enable */ + bool use_mr_rma_event; opal_atomic_int64_t outstanding_rdma; opal_atomic_int64_t outstanding_send; diff --git a/opal/mca/btl/ofi/btl_ofi_component.c b/opal/mca/btl/ofi/btl_ofi_component.c index 44dd5f5d372..b7d54dc1188 100644 --- a/opal/mca/btl/ofi/btl_ofi_component.c +++ b/opal/mca/btl/ofi/btl_ofi_component.c @@ -224,9 +224,10 @@ static int mca_btl_ofi_component_register(void) "enable_rma_event", "Request FI_RMA_EVENT so notification counters can be bound " "to memory regions, enabling RMA with notification in a " - "single operation. Requesting this capability may restrict " - "which memory registration modes a provider offers, so the " - "provider query is retried without it if nothing matches.", + "single operation. It is only requested from the provider " + "and NIC already selected, so it never changes which " + "provider is used; if that provider does not offer it, " + "notification counters are simply not used.", MCA_BASE_VAR_TYPE_BOOL, NULL, 0, 0, OPAL_INFO_LVL_5, MCA_BASE_VAR_SCOPE_READONLY, @@ -274,6 +275,65 @@ void mca_btl_ofi_exit(void) * then create a BTL instance for selected interfaces */ +/** + * Look for a variant of {info} -- the same provider on the same NIC -- that + * also offers FI_RMA_EVENT, so notification counters can be bound to its + * memory regions. + * + * FI_RMA_EVENT is a secondary capability, and fi_getinfo only returns + * providers that support every secondary capability in the hints. Asking for + * it during provider selection would therefore steer the btl to whichever + * provider happens to offer it, e.g. a TCP provider over the RDMA NIC. Asking + * only once a provider has been selected can add the capability but never + * changes the choice. + * + * @param[in] info the selected provider + * @param[in] api_version version the selection query succeeded with + * @param[in] mode mode bits this btl supports (as in the selection hints) + * @param[in] mr_mode memory registration modes this btl supports (likewise) + * + * @returns a new fi_info the caller must free, or NULL if the provider does not + * offer FI_RMA_EVENT there. {info} remains usable either way. + */ +static struct fi_info *mca_btl_ofi_rma_event_info(struct fi_info *info, uint32_t api_version, + uint64_t mode, int mr_mode) +{ + struct fi_info *hints, *info_list = NULL, *match = NULL; + int rc; + + hints = fi_dupinfo(info); + if (NULL == hints) { + return NULL; + } + + hints->caps |= FI_RMA_EVENT; + hints->mode = mode; + /* counters are registered with FI_RMA_EVENT and enabled once bound, which + * is what this mode asks of the application */ + hints->domain_attr->mr_mode = mr_mode | FI_MR_RMA_EVENT; + + rc = fi_getinfo(api_version, NULL, NULL, 0, hints, &info_list); + (void) fi_freeinfo(hints); + if (0 != rc) { + BTL_VERBOSE(("%s does not offer FI_RMA_EVENT on %s: %s", info->fabric_attr->prov_name, + info->domain_attr->name, fi_strerror(-rc))); + return NULL; + } + + for (struct fi_info *cur = info_list; NULL != cur; cur = cur->next) { + if ((cur->caps & FI_RMA_EVENT) + && 0 == strcmp(cur->fabric_attr->prov_name, info->fabric_attr->prov_name) + && 0 == strcmp(cur->domain_attr->name, info->domain_attr->name)) { + match = fi_dupinfo(cur); + break; + } + } + + (void) fi_freeinfo(info_list); + + return match; +} + static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, bool enable_progress_threads, bool enable_mpi_threads) @@ -305,8 +365,9 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, struct fi_tx_attr tx_attr = {0}; struct fi_fabric_attr fabric_attr = {0}; struct fi_domain_attr domain_attr = {0}; + struct fi_info *rma_event_info = NULL; uint64_t required_caps; - bool try_rma_event = mca_btl_ofi_component.enable_rma_event; + uint32_t api_version = FI_VERSION(1, 18); switch (mca_btl_ofi_component.mode) { @@ -393,32 +454,22 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, no_hmem: #endif - /* Request the ability to bind notification counters to memory regions. - * This is never added to required_caps: a provider without it is still - * perfectly usable, it just cannot accelerate RMA with notification. It - * has to go in the hints regardless because fi_getinfo treats requested - * caps as mandatory, which is also why it has its own fallback below. */ -no_rma_event: - if (try_rma_event) { - hints.caps |= FI_RMA_EVENT; - } else { - hints.caps &= ~FI_RMA_EVENT; - } - hints.fabric_attr->fabric = opal_common_ofi.fabric; hints.domain_attr->domain = opal_common_ofi.domain; /* Do the query. The earliest version that supports FI_HMEM hints is 1.9. * The earliest version the explictly allow provider to call CUDA API is 1.18 */ - rc = fi_getinfo(FI_VERSION(1, 18), NULL, NULL, 0, &hints, &info_list); + api_version = FI_VERSION(1, 18); + rc = fi_getinfo(api_version, NULL, NULL, 0, &hints, &info_list); if (FI_ENODATA == -rc && (hints.fabric_attr->fabric || hints.domain_attr->domain)) { /* Retry without fabric and domain */ hints.fabric_attr->fabric = NULL; hints.domain_attr->domain = NULL; - rc = fi_getinfo(FI_VERSION(1, 18), NULL, NULL, 0, &hints, &info_list); + rc = fi_getinfo(api_version, NULL, NULL, 0, &hints, &info_list); } if (FI_ENOSYS == -rc) { - rc = fi_getinfo(FI_VERSION(1, 9), NULL, NULL, 0, &hints, &info_list); + api_version = FI_VERSION(1, 9); + rc = fi_getinfo(api_version, NULL, NULL, 0, &hints, &info_list); } if ((FI_ENODATA == -rc) @@ -426,17 +477,6 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, && 0 == opal_common_ofi_count_providers_in_list(info_list, include_list)) || (0 == rc && !include_list && exclude_list && opal_common_ofi_providers_subset_of_list(info_list, exclude_list))) { - /* Notification counters are an optimization, so give them up before - * giving up anything else. */ - if (try_rma_event) { - BTL_VERBOSE(("no provider matched with FI_RMA_EVENT, retrying without it")); - try_rma_event = false; - if (info_list) { - (void) fi_freeinfo(info_list); - info_list = NULL; - } - goto no_rma_event; - } #if defined(FI_HMEM) /* Attempt selecting a provider without FI_HMEM hints */ if (hints.caps & FI_HMEM) { @@ -504,7 +544,27 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, * capabilities as the initial one. */ selected_info = opal_common_ofi_select_provider(info, &opal_process_info); - rc = mca_btl_ofi_init_device(selected_info); + + /* Notification counters are an optimization: try the same + * provider and NIC with FI_RMA_EVENT first, and use the plain + * variant if that is not offered or does not initialize. */ + rc = OPAL_ERR_NOT_AVAILABLE; + if (mca_btl_ofi_component.enable_rma_event && !(selected_info->caps & FI_RMA_EVENT)) { + if (NULL != rma_event_info) { + (void) fi_freeinfo(rma_event_info); + } + rma_event_info = mca_btl_ofi_rma_event_info(selected_info, api_version, hints.mode, + hints.domain_attr->mr_mode); + if (NULL != rma_event_info) { + rc = mca_btl_ofi_init_device(rma_event_info); + if (OPAL_SUCCESS == rc) { + selected_info = rma_event_info; + } + } + } + if (OPAL_SUCCESS != rc) { + rc = mca_btl_ofi_init_device(selected_info); + } if (OPAL_SUCCESS == rc) { info = selected_info; break; @@ -542,6 +602,11 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, if (info_list) { (void) fi_freeinfo(info_list); } + /* freed alongside info_list: a module keeps pointers into the fi_info it + * was initialized from */ + if (rma_event_info) { + (void) fi_freeinfo(rma_event_info); + } return base_modules; } @@ -738,6 +803,10 @@ static int mca_btl_ofi_init_device(struct fi_info *info) module->use_fi_mr_bind = true; } + if (ofi_info->domain_attr->mr_mode & FI_MR_RMA_EVENT) { + module->use_mr_rma_event = true; + } + /* create endpoint list */ OBJ_CONSTRUCT(&module->endpoints, opal_list_t); OBJ_CONSTRUCT(&module->module_lock, opal_mutex_t); diff --git a/opal/mca/btl/ofi/btl_ofi_notification.c b/opal/mca/btl/ofi/btl_ofi_notification.c index 5df0d0a4935..db3431685f4 100644 --- a/opal/mca/btl/ofi/btl_ofi_notification.c +++ b/opal/mca/btl/ofi/btl_ofi_notification.c @@ -18,11 +18,12 @@ * Hardware notification counters. * * A notification is a libfabric counter (fi_cntr) bound to a memory region - * with FI_REMOTE_WRITE | FI_REMOTE_READ. Once a region has been registered - * this way the adapter increments the counter every time a remote operation - * on that region completes here, without any involvement of this process's - * CPU and without the origin having to issue a second operation to announce - * that the data has landed. + * with FI_REMOTE_WRITE. Once a region has been registered this way the + * adapter increments the counter every time a remote write or atomic + * operation on that region completes here, without any involvement of this + * process's CPU and without the origin having to issue a second operation to + * announce that the data has landed. libfabric defines no counter event for + * remote reads, so reads of the region are not counted (fi_mr(3)). * * The counter counts operations on one memory region, so a consumer that * needs several independent counters over the same buffer obtains them by @@ -82,7 +83,9 @@ mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t attr.context = NULL; attr.requested_key = (uint64_t) (uintptr_t) notification; - rc = fi_mr_regattr(ofi_btl->domain, &attr, 0, ¬ification->mr); + /* FI_RMA_EVENT declares at creation that a completion counter will be + * associated with the region, which FI_MR_RMA_EVENT domains require */ + rc = fi_mr_regattr(ofi_btl->domain, &attr, FI_RMA_EVENT, ¬ification->mr); if (FI_SUCCESS != rc) { BTL_VERBOSE(("%s failed fi_mr_regattr with err=%s", ofi_btl->linux_device_name, fi_strerror(-rc))); @@ -90,14 +93,9 @@ mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t goto fail; } - /* Bind the counter for both remote writes and remote reads so that a - * consumer can notify on either. We deliberately do not fall back to - * counting writes alone: a counter that silently misses remote reads - * would leave a get-with-notification permanently unnoticed at the - * target, which is worse than reporting no support at all and letting - * the consumer use its own fallback path. */ - rc = fi_mr_bind(notification->mr, ¬ification->cntr->fid, - FI_REMOTE_WRITE | FI_REMOTE_READ); + /* FI_REMOTE_WRITE is the only event libfabric defines for counting + * operations on a memory region; it covers remote writes and atomics. */ + rc = fi_mr_bind(notification->mr, ¬ification->cntr->fid, FI_REMOTE_WRITE); if (FI_SUCCESS != rc) { BTL_VERBOSE(("%s failed to bind notification counter with err=%s", ofi_btl->linux_device_name, fi_strerror(-rc))); @@ -113,14 +111,16 @@ mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t } } - /* Unlike an ordinary registration this is required unconditionally: an mr - * that has anything bound to it is created disabled and does not service - * remote operations until enabled. */ - rc = fi_mr_enable(notification->mr); - if (FI_SUCCESS != rc) { - BTL_VERBOSE(("%s failed fi_mr_enable with err=%s", ofi_btl->linux_device_name, - fi_strerror(-rc))); - goto fail; + /* A region registered with FI_RMA_EVENT in an FI_MR_RMA_EVENT domain, or + * in an FI_MR_ENDPOINT domain, is created disabled and does not service + * remote operations until enabled, which must come after every binding. */ + if (ofi_btl->use_mr_rma_event || ofi_btl->use_fi_mr_bind) { + rc = fi_mr_enable(notification->mr); + if (FI_SUCCESS != rc) { + BTL_VERBOSE(("%s failed fi_mr_enable with err=%s", ofi_btl->linux_device_name, + fi_strerror(-rc))); + goto fail; + } } notification->handle.rkey = fi_mr_key(notification->mr); From 42b8a551c6bc8d638b8466def91e2ae41ae2e4df Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Thu, 17 Sep 2026 13:47:14 -0400 Subject: [PATCH 04/10] osc/rdma: fix notified RMA error handling and bounds Agree on errors before set_num_notify changes state, report bounds, make reset safe against btl atomics, and use atomics for get_notify. Signed-off-by: Joseph Antony --- ompi/mca/osc/rdma/osc_rdma.h | 4 +- ompi/mca/osc/rdma/osc_rdma_comm.c | 40 ++--- ompi/mca/osc/rdma/osc_rdma_component.c | 1 + ompi/mca/osc/rdma/osc_rdma_module.c | 171 ++++++++++++++------- ompi/test/general/win_notify_unsupported.c | 78 +++++----- 5 files changed, 166 insertions(+), 128 deletions(-) diff --git a/ompi/mca/osc/rdma/osc_rdma.h b/ompi/mca/osc/rdma/osc_rdma.h index 295b450ceb9..a208232d6b8 100644 --- a/ompi/mca/osc/rdma/osc_rdma.h +++ b/ompi/mca/osc/rdma/osc_rdma.h @@ -369,7 +369,7 @@ int ompi_osc_rdma_free (struct ompi_win_t *win); #define OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, rank) \ if (OPAL_UNLIKELY(NULL == (module)->notify_counts || (notify) < 0 || \ (notify) >= (module)->notify_counts[(rank)])) { \ - return MPI_ERR_NOTIFY_IDX; \ + return MPI_ERR_RMA_NOTIFICATION; \ } /** @@ -395,6 +395,8 @@ int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value); int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t *info, int num_notifications); +int ompi_osc_rdma_win_get_notify_bounds (struct ompi_win_t *win, int *num_sb, int *num_ub, + OMPI_MPI_COUNT_TYPE *value_ub); int ompi_osc_rdma_win_get_num_notify (struct ompi_win_t *win, int target_rank, int *num_notifications); diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.c b/ompi/mca/osc/rdma/osc_rdma_comm.c index 5af7e599b05..3d88ca96a60 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.c +++ b/ompi/mca/osc/rdma/osc_rdma_comm.c @@ -969,21 +969,23 @@ static void ompi_osc_rdma_notify_atomic_complete (void *cbdata, void *cbcontext, * the notification is visible at the target) waits for it. */ /** - * @brief can this notified operation be carried by a single btl operation? + * @brief can this notified put be carried by a single btl operation? * * When the btl provides notification counters the notification is a side * effect of the data movement: the origin targets the registration handle the * target published for that notification index and the adapter increments the - * matching counter once per operation it completes. That equivalence only + * matching counter once per remote write it completes. That equivalence only * holds when the transfer maps to exactly one btl operation, since the counter * counts operations and the standard requires exactly one increment per * notified call, so everything that might be split or served without touching - * the adapter has to take the atomic path instead. + * the adapter has to take the atomic path instead. The counters only count + * remote writes (see MCA_BTL_FLAGS_NOTIFIED_RMA), so a notified get always + * takes the atomic path. */ static inline bool ompi_osc_rdma_notify_single_op (ompi_osc_rdma_module_t *module, ompi_osc_rdma_peer_t *peer, size_t origin_count, ompi_datatype_t *origin_datatype, size_t target_count, ompi_datatype_t *target_datatype, - size_t limit, size_t alignment, size_t *size) + size_t limit, size_t *size) { size_t origin_size, target_size; @@ -1011,12 +1013,6 @@ static inline bool ompi_osc_rdma_notify_single_op (ompi_osc_rdma_module_t *modul return false; } - /* a transfer that does not meet the btl's alignment requirement is broken - * into a head, a body and a tail */ - if (alignment && (origin_size & (alignment - 1))) { - return false; - } - *size = origin_size; return true; @@ -1077,7 +1073,7 @@ int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi * with the put itself: no second operation to issue and nothing to order * it against, so the origin never has to stall here */ if (ompi_osc_rdma_notify_single_op (module, peer, origin_count, origin_datatype, target_count, - target_datatype, module->put_limit, 0, &size)) { + target_datatype, module->put_limit, &size)) { mca_btl_base_registration_handle_t *target_handle; uint64_t target_address; @@ -1115,7 +1111,6 @@ int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datat ompi_osc_rdma_module_t *module = GET_MODULE(win); ompi_osc_rdma_peer_t *peer; ompi_osc_rdma_sync_t *sync; - size_t size; int ret; OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "get_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", @@ -1129,25 +1124,8 @@ int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datat return OMPI_ERR_RMA_SYNC; } - if (ompi_osc_rdma_notify_single_op (module, peer, origin_count, origin_datatype, source_count, - source_datatype, module->get_limit, module->get_alignment, &size)) { - mca_btl_base_registration_handle_t *source_handle; - uint64_t source_address; - - ret = osc_rdma_get_remote_segment (module, peer, source_disp, size, &source_address, &source_handle); - if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { - return ret; - } - - /* an unaligned source is fetched as a head, a body and a tail, which - * the adapter would count three times */ - if (!(source_address & ALIGNMENT_MASK(module->get_alignment))) { - return ompi_osc_rdma_get_contig (sync, peer, source_address, - ompi_osc_rdma_peer_notify_handle (module, source_rank, notify), - origin_addr, size, NULL); - } - } - + /* no btl notification counter path: the counters only count remote writes, + * so a remote read through a notification handle would never be counted */ ret = ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, source_disp, source_count, source_datatype, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { diff --git a/ompi/mca/osc/rdma/osc_rdma_component.c b/ompi/mca/osc/rdma/osc_rdma_component.c index 6ebf144f3e2..d7acab3a80c 100644 --- a/ompi/mca/osc/rdma/osc_rdma_component.c +++ b/ompi/mca/osc/rdma/osc_rdma_component.c @@ -139,6 +139,7 @@ ompi_osc_base_module_t ompi_osc_rdma_module_rdma_template = { .osc_win_reset_notify_value = ompi_osc_rdma_win_reset_notify_value, .osc_win_set_num_notify = ompi_osc_rdma_win_set_num_notify, .osc_win_get_num_notify = ompi_osc_rdma_win_get_num_notify, + .osc_win_get_notify_bounds = ompi_osc_rdma_win_get_notify_bounds, .osc_fence = ompi_osc_rdma_fence_atomic, diff --git a/ompi/mca/osc/rdma/osc_rdma_module.c b/ompi/mca/osc/rdma/osc_rdma_module.c index 1ad8d56b61b..d0dc9538202 100644 --- a/ompi/mca/osc/rdma/osc_rdma_module.c +++ b/ompi/mca/osc/rdma/osc_rdma_module.c @@ -280,6 +280,57 @@ static int ompi_osc_rdma_notify_counters_setup (ompi_osc_rdma_module_t *module, return OMPI_SUCCESS; } +/** + * @brief atomically read and zero notification counter {notify} in the window state + * + * Remote origins increment this counter with btl atomics in both notification + * modes: a window that notifies through btl notification counters still falls + * back to the state counter for every transfer the adapter cannot count + * (non-contiguous datatypes, transfers over the put limit, gets). The + * read-and-zero therefore has to be atomic with respect to btl atomics. Use a + * CPU atomic only when the peer flags indicate it is safe to mix CPU and btl + * atomics, otherwise loop over a btl compare-and-swap on our own state. + */ +static int ompi_osc_rdma_notify_state_fetch_and_zero (ompi_osc_rdma_module_t *module, int notify, + ompi_osc_rdma_lock_t *value) +{ + ompi_osc_rdma_peer_t *my_peer = module->my_peer; + ompi_osc_rdma_lock_t old_value; + + if (ompi_osc_rdma_peer_local_state (my_peer)) { + old_value = module->state->notify_counters[notify]; + while (!ompi_osc_rdma_lock_compare_exchange ((osc_rdma_atomic_counter_t *) (module->state->notify_counters + notify), + &old_value, 0)); + } else { + uint64_t address = (uint64_t) (intptr_t) my_peer->state + offsetof (ompi_osc_rdma_state_t, notify_counters) + + (uint64_t) notify * sizeof (osc_rdma_counter_t); + ompi_osc_rdma_lock_t result; + int ret; + + /* the local value is a guess at the current value. if another origin + * updates the counter concurrently the compare-and-swap fails and + * returns the new value to retry with */ + old_value = module->state->notify_counters[notify]; + + do { + ret = ompi_osc_rdma_lock_btl_cswap (module, my_peer, address, old_value, 0, &result); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + + if (result == old_value) { + break; + } + + old_value = result; + } while (1); + } + + *value = old_value; + + return OMPI_SUCCESS; +} + int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value) { ompi_osc_rdma_module_t *module = GET_MODULE(win); @@ -287,6 +338,13 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI OMPI_OSC_RDMA_CHECK_NOTIFY_IDX(module, notify, my_rank); + /* the standard's usage model is polling this function until a counter + * reaches a threshold. progress the module so counter updates arrive even + * when the btl requires target-side progress (emulated atomics). this + * applies with btl notification counters too, since the state counter + * still carries every notification the adapter cannot count */ + ompi_osc_rdma_progress (module); + if (module->use_notify_counters) { mca_btl_base_module_t *btl = module->accelerated_btl; uint64_t hardware; @@ -310,11 +368,6 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI return OMPI_SUCCESS; } - /* the standard's usage model is polling this function until a counter - * reaches a threshold. progress the module so counter updates arrive even - * when the btl requires target-side progress (emulated atomics) */ - ompi_osc_rdma_progress (module); - *value = (OMPI_MPI_COUNT_TYPE) ((volatile osc_rdma_counter_t *) module->state->notify_counters)[notify]; /* ensure loads of the window data are not reordered before the counter read */ opal_atomic_rmb (); @@ -325,7 +378,6 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OMPI_MPI_COUNT_TYPE *value) { ompi_osc_rdma_module_t *module = GET_MODULE(win); - ompi_osc_rdma_peer_t *my_peer = module->my_peer; int my_rank = ompi_comm_rank (module->comm); ompi_osc_rdma_lock_t old_value; int ret; @@ -341,55 +393,28 @@ int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OM return ret; } + ret = ompi_osc_rdma_notify_state_fetch_and_zero (module, notify, &old_value); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; + } + /* the adapter's counter is monotonic and cannot be zeroed, so record * where this reset happened and report differences from there. any * increment that lands between the read above and the store below is * simply attributed to the next interval, which is what an atomic - * fetch-and-zero at the instant of the read would also have done. */ - *value = (OMPI_MPI_COUNT_TYPE) (hardware - module->notify_reset_base[notify]); + * fetch-and-zero at the instant of the read would also have done. the + * base only moves once both halves have been read, so a failure above + * loses no notifications. */ + *value = (OMPI_MPI_COUNT_TYPE) (hardware - module->notify_reset_base[notify]) + + (OMPI_MPI_COUNT_TYPE) old_value; module->notify_reset_base[notify] = hardware; - /* same-node origins notify through the state counter, which only ever - * sees CPU atomics in this mode, so it can be zeroed directly */ - old_value = module->state->notify_counters[notify]; - while (!ompi_osc_rdma_lock_compare_exchange ((osc_rdma_atomic_counter_t *) (module->state->notify_counters + notify), - &old_value, 0)); - - *value += (OMPI_MPI_COUNT_TYPE) old_value; - return OMPI_SUCCESS; } - /* the counter is incremented by remote origins with btl atomics, so the - * read-and-zero must be atomic with respect to those. use a CPU atomic - * only when the peer flags indicate it is safe to mix CPU and btl - * atomics, otherwise loop over a btl compare-and-swap on our own state */ - if (ompi_osc_rdma_peer_local_state (my_peer)) { - old_value = module->state->notify_counters[notify]; - while (!ompi_osc_rdma_lock_compare_exchange ((osc_rdma_atomic_counter_t *) (module->state->notify_counters + notify), - &old_value, 0)); - } else { - uint64_t address = (uint64_t) (intptr_t) my_peer->state + offsetof (ompi_osc_rdma_state_t, notify_counters) + - (uint64_t) notify * sizeof (osc_rdma_counter_t); - ompi_osc_rdma_lock_t result; - - /* the local value is a guess at the current value. if another origin - * updates the counter concurrently the compare-and-swap fails and - * returns the new value to retry with */ - old_value = module->state->notify_counters[notify]; - - do { - ret = ompi_osc_rdma_lock_btl_cswap (module, my_peer, address, old_value, 0, &result); - if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { - return ret; - } - - if (result == old_value) { - break; - } - - old_value = result; - } while (1); + ret = ompi_osc_rdma_notify_state_fetch_and_zero (module, notify, &old_value); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; } *value = (OMPI_MPI_COUNT_TYPE) old_value; @@ -399,27 +424,44 @@ int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OM int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t *info, int num_notifications) { + /* reasons a rank cannot take part, in order of precedence when ranks disagree */ + enum { NOTIFY_OK = 0, NOTIFY_ERR_ARG, NOTIFY_ERR_RMA_SYNC, NOTIFY_ERR_NO_MEM }; + static const int notify_errors[] = {OMPI_SUCCESS, MPI_ERR_ARG, OMPI_ERR_RMA_SYNC, + OMPI_ERR_OUT_OF_RESOURCE}; ompi_osc_rdma_module_t *module = GET_MODULE(win); int my_rank = ompi_comm_rank (module->comm); int max_count = 0, supported, all_supported = 0; + int my_status = NOTIFY_OK, status; int my_count, ret; (void) info; /* "mpi_assert_same_num_notifications" is an optimization hint only */ + /* This is a synchronizing collective, so a rank that rejects its own call + * must not return before the rest of the group knows: it would leave them + * blocked in the allgather below. Every local failure is decided here, + * before any state changes, and agreed on with one allreduce, so the group + * either fails together with the attached counts unchanged or proceeds. */ if (OPAL_UNLIKELY(num_notifications < 0 || num_notifications > OMPI_OSC_RDMA_NOTIFY_MAX)) { - return MPI_ERR_ARG; + my_status = NOTIFY_ERR_ARG; + } else if (OPAL_UNLIKELY(ompi_osc_rdma_access_epoch_active (module))) { + /* it is erroneous to call MPI_WIN_SET_NUM_NOTIFY while an access epoch is open */ + my_status = NOTIFY_ERR_RMA_SYNC; + } else if (NULL == module->notify_counts) { + module->notify_counts = calloc (ompi_comm_size (module->comm), sizeof (int)); + if (OPAL_UNLIKELY(NULL == module->notify_counts)) { + my_status = NOTIFY_ERR_NO_MEM; + } } - /* it is erroneous to call MPI_WIN_SET_NUM_NOTIFY while an access epoch is open */ - if (OPAL_UNLIKELY(ompi_osc_rdma_access_epoch_active (module))) { - return OMPI_ERR_RMA_SYNC; + ret = module->comm->c_coll->coll_allreduce (&my_status, &status, 1, MPI_INT, MPI_MAX, module->comm, + module->comm->c_coll->coll_allreduce_module); + if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { + return ret; } - if (NULL == module->notify_counts) { - module->notify_counts = calloc (ompi_comm_size (module->comm), sizeof (int)); - if (OPAL_UNLIKELY(NULL == module->notify_counts)) { - return OMPI_ERR_OUT_OF_RESOURCE; - } + if (OPAL_UNLIKELY(NOTIFY_OK != status)) { + /* report our own problem if we had one, otherwise the group's */ + return notify_errors[NOTIFY_OK != my_status ? my_status : status]; } /* the number of attached notification counters is never decreased (section 12.6.1) */ @@ -489,3 +531,22 @@ int ompi_osc_rdma_win_get_num_notify (struct ompi_win_t *win, int target_rank, i return OMPI_SUCCESS; } + +int ompi_osc_rdma_win_get_notify_bounds (struct ompi_win_t *win __opal_attribute_unused__, int *num_sb, + int *num_ub, OMPI_MPI_COUNT_TYPE *value_ub) +{ + /* MPI-5.1 section 12.6.1: NUM_UB is the number of counters the + * implementation supports, NUM_SB the number it supports efficiently. The + * counters live in a fixed region of the window state, so any count up to + * OMPI_OSC_RDMA_NOTIFY_MAX costs the same and MPI_WIN_SET_NUM_NOTIFY rejects + * anything beyond it. */ + *num_sb = OMPI_OSC_RDMA_NOTIFY_MAX; + *num_ub = OMPI_OSC_RDMA_NOTIFY_MAX; + + /* the state counters are int64_t and the btl counters are reported as a + * difference from a base, both only ever incremented by one per notified + * operation, and the value is returned as a signed MPI_Count */ + *value_ub = (OMPI_MPI_COUNT_TYPE) INT64_MAX; + + return OMPI_SUCCESS; +} diff --git a/ompi/test/general/win_notify_unsupported.c b/ompi/test/general/win_notify_unsupported.c index ac5754502f9..d33ceea15fc 100644 --- a/ompi/test/general/win_notify_unsupported.c +++ b/ompi/test/general/win_notify_unsupported.c @@ -9,15 +9,20 @@ /* * Notified RMA (MPI-5.1 section 12.6) is optional: an osc component that - * does not implement it leaves the corresponding entries of the module - * struct NULL. Every notified entry point must report that as + * does not implement part of it leaves the corresponding entries of the + * module struct NULL. Every such entry point must report that as * MPI_ERR_UNSUPPORTED_OPERATION rather than calling through a NULL * function pointer. * - * osc/rdma is forced because it is a general-purpose component that does - * not implement any of these, so it exercises the guard on all twelve - * entry points. If it cannot be selected in this build the test reports - * that and passes trivially. + * osc/rdma is forced because it is a general-purpose component that + * implements the blocking put and get with notification and the counter + * management calls, but none of the accumulate or request-based notified + * operations, so it exercises the guard on those entry points. If it cannot + * be selected in this build the test reports that and passes trivially. + * + * Because osc/rdma does implement counter management, the window must also + * report usable notification bounds rather than the zeros cached for a + * component with no support at all. * * Note: the library is compiled with -DNDEBUG, so assert() is a no-op * here -- all verification must go through test_verify(). @@ -25,6 +30,7 @@ #include "ompi_config.h" +#include #include #include @@ -59,33 +65,11 @@ int main(int argc, char *argv[]) int src = 1; int result = 0; - MPI_Count value = 0; - int num = 0; MPI_Request req = MPI_REQUEST_NULL; - /* Counter management and the accessors are usable outside an epoch. */ - rc = MPI_Win_set_num_notify(win, MPI_INFO_NULL, 4); - test_verify("Win_set_num_notify reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); - rc = MPI_Win_get_num_notify(win, 0, &num); - test_verify("Win_get_num_notify reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); - rc = MPI_Win_get_notify_value(win, 0, &value); - test_verify("Win_get_notify_value reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); - rc = MPI_Win_reset_notify_value(win, 0, &value); - test_verify("Win_reset_notify_value reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); - MPI_Win_lock_all(0, win); - /* The four blocking communication operations. */ - rc = MPI_Put_notify(&src, 1, MPI_INT, 0, 0, 1, MPI_INT, 0, win); - test_verify("Put_notify reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); - rc = MPI_Get_notify(&result, 1, MPI_INT, 0, 0, 1, MPI_INT, 0, win); - test_verify("Get_notify reports unsupported", - MPI_ERR_UNSUPPORTED_OPERATION == rc); + /* The blocking accumulate operations. */ rc = MPI_Accumulate_notify(&src, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_SUM, 0, win); test_verify("Accumulate_notify reports unsupported", @@ -113,8 +97,9 @@ int main(int argc, char *argv[]) /* The guard sits ahead of the MPI_PROC_NULL no-op, so an unsupported * operation is reported identically no matter what the target is. */ - rc = MPI_Put_notify(&src, 1, MPI_INT, MPI_PROC_NULL, 0, 1, MPI_INT, 0, win); - test_verify("Put_notify to MPI_PROC_NULL reports unsupported", + rc = MPI_Accumulate_notify(&src, 1, MPI_INT, MPI_PROC_NULL, 0, 1, MPI_INT, + MPI_SUM, 0, win); + test_verify("Accumulate_notify to MPI_PROC_NULL reports unsupported", MPI_ERR_UNSUPPORTED_OPERATION == rc); rc = MPI_Rput_notify(&src, 1, MPI_INT, MPI_PROC_NULL, 0, 1, MPI_INT, 0, win, &req); @@ -124,25 +109,36 @@ int main(int argc, char *argv[]) /* Nothing above should have moved any data. */ test_verify("no unsupported operation touched the window", 0 == base[0]); + MPI_Win_unlock_all(win); + /* MPI-5.1 section 12.2.6: the notification bounds are cached on every - * window, including one whose component cannot do notified communication at - * all. Reporting zero there is the honest answer, and is consistent with - * every operation above having been refused. */ + * window. osc/rdma can attach counters, so it must not report the zeros + * that mean "no notification support". */ int *num_sb = NULL, *num_ub = NULL; MPI_Count *value_ub = NULL; int flag = 0; rc = MPI_Win_get_attr(win, MPI_WIN_NOTIFICATION_NUM_SB, &num_sb, &flag); - test_verify("NUM_SB is present and zero without notification support", - MPI_SUCCESS == rc && flag && NULL != num_sb && 0 == *num_sb); + test_verify("NUM_SB is present and positive", + MPI_SUCCESS == rc && flag && NULL != num_sb && 0 < *num_sb); rc = MPI_Win_get_attr(win, MPI_WIN_NOTIFICATION_NUM_UB, &num_ub, &flag); - test_verify("NUM_UB is present and zero without notification support", - MPI_SUCCESS == rc && flag && NULL != num_ub && 0 == *num_ub); + test_verify("NUM_UB is present and positive", + MPI_SUCCESS == rc && flag && NULL != num_ub && 0 < *num_ub); + test_verify("NUM_SB does not exceed NUM_UB", + NULL != num_sb && NULL != num_ub && *num_sb <= *num_ub); rc = MPI_Win_get_attr(win, MPI_WIN_NOTIFICATION_VALUE_UB, &value_ub, &flag); - test_verify("VALUE_UB is present and zero without notification support", - MPI_SUCCESS == rc && flag && NULL != value_ub && 0 == *value_ub); + test_verify("VALUE_UB is present and positive", + MPI_SUCCESS == rc && flag && NULL != value_ub && 0 < *value_ub); + + /* Every count up to NUM_UB can be attached, and nothing beyond it. */ + if (NULL != num_ub && 0 < *num_ub && INT_MAX > *num_ub) { + rc = MPI_Win_set_num_notify(win, MPI_INFO_NULL, *num_ub); + test_verify("Win_set_num_notify accepts NUM_UB counters", MPI_SUCCESS == rc); + rc = MPI_Win_set_num_notify(win, MPI_INFO_NULL, *num_ub + 1); + test_verify("Win_set_num_notify rejects more than NUM_UB counters", + MPI_ERR_ARG == rc); + } - MPI_Win_unlock_all(win); MPI_Win_free(&win); int r = test_finalize(); From 69fb450250f93d6c6f93b7efe46847dc0659f344 Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Thu, 17 Sep 2026 13:47:14 -0400 Subject: [PATCH 05/10] osc/sm: fail notify collectives together on one bad rank Also unlink the grown counter segment when an attach fails. Signed-off-by: Joseph Antony --- ompi/mca/osc/sm/osc_sm_comm.c | 38 ++++++++++---- ompi/mca/osc/sm/osc_sm_component.c | 28 +++++++++- test/simple/win_notify_multi.c | 83 ++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 16 deletions(-) diff --git a/ompi/mca/osc/sm/osc_sm_comm.c b/ompi/mca/osc/sm/osc_sm_comm.c index 6f0213641e4..d26503d1a59 100644 --- a/ompi/mca/osc/sm/osc_sm_comm.c +++ b/ompi/mca/osc/sm/osc_sm_comm.c @@ -163,6 +163,13 @@ osc_sm_grow_notify_counters(ompi_osc_sm_module_t *module, const unsigned long *n } if (0 != status) { + /* Every MPI process has already tried to attach, so nobody needs the + * name any more. Rank 0 created the backing file and must remove it, + * or it outlives the job in the backing directory. Unlink before + * detaching: detach resets the descriptor, including the name. */ + if (0 == rank) { + opal_shmem_unlink(&new_seg_ds); + } if (NULL != new_base) { opal_shmem_segment_detach(&new_seg_ds); } @@ -249,7 +256,6 @@ ompi_osc_sm_win_set_num_notify(struct ompi_win_t *win, } /* Valid counts come from an int, so they can never equal ULONG_MAX */ requested = ULONG_MAX; - goto agree; } /* mpi_assert_max_num_notify is this rank's promise not to ask for more @@ -257,18 +263,17 @@ ompi_osc_sm_win_set_num_notify(struct ompi_win_t *win, * request above the current capacity but within the promise is served by * growing into a new shared segment. */ - memset((void *) module->notify_bases[rank], 0, - module->node_states[rank].notify_counter_capacity * sizeof(int64_t)); - module->node_states[rank].notify_counter_count = - (requested > module->node_states[rank].notify_counter_capacity) - ? module->node_states[rank].notify_counter_capacity - : (uint32_t) requested; - opal_atomic_wmb(); - if (1 == comm_size) { /* No shared segment for a single-process window; the counters are a * plain allocation, so growing them is a plain reallocation and none of * the collective machinery below applies. */ + memset((void *) module->notify_bases[0], 0, + module->node_states[0].notify_counter_capacity * sizeof(int64_t)); + module->node_states[0].notify_counter_count = + (requested > module->node_states[0].notify_counter_capacity) + ? module->node_states[0].notify_counter_capacity + : (uint32_t) requested; + if (requested > module->node_states[0].notify_counter_capacity) { void *grown = calloc(requested, sizeof(int64_t)); if (NULL == grown) { @@ -282,7 +287,6 @@ ompi_osc_sm_win_set_num_notify(struct ompi_win_t *win, return OMPI_SUCCESS; } -agree: new_caps = malloc(sizeof(*new_caps) * comm_size); if (NULL == new_caps) { return OMPI_ERR_TEMP_OUT_OF_RESOURCE; @@ -325,7 +329,19 @@ ompi_osc_sm_win_set_num_notify(struct ompi_win_t *win, free(new_caps); - return OMPI_SUCCESS; + /* The group agreed the request is valid and fits the existing counters, so + * only now reset them (MPI-5.1 section 12.6.1) and publish the new count -- + * a failed call above leaves both untouched. Origins validate indices + * against the count in node_states, which each rank writes for itself, so + * nobody may return until everyone has published. The barrier provides + * that, and it also ensures no origin can notify us before our reset. */ + memset((void *) module->notify_bases[rank], 0, + module->node_states[rank].notify_counter_capacity * sizeof(int64_t)); + module->node_states[rank].notify_counter_count = (uint32_t) requested; + opal_atomic_wmb(); + + return module->comm->c_coll->coll_barrier(module->comm, + module->comm->c_coll->coll_barrier_module); } int diff --git a/ompi/mca/osc/sm/osc_sm_component.c b/ompi/mca/osc/sm/osc_sm_component.c index dc8403b3410..5fcb00e2558 100644 --- a/ompi/mca/osc/sm/osc_sm_component.c +++ b/ompi/mca/osc/sm/osc_sm_component.c @@ -25,6 +25,8 @@ #include "ompi_config.h" +#include + #include "ompi/mca/osc/osc.h" #include "ompi/mca/osc/base/base.h" #include "ompi/mca/osc/base/osc_base_obj_convert.h" @@ -313,6 +315,7 @@ component_select(struct ompi_win_t *win, void **base, size_t size, ptrdiff_t dis int ret = OMPI_ERROR; size_t memory_alignment = OPAL_ALIGN_MIN; unsigned int notify_assert = 0, notify_reserved = 0; + bool notify_assert_bad = false; assert(MPI_WIN_FLAVOR_SHARED == flavor || MPI_WIN_FLAVOR_ALLOCATE == flavor); @@ -346,7 +349,15 @@ component_select(struct ompi_win_t *win, void **base, size_t size, ptrdiff_t dis /* How many notification counters to reserve per MPI process. Read before * the segment is sized, since the reservation is part of its layout. */ ret = osc_sm_reserved_notify_counters(info, ¬ify_assert, ¬ify_reserved); - if (OMPI_SUCCESS != ret) goto error; + if (OMPI_SUCCESS != ret) { + /* The info key is per process, so a malformed value may be local to + * this rank. Window creation is collective: returning now would leave + * every other rank blocked in the allgather below. A single-process + * window has nobody to agree with and can fail now; otherwise the + * problem rides through that allgather so all ranks fail together. */ + if (1 == comm_size) goto error; + notify_assert_bad = true; + } module->notify_max_assert = notify_assert; snprintf(module->notify_max_assert_str, sizeof(module->notify_max_assert_str), "%u", notify_assert); @@ -428,7 +439,9 @@ component_select(struct ompi_win_t *win, void **base, size_t size, ptrdiff_t dis } gather_values[0] = size; - gather_values[1] = notify_reserved; + /* Reservations come from an unsigned int, so they can never equal + * ULONG_MAX */ + gather_values[1] = notify_assert_bad ? ULONG_MAX : notify_reserved; ret = module->comm->c_coll->coll_allgather(gather_values, 2, MPI_UNSIGNED_LONG, rbuf, 2, MPI_UNSIGNED_LONG, module->comm, @@ -438,6 +451,17 @@ component_select(struct ompi_win_t *win, void **base, size_t size, ptrdiff_t dis goto error; } + for (i = 0 ; i < comm_size ; ++i) { + if (ULONG_MAX == rbuf[2 * i + 1]) { + /* Some rank was given a malformed mpi_assert_max_num_notify. + * Every rank sees the same gathered array, so they all fail + * here, before the segment is created. */ + free(rbuf); + ret = MPI_ERR_INFO; + goto error; + } + } + total = 0; total_counters = 0; for (i = 0 ; i < comm_size ; ++i) { diff --git a/test/simple/win_notify_multi.c b/test/simple/win_notify_multi.c index c668cbfe565..40bd672c786 100644 --- a/test/simple/win_notify_multi.c +++ b/test/simple/win_notify_multi.c @@ -16,9 +16,10 @@ * untested: the notification counters carved out of the shared segment at * window creation, the allgather that sizes them, the collective * reallocation MPI_Win_set_num_notify performs when a rank asks for more - * counters than were reserved, and the recomputation of each rank's - * counter base that follows it. This program exercises those, so it has - * to be launched as a real job: + * counters than were reserved, the recomputation of each rank's counter + * base that follows it, and the group failing together when only one rank + * passes an invalid argument to a collective. This program exercises + * those, so it has to be launched as a real job: * * mpirun --np 4 ./win_notify_multi * @@ -353,6 +354,80 @@ static void test_info_and_attributes(void) /* ------------------------------------------------------------------ */ +/* mpi_assert_max_num_notify is per process, so a malformed value may reach + * only one rank. Window creation is collective: that rank must not give up + * on its own, or every other rank blocks in the creation forever. They all + * have to fail together instead. */ +static void test_one_rank_bad_assert(void) +{ + MPI_Info info = MPI_INFO_NULL; + MPI_Win win = MPI_WIN_NULL; + int *base = NULL; + int rc; + + MPI_Info_create(&info); + MPI_Info_set(info, "mpi_assert_max_num_notify", (0 == rank) ? "-1" : "8"); + + rc = MPI_Win_allocate_shared(win_elems * sizeof(int), sizeof(int), info, + MPI_COMM_WORLD, &base, &win); + MPI_Info_free(&info); + + /* A window that exists on some ranks and not others cannot be freed + * collectively, so a wrongly created one is reported and left alone. */ + check("window creation fails on every rank when one rank's assertion " + "is malformed", MPI_SUCCESS != rc); + MPI_Barrier(MPI_COMM_WORLD); +} + +/* The same holds for MPI_Win_set_num_notify: a rank with an invalid count + * must not leave the group blocked, and ranks whose own count was valid must + * not act on it. The call fails everywhere, with every attached count and + * every counter as it was. */ +static void test_one_rank_bad_count(void) +{ + int *base = NULL; + MPI_Win win; + int right = (rank + 1) % nprocs; + int num = -1; + int i, rc; + + win = make_window(MPI_INFO_NULL, &base); + if (MPI_WIN_NULL == win) { + return; + } + + rc = MPI_Win_set_num_notify(win, MPI_INFO_NULL, 2); + check("Win_set_num_notify succeeds within the reservation", + MPI_SUCCESS == rc); + MPI_Barrier(MPI_COMM_WORLD); + + MPI_Win_lock_all(0, win); + rc = MPI_Put_notify(&rank, 1, MPI_INT, right, 0, 1, MPI_INT, 1, win); + check("Put_notify to the right neighbour succeeds", MPI_SUCCESS == rc); + MPI_Win_flush(right, win); + check("the notification from the left neighbour arrives", + await_notify(win, 1, 1)); + MPI_Win_unlock_all(win); + MPI_Barrier(MPI_COMM_WORLD); + + rc = MPI_Win_set_num_notify(win, MPI_INFO_NULL, (0 == rank) ? -1 : 3); + check("Win_set_num_notify fails on every rank when one rank's count " + "is invalid", MPI_ERR_ARG == rc); + + for (i = 0 ; i < nprocs ; ++i) { + rc = MPI_Win_get_num_notify(win, i, &num); + check("a failed Win_set_num_notify leaves every attached count " + "unchanged", MPI_SUCCESS == rc && 2 == num); + } + check("a failed Win_set_num_notify leaves the counters unchanged", + await_notify(win, 1, 1)); + + MPI_Barrier(MPI_COMM_WORLD); + MPI_Win_free(&win); +} + +/* ------------------------------------------------------------------ */ + int main(int argc, char *argv[]) { int *base = NULL; @@ -409,6 +484,8 @@ int main(int argc, char *argv[]) test_collective_growth(); test_asymmetric_counts(); test_info_and_attributes(); + test_one_rank_bad_assert(); + test_one_rank_bad_count(); MPI_Allreduce(&failures, &total, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (0 == rank) { From 642574df2fcf6365cc72478f216807bc40ea991e Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Mon, 9 Nov 2026 08:09:16 -0500 Subject: [PATCH 06/10] btl/ofi: fix inverted result of mca_btl_ofi_context_trylock trylock returned true when the lock was already held, so the component progress loop progressed contexts another thread owned and skipped free ones. Return true when the lock is acquired, and update mca_btl_ofi_context_lock to match. Signed-off-by: Joseph Antony --- opal/mca/btl/ofi/btl_ofi.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opal/mca/btl/ofi/btl_ofi.h b/opal/mca/btl/ofi/btl_ofi.h index c63d82739b8..3ac067f9728 100644 --- a/opal/mca/btl/ofi/btl_ofi.h +++ b/opal/mca/btl/ofi/btl_ofi.h @@ -397,14 +397,15 @@ int mca_btl_ofi_post_recvs(mca_btl_base_module_t *module, mca_btl_ofi_context_t void mca_btl_ofi_exit(void); /* thread atomics */ +/* returns true if the lock was acquired */ static inline bool mca_btl_ofi_context_trylock(mca_btl_ofi_context_t *context) { - return (context->lock || OPAL_ATOMIC_SWAP_32(&context->lock, 1)); + return !(context->lock || OPAL_ATOMIC_SWAP_32(&context->lock, 1)); } static inline void mca_btl_ofi_context_lock(mca_btl_ofi_context_t *context) { - while (mca_btl_ofi_context_trylock(context)) + while (!mca_btl_ofi_context_trylock(context)) ; } From 195cd9e43248452072f41c1ece93c21caf81bccb Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Mon, 9 Nov 2026 08:09:18 -0500 Subject: [PATCH 07/10] btl/ofi: always ask the rcache for full access mca_btl_ofi_reg_mem registers every region with full libfabric access, but register_mem passed the caller's access flags to the rcache. grdma reuses a cached registration only if it has all requested rights, so memory used locally first and later exposed remotely was registered a second time. ofi_rxm rejects the duplicate key, and its cleanup removes the first registration's key too, so the peer's next atomic fails with "Permission denied". Request MCA_BTL_REG_FLAG_ACCESS_ANY so a cached registration is always reused. Signed-off-by: Joseph Antony --- opal/mca/btl/ofi/btl_ofi_module.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/opal/mca/btl/ofi/btl_ofi_module.c b/opal/mca/btl/ofi/btl_ofi_module.c index b985e06aab0..aa2b8cc3040 100644 --- a/opal/mca/btl/ofi/btl_ofi_module.c +++ b/opal/mca/btl/ofi/btl_ofi_module.c @@ -213,11 +213,10 @@ void mca_btl_ofi_rcache_init(mca_btl_ofi_module_t *module) static struct mca_btl_base_registration_handle_t * mca_btl_ofi_register_mem(struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, void *base, size_t size, - uint32_t flags) + uint32_t flags __opal_attribute_unused__) { mca_btl_ofi_module_t *ofi_module = (mca_btl_ofi_module_t *) btl; mca_btl_ofi_reg_t *reg; - int access_flags = flags & MCA_BTL_REG_FLAG_ACCESS_ANY; int rc, dev_id; uint32_t cache_flags = 0; uint64_t check_addr_flags; @@ -226,7 +225,12 @@ mca_btl_ofi_register_mem(struct mca_btl_base_module_t *btl, cache_flags |= MCA_RCACHE_FLAGS_ACCELERATOR_MEM; } - rc = ofi_module->rcache->rcache_register(ofi_module->rcache, base, size, cache_flags, access_flags, + // mca_btl_ofi_reg_mem always registers with full access, so ask the + // rcache for full access too. Otherwise a request with more rights than + // a cached registration makes the rcache register the same memory again, + // and ofi_rxm rejects the second registration's duplicate key. + rc = ofi_module->rcache->rcache_register(ofi_module->rcache, base, size, cache_flags, + MCA_BTL_REG_FLAG_ACCESS_ANY, (mca_rcache_base_registration_t **) ®); if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) { return NULL; From b60d7ed9a229f3e9fdc55e2e40db49424d27febb Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Mon, 9 Nov 2026 08:09:42 -0500 Subject: [PATCH 08/10] btl/ofi, osc/rdma: carry notifications as remote CQ data Replace the atomic-add notification with a write that carries the notification key as remote CQ data. The target's progress reads the key from its completion queue and advances the matching fi_cntr, so a notified put is a single network operation. get_notify is a read followed by a zero-byte write with the key. The write is ordered with FI_FENCE where the provider supports it, and otherwise posted from the read's completion. New descriptor flags MCA_BTL_DES_FLAGS_RDMA_MORE and MCA_BTL_DES_FLAGS_RDMA_FENCE let osc/rdma batch the pieces of a noncontiguous put (FI_MORE) and fence the final notifying one. Writes that get -FI_EAGAIN are queued and retried from progress. Signed-off-by: Joseph Antony --- ompi/mca/osc/rdma/osc_rdma.h | 43 ++-- ompi/mca/osc/rdma/osc_rdma_accumulate.c | 8 +- ompi/mca/osc/rdma/osc_rdma_btl_comm.h | 19 +- ompi/mca/osc/rdma/osc_rdma_comm.c | 200 ++++++++--------- ompi/mca/osc/rdma/osc_rdma_comm.h | 9 +- ompi/mca/osc/rdma/osc_rdma_module.c | 101 ++++----- opal/mca/btl/btl.h | 168 ++++++++------ opal/mca/btl/ofi/btl_ofi.h | 115 ++++++++-- opal/mca/btl/ofi/btl_ofi_component.c | 153 ++++++++----- opal/mca/btl/ofi/btl_ofi_context.c | 140 ++++++++++-- opal/mca/btl/ofi/btl_ofi_module.c | 3 + opal/mca/btl/ofi/btl_ofi_notification.c | 278 +++++++++++++----------- opal/mca/btl/ofi/btl_ofi_rdma.c | 267 +++++++++++++++++++---- 13 files changed, 1009 insertions(+), 495 deletions(-) diff --git a/ompi/mca/osc/rdma/osc_rdma.h b/ompi/mca/osc/rdma/osc_rdma.h index a208232d6b8..6a76ad5a9ab 100644 --- a/ompi/mca/osc/rdma/osc_rdma.h +++ b/ompi/mca/osc/rdma/osc_rdma.h @@ -231,12 +231,11 @@ struct ompi_osc_rdma_module_t { * the target's attached count */ int *notify_counts; - /** notification counters bound to the window base by the accelerated btl, - * one per attached notification index. when these are in use an origin - * notifies by targeting the matching registration handle, so the adapter - * increments the counter as a side effect of the data movement itself and - * no separate atomic (and no flush to order it) is needed. NULL entries - * are indices that are not attached. */ + /** notification counters created by the accelerated btl, one per + * attached notification index. when these are in use an origin notifies + * by sending the matching key along with the data movement (btl + * put_notify/get_notify), so no separate atomic (and no flush to order it) + * is needed. NULL entries are indices that are not attached. */ struct mca_btl_base_notification_t *notify_handles[OMPI_OSC_RDMA_NOTIFY_MAX]; /** raw value of each hardware counter at the last reset. the btl never @@ -244,16 +243,13 @@ struct ompi_osc_rdma_module_t { * subsequent reads report the difference. */ uint64_t notify_reset_base[OMPI_OSC_RDMA_NOTIFY_MAX]; - /** registration handles published by every rank for their notification - * counters, packed as [rank * notify_stride + notify] entries of - * notify_handle_size bytes each. NULL when counters are not in use. */ - unsigned char *notify_peer_handles; + /** btl notification keys published by every rank for their notification + * counters, packed as [rank * notify_stride + notify]. NULL when counters + * are not in use. */ + uint64_t *notify_peer_keys; - /** size in bytes of one published notification registration handle */ - size_t notify_handle_size; - - /** number of handles published per rank, i.e. the group-wide maximum - * number of attached notification counters */ + /** number of keys published per rank, i.e. the group-wide maximum number + * of attached notification counters */ int notify_stride; /** true when this window notifies through btl notification counters @@ -373,19 +369,16 @@ int ompi_osc_rdma_free (struct ompi_win_t *win); } /** - * @brief registration handle rank {rank} published for its notification counter {notify} + * @brief btl notification key rank {rank} published for its notification counter {notify} * - * An origin targets this handle instead of the ordinary window handle to make - * the adapter at the target count the operation. Only valid when - * module->use_notify_counters is set. + * An origin passes this key to the btl's put_notify/get_notify to make the + * target advance that counter once the operation has completed there. Only + * valid when module->use_notify_counters is set. */ -static inline mca_btl_base_registration_handle_t * -ompi_osc_rdma_peer_notify_handle (ompi_osc_rdma_module_t *module, int rank, int notify) +static inline const uint64_t * +ompi_osc_rdma_peer_notify_key (ompi_osc_rdma_module_t *module, int rank, int notify) { - size_t offset = ((size_t) rank * (size_t) module->notify_stride + (size_t) notify) - * module->notify_handle_size; - - return (mca_btl_base_registration_handle_t *) (module->notify_peer_handles + offset); + return module->notify_peer_keys + (size_t) rank * (size_t) module->notify_stride + (size_t) notify; } void ompi_osc_rdma_notify_counters_destroy (ompi_osc_rdma_module_t *module); diff --git a/ompi/mca/osc/rdma/osc_rdma_accumulate.c b/ompi/mca/osc/rdma/osc_rdma_accumulate.c index 2c03651926e..9893a412881 100644 --- a/ompi/mca/osc/rdma/osc_rdma_accumulate.c +++ b/ompi/mca/osc/rdma/osc_rdma_accumulate.c @@ -493,7 +493,7 @@ static inline int ompi_osc_rdma_gacc_contig (ompi_osc_rdma_sync_t *sync, const v } if (&ompi_mpi_op_replace.op == op) { - return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, (void *) source, len, request); + return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, (void *) source, len, request, 0, NULL); } if (&ompi_mpi_op_no_op.op != op) { @@ -513,7 +513,7 @@ static inline int ompi_osc_rdma_gacc_contig (ompi_osc_rdma_sync_t *sync, const v return ret; } - return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, ptr, len, request); + return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, ptr, len, request, 0, NULL); } /* nothing more to do for this request */ @@ -522,7 +522,7 @@ static inline int ompi_osc_rdma_gacc_contig (ompi_osc_rdma_sync_t *sync, const v return OMPI_SUCCESS; } - return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, (void *) source, len, request); + return ompi_osc_rdma_put_contig (sync, peer, target_address, target_handle, (void *) source, len, request, 0, NULL); } static void ompi_osc_rdma_gacc_master_cleanup (ompi_osc_rdma_request_t *request) @@ -913,7 +913,7 @@ static inline int cas_rdma (ompi_osc_rdma_sync_t *sync, const void *source_addr, do { ret = ompi_osc_rdma_btl_put(module, peer->data_btl_index, peer->data_endpoint, ptr, target_address, local_handle, target_handle, - len, 0, MCA_BTL_NO_ORDER, + len, 0, MCA_BTL_NO_ORDER, NULL, ompi_osc_rdma_cas_put_complete, (void *) &complete, NULL); if (OPAL_SUCCESS == ret || (OPAL_ERR_OUT_OF_RESOURCE != ret && OPAL_ERR_TEMP_OUT_OF_RESOURCE != ret)) { break; diff --git a/ompi/mca/osc/rdma/osc_rdma_btl_comm.h b/ompi/mca/osc/rdma/osc_rdma_btl_comm.h index 718036d0a76..2d64005b627 100644 --- a/ompi/mca/osc/rdma/osc_rdma_btl_comm.h +++ b/ompi/mca/osc/rdma/osc_rdma_btl_comm.h @@ -26,23 +26,32 @@ void ompi_osc_rdma_atomic_complete(mca_btl_base_module_t *btl, struct mca_btl_ba void *context, void *data, int status); +/* {notify_key}, when not NULL, notifies the target through btl notified RMA. that + * is only valid when the accelerated btl sets MCA_BTL_FLAGS_NOTIFIED_RMA, which + * module->use_notify_counters guarantees */ static inline int ompi_osc_rdma_btl_put(ompi_osc_rdma_module_t *module, uint8_t btl_index, struct mca_btl_base_endpoint_t *endpoint, void *local_address, uint64_t remote_address, struct mca_btl_base_registration_handle_t *local_handle, struct mca_btl_base_registration_handle_t *remote_handle, - size_t size, int flags, int order, + size_t size, int flags, int order, const uint64_t *notify_key, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata) { if (module->use_accelerated_btl) { mca_btl_base_module_t *btl = ompi_osc_rdma_selected_btl(module, btl_index); + if (NULL != notify_key) { + return btl->btl_put_notify(btl, endpoint, local_address, remote_address, + local_handle, remote_handle, size, flags, order, + *notify_key, cbfunc, cbcontext, cbdata); + } return btl->btl_put(btl, endpoint, local_address, remote_address, local_handle, remote_handle, size, flags, order, cbfunc, cbcontext, cbdata); } else { mca_btl_base_am_rdma_module_t *am_rdma = ompi_osc_rdma_selected_am_rdma(module, btl_index); + assert (NULL == notify_key); return am_rdma->am_btl_put(am_rdma, endpoint, local_address, remote_address, local_handle, remote_handle, size, flags, order, cbfunc, cbcontext, cbdata); @@ -56,18 +65,24 @@ ompi_osc_rdma_btl_get(ompi_osc_rdma_module_t *module, uint8_t btl_index, void *local_address, uint64_t remote_address, struct mca_btl_base_registration_handle_t *local_handle, struct mca_btl_base_registration_handle_t *remote_handle, - size_t size, int flags, int order, + size_t size, int flags, int order, const uint64_t *notify_key, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata) { if (module->use_accelerated_btl) { mca_btl_base_module_t *btl = ompi_osc_rdma_selected_btl(module, btl_index); + if (NULL != notify_key) { + return btl->btl_get_notify(btl, endpoint, local_address, remote_address, + local_handle, remote_handle, size, flags, order, + *notify_key, cbfunc, cbcontext, cbdata); + } return btl->btl_get(btl, endpoint, local_address, remote_address, local_handle, remote_handle, size, flags, order, cbfunc, cbcontext, cbdata); } else { mca_btl_base_am_rdma_module_t *am_rdma = ompi_osc_rdma_selected_am_rdma(module, btl_index); + assert (NULL == notify_key); return am_rdma->am_btl_get(am_rdma, endpoint, local_address, remote_address, local_handle, remote_handle, size, flags, order, cbfunc, cbcontext, cbdata); diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.c b/ompi/mca/osc/rdma/osc_rdma_comm.c index 3d88ca96a60..ef98b033087 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.c +++ b/ompi/mca/osc/rdma/osc_rdma_comm.c @@ -52,7 +52,7 @@ static inline void ompi_osc_rdma_cleanup_rdma (ompi_osc_rdma_sync_t *sync, bool static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t source_address, mca_btl_base_registration_handle_t *source_handle, void *target_buffer, size_t size, - ompi_osc_rdma_request_t *request); + ompi_osc_rdma_request_t *request, int flags, const uint64_t *notify_key); static void ompi_osc_get_data_complete (struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, void *local_address, mca_btl_base_registration_handle_t *local_handle, @@ -108,7 +108,7 @@ int ompi_osc_get_data_blocking (ompi_osc_rdma_module_t *module, uint8_t btl_inde do { ret = ompi_osc_rdma_btl_get(module, btl_index, endpoint, ptr, aligned_addr, local_handle, source_handle, aligned_len, 0, MCA_BTL_NO_ORDER, - ompi_osc_get_data_complete, (void *) &read_complete, NULL); + NULL, ompi_osc_get_data_complete, (void *) &read_complete, NULL); if (!ompi_osc_rdma_oor (ret)) { break; } @@ -157,6 +157,8 @@ int ompi_osc_get_data_blocking (ompi_osc_rdma_module_t *module, uint8_t btl_inde * @param[in] size number of bytes to transfer * @param[in] module osc rdma module * @param[in] request osc rdma request if used (can be NULL) + * @param[in] flags btl flags for the transfer (MCA_BTL_DES_FLAGS_RDMA_*) + * @param[in] notify_key if not NULL, notify the target with this btl notification key * * @returns OMPI_SUCCESS on success * @returns OMPI_ERR_OUT_OF_RESOURCE on temporary error @@ -166,7 +168,7 @@ int ompi_osc_get_data_blocking (ompi_osc_rdma_module_t *module, uint8_t btl_inde */ typedef int (*ompi_osc_rdma_fn_t) (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t remote_address, mca_btl_base_registration_handle_t *remote_handle, void *local_address, size_t size, - ompi_osc_rdma_request_t *request); + ompi_osc_rdma_request_t *request, int flags, const uint64_t *notify_key); /** * @brief break down rdma transaction into contiguous regions @@ -184,15 +186,21 @@ typedef int (*ompi_osc_rdma_fn_t) (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_pee * @param[in] max_rdma_len maximum length of an rdma request (usually btl limitation) * @param[in] rdma_fn function to use for contiguous rdma operations * @param[in] alloc_reqs true if rdma_fn requires a valid request object (any allocated objects will be marked internal) + * @param[in] notify_key if not NULL, notify the target with this btl notification key once the whole + * transfer has completed there * * This function does the work of breaking a non-contiguous rdma transfer into contiguous components. It will * continue to submit rdma transfers until the entire region is transferred or a fatal error occurs. + * + * Every transfer but the last is posted with MCA_BTL_DES_FLAGS_RDMA_MORE so the btl can batch them on their way + * to the network. A notification rides on the last transfer, fenced behind the earlier ones so the target is only + * told once all of the data is in place, and so is delivered exactly once per call. */ static int ompi_osc_rdma_master_noncontig (ompi_osc_rdma_sync_t *sync, void *local_address, size_t local_count, ompi_datatype_t *local_datatype, ompi_osc_rdma_peer_t *peer, uint64_t remote_address, mca_btl_base_registration_handle_t *remote_handle, size_t remote_count, ompi_datatype_t *remote_datatype, ompi_osc_rdma_request_t *request, const size_t max_rdma_len, - const ompi_osc_rdma_fn_t rdma_fn, const bool alloc_reqs) + const ompi_osc_rdma_fn_t rdma_fn, const bool alloc_reqs, const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; struct iovec local_iovec[OMPI_OSC_RDMA_DECODE_MAX], remote_iovec[OMPI_OSC_RDMA_DECODE_MAX]; @@ -201,12 +209,19 @@ static int ompi_osc_rdma_master_noncontig (ompi_osc_rdma_sync_t *sync, void *loc uint32_t local_iov_index, remote_iov_index; /* needed for opal_convertor_raw but not used */ size_t local_size, remote_size, rdma_len; + size_t total_size, scheduled = 0; ompi_osc_rdma_request_t *subreq; + bool done, last, force_flush = false; + int flags; int ret; - bool done; subreq = NULL; + /* the convertors cannot tell whether a segment is the last one (the final decode may come back empty), so + * count bytes instead */ + ompi_datatype_type_size (remote_datatype, &total_size); + total_size *= remote_count; + OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "scheduling rdma on non-contiguous datatype(s) or large region"); /* prepare convertors for the source and target. these convertors will be used to determine the @@ -274,8 +289,19 @@ static int ompi_osc_rdma_master_noncontig (ompi_osc_rdma_sync_t *sync, void *loc local_iovec[local_iov_index].iov_base, remote_iovec[remote_iov_index].iov_base, (unsigned long) remote_iovec[remote_iov_index].iov_len); + last = (scheduled + rdma_len == total_size); + if (last) { + /* fence the notification behind the earlier pieces, if any */ + flags = (NULL != notify_key && 0 != scheduled) ? MCA_BTL_DES_FLAGS_RDMA_FENCE : 0; + } else { + /* once the btl has pushed back, post without the hint so it hands everything it has + * batched to the network instead of waiting for more */ + flags = force_flush ? 0 : MCA_BTL_DES_FLAGS_RDMA_MORE; + } + ret = rdma_fn (sync, peer, (uint64_t) (intptr_t) remote_iovec[remote_iov_index].iov_base, remote_handle, - local_iovec[local_iov_index].iov_base, rdma_len, subreq); + local_iovec[local_iov_index].iov_base, rdma_len, subreq, flags, + last ? notify_key : NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { if (OPAL_UNLIKELY(OMPI_ERR_OUT_OF_RESOURCE != ret)) { if (request) { @@ -291,10 +317,13 @@ static int ompi_osc_rdma_master_noncontig (ompi_osc_rdma_sync_t *sync, void *loc } /* progress and try again */ + force_flush = true; ompi_osc_rdma_progress (module); continue; } subreq = NULL; + force_flush = false; + scheduled += rdma_len; /* adjust io vectors */ local_iovec[local_iov_index].iov_len -= rdma_len; @@ -328,7 +357,8 @@ static inline int ompi_osc_rdma_master (ompi_osc_rdma_sync_t *sync, void *local_ uint64_t remote_address, mca_btl_base_registration_handle_t *remote_handle, size_t remote_count, ompi_datatype_t *remote_datatype, ompi_osc_rdma_request_t *request, const size_t max_rdma_len, - const ompi_osc_rdma_fn_t rdma_fn, const bool alloc_reqs) + const ompi_osc_rdma_fn_t rdma_fn, const bool alloc_reqs, + const uint64_t *notify_key) { size_t rdma_len; ptrdiff_t lb, extent; @@ -359,7 +389,8 @@ static inline int ompi_osc_rdma_master (ompi_osc_rdma_sync_t *sync, void *local_ rdma_len); do { - ret = rdma_fn (sync, peer, remote_address, remote_handle, local_address, rdma_len, request); + ret = rdma_fn (sync, peer, remote_address, remote_handle, local_address, rdma_len, request, 0, + notify_key); if (OPAL_LIKELY(OPAL_SUCCESS == ret)) { return OMPI_SUCCESS; } @@ -370,7 +401,7 @@ static inline int ompi_osc_rdma_master (ompi_osc_rdma_sync_t *sync, void *local_ return ompi_osc_rdma_master_noncontig (sync, local_address, local_count, local_datatype, peer, remote_address, remote_handle, remote_count, remote_datatype, request, - max_rdma_len, rdma_fn, alloc_reqs); + max_rdma_len, rdma_fn, alloc_reqs, notify_key); } static int ompi_osc_rdma_copy_local (const void *source, size_t source_count, ompi_datatype_t *source_datatype, @@ -454,7 +485,8 @@ static void ompi_osc_rdma_put_complete_flush (struct mca_btl_base_module_t *btl, static int ompi_osc_rdma_put_real (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t target_address, mca_btl_base_registration_handle_t *target_handle, void *ptr, mca_btl_base_registration_handle_t *local_handle, size_t size, - mca_btl_base_rdma_completion_fn_t cb, void *context, void *cbdata) { + mca_btl_base_rdma_completion_fn_t cb, void *context, void *cbdata, + int flags, const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; int ret; @@ -467,7 +499,7 @@ static int ompi_osc_rdma_put_real (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_pee do { ret = ompi_osc_rdma_btl_put(module, peer->data_btl_index, peer->data_endpoint, ptr, target_address, local_handle, target_handle, - size, 0, MCA_BTL_NO_ORDER, cb, context, cbdata); + size, flags, MCA_BTL_NO_ORDER, notify_key, cb, context, cbdata); if (OPAL_UNLIKELY(OMPI_SUCCESS == ret)) { return OMPI_SUCCESS; } @@ -478,6 +510,9 @@ static int ompi_osc_rdma_put_real (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_pee break; } + /* the btl pushed back: don't ask it to hold this one for more */ + flags &= ~MCA_BTL_DES_FLAGS_RDMA_MORE; + /* spin a bit on progress */ ompi_osc_rdma_progress (module); } while (1); @@ -489,7 +524,7 @@ static int ompi_osc_rdma_put_real (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_pee int ompi_osc_rdma_put_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t target_address, mca_btl_base_registration_handle_t *target_handle, void *source_buffer, size_t size, - ompi_osc_rdma_request_t *request) + ompi_osc_rdma_request_t *request, int flags, const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; mca_btl_base_registration_handle_t *local_handle = NULL; @@ -543,7 +578,7 @@ int ompi_osc_rdma_put_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t * } ret = ompi_osc_rdma_put_real (sync, peer, target_address, target_handle, ptr, local_handle, size, cbfunc, - cbcontext, frag); + cbcontext, frag, flags, notify_key); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { ompi_osc_rdma_cleanup_rdma (sync, false, frag, local_handle, request); } @@ -607,7 +642,7 @@ static int ompi_osc_rdma_get_partial (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_ subreq->parent_request = request; (void) OPAL_THREAD_ADD_FETCH32 (&request->outstanding_requests, 1); - ret = ompi_osc_rdma_get_contig (sync, peer, source_address, source_handle, target_buffer, size, subreq); + ret = ompi_osc_rdma_get_contig (sync, peer, source_address, source_handle, target_buffer, size, subreq, 0, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { OMPI_OSC_RDMA_REQUEST_RETURN(subreq); ompi_osc_rdma_request_deref (request); @@ -618,7 +653,7 @@ static int ompi_osc_rdma_get_partial (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t source_address, mca_btl_base_registration_handle_t *source_handle, void *target_buffer, size_t size, - ompi_osc_rdma_request_t *request) + ompi_osc_rdma_request_t *request, int flags, const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; const size_t btl_alignment_mask = ALIGNMENT_MASK(module->get_alignment); @@ -658,6 +693,8 @@ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_p if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { return ret; } + /* a notification must also cover the partial read */ + flags |= MCA_BTL_DES_FLAGS_RDMA_FENCE; source_address += subsize; target_buffer = (void *) ((intptr_t) target_buffer + subsize); @@ -678,6 +715,8 @@ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_p if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { return ret; } + /* a notification must also cover the partial read */ + flags |= MCA_BTL_DES_FLAGS_RDMA_FENCE; } /* (remaining) user request is now correctly aligned */ } @@ -728,7 +767,7 @@ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_p do { ret = ompi_osc_rdma_btl_get(module, peer->data_btl_index, peer->data_endpoint, ptr, aligned_source_base, local_handle, source_handle, - aligned_len, 0, MCA_BTL_NO_ORDER, + aligned_len, flags, MCA_BTL_NO_ORDER, notify_key, ompi_osc_rdma_get_complete, request, frag); if (OPAL_LIKELY(OMPI_SUCCESS == ret)) { return OMPI_SUCCESS; @@ -740,6 +779,9 @@ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_p break; } + /* the btl pushed back: don't ask it to hold this one for more */ + flags &= ~MCA_BTL_DES_FLAGS_RDMA_MORE; + /* spin a bit on progress */ for (int i = 0 ; i < 10 ; ++i) { ompi_osc_rdma_progress (module); @@ -756,7 +798,8 @@ static int ompi_osc_rdma_get_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_p static inline int ompi_osc_rdma_put_w_req (ompi_osc_rdma_sync_t *sync, const void *origin_addr, int origin_count, ompi_datatype_t *origin_datatype, ompi_osc_rdma_peer_t *peer, ptrdiff_t target_disp, int target_count, - ompi_datatype_t *target_datatype, ompi_osc_rdma_request_t *request) + ompi_datatype_t *target_datatype, ompi_osc_rdma_request_t *request, + const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; mca_btl_base_registration_handle_t *target_handle; @@ -793,12 +836,13 @@ static inline int ompi_osc_rdma_put_w_req (ompi_osc_rdma_sync_t *sync, const voi return ompi_osc_rdma_master (sync, (void *) origin_addr, origin_count, origin_datatype, peer, target_address, target_handle, target_count, target_datatype, request, - module->put_limit, ompi_osc_rdma_put_contig, false); + module->put_limit, ompi_osc_rdma_put_contig, false, notify_key); } static inline int ompi_osc_rdma_get_w_req (ompi_osc_rdma_sync_t *sync, void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, ompi_osc_rdma_peer_t *peer, ptrdiff_t source_disp, size_t source_count, - ompi_datatype_t *source_datatype, ompi_osc_rdma_request_t *request) + ompi_datatype_t *source_datatype, ompi_osc_rdma_request_t *request, + const uint64_t *notify_key) { ompi_osc_rdma_module_t *module = sync->module; mca_btl_base_registration_handle_t *source_handle; @@ -833,7 +877,7 @@ static inline int ompi_osc_rdma_get_w_req (ompi_osc_rdma_sync_t *sync, void *ori return ompi_osc_rdma_master (sync, origin_addr, origin_count, origin_datatype, peer, source_address, source_handle, source_count, source_datatype, request, - module->get_limit, ompi_osc_rdma_get_contig, true); + module->get_limit, ompi_osc_rdma_get_contig, true, notify_key); } int ompi_osc_rdma_put (const void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, int target_rank, ptrdiff_t target_disp, size_t target_count, @@ -853,7 +897,7 @@ int ompi_osc_rdma_put (const void *origin_addr, size_t origin_count, ompi_dataty } return ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, - target_count, target_datatype, NULL); + target_count, target_datatype, NULL, NULL); } int ompi_osc_rdma_rput (const void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, @@ -880,7 +924,7 @@ int ompi_osc_rdma_rput (const void *origin_addr, size_t origin_count, ompi_datat rdma_request->type = OMPI_OSC_RDMA_TYPE_PUT; ret = ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, - target_count, target_datatype, rdma_request); + target_count, target_datatype, rdma_request, NULL); if (OMPI_SUCCESS != ret) { OMPI_OSC_RDMA_REQUEST_RETURN(rdma_request); return ret; @@ -909,7 +953,7 @@ int ompi_osc_rdma_get (void *origin_addr, size_t origin_count, ompi_datatype_t * } return ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, - source_disp, source_count, source_datatype, NULL); + source_disp, source_count, source_datatype, NULL, NULL); } int ompi_osc_rdma_rget (void *origin_addr, size_t origin_count, ompi_datatype_t *origin_datatype, @@ -936,7 +980,7 @@ int ompi_osc_rdma_rget (void *origin_addr, size_t origin_count, ompi_datatype_t rdma_request->type = OMPI_OSC_RDMA_TYPE_GET; ret = ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, - source_disp, source_count, source_datatype, rdma_request); + source_disp, source_count, source_datatype, rdma_request, NULL); if (OMPI_SUCCESS != ret) { OMPI_OSC_RDMA_REQUEST_RETURN(rdma_request); return ret; @@ -959,6 +1003,25 @@ static void ompi_osc_rdma_notify_atomic_complete (void *cbdata, void *cbcontext, ompi_osc_rdma_sync_rdma_dec_always (sync); } +/** + * @brief can this notified operation travel with the btl's notified RMA? + * + * With btl notified RMA the notification is part of the data movement: the + * operation carries the key the target published for the notification index, + * and the target's counter advances once the data has been placed (put) or + * read (get). That covers contiguous and non-contiguous transfers alike, since + * the transfer engine attaches the notification to the last btl operation and + * fences it behind the others. Peers reached with loads and stores never see + * the btl, and empty transfers have nothing to attach the notification to; + * both take the atomic path. + */ +static inline bool ompi_osc_rdma_notify_use_btl (ompi_osc_rdma_module_t *module, ompi_osc_rdma_peer_t *peer, + size_t origin_count, size_t target_count) +{ + return module->use_notify_counters && !ompi_osc_rdma_peer_local_base (peer) + && 0 != origin_count && 0 != target_count; +} + /** * @brief atomically increment notification counter {notify} at {peer} * @@ -968,56 +1031,6 @@ static void ompi_osc_rdma_notify_atomic_complete (void *cbdata, void *cbcontext, * against the sync object so that MPI_WIN_FLUSH (which must guarantee * the notification is visible at the target) waits for it. */ -/** - * @brief can this notified put be carried by a single btl operation? - * - * When the btl provides notification counters the notification is a side - * effect of the data movement: the origin targets the registration handle the - * target published for that notification index and the adapter increments the - * matching counter once per remote write it completes. That equivalence only - * holds when the transfer maps to exactly one btl operation, since the counter - * counts operations and the standard requires exactly one increment per - * notified call, so everything that might be split or served without touching - * the adapter has to take the atomic path instead. The counters only count - * remote writes (see MCA_BTL_FLAGS_NOTIFIED_RMA), so a notified get always - * takes the atomic path. - */ -static inline bool ompi_osc_rdma_notify_single_op (ompi_osc_rdma_module_t *module, ompi_osc_rdma_peer_t *peer, - size_t origin_count, ompi_datatype_t *origin_datatype, - size_t target_count, ompi_datatype_t *target_datatype, - size_t limit, size_t *size) -{ - size_t origin_size, target_size; - - if (!module->use_notify_counters) { - return false; - } - - /* a peer we can reach with loads and stores is served by a direct copy - * that the adapter never observes */ - if (ompi_osc_rdma_peer_local_base (peer)) { - return false; - } - - if (!ompi_datatype_is_contiguous_memory_layout (origin_datatype, origin_count) - || !ompi_datatype_is_contiguous_memory_layout (target_datatype, target_count)) { - return false; - } - - ompi_datatype_type_size (origin_datatype, &origin_size); - ompi_datatype_type_size (target_datatype, &target_size); - origin_size *= origin_count; - target_size *= target_count; - - if (origin_size != target_size || 0 == origin_size || origin_size > limit) { - return false; - } - - *size = origin_size; - - return true; -} - static int ompi_osc_rdma_notify_increment (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, int notify) { ompi_osc_rdma_module_t *module = sync->module; @@ -1055,7 +1068,6 @@ int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi ompi_osc_rdma_module_t *module = GET_MODULE(win); ompi_osc_rdma_peer_t *peer; ompi_osc_rdma_sync_t *sync; - size_t size; int ret; OSC_RDMA_VERBOSE(MCA_BASE_VERBOSE_TRACE, "put_notify: 0x%lx, %zu, %s, %d, %d, %zu, %s, %d, %s", @@ -1069,28 +1081,16 @@ int ompi_osc_rdma_put_notify (const void *origin_addr, size_t origin_count, ompi return OMPI_ERR_RMA_SYNC; } - /* when the btl counts operations in hardware the notification rides along - * with the put itself: no second operation to issue and nothing to order - * it against, so the origin never has to stall here */ - if (ompi_osc_rdma_notify_single_op (module, peer, origin_count, origin_datatype, target_count, - target_datatype, module->put_limit, &size)) { - mca_btl_base_registration_handle_t *target_handle; - uint64_t target_address; - - ret = osc_rdma_get_remote_segment (module, peer, target_disp, size, &target_address, &target_handle); - if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { - return ret; - } - - /* targeting the handle the peer published for this notification index - * is what selects which of its counters the adapter advances */ - return ompi_osc_rdma_put_contig (sync, peer, target_address, - ompi_osc_rdma_peer_notify_handle (module, target_rank, notify), - (void *) (intptr_t) origin_addr, size, NULL); + /* the notification rides along with the data: no second operation to + * issue and nothing to order it against, so the origin never stalls here */ + if (ompi_osc_rdma_notify_use_btl (module, peer, origin_count, target_count)) { + return ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, + target_count, target_datatype, NULL, + ompi_osc_rdma_peer_notify_key (module, target_rank, notify)); } ret = ompi_osc_rdma_put_w_req (sync, origin_addr, origin_count, origin_datatype, peer, target_disp, - target_count, target_datatype, NULL); + target_count, target_datatype, NULL, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { return ret; } @@ -1124,10 +1124,16 @@ int ompi_osc_rdma_get_notify (void *origin_addr, size_t origin_count, ompi_datat return OMPI_ERR_RMA_SYNC; } - /* no btl notification counter path: the counters only count remote writes, - * so a remote read through a notification handle would never be counted */ + /* the btl orders the notification behind the read itself, so the origin + * does not wait for the data before notifying */ + if (ompi_osc_rdma_notify_use_btl (module, peer, origin_count, source_count)) { + return ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, + source_disp, source_count, source_datatype, NULL, + ompi_osc_rdma_peer_notify_key (module, source_rank, notify)); + } + ret = ompi_osc_rdma_get_w_req (sync, origin_addr, origin_count, origin_datatype, peer, - source_disp, source_count, source_datatype, NULL); + source_disp, source_count, source_datatype, NULL, NULL); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { return ret; } diff --git a/ompi/mca/osc/rdma/osc_rdma_comm.h b/ompi/mca/osc/rdma/osc_rdma_comm.h index 45e9bb5a91b..ec87e33a876 100644 --- a/ompi/mca/osc/rdma/osc_rdma_comm.h +++ b/ompi/mca/osc/rdma/osc_rdma_comm.h @@ -126,8 +126,15 @@ int ompi_osc_get_data_blocking (ompi_osc_rdma_module_t *module, uint8_t btl_inde mca_btl_base_registration_handle_t *source_handle, void *data, size_t len); +/** + * @brief put a contiguous region + * + * @param[in] flags btl flags for the put (MCA_BTL_DES_FLAGS_RDMA_*) + * @param[in] notify_key if not NULL, the btl notification key to notify the target with + * (requires a btl with MCA_BTL_FLAGS_NOTIFIED_RMA) + */ int ompi_osc_rdma_put_contig (ompi_osc_rdma_sync_t *sync, ompi_osc_rdma_peer_t *peer, uint64_t target_address, mca_btl_base_registration_handle_t *target_handle, void *source_buffer, size_t size, - ompi_osc_rdma_request_t *request); + ompi_osc_rdma_request_t *request, int flags, const uint64_t *notify_key); #endif /* OMPI_OSC_RDMA_COMM_H */ diff --git a/ompi/mca/osc/rdma/osc_rdma_module.c b/ompi/mca/osc/rdma/osc_rdma_module.c index d0dc9538202..1c5be9f3a6f 100644 --- a/ompi/mca/osc/rdma/osc_rdma_module.c +++ b/ompi/mca/osc/rdma/osc_rdma_module.c @@ -170,103 +170,89 @@ void ompi_osc_rdma_notify_counters_destroy (ompi_osc_rdma_module_t *module) for (int i = 0 ; i < OMPI_OSC_RDMA_NOTIFY_MAX ; ++i) { if (NULL != module->notify_handles[i]) { if (NULL != btl) { - (void) btl->btl_deregister_notification (btl, module->notify_handles[i]); + (void) btl->btl_notification_destroy (btl, module->notify_handles[i]); } module->notify_handles[i] = NULL; } } - free (module->notify_peer_handles); - module->notify_peer_handles = NULL; + free (module->notify_peer_keys); + module->notify_peer_keys = NULL; module->notify_stride = 0; module->use_notify_counters = false; } /** - * @brief can this window notify through btl notification counters? + * @brief can this window notify through btl notified RMA? * - * Dynamic windows are excluded because a counter is bound to one registration - * and the attached regions of a dynamic window come and go. + * The notification key travels with the data movement and works with the + * ordinary registration of whatever region an operation targets, so every + * window flavor (including dynamic windows) qualifies. */ static bool ompi_osc_rdma_notify_counters_available (ompi_osc_rdma_module_t *module) { - return module->use_accelerated_btl && MPI_WIN_FLAVOR_DYNAMIC != module->flavor - && module->use_memory_registration && module->size + return module->use_accelerated_btl && !!(module->accelerated_btl->btl_flags & MCA_BTL_FLAGS_NOTIFIED_RMA); } /** - * @brief attach {my_count} notification counters to the window and publish their handles + * @brief attach {my_count} notification counters to the window and publish their keys * - * Each counter is a separate registration of the same window memory, so an - * origin picks which counter to advance purely by choosing which registration - * handle to target -- the adapter then counts the data movement itself and no + * Each counter is a btl notification. An origin picks which counter to advance + * by sending that counter's key along with the data (btl put_notify/get_notify), + * and the target's btl advances it once the operation has completed there -- no * follow-up atomic (nor a flush to order it behind the data) is required. * * Every rank in the group runs the same sequence of collectives here, whether - * or not its own registrations succeeded, so that a failure on one rank makes + * or not its own counters were created, so that a failure on one rank makes * the whole window fall back rather than deadlocking the rest. */ static int ompi_osc_rdma_notify_counters_setup (ompi_osc_rdma_module_t *module, int my_count, int max_count) { mca_btl_base_module_t *btl = module->accelerated_btl; - size_t handle_size = btl->btl_registration_handle_size; - ompi_osc_rdma_region_t *region; - unsigned char *my_handles = NULL; - int registered_ok = 1; + uint64_t *my_keys = NULL; + int created_ok = 1; int all_ok = 0; int ret; - /* a non-dynamic window always has exactly one region, which describes the - * memory the window was created over */ - region = (ompi_osc_rdma_region_t *) module->state->regions; - - module->notify_handle_size = handle_size; module->notify_stride = max_count; - module->notify_peer_handles = calloc ((size_t) ompi_comm_size (module->comm) * (size_t) max_count, - handle_size); - my_handles = calloc ((size_t) max_count, handle_size); + module->notify_peer_keys = calloc ((size_t) ompi_comm_size (module->comm) * (size_t) max_count, + sizeof (uint64_t)); + my_keys = calloc ((size_t) max_count, sizeof (uint64_t)); - if (NULL == module->notify_peer_handles || NULL == my_handles) { - registered_ok = 0; + if (NULL == module->notify_peer_keys || NULL == my_keys) { + created_ok = 0; } - for (int i = 0 ; registered_ok && i < my_count ; ++i) { - mca_btl_base_registration_handle_t *handle = NULL; - - module->notify_handles[i] = btl->btl_register_notification (btl, - (void *) (intptr_t) region->base, - (size_t) region->len, - MCA_BTL_REG_FLAG_ACCESS_ANY, &handle); - if (OPAL_UNLIKELY(NULL == module->notify_handles[i])) { - /* leaving some indices counted by the adapter and others not would + for (int i = 0 ; created_ok && i < my_count ; ++i) { + ret = btl->btl_notification_create (btl, &module->notify_handles[i], my_keys + i); + if (OPAL_UNLIKELY(OPAL_SUCCESS != ret)) { + /* leaving some indices counted by the btl and others not would * be worse than not using counters at all */ - registered_ok = 0; + module->notify_handles[i] = NULL; + created_ok = 0; break; } - - memcpy (my_handles + (size_t) i * handle_size, handle, handle_size); } - ret = module->comm->c_coll->coll_allreduce (®istered_ok, &all_ok, 1, MPI_INT, MPI_MIN, module->comm, + ret = module->comm->c_coll->coll_allreduce (&created_ok, &all_ok, 1, MPI_INT, MPI_MIN, module->comm, module->comm->c_coll->coll_allreduce_module); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { all_ok = 0; } if (!all_ok) { - free (my_handles); + free (my_keys); ompi_osc_rdma_notify_counters_destroy (module); /* not an error: the caller notifies with atomics instead */ return OMPI_SUCCESS; } - ret = module->comm->c_coll->coll_allgather (my_handles, (int) ((size_t) max_count * handle_size), MPI_BYTE, - module->notify_peer_handles, - (int) ((size_t) max_count * handle_size), MPI_BYTE, + ret = module->comm->c_coll->coll_allgather (my_keys, max_count, MPI_UINT64_T, + module->notify_peer_keys, max_count, MPI_UINT64_T, module->comm, module->comm->c_coll->coll_allgather_module); - free (my_handles); + free (my_keys); if (OPAL_UNLIKELY(OMPI_SUCCESS != ret)) { ompi_osc_rdma_notify_counters_destroy (module); @@ -285,8 +271,8 @@ static int ompi_osc_rdma_notify_counters_setup (ompi_osc_rdma_module_t *module, * * Remote origins increment this counter with btl atomics in both notification * modes: a window that notifies through btl notification counters still falls - * back to the state counter for every transfer the adapter cannot count - * (non-contiguous datatypes, transfers over the put limit, gets). The + * back to the state counter for every transfer the btl does not carry (origins + * on this node that reach the window with loads and stores, empty transfers). The * read-and-zero therefore has to be atomic with respect to btl atomics. Use a * CPU atomic only when the peer flags indicate it is safe to mix CPU and btl * atomics, otherwise loop over a btl compare-and-swap on our own state. @@ -340,9 +326,9 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI /* the standard's usage model is polling this function until a counter * reaches a threshold. progress the module so counter updates arrive even - * when the btl requires target-side progress (emulated atomics). this - * applies with btl notification counters too, since the state counter - * still carries every notification the adapter cannot count */ + * when the btl requires target-side progress: btl notification counters + * only advance when progress processes the completions that carry them, + * and emulated atomics update the state counter the same way */ ompi_osc_rdma_progress (module); if (module->use_notify_counters) { @@ -356,7 +342,7 @@ int ompi_osc_rdma_win_get_notify_value (struct ompi_win_t *win, int notify, OMPI } /* origins on this node reach the window with a direct copy that the - * adapter never sees, so their notifications land in the state counter + * btl never sees, so their notifications land in the state counter * instead. both count operations on this index, so the value the * standard asks for is their sum. */ *value = (OMPI_MPI_COUNT_TYPE) (hardware - module->notify_reset_base[notify] @@ -398,7 +384,7 @@ int ompi_osc_rdma_win_reset_notify_value (struct ompi_win_t *win, int notify, OM return ret; } - /* the adapter's counter is monotonic and cannot be zeroed, so record + /* the btl's counter is monotonic and is never zeroed, so record * where this reset happened and report differences from there. any * increment that lands between the read above and the store below is * simply attributed to the next interval, which is what an atomic @@ -484,10 +470,11 @@ int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t return ret; } - /* A hardware counter cannot be zeroed, so rather than track a reset base - * for a counter nobody has used yet, drop the existing registrations and - * attach fresh ones. This call is collective and no access epoch is open, - * so no origin can be holding a handle we are about to invalidate. */ + /* A btl counter is never zeroed, so rather than track a reset base for a + * counter nobody has used yet, drop the existing counters and attach fresh + * ones. This call is collective and no access epoch is open, so no origin + * can be about to use a key we are about to invalidate, and the btl drops + * any late notification for a destroyed counter. */ ompi_osc_rdma_notify_counters_destroy (module); memset (module->notify_reset_base, 0, sizeof (module->notify_reset_base)); @@ -512,7 +499,7 @@ int ompi_osc_rdma_win_set_num_notify (struct ompi_win_t *win, struct opal_info_t if (all_supported) { /* falls back silently (leaving use_notify_counters clear) if the - * adapter will not hand out counters everywhere */ + * btl will not hand out counters everywhere */ ret = ompi_osc_rdma_notify_counters_setup (module, my_count, max_count); } diff --git a/opal/mca/btl/btl.h b/opal/mca/btl/btl.h index c88bc0c223d..20f1f385917 100644 --- a/opal/mca/btl/btl.h +++ b/opal/mca/btl/btl.h @@ -273,20 +273,18 @@ typedef uint8_t mca_btl_base_tag_t; */ #define MCA_BTL_FLAGS_RDMA_REMOTE_COMPLETION 0x800000 -/** The BTL can associate a hardware notification counter with a memory - * region. The counter is incremented by the network adapter at the target - * once a remote operation on that region has completed there, without any - * involvement of the target's CPU. - * - * A BTL that sets this flag must provide btl_register_notification, - * btl_deregister_notification, btl_notification_read and - * btl_notification_wait. The registration handle returned alongside the - * counter is an ordinary handle: an origin uses it as the remote handle of - * a btl_put, and the counter at the target counts those operations. Only - * operations that modify the region (puts and atomics) are counted; a - * btl_get through the handle is not. This lets a consumer implement - * RMA-with-notification for writes with a single network operation rather - * than a data transfer followed by a separate atomic update. +/** The BTL can carry a notification with an RDMA operation. A notification + * is a counter local to the process that created it, named to origins by a + * key. btl_put_notify and btl_get_notify deliver that key to the target + * alongside the data movement (e.g. as immediate data), and the target + * increments the named counter by one once the operation has completed in its + * memory. This lets a consumer implement RMA-with-notification with the data + * movement itself rather than a transfer followed by a separate atomic. + * + * A BTL that sets this flag must provide btl_notification_create, + * btl_notification_destroy, btl_notification_read, btl_notification_wait, + * btl_put_notify and btl_get_notify. Delivery may require the target to + * progress the BTL. */ #define MCA_BTL_FLAGS_NOTIFIED_RMA 0x1000000 @@ -532,6 +530,22 @@ OPAL_DECLSPEC OBJ_CLASS_DECLARATION(mca_btl_base_descriptor_t); */ #define MCA_BTL_DES_FLAGS_SIGNAL 0x0040 +/* Hint for btl_put, btl_get, btl_put_notify and btl_get_notify: the caller + * will post another RDMA operation immediately after this one returns, so the + * BTL may defer handing this one to the network (e.g. ringing a doorbell) + * until it can do so for both. A caller that sets this flag must follow it + * with an operation that does not. BTLs that cannot use the hint ignore it. + */ +#define MCA_BTL_DES_FLAGS_RDMA_MORE 0x0080 + +/* btl_put_notify and btl_get_notify only: do not deliver this operation (or + * its notification) to the target until every RDMA operation the calling + * thread previously posted to the same endpoint has completed there. Used to + * make a single notification cover a transfer that was split into several + * operations. + */ +#define MCA_BTL_DES_FLAGS_RDMA_FENCE 0x0100 + /** * Maximum number of allowed segments in src/dst fields of a descriptor. */ @@ -1187,65 +1201,58 @@ typedef int (*mca_btl_base_module_flush_fn_t)(struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint); /** - * @brief Opaque handle to a hardware notification counter. + * @brief Opaque handle to a notification counter. * * The contents are private to the BTL. A notification is always local to the - * process that created it; it cannot be passed to or read by a peer. + * process that created it; peers name it by the key returned when it was + * created. */ struct mca_btl_base_notification_t; typedef struct mca_btl_base_notification_t mca_btl_base_notification_t; /** - * @brief Register a memory region and bind a notification counter to it - * - * Registers [base, base + size) and associates a hardware counter with the - * registration. The counter starts at zero and is incremented by the network - * adapter once per completed remote operation that modifies the region (a - * put or an atomic), after the data movement has completed at this process. - * Remote reads of the region are not counted. - * - * The same memory may be registered this way more than once. Each such - * registration yields a distinct handle and a distinct counter, so a consumer - * that needs several independent counters over one buffer can obtain them by - * registering the buffer once per counter and publishing a different handle to - * origins for each. Registrations consume adapter resources, so callers should - * not create more than they need. + * @brief Create a notification counter * - * Ownership of both the notification and the registration handle passes to the - * caller; both are released by btl_deregister_notification. + * The counter starts at zero and is incremented by one for every + * btl_put_notify or btl_get_notify that names {key} and targets this process, + * once the operation has completed here. The key is only meaningful to this + * BTL module on this process; a consumer publishes it to the origins that + * should be able to notify. Counters consume BTL resources, so callers should + * not create more than they need. * - * @param[IN] btl BTL module - * @param[IN] base Pointer to start of region - * @param[IN] size Size of region - * @param[IN] flags Registration flags including access permissions - * @param[OUT] handle Registration handle to publish to origins + * @param[IN] btl BTL module + * @param[OUT] notification New notification + * @param[OUT] key Key origins pass to btl_put_notify/btl_get_notify * - * @returns a notification handle, or NULL if the region could not be - * registered or no counter could be associated with it. + * @retval OPAL_SUCCESS The counter was created + * @retval OPAL_ERR_OUT_OF_RESOURCE No more counters are available */ -typedef struct mca_btl_base_notification_t *(*mca_btl_base_module_register_notification_fn_t)( - struct mca_btl_base_module_t *btl, void *base, size_t size, uint32_t flags, - struct mca_btl_base_registration_handle_t **handle); +typedef int (*mca_btl_base_module_notification_create_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t **notification, + uint64_t *key); /** - * @brief Release a notification counter and its memory registration + * @brief Release a notification counter * - * No remote operation may target the associated region after this call. + * Operations that name the counter's key and arrive after this call are + * discarded, even if a later btl_notification_create hands out the same + * resources again. * * @param[IN] btl BTL module the notification was created with * @param[IN] notification Notification to release */ -typedef int (*mca_btl_base_module_deregister_notification_fn_t)( +typedef int (*mca_btl_base_module_notification_destroy_fn_t)( struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification); /** * @brief Read the current value of a notification counter * - * This is a local operation and does not guarantee progress of outstanding - * operations. The value is monotonically non-decreasing for the lifetime of - * the notification; the BTL never resets it. A consumer that needs to reset a - * counter should record the value at reset time and subtract it from - * subsequent reads, which avoids racing against increments still in flight. + * This is a local operation and does not progress the BTL, so notifications + * that have arrived but not been processed yet are not included. The value is + * monotonically non-decreasing for the lifetime of the notification; the BTL + * never resets it. A consumer that needs to reset a counter should record the + * value at reset time and subtract it from subsequent reads, which avoids + * racing against increments still in flight. * * @param[IN] btl BTL module * @param[IN] notification Notification to read @@ -1258,24 +1265,61 @@ typedef int (*mca_btl_base_module_notification_read_fn_t)( /** * @brief Wait for a notification counter to reach a threshold * - * Blocks until the counter is greater than or equal to threshold, or until - * timeout milliseconds have elapsed. A timeout of 0 polls once and returns - * immediately; a negative timeout blocks indefinitely. - * - * Returns OPAL_SUCCESS if the threshold was reached and OPAL_ERR_TIMEOUT if it - * was not. Because this call may block inside the adapter or the kernel, a - * caller that must also guarantee MPI progress should either use a short - * timeout or poll with btl_notification_read instead. + * Progresses the BTL until the counter is greater than or equal to threshold, + * or until timeout milliseconds have elapsed. A timeout of 0 polls once and + * returns immediately; a negative timeout waits indefinitely. * * @param[IN] btl BTL module * @param[IN] notification Notification to wait on * @param[IN] threshold Counter value to wait for - * @param[IN] timeout Milliseconds to wait, or negative to block + * @param[IN] timeout Milliseconds to wait, or negative to wait forever + * + * @retval OPAL_SUCCESS The threshold was reached + * @retval OPAL_ERR_TIMEOUT It was not reached in time */ typedef int (*mca_btl_base_module_notification_wait_fn_t)( struct mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, uint64_t threshold, int timeout); +/** + * @brief Initiate an asynchronous put that notifies the target + * + * Same as btl_put, and in addition increments the counter that {key} names at + * the target once the data has been placed there. The counter is incremented + * exactly once per call. The completion callback is not called before the + * notification has been delivered, so a btl_flush covers it. + * + * A zero-byte put is allowed and only delivers the notification. + * + * Accepts MCA_BTL_DES_FLAGS_RDMA_MORE and MCA_BTL_DES_FLAGS_RDMA_FENCE in + * {flags}; the other parameters and return values are as for btl_put. + */ +typedef int (*mca_btl_base_module_put_notify_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + struct mca_btl_base_registration_handle_t *local_handle, + struct mca_btl_base_registration_handle_t *remote_handle, size_t size, int flags, int order, + uint64_t key, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata); + +/** + * @brief Initiate an asynchronous get that notifies the target + * + * Same as btl_get, and in addition increments the counter that {key} names at + * the target once the data has been read from its memory, i.e. once the + * target may overwrite the source buffer. The counter is incremented exactly + * once per call. The completion callback is not called before the + * notification has been delivered, so a btl_flush covers it. + * + * Accepts MCA_BTL_DES_FLAGS_RDMA_MORE and MCA_BTL_DES_FLAGS_RDMA_FENCE in + * {flags}; the other parameters and return values are as for btl_get. + */ +typedef int (*mca_btl_base_module_get_notify_fn_t)( + struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + struct mca_btl_base_registration_handle_t *local_handle, + struct mca_btl_base_registration_handle_t *remote_handle, size_t size, int flags, int order, + uint64_t key, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata); + /** * BTL module interface functions and attributes. */ @@ -1354,10 +1398,12 @@ struct mca_btl_base_module_t { /* notification counters. only valid when the BTL sets * MCA_BTL_FLAGS_NOTIFIED_RMA */ - mca_btl_base_module_register_notification_fn_t btl_register_notification; - mca_btl_base_module_deregister_notification_fn_t btl_deregister_notification; + mca_btl_base_module_notification_create_fn_t btl_notification_create; + mca_btl_base_module_notification_destroy_fn_t btl_notification_destroy; mca_btl_base_module_notification_read_fn_t btl_notification_read; mca_btl_base_module_notification_wait_fn_t btl_notification_wait; + mca_btl_base_module_put_notify_fn_t btl_put_notify; + mca_btl_base_module_get_notify_fn_t btl_get_notify; }; unsigned char padding[256]; /**< padding to future-proof the btl module */ diff --git a/opal/mca/btl/ofi/btl_ofi.h b/opal/mca/btl/ofi/btl_ofi.h index 3ac067f9728..b8c3744ddfd 100644 --- a/opal/mca/btl/ofi/btl_ofi.h +++ b/opal/mca/btl/ofi/btl_ofi.h @@ -75,6 +75,7 @@ enum mca_btl_ofi_hdr_type { MCA_BTL_OFI_TYPE_AOP, MCA_BTL_OFI_TYPE_AFOP, MCA_BTL_OFI_TYPE_CSWAP, + MCA_BTL_OFI_TYPE_GET_NOTIFY, MCA_BTL_OFI_TYPE_SEND, MCA_BTL_OFI_TYPE_RECV, MCA_BTL_OFI_TYPE_TOTAL @@ -99,6 +100,12 @@ struct mca_btl_ofi_context_t { opal_free_list_t frag_comp_list; opal_free_list_t frag_list; + /* get_notify completions whose notification write could not be posted + * from the completion path yet (the provider returned FI_EAGAIN). the + * lock is a leaf: nothing else is acquired while it is held */ + opal_list_t notify_pending; + opal_atomic_lock_t notify_pending_lock; + /* for thread locking */ opal_atomic_int32_t lock; }; @@ -128,9 +135,29 @@ struct mca_btl_ofi_module_t { bool use_virt_addr; bool is_scalable_ep; bool use_fi_mr_bind; - /** the domain was opened with FI_MR_RMA_EVENT: memory regions registered - * for a notification counter are created disabled and need fi_mr_enable */ - bool use_mr_rma_event; + + /** notified RMA: notification keys travel as remote CQ data, the CQs use + * FI_CQ_FORMAT_DATA and the receive side is bound so remote writes that + * carry data generate a completion here */ + bool use_notify; + /** the endpoint honours FI_FENCE, which orders a notification behind all + * earlier operations to the same peer without involving the CPU */ + bool use_fence; + /** flags added to the zero-byte write that carries a get_notify's key + * (see mca_btl_ofi_init_device) */ + uint64_t notify_write_flags; + + /** notification counters indexed by the slot part of their key, each + * with the slot's generation, which is folded into the key so that a late + * notification for a destroyed counter is not credited to its successor. + * a slot is only written with every context lock held, so a progress + * thread that holds its context lock can use an entry without further + * locking */ + struct mca_btl_ofi_notify_slot_t { + struct mca_btl_base_notification_t *notify; + uint16_t generation; + } *notify_table; + int notify_table_size; opal_atomic_int64_t outstanding_rdma; opal_atomic_int64_t outstanding_send; @@ -174,10 +201,15 @@ struct mca_btl_ofi_component_t { bool disable_hmem; - /** request FI_RMA_EVENT so that notification counters can be bound to - * memory regions. optional: if no provider offers it the query is retried - * without it. */ - bool enable_rma_event; + /** offer notified RMA (immediate data that advances a counter at the + * target) when the provider supports remote CQ data */ + bool enable_notify; + /** order notifications with FI_FENCE (mca_base_var_enum_auto_bool): auto + * (-1) when the provider offers it and is not known to mishandle it, false + * never, true whenever offered */ + int notify_use_fence; + /** maximum number of notification counters per module */ + int notify_max; /** All BTL OFI modules (1 per tl) */ mca_btl_ofi_module_t *modules[MCA_BTL_OFI_MAX_MODULES]; @@ -257,6 +289,18 @@ struct mca_btl_ofi_rdma_completion_t { mca_btl_base_rdma_completion_fn_t cbfunc; void *cbcontext; void *cbdata; + + /* get_notify only, kept last so other operations do not touch these cache + * lines: the read and the zero-byte write that carries the notification + * are separate libfabric operations, each needing its own context. the + * callback runs once both have completed. both complete on the same CQ, + * under that context's lock, so pending_ops needs no atomics */ + mca_btl_ofi_completion_context_t notify_ctx; + uint64_t remote_address; + uint64_t rkey; + uint64_t notify_key; + int pending_ops; + int status; }; typedef struct mca_btl_ofi_rdma_completion_t mca_btl_ofi_rdma_completion_t; @@ -374,19 +418,41 @@ int mca_btl_ofi_reg_mem(void *reg_data, void *base, size_t size, mca_rcache_base_registration_t *reg); int mca_btl_ofi_dereg_mem(void *reg_data, mca_rcache_base_registration_t *reg); -/* hardware notification counters. only installed on the module when the - * selected provider reports FI_RMA_EVENT. */ -struct mca_btl_base_notification_t * -mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t size, - uint32_t flags, mca_btl_base_registration_handle_t **handle); -int mca_btl_ofi_deregister_notification(mca_btl_base_module_t *btl, - struct mca_btl_base_notification_t *notification); +/* notified RMA. only installed on the module when the selected provider + * supports remote CQ data (see use_notify). */ +int mca_btl_ofi_notification_create(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t **notification, + uint64_t *key); +int mca_btl_ofi_notification_destroy(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification); int mca_btl_ofi_notification_read(mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, uint64_t *value); int mca_btl_ofi_notification_wait(mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, uint64_t threshold, int timeout); +int mca_btl_ofi_put_notify(mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + mca_btl_base_registration_handle_t *local_handle, + mca_btl_base_registration_handle_t *remote_handle, size_t size, + int flags, int order, uint64_t key, + mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, + void *cbdata); +int mca_btl_ofi_get_notify(mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + mca_btl_base_registration_handle_t *local_handle, + mca_btl_base_registration_handle_t *remote_handle, size_t size, + int flags, int order, uint64_t key, + mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, + void *cbdata); +/* post the zero-byte write that delivers a get_notify notification. returns + * -FI_EAGAIN if the provider cannot take it yet */ +int mca_btl_ofi_post_notify_write(mca_btl_ofi_context_t *context, + mca_btl_ofi_rdma_completion_t *comp); +/* credit a notification that arrived as remote CQ data. called from progress + * with the context lock held */ +void mca_btl_ofi_notification_deliver(mca_btl_ofi_module_t *module, uint64_t key); +void mca_btl_ofi_notification_finalize(mca_btl_ofi_module_t *module); int mca_btl_ofi_context_progress(mca_btl_ofi_context_t *context); @@ -415,5 +481,26 @@ static inline void mca_btl_ofi_context_unlock(mca_btl_ofi_context_t *context) context->lock = 0; } +/* post the zero-byte write that delivers a get_notify notification, or, if + * the provider cannot take it yet, queue it for progress to retry (see + * mca_btl_ofi_context_progress). returns 0 once posted or queued */ +static inline int mca_btl_ofi_post_or_queue_notify_write(mca_btl_ofi_context_t *context, + mca_btl_ofi_rdma_completion_t *comp) +{ + int ret = mca_btl_ofi_post_notify_write(context, comp); + + if (OPAL_UNLIKELY(-FI_EAGAIN == ret)) { + opal_atomic_lock(&context->notify_pending_lock); + opal_list_append(&context->notify_pending, (opal_list_item_t *) comp); + opal_atomic_unlock(&context->notify_pending_lock); + return 0; + } + if (OPAL_UNLIKELY(0 != ret)) { + BTL_ERROR(("fi_writemsg failed with %d:%s", ret, fi_strerror(-ret))); + } + + return ret; +} + END_C_DECLS #endif diff --git a/opal/mca/btl/ofi/btl_ofi_component.c b/opal/mca/btl/ofi/btl_ofi_component.c index b7d54dc1188..d4d8183f3d2 100644 --- a/opal/mca/btl/ofi/btl_ofi_component.c +++ b/opal/mca/btl/ofi/btl_ofi_component.c @@ -219,20 +219,47 @@ static int mca_btl_ofi_component_register(void) MCA_BASE_VAR_SCOPE_READONLY, &mca_btl_ofi_component.disable_hmem); - mca_btl_ofi_component.enable_rma_event = true; + mca_btl_ofi_component.enable_notify = true; mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, - "enable_rma_event", - "Request FI_RMA_EVENT so notification counters can be bound " - "to memory regions, enabling RMA with notification in a " - "single operation. It is only requested from the provider " - "and NIC already selected, so it never changes which " - "provider is used; if that provider does not offer it, " - "notification counters are simply not used.", + "enable_notify", + "Offer notified RMA: a put or get carries a key to the " + "target as remote CQ data (immediate data), and the target " + "advances the notification counter it names once the " + "operation has completed there. Used when the provider " + "supports remote CQ data of at least 4 bytes without " + "consuming receive buffers.", MCA_BASE_VAR_TYPE_BOOL, NULL, 0, 0, OPAL_INFO_LVL_5, MCA_BASE_VAR_SCOPE_READONLY, - &mca_btl_ofi_component.enable_rma_event); + &mca_btl_ofi_component.enable_notify); + mca_btl_ofi_component.notify_use_fence = -1; + mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, + "notify_use_fence", + "Order notifications behind their data with FI_FENCE, so " + "the adapter rather than the CPU enforces the ordering. " + "auto (default): when offered, except by providers known " + "to mishandle it (sockets); false: never; true: whenever " + "offered. Never changes which provider is used.", + MCA_BASE_VAR_TYPE_INT, &mca_base_var_enum_auto_bool, 0, 0, + OPAL_INFO_LVL_5, + MCA_BASE_VAR_SCOPE_READONLY, + &mca_btl_ofi_component.notify_use_fence); + + mca_btl_ofi_component.notify_max = 1024; + mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, + "notify_max", + "Maximum number of notification counters per module " + "(1-65536)", + MCA_BASE_VAR_TYPE_INT, NULL, 0, 0, + OPAL_INFO_LVL_5, + MCA_BASE_VAR_SCOPE_READONLY, + &mca_btl_ofi_component.notify_max); + if (mca_btl_ofi_component.notify_max < 1) { + mca_btl_ofi_component.notify_max = 1; + } else if (mca_btl_ofi_component.notify_max > 65536) { + mca_btl_ofi_component.notify_max = 65536; + } /* for now we want this component to lose to the MTL. */ module->super.btl_exclusivity = MCA_BTL_EXCLUSIVITY_HIGH - 50; @@ -275,17 +302,29 @@ void mca_btl_ofi_exit(void) * then create a BTL instance for selected interfaces */ +/* should notifications use FI_FENCE on this provider, if it offers it? + * + * The sockets provider (libfabric 1.22 and earlier) claims a connection for a + * fenced operation before checking the fence and keeps it while it waits, + * which also blocks the responses it owes its peer. When both peers have a + * fenced operation waiting, neither completes. */ +static bool mca_btl_ofi_fence_wanted(const struct fi_info *info) +{ + return mca_btl_ofi_component.enable_notify && 0 != mca_btl_ofi_component.notify_use_fence + && (1 == mca_btl_ofi_component.notify_use_fence + || 0 != strcmp(info->fabric_attr->prov_name, "sockets")); +} + /** * Look for a variant of {info} -- the same provider on the same NIC -- that - * also offers FI_RMA_EVENT, so notification counters can be bound to its - * memory regions. + * also offers FI_FENCE, so a notification can be ordered behind the data it + * announces by the adapter. * - * FI_RMA_EVENT is a secondary capability, and fi_getinfo only returns - * providers that support every secondary capability in the hints. Asking for - * it during provider selection would therefore steer the btl to whichever - * provider happens to offer it, e.g. a TCP provider over the RDMA NIC. Asking - * only once a provider has been selected can add the capability but never - * changes the choice. + * FI_FENCE is a secondary capability, and fi_getinfo only returns providers + * that support every secondary capability in the hints. Asking for it during + * provider selection would therefore steer the btl to whichever provider + * happens to offer it. Asking only once a provider has been selected can add + * the capability but never changes the choice. * * @param[in] info the selected provider * @param[in] api_version version the selection query succeeded with @@ -293,10 +332,10 @@ void mca_btl_ofi_exit(void) * @param[in] mr_mode memory registration modes this btl supports (likewise) * * @returns a new fi_info the caller must free, or NULL if the provider does not - * offer FI_RMA_EVENT there. {info} remains usable either way. + * offer FI_FENCE there. {info} remains usable either way. */ -static struct fi_info *mca_btl_ofi_rma_event_info(struct fi_info *info, uint32_t api_version, - uint64_t mode, int mr_mode) +static struct fi_info *mca_btl_ofi_fence_info(struct fi_info *info, uint32_t api_version, + uint64_t mode, int mr_mode) { struct fi_info *hints, *info_list = NULL, *match = NULL; int rc; @@ -306,22 +345,20 @@ static struct fi_info *mca_btl_ofi_rma_event_info(struct fi_info *info, uint32_t return NULL; } - hints->caps |= FI_RMA_EVENT; + hints->caps |= FI_FENCE; hints->mode = mode; - /* counters are registered with FI_RMA_EVENT and enabled once bound, which - * is what this mode asks of the application */ - hints->domain_attr->mr_mode = mr_mode | FI_MR_RMA_EVENT; + hints->domain_attr->mr_mode = mr_mode; rc = fi_getinfo(api_version, NULL, NULL, 0, hints, &info_list); (void) fi_freeinfo(hints); if (0 != rc) { - BTL_VERBOSE(("%s does not offer FI_RMA_EVENT on %s: %s", info->fabric_attr->prov_name, + BTL_VERBOSE(("%s does not offer FI_FENCE on %s: %s", info->fabric_attr->prov_name, info->domain_attr->name, fi_strerror(-rc))); return NULL; } for (struct fi_info *cur = info_list; NULL != cur; cur = cur->next) { - if ((cur->caps & FI_RMA_EVENT) + if ((cur->caps & FI_FENCE) && 0 == strcmp(cur->fabric_attr->prov_name, info->fabric_attr->prov_name) && 0 == strcmp(cur->domain_attr->name, info->domain_attr->name)) { match = fi_dupinfo(cur); @@ -365,7 +402,7 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, struct fi_tx_attr tx_attr = {0}; struct fi_fabric_attr fabric_attr = {0}; struct fi_domain_attr domain_attr = {0}; - struct fi_info *rma_event_info = NULL; + struct fi_info *fence_info = NULL; uint64_t required_caps; uint32_t api_version = FI_VERSION(1, 18); @@ -545,20 +582,20 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, */ selected_info = opal_common_ofi_select_provider(info, &opal_process_info); - /* Notification counters are an optimization: try the same - * provider and NIC with FI_RMA_EVENT first, and use the plain + /* Fenced notifications are an optimization: try the same + * provider and NIC with FI_FENCE first, and use the plain * variant if that is not offered or does not initialize. */ rc = OPAL_ERR_NOT_AVAILABLE; - if (mca_btl_ofi_component.enable_rma_event && !(selected_info->caps & FI_RMA_EVENT)) { - if (NULL != rma_event_info) { - (void) fi_freeinfo(rma_event_info); + if (mca_btl_ofi_fence_wanted(selected_info) && !(selected_info->caps & FI_FENCE)) { + if (NULL != fence_info) { + (void) fi_freeinfo(fence_info); } - rma_event_info = mca_btl_ofi_rma_event_info(selected_info, api_version, hints.mode, - hints.domain_attr->mr_mode); - if (NULL != rma_event_info) { - rc = mca_btl_ofi_init_device(rma_event_info); + fence_info = mca_btl_ofi_fence_info(selected_info, api_version, hints.mode, + hints.domain_attr->mr_mode); + if (NULL != fence_info) { + rc = mca_btl_ofi_init_device(fence_info); if (OPAL_SUCCESS == rc) { - selected_info = rma_event_info; + selected_info = fence_info; } } } @@ -604,8 +641,8 @@ static mca_btl_base_module_t **mca_btl_ofi_component_init(int *num_btl_modules, } /* freed alongside info_list: a module keeps pointers into the fi_info it * was initialized from */ - if (rma_event_info) { - (void) fi_freeinfo(rma_event_info); + if (fence_info) { + (void) fi_freeinfo(fence_info); } return base_modules; @@ -699,6 +736,25 @@ static int mca_btl_ofi_init_device(struct fi_info *info) num_contexts_to_create = mca_btl_ofi_component.num_contexts_per_module; + /* Notified RMA carries a notification key as remote CQ data, which has + * to be decided before the contexts are created: their CQs must report + * the data, and the receive side must be bound to see it. Providers that + * need a posted receive buffer per remote CQ data (FI_RX_CQ_DATA) were + * not asked for in the hints mode, and a one-sided btl posts none. RMA, + * and so btl_register_mem, is only set up by mca_btl_ofi_module_alloc() + * for the one-sided modes. */ + module->use_notify = mca_btl_ofi_component.enable_notify + && NULL != module->super.btl_register_mem + && domain_attr->cq_data_size >= 4 + && !(ofi_info->mode & FI_RX_CQ_DATA); + /* FI_FENCE orders operations on one transmit context. Without thread + * local storage a thread's operations are spread round robin over the + * contexts, so a fence on one of them would not cover the others. */ + module->use_fence = module->use_notify && mca_btl_ofi_fence_wanted(ofi_info) + && (ofi_info->caps & FI_FENCE) + && (OPAL_HAVE_THREAD_LOCAL || 1 == num_contexts_to_create); + module->notify_write_flags = module->use_fence ? FI_FENCE : 0; + /* If the domain support scalable endpoint. */ if (domain_attr->max_ep_tx_ctx > 1 && !disable_sep) { @@ -782,16 +838,17 @@ static int mca_btl_ofi_init_device(struct fi_info *info) } #endif - /* Notification counters are bound to memory regions, so they are only - * meaningful when this module does RDMA at all. btl_register_mem is set - * by mca_btl_ofi_module_alloc() for the one-sided modes only. */ - if ((ofi_info->caps & FI_RMA_EVENT) && NULL != module->super.btl_register_mem) { + if (module->use_notify) { module->super.btl_flags |= MCA_BTL_FLAGS_NOTIFIED_RMA; - module->super.btl_register_notification = mca_btl_ofi_register_notification; - module->super.btl_deregister_notification = mca_btl_ofi_deregister_notification; + module->super.btl_notification_create = mca_btl_ofi_notification_create; + module->super.btl_notification_destroy = mca_btl_ofi_notification_destroy; module->super.btl_notification_read = mca_btl_ofi_notification_read; module->super.btl_notification_wait = mca_btl_ofi_notification_wait; - BTL_VERBOSE(("%s supports notification counters", linux_device_name)); + module->super.btl_put_notify = mca_btl_ofi_put_notify; + module->super.btl_get_notify = mca_btl_ofi_get_notify; + BTL_VERBOSE(("%s supports notified RMA (%zu bytes of remote CQ data, %s)", + linux_device_name, domain_attr->cq_data_size, + module->use_fence ? "fenced" : "unfenced")); } if (ofi_info->domain_attr->mr_mode == MCA_BTL_OFI_MR_BASIC @@ -803,10 +860,6 @@ static int mca_btl_ofi_init_device(struct fi_info *info) module->use_fi_mr_bind = true; } - if (ofi_info->domain_attr->mr_mode & FI_MR_RMA_EVENT) { - module->use_mr_rma_event = true; - } - /* create endpoint list */ OBJ_CONSTRUCT(&module->endpoints, opal_list_t); OBJ_CONSTRUCT(&module->module_lock, opal_mutex_t); diff --git a/opal/mca/btl/ofi/btl_ofi_context.c b/opal/mca/btl/ofi/btl_ofi_context.c index 8bc3edd302c..a26603baa64 100644 --- a/opal/mca/btl/ofi/btl_ofi_context.c +++ b/opal/mca/btl/ofi/btl_ofi_context.c @@ -79,6 +79,9 @@ mca_btl_ofi_context_t *mca_btl_ofi_context_alloc_normal(struct fi_info *info, return NULL; } + OBJ_CONSTRUCT(&context->notify_pending, opal_list_t); + opal_atomic_lock_init(&context->notify_pending_lock, OPAL_ATOMIC_LOCK_UNLOCKED); + /* Don't really need to check, just avoiding compiler warning because * BTL_VERBOSE is a no op in performance build and the compiler will * complain about unused variable. */ @@ -87,7 +90,8 @@ mca_btl_ofi_context_t *mca_btl_ofi_context_alloc_normal(struct fi_info *info, goto single_fail; } - cq_attr.format = FI_CQ_FORMAT_CONTEXT; + /* notifications arrive as remote CQ data, which only the data format reports */ + cq_attr.format = btl->use_notify ? FI_CQ_FORMAT_DATA : FI_CQ_FORMAT_CONTEXT; cq_attr.wait_obj = FI_WAIT_NONE; rc = fi_cq_open(domain, &cq_attr, &context->cq, NULL); if (0 != rc) { @@ -156,6 +160,11 @@ mca_btl_ofi_context_t *mca_btl_ofi_context_alloc_scalable(struct fi_info *info, return NULL; } + for (i = 0; i < num_contexts; i++) { + OBJ_CONSTRUCT(&contexts[i].notify_pending, opal_list_t); + opal_atomic_lock_init(&contexts[i].notify_pending_lock, OPAL_ATOMIC_LOCK_UNLOCKED); + } + /* Don't really need to check, just avoiding compiler warning because * BTL_VERBOSE is a no op in performance build and the compiler will * complain about unused variable. */ @@ -190,8 +199,9 @@ mca_btl_ofi_context_t *mca_btl_ofi_context_alloc_scalable(struct fi_info *info, goto scalable_fail; } - /* create CQ */ - cq_attr.format = FI_CQ_FORMAT_CONTEXT; + /* create CQ. notifications arrive as remote CQ data, which only + * the data format reports */ + cq_attr.format = btl->use_notify ? FI_CQ_FORMAT_DATA : FI_CQ_FORMAT_CONTEXT; cq_attr.wait_obj = FI_WAIT_NONE; rc = fi_cq_open(domain, &cq_attr, &contexts[i].cq, NULL); if (0 != rc) { @@ -206,8 +216,9 @@ mca_btl_ofi_context_t *mca_btl_ofi_context_alloc_scalable(struct fi_info *info, goto scalable_fail; } - /* bind cq to receiving context */ - if (TWO_SIDED_ENABLED) { + /* bind cq to receiving context. a remote write that carries CQ + * data (a notification) also completes on the receive side */ + if (TWO_SIDED_ENABLED || btl->use_notify) { rc = fi_ep_bind(contexts[i].rx_ctx, (fid_t) contexts[i].cq, FI_RECV); if (0 != rc) { BTL_VERBOSE( @@ -270,6 +281,8 @@ void mca_btl_ofi_context_finalize(mca_btl_ofi_context_t *context, bool scalable_ fi_close(&context->cq->fid); } + OBJ_DESTRUCT(&context->notify_pending); + /* Can we destruct the object that hasn't been constructed? */ if (context->rdma_comp_list.fl_num_allocated != 0){ OBJ_DESTRUCT(&context->rdma_comp_list); @@ -328,6 +341,30 @@ static void inline complete_op_context(mca_btl_ofi_context_t* context, = (mca_btl_ofi_rdma_completion_t *) c_ctx->comp; switch (comp->type) { + case MCA_BTL_OFI_TYPE_GET_NOTIFY: + /* one of the read or the notification write finished */ + if (OPAL_SUCCESS != rc) { + rdma_comp->status = rc; + } + + if (c_ctx == &rdma_comp->comp_ctx && !((mca_btl_ofi_module_t *) comp->btl)->use_fence) { + /* the read has completed, so the target is done with its memory + * and may be told. the write's completion then finishes the get, + * so this one is not counted. a failed read is not notified. */ + if (OPAL_SUCCESS == rdma_comp->status) { + if (OPAL_LIKELY(0 == mca_btl_ofi_post_or_queue_notify_write(context, rdma_comp))) { + return; + } + rdma_comp->status = OPAL_ERROR; + } + } + + if (0 != --rdma_comp->pending_ops) { + return; + } + + rc = rdma_comp->status; + /* fall through */ case MCA_BTL_OFI_TYPE_GET: case MCA_BTL_OFI_TYPE_PUT: case MCA_BTL_OFI_TYPE_AOP: @@ -364,25 +401,98 @@ static void inline complete_op_context(mca_btl_ofi_context_t* context, opal_free_list_return(comp->my_list, (opal_free_list_item_t *) comp); } +/* post the get_notify notification writes the provider pushed back on */ +static int mca_btl_ofi_progress_notify_pending(mca_btl_ofi_context_t *context) +{ + mca_btl_ofi_rdma_completion_t *comp; + int events = 0; + int ret; + + opal_atomic_lock(&context->notify_pending_lock); + while (!opal_list_is_empty(&context->notify_pending)) { + comp = (mca_btl_ofi_rdma_completion_t *) opal_list_get_first(&context->notify_pending); + ret = mca_btl_ofi_post_notify_write(context, comp); + if (-FI_EAGAIN == ret) { + break; + } + if (OPAL_UNLIKELY(0 != ret)) { + BTL_ERROR(("fi_writemsg failed with %d:%s", ret, fi_strerror(-ret))); + MCA_BTL_OFI_ABORT(); + } + (void) opal_list_remove_first(&context->notify_pending); + ++events; + } + opal_atomic_unlock(&context->notify_pending_lock); + + return events; +} + +/* read and complete the entries of a FI_CQ_FORMAT_CONTEXT CQ */ +static inline int mca_btl_ofi_cq_read_context(mca_btl_ofi_context_t *context, int *events) +{ + struct fi_cq_entry cq_entry[MCA_BTL_OFI_DEFAULT_MAX_CQE]; + int ret = fi_cq_read(context->cq, &cq_entry, mca_btl_ofi_component.num_cqe_read); + + for (int i = 0; i < ret; i++) { + if (NULL != cq_entry[i].op_context) { + ++*events; + complete_op_context(context, cq_entry[i].op_context, OPAL_SUCCESS); + } + } + + return ret; +} + +/* notified RMA: the CQ uses FI_CQ_FORMAT_DATA so remote CQ data (a peer's + * notification) is reported. kept out of line so the plain progress path keeps + * its small stack frame */ +static __opal_attribute_noinline__ int mca_btl_ofi_cq_read_data(mca_btl_ofi_context_t *context, + int *events) +{ + struct fi_cq_data_entry cq_entry[MCA_BTL_OFI_DEFAULT_MAX_CQE]; + int ret; + + if (OPAL_UNLIKELY(!opal_list_is_empty(&context->notify_pending))) { + *events += mca_btl_ofi_progress_notify_pending(context); + } + + ret = fi_cq_read(context->cq, cq_entry, mca_btl_ofi_component.num_cqe_read); + + for (int i = 0; i < ret; i++) { + if (NULL != cq_entry[i].op_context) { + /* one of our own operations. some providers (sockets) also set + * FI_REMOTE_CQ_DATA on the origin's completion of a write that + * carried data, so the context, not the flag, is what tells the + * two apart */ + ++*events; + complete_op_context(context, cq_entry[i].op_context, OPAL_SUCCESS); + } else if (cq_entry[i].flags & FI_REMOTE_CQ_DATA) { + /* a peer's notified operation has landed here (fi_cq(3): no + * context without FI_RX_CQ_DATA): the data is in place, so credit + * the counter it names */ + ++*events; + mca_btl_ofi_notification_deliver(context->btl, cq_entry[i].data); + } + } + + return ret; +} + int mca_btl_ofi_context_progress(mca_btl_ofi_context_t *context) { int ret = 0; - int events_read; int events = 0; - struct fi_cq_entry cq_entry[MCA_BTL_OFI_DEFAULT_MAX_CQE]; struct fi_cq_err_entry cqerr = {0}; - ret = fi_cq_read(context->cq, &cq_entry, mca_btl_ofi_component.num_cqe_read); + if (context->btl->use_notify) { + ret = mca_btl_ofi_cq_read_data(context, &events); + } else { + ret = mca_btl_ofi_cq_read_context(context, &events); + } if (0 < ret) { - events_read = ret; - for (int i = 0; i < events_read; i++) { - if (NULL != cq_entry[i].op_context) { - ++events; - complete_op_context(context, cq_entry[i].op_context, OPAL_SUCCESS); - } - } + /* completed above */ } else if (OPAL_UNLIKELY(ret == -FI_EAVAIL)) { ret = fi_cq_readerr(context->cq, &cqerr, 0); diff --git a/opal/mca/btl/ofi/btl_ofi_module.c b/opal/mca/btl/ofi/btl_ofi_module.c index aa2b8cc3040..5a8c758cbe3 100644 --- a/opal/mca/btl/ofi/btl_ofi_module.c +++ b/opal/mca/btl/ofi/btl_ofi_module.c @@ -379,6 +379,9 @@ int mca_btl_ofi_finalize(mca_btl_base_module_t *btl) ofi_btl->rcache = NULL; } + /* counters are domain objects; release any a consumer left behind */ + mca_btl_ofi_notification_finalize(ofi_btl); + /* Close basic ep before closing its attached resources. */ if (NULL != ofi_btl->ofi_endpoint && !ofi_btl->is_scalable_ep) { fi_close(&ofi_btl->ofi_endpoint->fid); diff --git a/opal/mca/btl/ofi/btl_ofi_notification.c b/opal/mca/btl/ofi/btl_ofi_notification.c index db3431685f4..a66180a29d8 100644 --- a/opal/mca/btl/ofi/btl_ofi_notification.c +++ b/opal/mca/btl/ofi/btl_ofi_notification.c @@ -13,186 +13,195 @@ #include "opal_config.h" #include "btl_ofi.h" +#include "opal/mca/timer/base/base.h" /** - * Hardware notification counters. + * Notification counters. * - * A notification is a libfabric counter (fi_cntr) bound to a memory region - * with FI_REMOTE_WRITE. Once a region has been registered this way the - * adapter increments the counter every time a remote write or atomic - * operation on that region completes here, without any involvement of this - * process's CPU and without the origin having to issue a second operation to - * announce that the data has landed. libfabric defines no counter event for - * remote reads, so reads of the region are not counted (fi_mr(3)). + * A notification is a libfabric counter (fi_cntr) that this process advances + * when a notified operation from a peer lands here. The peer names the + * counter by a key, which it sends as the remote CQ data of an RMA write (see + * mca_btl_ofi_put_notify / mca_btl_ofi_get_notify). The write produces a + * completion in this process's CQ once its data has been placed, and progress + * hands the key to mca_btl_ofi_notification_deliver, which increments the + * counter. * - * The counter counts operations on one memory region, so a consumer that - * needs several independent counters over the same buffer obtains them by - * registering the buffer once per counter and publishing a different rkey to - * origins for each. These registrations deliberately bypass the rcache: it - * keys on the address range and would hand back the same registration every - * time, which is the opposite of what is wanted here. + * Because the key travels with the data, counters need no memory registration + * of their own: every counter works with the ordinary registration of any + * region, and creating one costs no adapter memory-region resources. * - * This requires the provider to have been opened with FI_RMA_EVENT, which is - * requested as an optional capability during component init. + * A key is a slot in the module's notification table plus that slot's + * generation. Remote CQ data is at least 4 bytes (fi_domain(3)), which fits + * both. The generation lets a notification that arrives after its counter was + * destroyed be dropped instead of credited to a later counter in the same slot. */ +#define MCA_BTL_OFI_NOTIFY_SLOT_BITS 16 +#define MCA_BTL_OFI_NOTIFY_SLOT_MASK ((1u << MCA_BTL_OFI_NOTIFY_SLOT_BITS) - 1) + struct mca_btl_base_notification_t { - struct fid_mr *mr; struct fid_cntr *cntr; - mca_btl_base_registration_handle_t handle; + uint64_t key; }; -struct mca_btl_base_notification_t * -mca_btl_ofi_register_notification(mca_btl_base_module_t *btl, void *base, size_t size, - uint32_t flags, mca_btl_base_registration_handle_t **handle) +static inline uint64_t mca_btl_ofi_notification_key(int slot, uint16_t generation) +{ + return ((uint64_t) generation << MCA_BTL_OFI_NOTIFY_SLOT_BITS) | (uint64_t) slot; +} + +/* a table slot is only written with every context lock held. progress reads + * the table with its own context lock held, so it never sees a notification + * that is being torn down. */ +static void mca_btl_ofi_notification_lock_all(mca_btl_ofi_module_t *module) +{ + for (int i = 0; i < module->num_contexts; ++i) { + mca_btl_ofi_context_lock(&module->contexts[i]); + } +} + +static void mca_btl_ofi_notification_unlock_all(mca_btl_ofi_module_t *module) +{ + for (int i = 0; i < module->num_contexts; ++i) { + mca_btl_ofi_context_unlock(&module->contexts[i]); + } +} + +int mca_btl_ofi_notification_create(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t **notification, + uint64_t *key) { - /* the same access flags as an ordinary registration. the counter binding - * below, not the access mask, is what decides which operations count. */ - static uint64_t access_flags = FI_REMOTE_WRITE | FI_REMOTE_READ | FI_READ | FI_WRITE; mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; - struct mca_btl_base_notification_t *notification; + struct mca_btl_base_notification_t *notify; struct fi_cntr_attr cntr_attr = {0}; - struct fi_mr_attr attr = {0}; - struct iovec iov = {0}; + int slot = -1; int rc; - (void) flags; - - notification = calloc(1, sizeof(*notification)); - if (OPAL_UNLIKELY(NULL == notification)) { - return NULL; + notify = calloc(1, sizeof(*notify)); + if (OPAL_UNLIKELY(NULL == notify)) { + return OPAL_ERR_OUT_OF_RESOURCE; } + /* only software increments this counter, and nothing blocks on it */ cntr_attr.events = FI_CNTR_EVENTS_COMP; - cntr_attr.wait_obj = FI_WAIT_UNSPEC; + cntr_attr.wait_obj = FI_WAIT_NONE; - rc = fi_cntr_open(ofi_btl->domain, &cntr_attr, ¬ification->cntr, NULL); + rc = fi_cntr_open(ofi_btl->domain, &cntr_attr, ¬ify->cntr, NULL); if (FI_SUCCESS != rc) { BTL_VERBOSE(("%s failed fi_cntr_open with err=%s", ofi_btl->linux_device_name, fi_strerror(-rc))); - notification->cntr = NULL; - goto fail; + free(notify); + return OPAL_ERR_OUT_OF_RESOURCE; } - iov.iov_base = base; - iov.iov_len = size; - attr.mr_iov = &iov; - attr.iov_count = 1; - attr.access = access_flags; - attr.offset = 0; - attr.context = NULL; - attr.requested_key = (uint64_t) (uintptr_t) notification; - - /* FI_RMA_EVENT declares at creation that a completion counter will be - * associated with the region, which FI_MR_RMA_EVENT domains require */ - rc = fi_mr_regattr(ofi_btl->domain, &attr, FI_RMA_EVENT, ¬ification->mr); - if (FI_SUCCESS != rc) { - BTL_VERBOSE(("%s failed fi_mr_regattr with err=%s", ofi_btl->linux_device_name, - fi_strerror(-rc))); - notification->mr = NULL; - goto fail; - } + OPAL_THREAD_LOCK(&ofi_btl->module_lock); - /* FI_REMOTE_WRITE is the only event libfabric defines for counting - * operations on a memory region; it covers remote writes and atomics. */ - rc = fi_mr_bind(notification->mr, ¬ification->cntr->fid, FI_REMOTE_WRITE); - if (FI_SUCCESS != rc) { - BTL_VERBOSE(("%s failed to bind notification counter with err=%s", - ofi_btl->linux_device_name, fi_strerror(-rc))); - goto fail; + if (NULL == ofi_btl->notify_table) { + ofi_btl->notify_table = calloc(mca_btl_ofi_component.notify_max, + sizeof(ofi_btl->notify_table[0])); + if (NULL != ofi_btl->notify_table) { + ofi_btl->notify_table_size = mca_btl_ofi_component.notify_max; + } } - if (ofi_btl->use_fi_mr_bind) { - rc = fi_mr_bind(notification->mr, &ofi_btl->ofi_endpoint->fid, 0ULL); - if (FI_SUCCESS != rc) { - BTL_VERBOSE(("%s failed to bind notification mr to endpoint with err=%s", - ofi_btl->linux_device_name, fi_strerror(-rc))); - goto fail; + for (int i = 0; i < ofi_btl->notify_table_size; ++i) { + if (NULL == ofi_btl->notify_table[i].notify) { + slot = i; + break; } } - /* A region registered with FI_RMA_EVENT in an FI_MR_RMA_EVENT domain, or - * in an FI_MR_ENDPOINT domain, is created disabled and does not service - * remote operations until enabled, which must come after every binding. */ - if (ofi_btl->use_mr_rma_event || ofi_btl->use_fi_mr_bind) { - rc = fi_mr_enable(notification->mr); - if (FI_SUCCESS != rc) { - BTL_VERBOSE(("%s failed fi_mr_enable with err=%s", ofi_btl->linux_device_name, - fi_strerror(-rc))); - goto fail; - } + if (OPAL_UNLIKELY(slot < 0)) { + OPAL_THREAD_UNLOCK(&ofi_btl->module_lock); + BTL_VERBOSE(("%s: all %d notification counters are in use", ofi_btl->linux_device_name, + ofi_btl->notify_table_size)); + (void) fi_close(¬ify->cntr->fid); + free(notify); + return OPAL_ERR_OUT_OF_RESOURCE; } - notification->handle.rkey = fi_mr_key(notification->mr); - notification->handle.desc = fi_mr_desc(notification->mr); + notify->key = mca_btl_ofi_notification_key(slot, ++ofi_btl->notify_table[slot].generation); - /* In case the provider doesn't support FI_MR_VIRT_ADDR, remote addresses - * are expressed as a distance from the base registered address. */ - if (ofi_btl->use_virt_addr) { - notification->handle.base_addr = 0; - } else { - notification->handle.base_addr = base; - } + mca_btl_ofi_notification_lock_all(ofi_btl); + ofi_btl->notify_table[slot].notify = notify; + mca_btl_ofi_notification_unlock_all(ofi_btl); - *handle = ¬ification->handle; + OPAL_THREAD_UNLOCK(&ofi_btl->module_lock); - return notification; + *notification = notify; + *key = notify->key; -fail: - if (NULL != notification->mr) { - fi_close(¬ification->mr->fid); - } - if (NULL != notification->cntr) { - fi_close(¬ification->cntr->fid); + return OPAL_SUCCESS; +} + +int mca_btl_ofi_notification_destroy(mca_btl_base_module_t *btl, + struct mca_btl_base_notification_t *notification) +{ + mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; + int rc; + + if (OPAL_UNLIKELY(NULL == notification)) { + return OPAL_SUCCESS; } + + OPAL_THREAD_LOCK(&ofi_btl->module_lock); + mca_btl_ofi_notification_lock_all(ofi_btl); + ofi_btl->notify_table[notification->key & MCA_BTL_OFI_NOTIFY_SLOT_MASK].notify = NULL; + mca_btl_ofi_notification_unlock_all(ofi_btl); + OPAL_THREAD_UNLOCK(&ofi_btl->module_lock); + + rc = fi_close(¬ification->cntr->fid); free(notification); - return NULL; + if (FI_SUCCESS != rc) { + BTL_ERROR(("%s: error closing notification counter: %s", __func__, fi_strerror(-rc))); + return OPAL_ERROR; + } + + return OPAL_SUCCESS; } -int mca_btl_ofi_deregister_notification(mca_btl_base_module_t *btl, - struct mca_btl_base_notification_t *notification) +void mca_btl_ofi_notification_deliver(mca_btl_ofi_module_t *module, uint64_t key) { - int ret = OPAL_SUCCESS; + uint64_t slot = key & MCA_BTL_OFI_NOTIFY_SLOT_MASK; + struct mca_btl_base_notification_t *notify; int rc; - if (OPAL_UNLIKELY(NULL == notification)) { - return OPAL_SUCCESS; + if (OPAL_UNLIKELY(slot >= (uint64_t) module->notify_table_size)) { + return; } - /* close the mr first, it references the counter */ - if (NULL != notification->mr) { - rc = fi_close(¬ification->mr->fid); - if (FI_SUCCESS != rc) { - BTL_ERROR(("%s: error unpinning notification memory: %s", __func__, fi_strerror(-rc))); - ret = OPAL_ERROR; - } + notify = module->notify_table[slot].notify; + /* a stale key: the counter it named has been destroyed */ + if (OPAL_UNLIKELY(NULL == notify || notify->key != key)) { + return; } - if (NULL != notification->cntr) { - rc = fi_close(¬ification->cntr->fid); - if (FI_SUCCESS != rc) { - BTL_ERROR(("%s: error closing notification counter: %s", __func__, fi_strerror(-rc))); - ret = OPAL_ERROR; - } + rc = fi_cntr_add(notify->cntr, 1); + if (OPAL_UNLIKELY(FI_SUCCESS != rc)) { + BTL_ERROR(("fi_cntr_add failed with err=%s", fi_strerror(-rc))); + MCA_BTL_OFI_ABORT(); } +} - free(notification); +void mca_btl_ofi_notification_finalize(mca_btl_ofi_module_t *module) +{ + for (int i = 0; i < module->notify_table_size; ++i) { + if (NULL != module->notify_table[i].notify) { + (void) fi_close(&module->notify_table[i].notify->cntr->fid); + free(module->notify_table[i].notify); + } + } - return ret; + free(module->notify_table); + module->notify_table = NULL; + module->notify_table_size = 0; } int mca_btl_ofi_notification_read(mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, uint64_t *value) { - uint64_t errors; - - errors = fi_cntr_readerr(notification->cntr); - if (OPAL_UNLIKELY(0 != errors)) { - BTL_ERROR(("notification counter reported %llu error(s)", (unsigned long long) errors)); - return OPAL_ERROR; - } + (void) btl; *value = fi_cntr_read(notification->cntr); @@ -203,18 +212,25 @@ int mca_btl_ofi_notification_wait(mca_btl_base_module_t *btl, struct mca_btl_base_notification_t *notification, uint64_t threshold, int timeout) { - int rc; + opal_timer_t deadline = 0; - rc = fi_cntr_wait(notification->cntr, threshold, timeout); - if (FI_SUCCESS == rc) { - return OPAL_SUCCESS; - } + (void) btl; - if (-FI_ETIMEDOUT == rc) { - return OPAL_ERR_TIMEOUT; + /* the counter only moves when progress processes the completions that + * carry notifications, so fi_cntr_wait cannot be used to block here */ + if (timeout > 0) { + deadline = opal_timer_base_get_usec() + (opal_timer_t) timeout * 1000; } - BTL_VERBOSE(("fi_cntr_wait failed with err=%s", fi_strerror(-rc))); + while (1) { + if (fi_cntr_read(notification->cntr) >= threshold) { + return OPAL_SUCCESS; + } - return OPAL_ERROR; + if (0 == timeout || (timeout > 0 && opal_timer_base_get_usec() >= deadline)) { + return OPAL_ERR_TIMEOUT; + } + + (void) mca_btl_ofi_component.super.btl_progress(); + } } diff --git a/opal/mca/btl/ofi/btl_ofi_rdma.c b/opal/mca/btl/ofi/btl_ofi_rdma.c index 5e7d0b082ed..f621a8eec21 100644 --- a/opal/mca/btl/ofi/btl_ofi_rdma.c +++ b/opal/mca/btl/ofi/btl_ofi_rdma.c @@ -16,6 +16,56 @@ #include "btl_ofi_rdma.h" +/* A flags argument to fi_writemsg / fi_readmsg replaces the endpoint's + * default op_flags rather than adding to them (fi_endpoint(3)), so restate + * the ones the endpoint was opened with (see mca_btl_ofi_component_init). */ +#define MCA_BTL_OFI_WRITE_FLAGS (FI_DELIVERY_COMPLETE | FI_COMPLETION) +#define MCA_BTL_OFI_READ_FLAGS (FI_COMPLETION) + +static inline uint64_t mca_btl_ofi_more_flag(int flags) +{ + return (flags & MCA_BTL_DES_FLAGS_RDMA_MORE) ? FI_MORE : 0; +} + +/* post a single-segment RMA write (with optional remote CQ data) or read */ +static inline ssize_t mca_btl_ofi_rma_msg(mca_btl_ofi_context_t *ofi_context, + mca_btl_ofi_endpoint_t *endpoint, bool write, + void *local_address, size_t size, void *desc, + uint64_t remote_address, uint64_t rkey, uint64_t data, + uint64_t op_flags, void *context) +{ + struct iovec iov = {.iov_base = local_address, .iov_len = size}; + struct fi_rma_iov rma_iov = {.addr = remote_address, .len = size, .key = rkey}; + struct fi_msg_rma msg = {.msg_iov = &iov, + .desc = &desc, + .iov_count = 1, + .addr = endpoint->peer_addr, + .rma_iov = &rma_iov, + .rma_iov_count = 1, + .context = context, + .data = data}; + + return write ? fi_writemsg(ofi_context->tx_ctx, &msg, op_flags) + : fi_readmsg(ofi_context->tx_ctx, &msg, op_flags); +} + +/* undo the bookkeeping for an operation the provider did not accept */ +static int mca_btl_ofi_post_failed(mca_btl_ofi_module_t *ofi_btl, + mca_btl_ofi_rdma_completion_t *comp, ssize_t rc, + const char *what) +{ + MCA_BTL_OFI_NUM_RDMA_DEC(ofi_btl); + opal_free_list_return(comp->base.my_list, (opal_free_list_item_t *) comp); + + if (-FI_EAGAIN == rc) { + return OPAL_ERR_OUT_OF_RESOURCE; + } + + BTL_ERROR(("%s failed with %zd:%s", what, rc, fi_strerror((int) -rc))); + MCA_BTL_OFI_ABORT(); + return OPAL_ERROR; +} + OBJ_CLASS_INSTANCE(mca_btl_ofi_rdma_completion_t, opal_free_list_item_t, NULL, NULL); mca_btl_ofi_rdma_completion_t * @@ -59,7 +109,7 @@ int mca_btl_ofi_get(mca_btl_base_module_t *btl, mca_btl_base_endpoint_t *endpoin void *cbdata) { - int rc; + ssize_t rc; mca_btl_ofi_rdma_completion_t *comp; mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; @@ -77,27 +127,116 @@ int mca_btl_ofi_get(mca_btl_base_module_t *btl, mca_btl_base_endpoint_t *endpoin remote_address = (remote_address - (uint64_t) remote_handle->base_addr); - /* Remote write data across the wire */ - rc = fi_read(ofi_context->tx_ctx, local_address, size, /* payload */ - (NULL == local_handle ? NULL : local_handle->desc), - btl_endpoint->peer_addr, - remote_address, - remote_handle->rkey, - &comp->comp_ctx); /* completion context */ + /* Remote read data across the wire */ + if (OPAL_LIKELY(!(flags & MCA_BTL_DES_FLAGS_RDMA_MORE))) { + rc = fi_read(ofi_context->tx_ctx, local_address, size, /* payload */ + (NULL == local_handle ? NULL : local_handle->desc), + btl_endpoint->peer_addr, + remote_address, + remote_handle->rkey, + &comp->comp_ctx); /* completion context */ + } else { + /* the caller has more to post: let the provider batch them */ + rc = mca_btl_ofi_rma_msg(ofi_context, btl_endpoint, false, local_address, size, + (NULL == local_handle ? NULL : local_handle->desc), + remote_address, remote_handle->rkey, 0, + MCA_BTL_OFI_READ_FLAGS | FI_MORE, &comp->comp_ctx); + } - if (-FI_EAGAIN == rc) { - MCA_BTL_OFI_NUM_RDMA_DEC(ofi_btl); - opal_free_list_return(comp->base.my_list, (opal_free_list_item_t *) comp); - return OPAL_ERR_OUT_OF_RESOURCE; + if (OPAL_UNLIKELY(0 != rc)) { + return mca_btl_ofi_post_failed(ofi_btl, comp, rc, "fi_read"); + } + + return OPAL_SUCCESS; +} + +/* ******************* notified RMA ******************* */ + +/** + * Notifications travel as remote CQ data: the key the target handed out for a + * counter rides in the immediate data of an RMA write, the write generates a + * completion at the target once its data has been placed, and the target's + * progress turns that completion into an increment of the counter (see + * mca_btl_ofi_notification_deliver). A notified put is therefore exactly one + * libfabric operation. + * + * libfabric has no remote CQ data for reads, so a notified get is the read + * followed by a zero-byte write that carries the key. The write must not reach + * the target before the read has finished with the target's memory. With + * FI_FENCE the adapter enforces that and both are posted back to back; + * otherwise the write is posted from the read's completion. + * + * MCA_BTL_DES_FLAGS_RDMA_FENCE extends the ordering to every earlier operation + * to the same peer. FI_FENCE gives exactly that; without it the btl waits for + * its outstanding operations to complete before posting. + */ + +/* apply MCA_BTL_DES_FLAGS_RDMA_FENCE: returns FI_FENCE for the adapter to + * enforce, or waits for outstanding operations when it cannot */ +static inline uint64_t mca_btl_ofi_notify_fence(mca_btl_ofi_module_t *ofi_btl, int flags) +{ + if (!(flags & MCA_BTL_DES_FLAGS_RDMA_FENCE)) { + return 0; + } + if (ofi_btl->use_fence) { + return FI_FENCE; + } + (void) mca_btl_ofi_flush(&ofi_btl->super, NULL); + return 0; +} + +/* a put, notified or not. inlined into both entry points with {notify} + * constant, so a plain put pays nothing for the notified variant */ +static inline int mca_btl_ofi_put_common(mca_btl_base_module_t *btl, + mca_btl_base_endpoint_t *endpoint, void *local_address, + uint64_t remote_address, + mca_btl_base_registration_handle_t *local_handle, + mca_btl_base_registration_handle_t *remote_handle, + size_t size, int flags, bool notify, uint64_t key, + mca_btl_base_rdma_completion_fn_t cbfunc, + void *cbcontext, void *cbdata) +{ + mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; + mca_btl_ofi_endpoint_t *btl_endpoint = (mca_btl_ofi_endpoint_t *) endpoint; + mca_btl_ofi_rdma_completion_t *comp; + mca_btl_ofi_context_t *ofi_context; + /* a zero-byte put_notify may come without a local handle */ + void *desc = (notify && NULL == local_handle) ? NULL : local_handle->desc; + uint64_t notify_flags = 0; + ssize_t rc; + + if (notify) { + notify_flags = FI_REMOTE_CQ_DATA | mca_btl_ofi_notify_fence(ofi_btl, flags); } - if (0 != rc) { - MCA_BTL_OFI_NUM_RDMA_DEC(ofi_btl); - opal_free_list_return(comp->base.my_list, (opal_free_list_item_t *) comp); - BTL_ERROR(("fi_read failed with %d:%s", rc, fi_strerror(-rc))); - MCA_BTL_OFI_ABORT(); + MCA_BTL_OFI_NUM_RDMA_INC(ofi_btl); + + ofi_context = get_ofi_context(ofi_btl); + + /* create completion context */ + comp = mca_btl_ofi_rdma_completion_alloc(btl, endpoint, ofi_context, local_address, + local_handle, cbfunc, cbcontext, cbdata, + MCA_BTL_OFI_TYPE_PUT); + + remote_address = (remote_address - (uint64_t) remote_handle->base_addr); + + /* Remote write data across the wire */ + if (OPAL_LIKELY(!notify && !(flags & MCA_BTL_DES_FLAGS_RDMA_MORE))) { + rc = fi_write(ofi_context->tx_ctx, local_address, size, /* payload */ + desc, btl_endpoint->peer_addr, remote_address, + remote_handle->rkey, &comp->comp_ctx); /* completion context */ + } else { + /* the key rides as remote CQ data; with RDMA_MORE the caller has more + * to post and the provider may batch them */ + rc = mca_btl_ofi_rma_msg(ofi_context, btl_endpoint, true, local_address, size, desc, + remote_address, remote_handle->rkey, key, + MCA_BTL_OFI_WRITE_FLAGS | mca_btl_ofi_more_flag(flags) | notify_flags, + &comp->comp_ctx); } + if (OPAL_UNLIKELY(0 != rc)) { + return mca_btl_ofi_post_failed(ofi_btl, comp, rc, "fi_write"); + } return OPAL_SUCCESS; } @@ -109,39 +248,91 @@ int mca_btl_ofi_put(mca_btl_base_module_t *btl, mca_btl_base_endpoint_t *endpoin int order, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata) { - int rc; + return mca_btl_ofi_put_common(btl, endpoint, local_address, remote_address, local_handle, + remote_handle, size, flags, false, 0, cbfunc, cbcontext, cbdata); +} + +int mca_btl_ofi_put_notify(mca_btl_base_module_t *btl, mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + mca_btl_base_registration_handle_t *local_handle, + mca_btl_base_registration_handle_t *remote_handle, size_t size, + int flags, int order, uint64_t key, + mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, + void *cbdata) +{ + return mca_btl_ofi_put_common(btl, endpoint, local_address, remote_address, local_handle, + remote_handle, size, flags, true, key, cbfunc, cbcontext, + cbdata); +} + +int mca_btl_ofi_post_notify_write(mca_btl_ofi_context_t *context, + mca_btl_ofi_rdma_completion_t *comp) +{ + mca_btl_ofi_endpoint_t *btl_endpoint = (mca_btl_ofi_endpoint_t *) comp->base.endpoint; + mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) comp->base.btl; + + /* zero bytes: the write only exists to carry the key. with FI_FENCE the + * adapter holds it until the read (and everything before it) completes */ + return (int) mca_btl_ofi_rma_msg(context, btl_endpoint, true, NULL, 0, NULL, + comp->remote_address, comp->rkey, comp->notify_key, + MCA_BTL_OFI_WRITE_FLAGS | FI_REMOTE_CQ_DATA + | ofi_btl->notify_write_flags, + &comp->notify_ctx); +} + +int mca_btl_ofi_get_notify(mca_btl_base_module_t *btl, mca_btl_base_endpoint_t *endpoint, + void *local_address, uint64_t remote_address, + mca_btl_base_registration_handle_t *local_handle, + mca_btl_base_registration_handle_t *remote_handle, size_t size, + int flags, int order, uint64_t key, + mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, + void *cbdata) +{ mca_btl_ofi_module_t *ofi_btl = (mca_btl_ofi_module_t *) btl; mca_btl_ofi_endpoint_t *btl_endpoint = (mca_btl_ofi_endpoint_t *) endpoint; + mca_btl_ofi_rdma_completion_t *comp; mca_btl_ofi_context_t *ofi_context; + ssize_t rc; + + /* the write's fence comes from notify_write_flags */ + (void) mca_btl_ofi_notify_fence(ofi_btl, flags); MCA_BTL_OFI_NUM_RDMA_INC(ofi_btl); ofi_context = get_ofi_context(ofi_btl); - /* create completion context */ - mca_btl_ofi_rdma_completion_t *comp; comp = mca_btl_ofi_rdma_completion_alloc(btl, endpoint, ofi_context, local_address, local_handle, cbfunc, cbcontext, cbdata, - MCA_BTL_OFI_TYPE_PUT); - - remote_address = (remote_address - (uint64_t) remote_handle->base_addr); - - /* Remote write data across the wire */ - rc = fi_write(ofi_context->tx_ctx, local_address, size, /* payload */ - local_handle->desc, btl_endpoint->peer_addr, remote_address, remote_handle->rkey, - &comp->comp_ctx); /* completion context */ - - if (-FI_EAGAIN == rc) { - MCA_BTL_OFI_NUM_RDMA_DEC(ofi_btl); - opal_free_list_return(comp->base.my_list, (opal_free_list_item_t *) comp); - return OPAL_ERR_OUT_OF_RESOURCE; + MCA_BTL_OFI_TYPE_GET_NOTIFY); + + comp->notify_ctx.comp = comp; + comp->remote_address = (remote_address - (uint64_t) remote_handle->base_addr); + comp->rkey = remote_handle->rkey; + comp->notify_key = key; + comp->status = OPAL_SUCCESS; + /* with FI_FENCE the write is posted right away and both it and the read + * complete through this object; without it the read's completion posts + * the write (see complete_op_context) */ + comp->pending_ops = ofi_btl->use_fence ? 2 : 1; + + /* with FI_FENCE the write follows immediately, so the read always has more behind it */ + rc = mca_btl_ofi_rma_msg(ofi_context, btl_endpoint, false, local_address, size, + (NULL == local_handle ? NULL : local_handle->desc), + comp->remote_address, comp->rkey, 0, + MCA_BTL_OFI_READ_FLAGS + | (ofi_btl->use_fence ? FI_MORE : mca_btl_ofi_more_flag(flags)), + &comp->comp_ctx); + if (OPAL_UNLIKELY(0 != rc)) { + return mca_btl_ofi_post_failed(ofi_btl, comp, rc, "fi_read"); } - if (0 != rc) { - MCA_BTL_OFI_NUM_RDMA_DEC(ofi_btl); - opal_free_list_return(comp->base.my_list, (opal_free_list_item_t *) comp); - BTL_ERROR(("fi_write failed with %d:%s", rc, fi_strerror(-rc))); - MCA_BTL_OFI_ABORT(); + if (ofi_btl->use_fence) { + /* the read is posted and owns the completion now, so a write that the + * provider cannot take yet is queued for progress to retry rather than + * reported to the caller. FI_FENCE still applies when it is posted. */ + if (OPAL_UNLIKELY(0 != mca_btl_ofi_post_or_queue_notify_write(ofi_context, comp))) { + MCA_BTL_OFI_ABORT(); + } } return OPAL_SUCCESS; From d2e1b6467e575d6190f3ee57878995494049fa86 Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Mon, 9 Nov 2026 08:10:01 -0500 Subject: [PATCH 09/10] test/simple: add notified_rma_paths Covers each shape a notified put/get can take in an RMA component: contiguous, noncontiguous, split by the btl put/get limit, back-to-back without a flush, and all ranks notifying one target, on both created and dynamic windows. Checks data and counter values. Signed-off-by: Joseph Antony --- .gitignore | 1 + test/simple/Makefile | 2 +- test/simple/notified_rma_paths.c | 304 +++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 test/simple/notified_rma_paths.c diff --git a/.gitignore b/.gitignore index 890ab558b4d..b160929e76a 100644 --- a/.gitignore +++ b/.gitignore @@ -441,6 +441,7 @@ test/simple/no-disconnect test/simple/nonzero test/simple/add_host test/simple/win_notify_multi +test/simple/notified_rma_paths oshmem/include/shmem.h oshmem/include/shmem_portable_platform.h diff --git a/test/simple/Makefile b/test/simple/Makefile index 7128377d568..d22506b5d71 100644 --- a/test/simple/Makefile +++ b/test/simple/Makefile @@ -6,7 +6,7 @@ PROGS = mpi_no_op mpi_barrier hello hello_nodename abort multi_abort comm_abort debugger singleton_client_server intercomm_create spawn_tree init-exit77 mpi_info \ info_spawn server client ring binding badcoll attach xlib ompio_file_info \ no-disconnect nonzero interlib pinterlib add_host \ - buffer_attach_detach_f08 win_notify_multi + buffer_attach_detach_f08 win_notify_multi notified_rma_paths all: $(PROGS) diff --git a/test/simple/notified_rma_paths.c b/test/simple/notified_rma_paths.c new file mode 100644 index 00000000000..8f4f47f694b --- /dev/null +++ b/test/simple/notified_rma_paths.c @@ -0,0 +1,304 @@ +/* + * Copyright (c) 2026 Joseph Antony. All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + * + * Exercises the transfer shapes a notified operation can take inside an RMA + * component, and the ordering guarantee for each: + * + * - contiguous put/get (one network operation) + * - non-contiguous put/get (several operations, one notification) + * - transfers larger than the btl put/get limit (split into several + * operations; run with e.g. --mca btl_ofi_put_limit 4096 + * --mca btl_ofi_get_limit 4096 to force this) + * - back-to-back notifications without an intervening flush + * - notifications to one target from every other rank + * - MPI_Win_create and MPI_Win_create_dynamic windows + * + * Windows are created with MPI_Win_create rather than MPI_Win_allocate so + * that on-node peers are reached through the network layer and not through + * shared memory, which is what this test is about. Run with at least 2 ranks: + * + * mpirun -np 4 --mca osc rdma --mca btl ofi,self ./notified_rma_paths + * + * Put: when the target sees the counter advance, every byte of the data must + * already be in its window. + * + * Get: when the target sees the counter advance, the origin must already have + * read the data, so the target may overwrite it. The target does exactly that + * the moment the notification arrives, and the origin checks it still got the + * old contents. + */ + +#include +#include +#include + +#include + +#define STRIDE 3 /* vector stride for the non-contiguous cases */ +#define TIMEOUT_SEC 60.0 + +/* elements in the large and non-contiguous cases (default 256 KB of int, + * large enough to be split) and iterations per case. both can be lowered + * from the command line for slow (software) providers: + * notified_rma_paths [nelem [iters]] */ +static int big_nelem = 64 * 1024; +static int niters = 16; + +static int rank, size; +static int failures = 0; + +#define CHECK(cond, ...) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "[%d] FAIL %s:%d: ", rank, __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + failures++; \ + } \ + } while (0) + +static int wait_counter(MPI_Win win, int idx, MPI_Count expect, const char *what) +{ + double start = MPI_Wtime(); + MPI_Count value = 0; + + do { + MPI_Win_get_notify_value(win, idx, &value); + if (value >= expect) { + CHECK(value == expect, "%s: counter %d overshot: %lld > %lld", what, idx, + (long long) value, (long long) expect); + return 1; + } + } while (MPI_Wtime() - start < TIMEOUT_SEC); + + CHECK(0, "%s: timed out waiting for counter %d: %lld < %lld", what, idx, (long long) value, + (long long) expect); + return 0; +} + +static int pattern(int owner, int iter, int i) +{ + return (owner + 1) * 1000003 + iter * 7919 + i; +} + +/* put a window from left to right neighbour and check the data is complete + * when the notification lands */ +static void test_put(MPI_Win win, int *winbuf, const MPI_Aint *bases, MPI_Datatype origin_type, + int origin_count, MPI_Datatype target_type, int target_count, int nelem, + int stride, const char *what) +{ + int right = (rank + 1) % size, left = (rank + size - 1) % size; + int *src = malloc((size_t) nelem * stride * sizeof(int)); + int ok; + + MPI_Win_reset_notify_value(win, 0, &(MPI_Count){0}); + MPI_Barrier(MPI_COMM_WORLD); + + for (int iter = 0; iter < niters; ++iter) { + for (int i = 0; i < nelem * stride; ++i) { + src[i] = pattern(rank, iter, i); + } + + MPI_Put_notify(src, origin_count, origin_type, right, bases[right], target_count, + target_type, 0, win); + /* the origin may not reuse src before the put completes locally */ + MPI_Win_flush_local(right, win); + + if (!wait_counter(win, 0, iter + 1, what)) { + break; + } + + ok = 1; + for (int i = 0; ok && i < nelem; ++i) { + if (winbuf[i * stride] != pattern(left, iter, i * stride)) { + CHECK(0, "%s: iter %d element %d = %d, expected %d (data behind notification)", + what, iter, i * stride, winbuf[i * stride], pattern(left, iter, i * stride)); + ok = 0; + } + } + + /* everyone has checked this round before the next one overwrites it */ + MPI_Barrier(MPI_COMM_WORLD); + } + + free(src); +} + +/* get from the right neighbour; the neighbour overwrites its window as soon as + * it is notified, so the origin must have finished reading by then */ +static void test_get(MPI_Win win, int *winbuf, const MPI_Aint *bases, MPI_Datatype origin_type, + int origin_count, MPI_Datatype source_type, int source_count, int nelem, + int stride, const char *what) +{ + int right = (rank + 1) % size; + int *dst = malloc((size_t) nelem * stride * sizeof(int)); + int ok; + + MPI_Win_reset_notify_value(win, 1, &(MPI_Count){0}); + + for (int iter = 0; iter < niters; ++iter) { + for (int i = 0; i < nelem * stride; ++i) { + winbuf[i] = pattern(rank, iter, i); + dst[i] = -1; + } + MPI_Win_sync(win); + MPI_Barrier(MPI_COMM_WORLD); + + MPI_Get_notify(dst, origin_count, origin_type, right, bases[right], source_count, + source_type, 1, win); + + /* my left neighbour is reading me: once told it is done, clobber */ + if (wait_counter(win, 1, iter + 1, what)) { + for (int i = 0; i < nelem * stride; ++i) { + winbuf[i] = -2; + } + MPI_Win_sync(win); + } + + MPI_Win_flush(right, win); + + ok = 1; + for (int i = 0; ok && i < nelem; ++i) { + if (dst[i * stride] != pattern(right, iter, i * stride)) { + CHECK(0, "%s: iter %d element %d = %d, expected %d (notified before read done)", + what, iter, i * stride, dst[i * stride], pattern(right, iter, i * stride)); + ok = 0; + } + } + + MPI_Barrier(MPI_COMM_WORLD); + } + + free(dst); +} + +/* every rank but 0 sends rank 0 a burst of notified puts with no flush in + * between; the counter must end at exactly the number sent */ +static void test_fan_in(MPI_Win win, int *winbuf, const MPI_Aint *bases, MPI_Aint unit, const char *what) +{ + const int burst = 64; + + MPI_Win_reset_notify_value(win, 2, &(MPI_Count){0}); + MPI_Barrier(MPI_COMM_WORLD); + + if (0 != rank) { + for (int i = 0; i < burst; ++i) { + int value = pattern(rank, i, 0); + MPI_Put_notify(&value, 1, MPI_INT, 0, bases[0] + rank * unit, 1, MPI_INT, 2, win); + MPI_Win_flush_local(0, win); + } + MPI_Win_flush(0, win); + } else { + if (wait_counter(win, 2, (MPI_Count) burst * (size - 1), what)) { + for (int r = 1; r < size; ++r) { + CHECK(winbuf[r] == pattern(r, burst - 1, 0), "%s: slot %d = %d, expected %d", + what, r, winbuf[r], pattern(r, burst - 1, 0)); + } + } + } + + MPI_Barrier(MPI_COMM_WORLD); +} + +static void run_all(MPI_Win win, int *winbuf, const MPI_Aint *bases, MPI_Aint unit, const char *flavor) +{ + MPI_Datatype vec; + char what[128]; + + MPI_Type_vector(big_nelem, 1, STRIDE, MPI_INT, &vec); + MPI_Type_commit(&vec); + + MPI_Win_lock_all(0, win); + + snprintf(what, sizeof(what), "%s put small contiguous", flavor); + test_put(win, winbuf, bases, MPI_INT, 16, MPI_INT, 16, 16, 1, what); + + snprintf(what, sizeof(what), "%s put large contiguous", flavor); + test_put(win, winbuf, bases, MPI_INT, big_nelem, MPI_INT, big_nelem, big_nelem, 1, what); + + snprintf(what, sizeof(what), "%s put non-contiguous", flavor); + test_put(win, winbuf, bases, vec, 1, vec, 1, big_nelem, STRIDE, what); + + snprintf(what, sizeof(what), "%s get small contiguous", flavor); + test_get(win, winbuf, bases, MPI_INT, 16, MPI_INT, 16, 16, 1, what); + + snprintf(what, sizeof(what), "%s get large contiguous", flavor); + test_get(win, winbuf, bases, MPI_INT, big_nelem, MPI_INT, big_nelem, big_nelem, 1, what); + + snprintf(what, sizeof(what), "%s get non-contiguous", flavor); + test_get(win, winbuf, bases, vec, 1, vec, 1, big_nelem, STRIDE, what); + + snprintf(what, sizeof(what), "%s fan-in", flavor); + test_fan_in(win, winbuf, bases, unit, what); + + MPI_Win_unlock_all(win); + + MPI_Type_free(&vec); +} + +int main(int argc, char *argv[]) +{ + size_t winbytes; + int *winbuf; + MPI_Win win; + int total; + + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + if (argc > 1) { + big_nelem = atoi(argv[1]); + } + if (argc > 2) { + niters = atoi(argv[2]); + } + + if (size < 2) { + if (0 == rank) { + fprintf(stderr, "notified_rma_paths needs at least 2 ranks\n"); + } + MPI_Finalize(); + return 77; + } + + winbytes = (size_t) big_nelem * STRIDE * sizeof(int); + winbuf = calloc(1, winbytes); + + /* MPI_Win_create: displacement unit sizeof(int), displacements from 0 */ + MPI_Aint *bases = calloc(size, sizeof(MPI_Aint)); + MPI_Win_create(winbuf, winbytes, sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win); + MPI_Win_set_num_notify(win, MPI_INFO_NULL, 3); + run_all(win, winbuf, bases, 1, "create"); + MPI_Win_free(&win); + + /* MPI_Win_create_dynamic: displacement unit 1 byte, displacements are the + * target's absolute addresses */ + MPI_Aint my_base; + memset(winbuf, 0, winbytes); + MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &win); + MPI_Win_attach(win, winbuf, winbytes); + MPI_Get_address(winbuf, &my_base); + MPI_Allgather(&my_base, 1, MPI_AINT, bases, 1, MPI_AINT, MPI_COMM_WORLD); + MPI_Win_set_num_notify(win, MPI_INFO_NULL, 3); + run_all(win, winbuf, bases, sizeof(int), "dynamic"); + MPI_Win_detach(win, winbuf); + MPI_Win_free(&win); + free(bases); + + free(winbuf); + + MPI_Allreduce(&failures, &total, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); + if (0 == rank) { + printf("notified_rma_paths: %s (%d failure(s))\n", total ? "FAILED" : "PASSED", total); + } + + MPI_Finalize(); + return total ? 1 : 0; +} From d61e4d2a5567e262e5340a6c1e3e5756fd47b883 Mon Sep 17 00:00:00 2001 From: Joseph Antony Date: Mon, 9 Nov 2026 08:10:19 -0500 Subject: [PATCH 10/10] btl/ofi: add btl_ofi_allow_rxm to use ofi_rxm providers ofi_rxm (e.g. verbs;ofi_rxm on InfiniBand) offers everything btl/ofi asks for, but is rejected by name because it does not promise FI_DELIVERY_COMPLETE. Let users opt in; it stays rejected by default, and other providers are unaffected. Over ofi_rxm, post the zero-byte get_notify write with FI_INJECT: rxm's direct path crashes on a zero-byte write without a memory descriptor over verbs (libfabric 1.22). Signed-off-by: Joseph Antony --- opal/mca/btl/ofi/README.md | 8 ++++++ opal/mca/btl/ofi/btl_ofi.h | 3 +++ opal/mca/btl/ofi/btl_ofi_component.c | 37 ++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/opal/mca/btl/ofi/README.md b/opal/mca/btl/ofi/README.md index c592f3a61c2..ca8d922afd6 100644 --- a/opal/mca/btl/ofi/README.md +++ b/opal/mca/btl/ofi/README.md @@ -17,6 +17,14 @@ the endpoint type of `FI_EP_RDM` only. This BTL does NOT support libfabric provider that requires local memory registration (`FI_MR_LOCAL`). +Providers layered on `ofi_rxm` (for example `verbs;ofi_rxm` on +InfiniBand) are skipped by default because `ofi_rxm` does not promise +`FI_DELIVERY_COMPLETE`: it passes RMA through to the connected endpoint +underneath, and emulates atomics with messages that the target applies +as it progresses. Set `btl_ofi_allow_rxm` to use them; remote completion +then relies on the endpoint underneath (a verbs reliable connected queue +pair completes a write once the target has acknowledged it). + BTL/OFI will initialize a module with ONLY the first compatible info returned from OFI. This means it will rely on OFI provider to do load balancing. The support for multiple device might be added later. diff --git a/opal/mca/btl/ofi/btl_ofi.h b/opal/mca/btl/ofi/btl_ofi.h index b8c3744ddfd..26b1d12dc02 100644 --- a/opal/mca/btl/ofi/btl_ofi.h +++ b/opal/mca/btl/ofi/btl_ofi.h @@ -201,6 +201,9 @@ struct mca_btl_ofi_component_t { bool disable_hmem; + /** accept the ofi_rxm utility provider (e.g. verbs;ofi_rxm on InfiniBand) */ + bool allow_rxm; + /** offer notified RMA (immediate data that advances a counter at the * target) when the provider supports remote CQ data */ bool enable_notify; diff --git a/opal/mca/btl/ofi/btl_ofi_component.c b/opal/mca/btl/ofi/btl_ofi_component.c index d4d8183f3d2..918bf27310d 100644 --- a/opal/mca/btl/ofi/btl_ofi_component.c +++ b/opal/mca/btl/ofi/btl_ofi_component.c @@ -93,12 +93,16 @@ static int validate_info(struct fi_info *info, uint64_t required_caps, char **in return OPAL_ERROR; } - /* ofi_rxm does not fulfill FI_DELIVERY_COMPLETE requirements. Thus we - * exclude it if it's detected. - */ + /* ofi_rxm does not promise FI_DELIVERY_COMPLETE, so it is only used + * when asked for (see the allow_rxm parameter) */ if (strstr(info->fabric_attr->prov_name, "ofi_rxm")) { - BTL_VERBOSE(("ofi_rxm does not support FI_DELIVERY_COMPLETE")); - return OPAL_ERROR; + if (!mca_btl_ofi_component.allow_rxm) { + BTL_VERBOSE(("ofi_rxm does not support FI_DELIVERY_COMPLETE " + "(set btl_ofi_allow_rxm to use it anyway)")); + return OPAL_ERROR; + } + BTL_VERBOSE(("accepting %s: remote completion relies on the endpoint " + "underneath ofi_rxm", info->fabric_attr->prov_name)); } /* we need exactly all the required bits */ @@ -219,6 +223,22 @@ static int mca_btl_ofi_component_register(void) MCA_BASE_VAR_SCOPE_READONLY, &mca_btl_ofi_component.disable_hmem); + mca_btl_ofi_component.allow_rxm = false; + mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, + "allow_rxm", + "Accept providers layered on ofi_rxm, such as " + "verbs;ofi_rxm on InfiniBand. ofi_rxm passes RMA through " + "to the connected endpoint underneath and does not " + "promise FI_DELIVERY_COMPLETE, so remote completion " + "relies on that endpoint (for verbs, a reliable connected " + "queue pair completes a write once the target has " + "acknowledged it). Its atomics are carried as messages " + "and applied by the target's progress.", + MCA_BASE_VAR_TYPE_BOOL, NULL, 0, 0, + OPAL_INFO_LVL_5, + MCA_BASE_VAR_SCOPE_READONLY, + &mca_btl_ofi_component.allow_rxm); + mca_btl_ofi_component.enable_notify = true; mca_base_component_var_register(&mca_btl_ofi_component.super.btl_version, "enable_notify", @@ -754,6 +774,13 @@ static int mca_btl_ofi_init_device(struct fi_info *info) && (ofi_info->caps & FI_FENCE) && (OPAL_HAVE_THREAD_LOCAL || 1 == num_contexts_to_create); module->notify_write_flags = module->use_fence ? FI_FENCE : 0; + /* ofi_rxm leaves its direct RMA path without a memory descriptor for a + * zero-byte write over a provider that needs local registration (verbs), + * and crashes (libfabric 1.22). Its inject path copies into a buffer it + * registered itself. */ + if (NULL != strstr(ofi_info->fabric_attr->prov_name, "ofi_rxm")) { + module->notify_write_flags |= FI_INJECT; + } /* If the domain support scalable endpoint. */ if (domain_attr->max_ep_tx_ctx > 1 && !disable_sep) {