Skip to content

Commit 3c717d4

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 73be454 commit 3c717d4

File tree

3 files changed

+156
-125
lines changed

3 files changed

+156
-125
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: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
void mca_btl_uct_context_enable_am_handler(mca_btl_uct_tl_t *tl,
51+
mca_btl_uct_device_context_t *context)
52+
{
53+
if (context->am_handler_installed) {
54+
return;
55+
}
56+
57+
BTL_VERBOSE(("installing AM handler for tl %s::%s context id %d",
58+
tl->uct_md->md_name, tl->uct_tl_name, context->context_id));
59+
uct_iface_set_am_handler(context->uct_iface, MCA_BTL_UCT_FRAG, mca_btl_uct_am_handler,
60+
context, MCA_BTL_UCT_CB_FLAG_SYNC);
61+
context->am_handler_installed = true;
62+
}
63+
64+
mca_btl_uct_device_context_t *mca_btl_uct_context_create(mca_btl_uct_module_t *module,
65+
mca_btl_uct_tl_t *tl, int context_id,
66+
bool enable_progress)
67+
{
68+
#if UCT_API >= UCT_VERSION(1, 6)
69+
uct_iface_params_t iface_params = {.field_mask = UCT_IFACE_PARAM_FIELD_OPEN_MODE
70+
| UCT_IFACE_PARAM_FIELD_DEVICE,
71+
.open_mode = UCT_IFACE_OPEN_MODE_DEVICE,
72+
.mode = {.device = {.tl_name = tl->uct_tl_name,
73+
.dev_name = tl->uct_dev_name}}};
74+
#else
75+
uct_iface_params_t iface_params = {.rndv_cb = NULL,
76+
.eager_cb = NULL,
77+
.stats_root = NULL,
78+
.rx_headroom = 0,
79+
.open_mode = UCT_IFACE_OPEN_MODE_DEVICE,
80+
.mode = {.device = {.tl_name = tl->uct_tl_name,
81+
.dev_name = tl->uct_dev_name}}};
82+
#endif
83+
mca_btl_uct_device_context_t *context;
84+
ucs_status_t ucs_status;
85+
int rc;
86+
87+
context = calloc(1, sizeof(*context));
88+
if (OPAL_UNLIKELY(NULL == context)) {
89+
return NULL;
90+
}
91+
92+
context->context_id = context_id;
93+
context->uct_btl = module;
94+
OBJ_CONSTRUCT(&context->completion_fifo, opal_fifo_t);
95+
OBJ_CONSTRUCT(&context->mutex, opal_recursive_mutex_t);
96+
OBJ_CONSTRUCT(&context->rdma_completions, opal_free_list_t);
97+
98+
rc = opal_free_list_init(&context->rdma_completions, sizeof(mca_btl_uct_uct_completion_t),
99+
opal_cache_line_size, OBJ_CLASS(mca_btl_uct_uct_completion_t), 0,
100+
opal_cache_line_size, 0, 4096, 128, NULL, 0, NULL, NULL, NULL);
101+
if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
102+
mca_btl_uct_context_destroy(context);
103+
return NULL;
104+
}
105+
106+
/* apparently (in contradiction to the spec) UCT is *not* thread safe. because we have to
107+
* use our own locks just go ahead and use UCS_THREAD_MODE_SINGLE. if they ever fix their
108+
* api then change this back to UCS_THREAD_MODE_MULTI and remove the locks around the
109+
* various UCT calls. */
110+
ucs_status = uct_worker_create(tl->ucs_async, UCS_THREAD_MODE_SINGLE, &context->uct_worker);
111+
if (OPAL_UNLIKELY(UCS_OK != ucs_status)) {
112+
BTL_VERBOSE(("could not create a UCT worker"));
113+
mca_btl_uct_context_destroy(context);
114+
return NULL;
115+
}
116+
117+
ucs_status = uct_iface_open(tl->uct_md->uct_md, context->uct_worker, &iface_params,
118+
tl->uct_tl_config, &context->uct_iface);
119+
if (OPAL_UNLIKELY(UCS_OK != ucs_status)) {
120+
BTL_VERBOSE(("could not open UCT interface. error code: %d", ucs_status));
121+
mca_btl_uct_context_destroy(context);
122+
return NULL;
123+
}
124+
125+
if (module != NULL && tl == module->am_tl) {
126+
mca_btl_uct_context_enable_am_handler(tl, context);
127+
}
128+
129+
if (enable_progress) {
130+
BTL_VERBOSE(("enabling progress for tl %s::%s context id %d",
131+
tl->uct_md->md_name, tl->uct_tl_name, context_id));
132+
mca_btl_uct_context_enable_progress(context);
133+
}
134+
135+
return context;
136+
}
137+
138+
void mca_btl_uct_context_destroy(mca_btl_uct_device_context_t *context)
139+
{
140+
if (context->uct_iface) {
141+
uct_iface_close(context->uct_iface);
142+
context->uct_iface = NULL;
143+
}
144+
145+
if (context->uct_worker) {
146+
uct_worker_destroy(context->uct_worker);
147+
context->uct_worker = NULL;
148+
}
149+
150+
OBJ_DESTRUCT(&context->completion_fifo);
151+
OBJ_DESTRUCT(&context->rdma_completions);
152+
free(context);
153+
}
154+

opal/mca/btl/uct/btl_uct_tl.c

Lines changed: 0 additions & 124 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,111 +305,6 @@ static int mca_btl_uct_populate_tl_attr(mca_btl_uct_tl_t *tl) {
324305
return rc;
325306
}
326307

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

0 commit comments

Comments
 (0)