Skip to content

Commit 349db3b

Browse files
committed
btl/uct: move device context code to a new file
There is a specific header for device contexts so it makes sense to move the context-specific code to a matching C file. No changes in this other than moving code around. Signed-off-by: Nathan Hjelm <hjelmn@google.com>
1 parent 5ed3871 commit 349db3b

File tree

3 files changed

+145
-114
lines changed

3 files changed

+145
-114
lines changed

opal/mca/btl/uct/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ sources = \
4848
btl_uct_tl.c \
4949
btl_uct_discover.c \
5050
btl_uct_modex.c \
51-
btl_uct_include_list.c
51+
btl_uct_include_list.c \
52+
btl_uct_device_context.c
5253

5354
# Make the output library in this directory, and name it either
5455
# mca_<type>_<name>.la (for DSO builds) or libmca_<type>_<name>.la
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+

opal/mca/btl/uct/btl_uct_tl.c

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
#include "opal/util/bit_ops.h"
2121
#include "opal/util/minmax.h"
2222

23-
#if HAVE_DECL_UCT_CB_FLAG_SYNC
24-
# define MCA_BTL_UCT_CB_FLAG_SYNC UCT_CB_FLAG_SYNC
25-
#else
26-
# define MCA_BTL_UCT_CB_FLAG_SYNC 0
27-
#endif
28-
2923
/**
3024
* @brief Convert UCT capabilities to BTL flags
3125
*/
@@ -264,19 +258,6 @@ static int mca_btl_uct_setup_connection_tl(mca_btl_uct_tl_t *tl)
264258
return UCS_OK == ucs_status ? OPAL_SUCCESS : OPAL_ERROR;
265259
}
266260

267-
static void mca_btl_uct_context_enable_progress(mca_btl_uct_device_context_t *context)
268-
{
269-
if (!context->progress_enabled) {
270-
#if HAVE_DECL_UCT_PROGRESS_THREAD_SAFE
271-
uct_iface_progress_enable(context->uct_iface,
272-
UCT_PROGRESS_THREAD_SAFE | UCT_PROGRESS_SEND | UCT_PROGRESS_RECV);
273-
#else
274-
uct_iface_progress_enable(context->uct_iface, UCT_PROGRESS_SEND | UCT_PROGRESS_RECV);
275-
#endif
276-
context->progress_enabled = true;
277-
}
278-
}
279-
280261
static int mca_btl_uct_populate_tl_attr(mca_btl_uct_tl_t *tl) {
281262
#if UCT_API >= UCT_VERSION(1, 6)
282263
uct_iface_params_t iface_params = {.field_mask = UCT_IFACE_PARAM_FIELD_OPEN_MODE
@@ -324,100 +305,6 @@ static int mca_btl_uct_populate_tl_attr(mca_btl_uct_tl_t *tl) {
324305
return rc;
325306
}
326307

327-
mca_btl_uct_device_context_t *mca_btl_uct_context_create(mca_btl_uct_module_t *module,
328-
mca_btl_uct_tl_t *tl, int context_id,
329-
bool enable_progress)
330-
{
331-
#if UCT_API >= UCT_VERSION(1, 6)
332-
uct_iface_params_t iface_params = {.field_mask = UCT_IFACE_PARAM_FIELD_OPEN_MODE
333-
| UCT_IFACE_PARAM_FIELD_DEVICE,
334-
.open_mode = UCT_IFACE_OPEN_MODE_DEVICE,
335-
.mode = {.device = {.tl_name = tl->uct_tl_name,
336-
.dev_name = tl->uct_dev_name}}};
337-
#else
338-
uct_iface_params_t iface_params = {.rndv_cb = NULL,
339-
.eager_cb = NULL,
340-
.stats_root = NULL,
341-
.rx_headroom = 0,
342-
.open_mode = UCT_IFACE_OPEN_MODE_DEVICE,
343-
.mode = {.device = {.tl_name = tl->uct_tl_name,
344-
.dev_name = tl->uct_dev_name}}};
345-
#endif
346-
mca_btl_uct_device_context_t *context;
347-
ucs_status_t ucs_status;
348-
int rc;
349-
350-
context = calloc(1, sizeof(*context));
351-
if (OPAL_UNLIKELY(NULL == context)) {
352-
return NULL;
353-
}
354-
355-
context->context_id = context_id;
356-
context->uct_btl = module;
357-
OBJ_CONSTRUCT(&context->completion_fifo, opal_fifo_t);
358-
OBJ_CONSTRUCT(&context->mutex, opal_recursive_mutex_t);
359-
OBJ_CONSTRUCT(&context->rdma_completions, opal_free_list_t);
360-
361-
rc = opal_free_list_init(&context->rdma_completions, sizeof(mca_btl_uct_uct_completion_t),
362-
opal_cache_line_size, OBJ_CLASS(mca_btl_uct_uct_completion_t), 0,
363-
opal_cache_line_size, 0, 4096, 128, NULL, 0, NULL, NULL, NULL);
364-
if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
365-
mca_btl_uct_context_destroy(context);
366-
return NULL;
367-
}
368-
369-
/* apparently (in contradiction to the spec) UCT is *not* thread safe. because we have to
370-
* use our own locks just go ahead and use UCS_THREAD_MODE_SINGLE. if they ever fix their
371-
* api then change this back to UCS_THREAD_MODE_MULTI and remove the locks around the
372-
* various UCT calls. */
373-
ucs_status = uct_worker_create(tl->ucs_async, UCS_THREAD_MODE_SINGLE, &context->uct_worker);
374-
if (OPAL_UNLIKELY(UCS_OK != ucs_status)) {
375-
BTL_VERBOSE(("could not create a UCT worker"));
376-
mca_btl_uct_context_destroy(context);
377-
return NULL;
378-
}
379-
380-
ucs_status = uct_iface_open(tl->uct_md->uct_md, context->uct_worker, &iface_params,
381-
tl->uct_tl_config, &context->uct_iface);
382-
if (OPAL_UNLIKELY(UCS_OK != ucs_status)) {
383-
BTL_VERBOSE(("could not open UCT interface. error code: %d", ucs_status));
384-
mca_btl_uct_context_destroy(context);
385-
return NULL;
386-
}
387-
388-
if (module != NULL && tl == module->am_tl) {
389-
BTL_VERBOSE(("installing AM handler for tl %s::%s context id %d",
390-
tl->uct_md->md_name, tl->uct_tl_name, context_id));
391-
uct_iface_set_am_handler(context->uct_iface, MCA_BTL_UCT_FRAG, mca_btl_uct_am_handler,
392-
context, MCA_BTL_UCT_CB_FLAG_SYNC);
393-
}
394-
395-
if (enable_progress) {
396-
BTL_VERBOSE(("enabling progress for tl %s::%s context id %d",
397-
tl->uct_md->md_name, tl->uct_tl_name, context_id));
398-
mca_btl_uct_context_enable_progress(context);
399-
}
400-
401-
return context;
402-
}
403-
404-
void mca_btl_uct_context_destroy(mca_btl_uct_device_context_t *context)
405-
{
406-
if (context->uct_iface) {
407-
uct_iface_close(context->uct_iface);
408-
context->uct_iface = NULL;
409-
}
410-
411-
if (context->uct_worker) {
412-
uct_worker_destroy(context->uct_worker);
413-
context->uct_worker = NULL;
414-
}
415-
416-
OBJ_DESTRUCT(&context->completion_fifo);
417-
OBJ_DESTRUCT(&context->rdma_completions);
418-
free(context);
419-
}
420-
421308
static mca_btl_uct_tl_t *mca_btl_uct_create_tl(mca_btl_uct_md_t *md,
422309
uct_tl_resource_desc_t *tl_desc, int priority)
423310
{

0 commit comments

Comments
 (0)