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-
2324typedef 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 */
6061typedef 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
520521uct_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
598625UCS_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
824851UCS_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}
0 commit comments