Skip to content

Commit 9fb9cfb

Browse files
committed
opal/common/ucx: Simplify Worker Pool TLS structure
Get rid of unneeded context and memory region identifiers Signed-off-by: Artem Polyakov <artpol84@gmail.com>
1 parent 1e7bf70 commit 9fb9cfb

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

opal/mca/common/ucx/common_ucx_wpool.c

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ opal_common_ucx_wpool_init(opal_common_ucx_wpool_t *wpool,
144144
return rc;
145145
}
146146

147-
wpool->cur_ctxid = wpool->cur_memid = 0;
148147
OBJ_CONSTRUCT(&wpool->mutex, opal_mutex_t);
149148
OBJ_CONSTRUCT(&wpool->tls_list, opal_list_t);
150149

@@ -400,8 +399,7 @@ opal_common_ucx_wpctx_create(opal_common_ucx_wpool_t *wpool, int comm_size,
400399
opal_common_ucx_ctx_t *ctx = calloc(1, sizeof(*ctx));
401400
int ret = OPAL_SUCCESS;
402401

403-
ctx->ctx_id = OPAL_ATOMIC_ADD_FETCH32(&wpool->cur_ctxid, 1);
404-
WPOOL_DBG_OUT(_dbg_ctx, "ctx_create: ctx_id = %d\n", (int)ctx->ctx_id);
402+
WPOOL_DBG_OUT(_dbg_ctx, "ctx_create: ctx = %p\n", (void*)ctx);
405403

406404
OBJ_CONSTRUCT(&ctx->mutex, opal_mutex_t);
407405
OBJ_CONSTRUCT(&ctx->tls_workers, opal_list_t);
@@ -553,10 +551,7 @@ int opal_common_ucx_wpmem_create(opal_common_ucx_ctx_t *ctx,
553551
ucs_status_t status;
554552
int ret = OPAL_SUCCESS;
555553

556-
mem->mem_id = OPAL_ATOMIC_ADD_FETCH32(&ctx->wpool->cur_memid, 1);
557-
558-
WPOOL_DBG_OUT(_dbg_mem, "ctx = %p, mem_id = %d\n",
559-
(void *)ctx, (int)mem->mem_id);
554+
WPOOL_DBG_OUT(_dbg_mem, "for ctx = %p\n", (void *)ctx);
560555

561556
mem->released = 0;
562557
mem->refcntr = 1; /* application holding this memory handler */
@@ -820,7 +815,7 @@ static void _common_ucx_tls_cleanup(_tlocal_table_t *tls)
820815
// Cleanup memory table
821816
size = tls->mem_tbl_size;
822817
for (i = 0; i < size; i++) {
823-
if (!tls->mem_tbl[i]->mem_id){
818+
if (NULL == tls->mem_tbl[i]->gmem){
824819
continue;
825820
}
826821
_tlocal_mem_record_cleanup(tls->mem_tbl[i]);
@@ -830,7 +825,7 @@ static void _common_ucx_tls_cleanup(_tlocal_table_t *tls)
830825
// Cleanup ctx table
831826
size = tls->ctx_tbl_size;
832827
for (i = 0; i < size; i++) {
833-
if (!tls->ctx_tbl[i]->ctx_id){
828+
if (NULL == tls->ctx_tbl[i]->gctx){
834829
continue;
835830
}
836831
_tlocal_ctx_record_cleanup(tls->ctx_tbl[i]);
@@ -883,23 +878,22 @@ _tlocal_tls_memtbl_extend(_tlocal_table_t *tbl, size_t append)
883878

884879

885880
static inline _tlocal_ctx_t *
886-
_tlocal_ctx_search(_tlocal_table_t *tls, int ctx_id)
881+
_tlocal_ctx_search(_tlocal_table_t *tls, opal_common_ucx_ctx_t *ctx)
887882
{
888883
size_t i;
889884
for(i=0; i<tls->ctx_tbl_size; i++) {
890-
if( tls->ctx_tbl[i]->ctx_id == ctx_id){
885+
if (tls->ctx_tbl[i]->gctx == ctx){
891886
return tls->ctx_tbl[i];
892887
}
893888
}
894-
WPOOL_DBG_OUT(_dbg_tls, "tls = %p, ctx_id = %d\n",
895-
(void *)tls, (int)ctx_id);
889+
WPOOL_DBG_OUT(_dbg_tls, "tls = %p, ctx = %p\n", (void *)tls, (void*)ctx);
896890
return NULL;
897891
}
898892

899893
static int
900894
_tlocal_ctx_record_cleanup(_tlocal_ctx_t *ctx_rec)
901895
{
902-
if (0 == ctx_rec->ctx_id) {
896+
if (NULL == ctx_rec->gctx) {
903897
return OPAL_SUCCESS;
904898
}
905899
/* Remove myself from the communication context structure
@@ -927,11 +921,13 @@ _tlocal_add_ctx(_tlocal_table_t *tls, opal_common_ucx_ctx_t *ctx)
927921
/* Try to find available record in the TLS table
928922
* In parallel perform deferred cleanups */
929923
for (i=0; i<tls->ctx_tbl_size; i++) {
930-
if (tls->ctx_tbl[i]->gctx->released ) {
931-
/* Found dirty record, need to clean first */
932-
_tlocal_ctx_record_cleanup(tls->ctx_tbl[i]);
924+
if (NULL != tls->ctx_tbl[i]->gctx) {
925+
if (tls->ctx_tbl[i]->gctx->released ) {
926+
/* Found dirty record, need to clean first */
927+
_tlocal_ctx_record_cleanup(tls->ctx_tbl[i]);
928+
}
933929
}
934-
if ((0 == tls->ctx_tbl[i]->ctx_id) && (0 > free_idx)) {
930+
if ((NULL != tls->ctx_tbl[i]->gctx) && (0 > free_idx)) {
935931
/* Found clean record */
936932
free_idx = i;
937933
}
@@ -947,7 +943,6 @@ _tlocal_add_ctx(_tlocal_table_t *tls, opal_common_ucx_ctx_t *ctx)
947943
}
948944
}
949945

950-
tls->ctx_tbl[free_idx]->ctx_id = ctx->ctx_id;
951946
tls->ctx_tbl[free_idx]->gctx = ctx;
952947
tls->ctx_tbl[free_idx]->winfo = _wpool_get_idle(tls->wpool, ctx->comm_size);
953948
if (NULL == tls->ctx_tbl[free_idx]->winfo) {
@@ -1009,13 +1004,13 @@ static int _tlocal_ctx_connect(_tlocal_ctx_t *ctx_rec, int target)
10091004
/* TLS memory management */
10101005

10111006
static inline _tlocal_mem_t *
1012-
_tlocal_search_mem(_tlocal_table_t *tls, int mem_id)
1007+
_tlocal_search_mem(_tlocal_table_t *tls, opal_common_ucx_wpmem_t *gmem)
10131008
{
10141009
size_t i;
1015-
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p mem_id = %d\n",
1016-
(void *)tls, (int)mem_id);
1010+
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p mem = %p\n",
1011+
(void *)tls, (void*)gmem);
10171012
for(i=0; i<tls->mem_tbl_size; i++) {
1018-
if( tls->mem_tbl[i]->mem_id == mem_id){
1013+
if( tls->mem_tbl[i]->gmem == gmem){
10191014
return tls->mem_tbl[i];
10201015
}
10211016
}
@@ -1066,12 +1061,14 @@ static _tlocal_mem_t *_tlocal_add_mem(_tlocal_table_t *tls,
10661061

10671062
/* Try to find available spot in the table */
10681063
for (i=0; i<tls->mem_tbl_size; i++) {
1069-
if (tls->mem_tbl[i]->gmem->released) {
1070-
/* Found a dirty record. Need to clean it first */
1071-
_tlocal_mem_record_cleanup(tls->mem_tbl[i]);
1072-
break;
1064+
if (NULL == tls->mem_tbl[i]->gmem) {
1065+
if (tls->mem_tbl[i]->gmem->released) {
1066+
/* Found a dirty record. Need to clean it first */
1067+
_tlocal_mem_record_cleanup(tls->mem_tbl[i]);
1068+
break;
1069+
}
10731070
}
1074-
if ((0 == tls->mem_tbl[i]->mem_id) && (0 > free_idx)) {
1071+
if ((NULL == tls->mem_tbl[i]->gmem) && (0 > free_idx)) {
10751072
/* Found a clear record */
10761073
free_idx = i;
10771074
}
@@ -1086,17 +1083,17 @@ static _tlocal_mem_t *_tlocal_add_mem(_tlocal_table_t *tls,
10861083
}
10871084
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p\n", (void *)tls);
10881085
}
1089-
tls->mem_tbl[free_idx]->mem_id = mem->mem_id;
1086+
10901087
tls->mem_tbl[free_idx]->gmem = mem;
10911088
tls->mem_tbl[free_idx]->mem = calloc(1, sizeof(*tls->mem_tbl[free_idx]->mem));
10921089

1093-
ctx_rec = _tlocal_ctx_search(tls, mem->ctx->ctx_id);
1090+
ctx_rec = _tlocal_ctx_search(tls, mem->ctx);
10941091
if (NULL == ctx_rec) {
10951092
// TODO: act accordingly - cleanup
10961093
return NULL;
10971094
}
1098-
WPOOL_DBG_OUT("tls = %p, ctx_id = %d\n",
1099-
(void *)tls, (int)mem->ctx->ctx_id);
1095+
WPOOL_DBG_OUT("tls = %p, ctx = %p\n",
1096+
(void *)tls, (void*)mem->ctx);
11001097

11011098
tls->mem_tbl[free_idx]->mem->worker = ctx_rec->winfo;
11021099
tls->mem_tbl[free_idx]->mem->rkeys = calloc(mem->ctx->comm_size,
@@ -1162,10 +1159,10 @@ opal_common_ucx_tlocal_fetch_spath(opal_common_ucx_wpmem_t *mem, int target)
11621159
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p\n",(void*)tls);
11631160

11641161
/* Obtain the worker structure */
1165-
ctx_rec = _tlocal_ctx_search(tls, mem->ctx->ctx_id);
1162+
ctx_rec = _tlocal_ctx_search(tls, mem->ctx);
11661163

1167-
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "ctx_id = %d, ctx_rec=%p\n",
1168-
(int)mem->ctx->ctx_id, (void *)ctx_rec);
1164+
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "ctx_id = %p, ctx_rec=%p\n",
1165+
(void*)mem->ctx, (void *)ctx_rec);
11691166
if (OPAL_UNLIKELY(NULL == ctx_rec)) {
11701167
ctx_rec = _tlocal_add_ctx(tls, mem->ctx);
11711168
if (NULL == ctx_rec) {
@@ -1191,9 +1188,9 @@ opal_common_ucx_tlocal_fetch_spath(opal_common_ucx_wpmem_t *mem, int target)
11911188
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "ep = %p\n", (void *)ep);
11921189

11931190
/* Obtain the memory region info */
1194-
mem_rec = _tlocal_search_mem(tls, mem->mem_id);
1195-
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p mem_rec = %p mem_id = %d\n",
1196-
(void *)tls, (void *)mem_rec, (int)mem->mem_id);
1191+
mem_rec = _tlocal_search_mem(tls, mem);
1192+
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p mem_rec = %p mem_id = %p\n",
1193+
(void *)tls, (void *)mem_rec, (void*)mem);
11971194
if (OPAL_UNLIKELY(mem_rec == NULL)) {
11981195
mem_rec = _tlocal_add_mem(tls, mem);
11991196
WPOOL_DBG_OUT(_dbg_tls || _dbg_mem, "tls = %p mem = %p\n",

opal/mca/common/ucx/common_ucx_wpool.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ typedef struct {
3737
opal_list_t idle_workers;
3838
opal_list_t active_workers;
3939

40-
opal_atomic_int32_t cur_ctxid, cur_memid;
4140
opal_list_t tls_list;
4241
} opal_common_ucx_wpool_t;
4342

4443
typedef struct {
45-
int ctx_id;
4644
opal_mutex_t mutex;
4745
opal_atomic_int32_t refcntr;
4846

@@ -61,7 +59,6 @@ typedef struct {
6159
} opal_common_ucx_ctx_t;
6260

6361
typedef struct {
64-
int mem_id;
6562
/* reference context to which memory region belongs */
6663
opal_common_ucx_ctx_t *ctx;
6764

opal/mca/common/ucx/common_ucx_wpool_int.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "common_ucx_wpool.h"
77

88
typedef struct {
9-
int ctx_id;
109
opal_common_ucx_ctx_t *gctx;
1110
opal_common_ucx_winfo_t *winfo;
1211
} _tlocal_ctx_t;
@@ -17,7 +16,6 @@ typedef struct {
1716
} _mem_info_t;
1817

1918
typedef struct {
20-
int mem_id;
2119
opal_common_ucx_wpmem_t *gmem;
2220
_mem_info_t *mem;
2321
opal_common_ucx_tlocal_fast_ptrs_t *mem_tls_ptr;
@@ -59,13 +57,13 @@ static int _tlocal_tls_memtbl_extend(_tlocal_table_t *tbl, size_t append);
5957
static _tlocal_table_t* _common_ucx_tls_init(opal_common_ucx_wpool_t *wpool);
6058
static void _common_ucx_tls_cleanup(_tlocal_table_t *tls);
6159
static inline _tlocal_ctx_t *_tlocal_ctx_search(_tlocal_table_t *tls,
62-
int ctx_id);
60+
opal_common_ucx_ctx_t *ctx);
6361
static int _tlocal_ctx_record_cleanup(_tlocal_ctx_t *ctx_rec);
6462
static _tlocal_ctx_t *_tlocal_add_ctx(_tlocal_table_t *tls,
6563
opal_common_ucx_ctx_t *ctx);
6664
static int _tlocal_ctx_connect(_tlocal_ctx_t *ctx, int target);
6765
static inline _tlocal_mem_t *_tlocal_search_mem(_tlocal_table_t *tls,
68-
int mem_id);
66+
opal_common_ucx_wpmem_t *gmem);
6967
static _tlocal_mem_t *_tlocal_add_mem(_tlocal_table_t *tls,
7068
opal_common_ucx_wpmem_t *mem);
7169
static int _tlocal_mem_create_rkey(_tlocal_mem_t *mem_rec, ucp_ep_h ep, int target);

0 commit comments

Comments
 (0)