|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2018 Los Alamos National Security, LLC. All rights |
| 3 | + * reserved. |
| 4 | + * Copyright (c) 2019 Triad National Security, LLC. All rights |
| 5 | + * reserved. |
| 6 | + * Copyright (c) 2021 Google, LLC. All rights reserved. |
| 7 | + * Copyright (c) 2022 Amazon.com, Inc. or its affiliates. |
| 8 | + * All Rights reserved. |
| 9 | + * $COPYRIGHT$ |
| 10 | + * |
| 11 | + * Additional copyrights may follow |
| 12 | + * |
| 13 | + * $HEADER$ |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef OSC_RDMA_BTL_COMM_H |
| 17 | +#define OSC_RDMA_BTL_COMM_H |
| 18 | + |
| 19 | +#include "osc_rdma_frag.h" |
| 20 | + |
| 21 | +#include "opal/mca/btl/btl.h" |
| 22 | + |
| 23 | + |
| 24 | +void ompi_osc_rdma_atomic_complete(mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, |
| 25 | + void *local_address, mca_btl_base_registration_handle_t *local_handle, |
| 26 | + void *context, void *data, int status); |
| 27 | + |
| 28 | + |
| 29 | +static inline int |
| 30 | +ompi_osc_rdma_btl_fop(ompi_osc_rdma_module_t *module, uint8_t btl_index, |
| 31 | + struct mca_btl_base_endpoint_t *endpoint, uint64_t address, |
| 32 | + mca_btl_base_registration_handle_t *address_handle, int op, |
| 33 | + int64_t operand, int flags, int64_t *result, const bool wait_for_completion, |
| 34 | + ompi_osc_rdma_pending_op_cb_fn_t cbfunc, void *cbdata, void *cbcontext) |
| 35 | +{ |
| 36 | + ompi_osc_rdma_pending_op_t *pending_op; |
| 37 | + mca_btl_base_module_t *selected_btl = ompi_osc_rdma_selected_btl (module, btl_index); |
| 38 | + int ret = OPAL_ERROR; |
| 39 | + |
| 40 | + pending_op = OBJ_NEW(ompi_osc_rdma_pending_op_t); |
| 41 | + assert (NULL != pending_op); |
| 42 | + |
| 43 | + if (!wait_for_completion) { |
| 44 | + /* NTH: need to keep track of pending ops to avoid a potential teardown problem */ |
| 45 | + pending_op->module = module; |
| 46 | + (void) opal_atomic_fetch_add_32 (&module->pending_ops, 1); |
| 47 | + } |
| 48 | + |
| 49 | + pending_op->op_result = (void *) result; |
| 50 | + pending_op->op_size = (MCA_BTL_ATOMIC_FLAG_32BIT & flags) ? 4 : 8; |
| 51 | + OBJ_RETAIN(pending_op); |
| 52 | + if (cbfunc) { |
| 53 | + pending_op->cbfunc = cbfunc; |
| 54 | + pending_op->cbdata = cbdata; |
| 55 | + pending_op->cbcontext = cbcontext; |
| 56 | + } |
| 57 | + |
| 58 | + /* spin until the btl has accepted the operation */ |
| 59 | + do { |
| 60 | + if (NULL == pending_op->op_frag) { |
| 61 | + ret = ompi_osc_rdma_frag_alloc (module, 8, &pending_op->op_frag, (char **) &pending_op->op_buffer); |
| 62 | + } |
| 63 | + |
| 64 | + if (NULL != pending_op->op_frag) { |
| 65 | + ret = selected_btl->btl_atomic_fop (selected_btl, endpoint, pending_op->op_buffer, |
| 66 | + (intptr_t) address, pending_op->op_frag->handle, address_handle, |
| 67 | + op, operand, flags, MCA_BTL_NO_ORDER, ompi_osc_rdma_atomic_complete, |
| 68 | + (void *) pending_op, NULL); |
| 69 | + } |
| 70 | + |
| 71 | + if (OPAL_LIKELY(!ompi_osc_rdma_oor(ret))) { |
| 72 | + break; |
| 73 | + } |
| 74 | + ompi_osc_rdma_progress (module); |
| 75 | + } while (1); |
| 76 | + |
| 77 | + if (OPAL_SUCCESS != ret) { |
| 78 | + if (OPAL_LIKELY(1 == ret)) { |
| 79 | + *result = ((int64_t *) pending_op->op_buffer)[0]; |
| 80 | + ret = OMPI_SUCCESS; |
| 81 | + ompi_osc_rdma_atomic_complete (selected_btl, endpoint, pending_op->op_buffer, |
| 82 | + pending_op->op_frag->handle, (void *) pending_op, NULL, OPAL_SUCCESS); |
| 83 | + } else { |
| 84 | + /* need to release here because ompi_osc_rdma_atomic_complete was not called */ |
| 85 | + OBJ_RELEASE(pending_op); |
| 86 | + } |
| 87 | + } else if (wait_for_completion) { |
| 88 | + while (!pending_op->op_complete) { |
| 89 | + ompi_osc_rdma_progress (module); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + OBJ_RELEASE(pending_op); |
| 94 | + |
| 95 | + return ret; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +static inline int |
| 100 | +ompi_osc_rdma_btl_op(ompi_osc_rdma_module_t *module, uint8_t btl_index, |
| 101 | + struct mca_btl_base_endpoint_t *endpoint, uint64_t address, |
| 102 | + mca_btl_base_registration_handle_t *address_handle, |
| 103 | + int op, int64_t operand, int flags, const bool wait_for_completion, |
| 104 | + ompi_osc_rdma_pending_op_cb_fn_t cbfunc, void *cbdata, void *cbcontext) |
| 105 | +{ |
| 106 | + ompi_osc_rdma_pending_op_t *pending_op; |
| 107 | + mca_btl_base_module_t *selected_btl = ompi_osc_rdma_selected_btl (module, btl_index); |
| 108 | + int ret; |
| 109 | + |
| 110 | + if (!(selected_btl->btl_flags & MCA_BTL_FLAGS_ATOMIC_OPS)) { |
| 111 | + return ompi_osc_rdma_btl_fop (module, btl_index, endpoint, address, address_handle, op, operand, flags, |
| 112 | + NULL, wait_for_completion, cbfunc, cbdata, cbcontext); |
| 113 | + } |
| 114 | + |
| 115 | + pending_op = OBJ_NEW(ompi_osc_rdma_pending_op_t); |
| 116 | + assert (NULL != pending_op); |
| 117 | + OBJ_RETAIN(pending_op); |
| 118 | + if (cbfunc) { |
| 119 | + pending_op->cbfunc = cbfunc; |
| 120 | + pending_op->cbdata = cbdata; |
| 121 | + pending_op->cbcontext = cbcontext; |
| 122 | + } |
| 123 | + |
| 124 | + if (!wait_for_completion) { |
| 125 | + /* NTH: need to keep track of pending ops to avoid a potential teardown problem */ |
| 126 | + pending_op->module = module; |
| 127 | + (void) opal_atomic_fetch_add_32 (&module->pending_ops, 1); |
| 128 | + } |
| 129 | + |
| 130 | + /* spin until the btl has accepted the operation */ |
| 131 | + do { |
| 132 | + ret = selected_btl->btl_atomic_op (selected_btl, endpoint, (intptr_t) address, address_handle, |
| 133 | + op, operand, flags, MCA_BTL_NO_ORDER, ompi_osc_rdma_atomic_complete, |
| 134 | + (void *) pending_op, NULL); |
| 135 | + |
| 136 | + if (OPAL_LIKELY(!ompi_osc_rdma_oor(ret))) { |
| 137 | + break; |
| 138 | + } |
| 139 | + ompi_osc_rdma_progress (module); |
| 140 | + } while (1); |
| 141 | + |
| 142 | + if (OPAL_SUCCESS != ret) { |
| 143 | + /* need to release here because ompi_osc_rdma_atomic_complete was not called */ |
| 144 | + OBJ_RELEASE(pending_op); |
| 145 | + if (OPAL_LIKELY(1 == ret)) { |
| 146 | + if (cbfunc) { |
| 147 | + cbfunc (cbdata, cbcontext, OMPI_SUCCESS); |
| 148 | + } |
| 149 | + ret = OMPI_SUCCESS; |
| 150 | + } |
| 151 | + } else if (wait_for_completion) { |
| 152 | + while (!pending_op->op_complete) { |
| 153 | + ompi_osc_rdma_progress (module); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + OBJ_RELEASE(pending_op); |
| 158 | + |
| 159 | + return ret; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +static inline int |
| 164 | +ompi_osc_rdma_btl_cswap(ompi_osc_rdma_module_t *module, uint8_t btl_index, |
| 165 | + struct mca_btl_base_endpoint_t *endpoint, uint64_t address, |
| 166 | + mca_btl_base_registration_handle_t *address_handle, |
| 167 | + int64_t compare, int64_t value, int flags, int64_t *result) |
| 168 | +{ |
| 169 | + ompi_osc_rdma_pending_op_t *pending_op; |
| 170 | + mca_btl_base_module_t *selected_btl = ompi_osc_rdma_selected_btl (module, btl_index); |
| 171 | + int ret; |
| 172 | + |
| 173 | + pending_op = OBJ_NEW(ompi_osc_rdma_pending_op_t); |
| 174 | + assert (NULL != pending_op); |
| 175 | + |
| 176 | + OBJ_RETAIN(pending_op); |
| 177 | + |
| 178 | + pending_op->op_result = (void *) result; |
| 179 | + pending_op->op_size = (MCA_BTL_ATOMIC_FLAG_32BIT & flags) ? 4 : 8; |
| 180 | + |
| 181 | + /* spin until the btl has accepted the operation */ |
| 182 | + do { |
| 183 | + if (NULL == pending_op->op_frag) { |
| 184 | + ret = ompi_osc_rdma_frag_alloc (module, 8, &pending_op->op_frag, (char **) &pending_op->op_buffer); |
| 185 | + } |
| 186 | + if (NULL != pending_op->op_frag) { |
| 187 | + ret = selected_btl->btl_atomic_cswap (selected_btl, endpoint, pending_op->op_buffer, |
| 188 | + address, pending_op->op_frag->handle, address_handle, compare, |
| 189 | + value, flags, 0, ompi_osc_rdma_atomic_complete, (void *) pending_op, |
| 190 | + NULL); |
| 191 | + } |
| 192 | + |
| 193 | + if (OPAL_LIKELY(!ompi_osc_rdma_oor(ret))) { |
| 194 | + break; |
| 195 | + } |
| 196 | + ompi_osc_rdma_progress (module); |
| 197 | + } while (1); |
| 198 | + |
| 199 | + if (OPAL_SUCCESS != ret) { |
| 200 | + if (OPAL_LIKELY(1 == ret)) { |
| 201 | + *result = ((int64_t *) pending_op->op_buffer)[0]; |
| 202 | + ret = OMPI_SUCCESS; |
| 203 | + } |
| 204 | + |
| 205 | + /* need to release here because ompi_osc_rdma_atomic_complete was not called */ |
| 206 | + OBJ_RELEASE(pending_op); |
| 207 | + } else { |
| 208 | + while (!pending_op->op_complete) { |
| 209 | + ompi_osc_rdma_progress (module); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + OBJ_RELEASE(pending_op); |
| 214 | + |
| 215 | + return ret; |
| 216 | +} |
| 217 | + |
| 218 | +#endif /* OSC_RDMA_BTL_COMM_H */ |
0 commit comments