Skip to content

Commit 3870eee

Browse files
committed
UCT/CUDA/CUDA_IPC: Separated get and put remote cache methods.
1 parent a4abe16 commit 3870eee

3 files changed

Lines changed: 81 additions & 60 deletions

File tree

src/uct/cuda/cuda_ipc/cuda_ipc_cache.c

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010

1111
#include "cuda_ipc_cache.h"
1212
#include "cuda_ipc_iface.h"
13+
#include "cuda_ipc.inl"
14+
15+
#include <ucs/datastruct/khash.h>
1316
#include <ucs/debug/log.h>
1417
#include <ucs/debug/memtrack_int.h>
1518
#include <ucs/profile/profile.h>
1619
#include <ucs/sys/sys.h>
1720
#include <ucs/sys/string.h>
1821
#include <ucs/sys/ptr_arith.h>
19-
#include <ucs/datastruct/khash.h>
22+
#include <ucs/type/rwlock.h>
2023
#include <uct/cuda/base/cuda_ctx.inl>
21-
#include "cuda_ipc.inl"
22-
2324
typedef struct uct_cuda_ipc_cache_hash_key {
2425
pid_t pid;
2526
ucs_sys_ns_t pid_ns;
@@ -59,7 +60,7 @@ KHASH_INIT(cuda_ipc_rem_cache, uct_cuda_ipc_cache_hash_key_t,
5960
*/
6061
typedef struct uct_cuda_ipc_remote_cache {
6162
khash_t(cuda_ipc_rem_cache) hash;
62-
ucs_recursive_spinlock_t lock;
63+
ucs_rw_spinlock_t lock;
6364
unsigned long max_regions; /**< Global max regions limit */
6465
size_t max_size; /**< Global max total size limit */
6566
} uct_cuda_ipc_remote_cache_t;
@@ -516,49 +517,75 @@ static void uct_cuda_ipc_cache_invalidate_regions(uct_cuda_ipc_cache_t *cache,
516517
cache->name, from, to);
517518
}
518519

519-
static ucs_status_t
520+
static UCS_F_ALWAYS_INLINE int
520521
uct_cuda_ipc_get_remote_cache(const uct_cuda_ipc_cache_hash_key_t *key,
521-
uct_cuda_ipc_cache_t **cache)
522+
uct_cuda_ipc_cache_t **cache_p)
523+
{
524+
khint_t it;
525+
int found;
526+
527+
ucs_rw_spinlock_read_lock(&uct_cuda_ipc_remote_cache.lock);
528+
529+
it = kh_get(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash, *key);
530+
found = (it != kh_end(&uct_cuda_ipc_remote_cache.hash));
531+
if (found) {
532+
*cache_p = kh_val(&uct_cuda_ipc_remote_cache.hash, it);
533+
}
534+
535+
ucs_rw_spinlock_read_unlock(&uct_cuda_ipc_remote_cache.lock);
536+
return found;
537+
}
538+
539+
static ucs_status_t
540+
uct_cuda_ipc_put_remote_cache(const uct_cuda_ipc_cache_hash_key_t *key,
541+
uct_cuda_ipc_cache_t **cache_p)
522542
{
523-
ucs_status_t status = UCS_OK;
543+
int ret;
544+
khint_t it;
545+
ucs_status_t status;
524546
char target_name[64];
525-
khiter_t khiter;
526-
int khret;
527547

528-
ucs_recursive_spin_lock(&uct_cuda_ipc_remote_cache.lock);
548+
if (uct_cuda_ipc_get_remote_cache(key, cache_p)) {
549+
return UCS_OK;
550+
}
529551

530-
khiter = kh_put(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash, *key,
531-
&khret);
532-
if ((khret == UCS_KH_PUT_BUCKET_EMPTY) ||
533-
(khret == UCS_KH_PUT_BUCKET_CLEAR)) {
534-
ucs_snprintf_safe(target_name, sizeof(target_name), "dest:%d:%u:%d",
535-
key->pid, key->pid_ns, key->cu_device);
536-
status = uct_cuda_ipc_create_cache(cache, target_name);
537-
if (status != UCS_OK) {
538-
kh_del(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash, khiter);
539-
ucs_error("could not create create cuda ipc cache: %s",
540-
ucs_status_string(status));
541-
goto err_unlock;
542-
}
552+
ucs_rw_spinlock_write_lock(&uct_cuda_ipc_remote_cache.lock);
543553

544-
kh_val(&uct_cuda_ipc_remote_cache.hash, khiter) = *cache;
545-
} else if (khret == UCS_KH_PUT_KEY_PRESENT) {
546-
*cache = kh_val(&uct_cuda_ipc_remote_cache.hash, khiter);
547-
} else {
548-
ucs_error("unable to use cuda_ipc remote_cache hash");
549-
status = UCS_ERR_NO_RESOURCE;
554+
it = kh_put(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash, *key,
555+
&ret);
556+
if (ret == UCS_KH_PUT_FAILED) {
557+
ucs_error("failed to allocate cuda_ipc remote_cache hash entry");
558+
status = UCS_ERR_NO_MEMORY;
559+
goto out_unlock;
550560
}
551-
err_unlock:
552-
ucs_recursive_spin_unlock(&uct_cuda_ipc_remote_cache.lock);
561+
562+
ucs_assertv(ret != UCS_KH_PUT_KEY_PRESENT, "key %d:%u:%d is present",
563+
key->pid, key->pid_ns, key->cu_device);
564+
ucs_assertv((ret == UCS_KH_PUT_BUCKET_EMPTY) ||
565+
(ret == UCS_KH_PUT_BUCKET_CLEAR),
566+
"invalid return value: %d", ret);
567+
568+
ucs_snprintf_safe(target_name, sizeof(target_name), "dest:%d:%u:%d",
569+
key->pid, key->pid_ns, key->cu_device);
570+
status = uct_cuda_ipc_create_cache(cache_p, target_name);
571+
if (status != UCS_OK) {
572+
kh_del(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash, it);
573+
ucs_error("could not create create cuda ipc cache: %s",
574+
ucs_status_string(status));
575+
goto out_unlock;
576+
}
577+
578+
kh_val(&uct_cuda_ipc_remote_cache.hash, it) = *cache_p;
579+
580+
out_unlock:
581+
ucs_rw_spinlock_write_unlock(&uct_cuda_ipc_remote_cache.lock);
553582
return status;
554583
}
555584

556-
ucs_status_t uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
557-
uintptr_t d_bptr,
558-
const void *mapped_addr,
559-
CUdevice cu_dev, int cache_enabled)
585+
void uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
586+
uintptr_t d_bptr, const void *mapped_addr,
587+
CUdevice cu_dev, int cache_enabled)
560588
{
561-
ucs_status_t status = UCS_OK;
562589
const uct_cuda_ipc_cache_hash_key_t key = {pid, pid_ns, cu_dev};
563590
uct_cuda_ipc_cache_t *cache;
564591
ucs_pgt_region_t *pgt_region;
@@ -569,12 +596,13 @@ ucs_status_t uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
569596
* see uct_cuda_ipc_map_memhandle for more details */
570597
if ((d_bptr == (uintptr_t)mapped_addr) &&
571598
uct_cuda_ipc_is_rkey_local(pid, pid_ns)) {
572-
return UCS_OK;
599+
return;
573600
}
574601

575-
status = uct_cuda_ipc_get_remote_cache(&key, &cache);
576-
if (status != UCS_OK) {
577-
return status;
602+
if (!uct_cuda_ipc_get_remote_cache(&key, &cache)) {
603+
ucs_debug("no remote cache found for key: %d:%u:%d", pid, pid_ns,
604+
cu_dev);
605+
return;
578606
}
579607

580608
/* use write lock because cache maybe modified */
@@ -592,7 +620,6 @@ ucs_status_t uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
592620
}
593621

594622
pthread_rwlock_unlock(&cache->lock);
595-
return status;
596623
}
597624

598625
UCS_PROFILE_FUNC(ucs_status_t, uct_cuda_ipc_map_memhandle,
@@ -626,7 +653,7 @@ UCS_PROFILE_FUNC(ucs_status_t, uct_cuda_ipc_map_memhandle,
626653
return UCS_OK;
627654
}
628655

629-
status = uct_cuda_ipc_get_remote_cache(&hash_key, &cache);
656+
status = uct_cuda_ipc_put_remote_cache(&hash_key, &cache);
630657
if (status != UCS_OK) {
631658
return status;
632659
}
@@ -822,7 +849,7 @@ void uct_cuda_ipc_cache_set_global_limits(unsigned long max_regions,
822849
}
823850

824851
UCS_STATIC_INIT {
825-
ucs_recursive_spinlock_init(&uct_cuda_ipc_remote_cache.lock, 0);
852+
ucs_rw_spinlock_init(&uct_cuda_ipc_remote_cache.lock);
826853
kh_init_inplace(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash);
827854
uct_cuda_ipc_remote_cache.max_regions = ULONG_MAX;
828855
uct_cuda_ipc_remote_cache.max_size = SIZE_MAX;
@@ -855,5 +882,5 @@ UCS_STATIC_CLEANUP {
855882
uct_cuda_ipc_destroy_cache(rem_cache);
856883
})
857884
kh_destroy_inplace(cuda_ipc_rem_cache, &uct_cuda_ipc_remote_cache.hash);
858-
ucs_recursive_spinlock_destroy(&uct_cuda_ipc_remote_cache.lock);
885+
ucs_rw_spinlock_cleanup(&uct_cuda_ipc_remote_cache.lock);
859886
}

src/uct/cuda/cuda_ipc/cuda_ipc_cache.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2018. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2018-2026. ALL RIGHTS RESERVED.
33
*
44
* See file LICENSE for terms.
55
*/
@@ -68,10 +68,9 @@ ucs_status_t uct_cuda_ipc_map_memhandle(uct_cuda_ipc_extended_rkey_t *key,
6868
ucs_log_level_t log_level);
6969

7070

71-
ucs_status_t uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
72-
uintptr_t d_bptr,
73-
const void *mapped_addr,
74-
CUdevice cu_dev, int cache_enabled);
71+
void uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
72+
uintptr_t d_bptr, const void *mapped_addr,
73+
CUdevice cu_dev, int cache_enabled);
7574

7675

7776
/**

src/uct/cuda/cuda_ipc/cuda_ipc_iface.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2018-2019. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2018-2026. ALL RIGHTS RESERVED.
33
* See file LICENSE for terms.
44
*/
55

@@ -312,17 +312,12 @@ static void uct_cuda_ipc_complete_event(uct_iface_h tl_iface,
312312
uct_cuda_ipc_iface_t);
313313
uct_cuda_ipc_event_desc_t *cuda_ipc_event = ucs_derived_of(cuda_event,
314314
uct_cuda_ipc_event_desc_t);
315-
ucs_status_t status;
316315

317-
status = uct_cuda_ipc_unmap_memhandle(cuda_ipc_event->pid,
318-
cuda_ipc_event->pid_ns,
319-
cuda_ipc_event->d_bptr,
320-
cuda_ipc_event->mapped_addr,
321-
cuda_ipc_event->cuda_device,
322-
iface->config.enable_cache);
323-
if (status != UCS_OK) {
324-
ucs_fatal("failed to unmap addr:%p", cuda_ipc_event->mapped_addr);
325-
}
316+
uct_cuda_ipc_unmap_memhandle(cuda_ipc_event->pid, cuda_ipc_event->pid_ns,
317+
cuda_ipc_event->d_bptr,
318+
cuda_ipc_event->mapped_addr,
319+
cuda_ipc_event->cuda_device,
320+
iface->config.enable_cache);
326321
}
327322

328323
static uct_iface_ops_t uct_cuda_ipc_iface_ops = {

0 commit comments

Comments
 (0)