|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2018 Los Alamos National Security, LLC. All rights |
| 4 | + * reserved. |
| 5 | + * Copyright (c) 2018 Research Organization for Information Science |
| 6 | + * and Technology (RIST). All rights reserved. |
| 7 | + * Copyright (c) 2018 Triad National Security, LLC. All rights |
| 8 | + * reserved. |
| 9 | + * Copyright (c) 2019-2025 Google, LLC. All rights reserved. |
| 10 | + * $COPYRIGHT$ |
| 11 | + * |
| 12 | + * Additional copyrights may follow |
| 13 | + * |
| 14 | + * $HEADER$ |
| 15 | + */ |
| 16 | + |
| 17 | +#include "opal_config.h" |
| 18 | + |
| 19 | +#include <stdbool.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <uct/api/uct.h> |
| 22 | +#include <uct/api/uct_def.h> |
| 23 | + |
| 24 | +#include "btl_uct.h" |
| 25 | +#include "btl_uct_device_context.h" |
| 26 | +#include "btl_uct_types.h" |
| 27 | + |
| 28 | +#include "opal/class/opal_free_list.h" |
| 29 | +#include "opal/class/opal_object.h" |
| 30 | + |
| 31 | +#if HAVE_DECL_UCT_CB_FLAG_SYNC |
| 32 | +# define MCA_BTL_UCT_CB_FLAG_SYNC UCT_CB_FLAG_SYNC |
| 33 | +#else |
| 34 | +# define MCA_BTL_UCT_CB_FLAG_SYNC 0 |
| 35 | +#endif |
| 36 | + |
| 37 | +static void mca_btl_uct_context_enable_progress(mca_btl_uct_device_context_t *context) |
| 38 | +{ |
| 39 | + if (!context->progress_enabled) { |
| 40 | +#if HAVE_DECL_UCT_PROGRESS_THREAD_SAFE |
| 41 | + uct_iface_progress_enable(context->uct_iface, |
| 42 | + UCT_PROGRESS_THREAD_SAFE | UCT_PROGRESS_SEND | UCT_PROGRESS_RECV); |
| 43 | +#else |
| 44 | + uct_iface_progress_enable(context->uct_iface, UCT_PROGRESS_SEND | UCT_PROGRESS_RECV); |
| 45 | +#endif |
| 46 | + context->progress_enabled = true; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +mca_btl_uct_device_context_t *mca_btl_uct_context_create(mca_btl_uct_module_t *module, |
| 51 | + mca_btl_uct_tl_t *tl, int context_id, |
| 52 | + bool enable_progress) |
| 53 | +{ |
| 54 | +#if UCT_API >= UCT_VERSION(1, 6) |
| 55 | + uct_iface_params_t iface_params = {.field_mask = UCT_IFACE_PARAM_FIELD_OPEN_MODE |
| 56 | + | UCT_IFACE_PARAM_FIELD_DEVICE, |
| 57 | + .open_mode = UCT_IFACE_OPEN_MODE_DEVICE, |
| 58 | + .mode = {.device = {.tl_name = tl->uct_tl_name, |
| 59 | + .dev_name = tl->uct_dev_name}}}; |
| 60 | +#else |
| 61 | + uct_iface_params_t iface_params = {.rndv_cb = NULL, |
| 62 | + .eager_cb = NULL, |
| 63 | + .stats_root = NULL, |
| 64 | + .rx_headroom = 0, |
| 65 | + .open_mode = UCT_IFACE_OPEN_MODE_DEVICE, |
| 66 | + .mode = {.device = {.tl_name = tl->uct_tl_name, |
| 67 | + .dev_name = tl->uct_dev_name}}}; |
| 68 | +#endif |
| 69 | + mca_btl_uct_device_context_t *context; |
| 70 | + ucs_status_t ucs_status; |
| 71 | + int rc; |
| 72 | + |
| 73 | + context = calloc(1, sizeof(*context)); |
| 74 | + if (OPAL_UNLIKELY(NULL == context)) { |
| 75 | + return NULL; |
| 76 | + } |
| 77 | + |
| 78 | + context->context_id = context_id; |
| 79 | + context->uct_btl = module; |
| 80 | + OBJ_CONSTRUCT(&context->completion_fifo, opal_fifo_t); |
| 81 | + OBJ_CONSTRUCT(&context->mutex, opal_recursive_mutex_t); |
| 82 | + OBJ_CONSTRUCT(&context->rdma_completions, opal_free_list_t); |
| 83 | + |
| 84 | + rc = opal_free_list_init(&context->rdma_completions, sizeof(mca_btl_uct_uct_completion_t), |
| 85 | + opal_cache_line_size, OBJ_CLASS(mca_btl_uct_uct_completion_t), 0, |
| 86 | + opal_cache_line_size, 0, 4096, 128, NULL, 0, NULL, NULL, NULL); |
| 87 | + if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) { |
| 88 | + mca_btl_uct_context_destroy(context); |
| 89 | + return NULL; |
| 90 | + } |
| 91 | + |
| 92 | + /* apparently (in contradiction to the spec) UCT is *not* thread safe. because we have to |
| 93 | + * use our own locks just go ahead and use UCS_THREAD_MODE_SINGLE. if they ever fix their |
| 94 | + * api then change this back to UCS_THREAD_MODE_MULTI and remove the locks around the |
| 95 | + * various UCT calls. */ |
| 96 | + ucs_status = uct_worker_create(tl->ucs_async, UCS_THREAD_MODE_SINGLE, &context->uct_worker); |
| 97 | + if (OPAL_UNLIKELY(UCS_OK != ucs_status)) { |
| 98 | + BTL_VERBOSE(("could not create a UCT worker")); |
| 99 | + mca_btl_uct_context_destroy(context); |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + ucs_status = uct_iface_open(tl->uct_md->uct_md, context->uct_worker, &iface_params, |
| 104 | + tl->uct_tl_config, &context->uct_iface); |
| 105 | + if (OPAL_UNLIKELY(UCS_OK != ucs_status)) { |
| 106 | + BTL_VERBOSE(("could not open UCT interface. error code: %d", ucs_status)); |
| 107 | + mca_btl_uct_context_destroy(context); |
| 108 | + return NULL; |
| 109 | + } |
| 110 | + |
| 111 | + if (module != NULL && tl == module->am_tl) { |
| 112 | + BTL_VERBOSE(("installing AM handler for tl %s::%s context id %d", |
| 113 | + tl->uct_md->md_name, tl->uct_tl_name, context_id)); |
| 114 | + uct_iface_set_am_handler(context->uct_iface, MCA_BTL_UCT_FRAG, mca_btl_uct_am_handler, |
| 115 | + context, MCA_BTL_UCT_CB_FLAG_SYNC); |
| 116 | + } |
| 117 | + |
| 118 | + if (enable_progress) { |
| 119 | + BTL_VERBOSE(("enabling progress for tl %s::%s context id %d", |
| 120 | + tl->uct_md->md_name, tl->uct_tl_name, context_id)); |
| 121 | + mca_btl_uct_context_enable_progress(context); |
| 122 | + } |
| 123 | + |
| 124 | + return context; |
| 125 | +} |
| 126 | + |
| 127 | +void mca_btl_uct_context_destroy(mca_btl_uct_device_context_t *context) |
| 128 | +{ |
| 129 | + if (context->uct_iface) { |
| 130 | + uct_iface_close(context->uct_iface); |
| 131 | + context->uct_iface = NULL; |
| 132 | + } |
| 133 | + |
| 134 | + if (context->uct_worker) { |
| 135 | + uct_worker_destroy(context->uct_worker); |
| 136 | + context->uct_worker = NULL; |
| 137 | + } |
| 138 | + |
| 139 | + OBJ_DESTRUCT(&context->completion_fifo); |
| 140 | + OBJ_DESTRUCT(&context->rdma_completions); |
| 141 | + free(context); |
| 142 | +} |
| 143 | + |
0 commit comments