4242#include "opal/mca/rcache/base/base.h"
4343#include "opal/mca/rcache/rcache.h"
4444#include "opal/mca/accelerator/accelerator.h"
45- #if OPAL_CUDA_GDR_SUPPORT
46- #include "opal/cuda/common_cuda.h"
47- #endif /* OPAL_CUDA_GDR_SUPPORT */
4845#include "opal/align.h"
4946#include "opal/util/sys_limits.h"
5047#include "rcache_grdma.h"
@@ -61,6 +58,7 @@ static int mca_rcache_grdma_invalidate_range(mca_rcache_base_module_t *rcache, v
6158static void mca_rcache_grdma_finalize (mca_rcache_base_module_t * rcache );
6259static bool mca_rcache_grdma_evict (mca_rcache_base_module_t * rcache );
6360static int mca_rcache_grdma_add_to_gc (mca_rcache_base_registration_t * grdma_reg );
61+ static int check_for_accelerator_freed_memory (mca_rcache_base_module_t * rcache , void * addr , size_t size );
6462
6563static inline bool registration_flags_cacheable (uint32_t flags )
6664{
@@ -75,9 +73,6 @@ static inline bool registration_is_cacheable(mca_rcache_base_registration_t *reg
7573 return registration_flags_cacheable (reg -> flags );
7674}
7775
78- #if OPAL_CUDA_GDR_SUPPORT
79- static int check_for_cuda_freed_memory (mca_rcache_base_module_t * rcache , void * addr , size_t size );
80- #endif /* OPAL_CUDA_GDR_SUPPORT */
8176static void mca_rcache_grdma_cache_contructor (mca_rcache_grdma_cache_t * cache )
8277{
8378 memset ((void * ) ((uintptr_t ) cache + sizeof (cache -> super )), 0 ,
@@ -328,8 +323,7 @@ static int mca_rcache_grdma_register(mca_rcache_base_module_t *rcache, void *add
328323 base = OPAL_DOWN_ALIGN_PTR (addr , page_size , unsigned char * );
329324 bound = OPAL_ALIGN_PTR ((intptr_t ) addr + size , page_size , unsigned char * ) - 1 ;
330325
331- #if OPAL_CUDA_GDR_SUPPORT
332- if (flags & MCA_RCACHE_FLAGS_CUDA_GPU_MEM ) {
326+ if (flags & MCA_RCACHE_FLAGS_ACCELERATOR_MEM ) {
333327 size_t psize ;
334328 int res = opal_accelerator .get_address_range (MCA_ACCELERATOR_NO_DEVICE_ID , addr , (void * * )& base , & psize );
335329 if (OPAL_SUCCESS != res ) {
@@ -338,9 +332,8 @@ static int mca_rcache_grdma_register(mca_rcache_base_module_t *rcache, void *add
338332 bound = base + psize - 1 ;
339333 /* Check to see if this memory is in the cache and if it has been freed. If so,
340334 * this call will boot it out of the cache. */
341- check_for_cuda_freed_memory (rcache , base , psize );
335+ check_for_accelerator_freed_memory (rcache , base , psize );
342336 }
343- #endif /* OPAL_CUDA_GDR_SUPPORT */
344337
345338 do_unregistration_gc (rcache );
346339
@@ -378,11 +371,9 @@ static int mca_rcache_grdma_register(mca_rcache_base_module_t *rcache, void *add
378371 grdma_reg -> flags = flags ;
379372 grdma_reg -> access_flags = access_flags ;
380373 grdma_reg -> ref_count = 1 ;
381- #if OPAL_CUDA_GDR_SUPPORT
382- if (flags & MCA_RCACHE_FLAGS_CUDA_GPU_MEM ) {
383- mca_common_cuda_get_buffer_id (grdma_reg );
374+ if (flags & MCA_RCACHE_FLAGS_ACCELERATOR_MEM ) {
375+ opal_accelerator .get_buffer_id (MCA_ACCELERATOR_NO_DEVICE_ID , grdma_reg -> base , & grdma_reg -> gpu_bufID );
384376 }
385- #endif /* OPAL_CUDA_GDR_SUPPORT */
386377
387378 while (OPAL_ERR_OUT_OF_RESOURCE
388379 == (rc = rcache_grdma -> resources .register_mem (rcache_grdma -> resources .reg_data , base ,
@@ -538,15 +529,34 @@ static int mca_rcache_grdma_invalidate_range(mca_rcache_base_module_t *rcache, v
538529 & args );
539530}
540531
532+ /* Check to see if the memory was freed between the time it was stored in
533+ * the registration cache and now. Return true if the memory was previously
534+ * freed. This is indicated by the BUFFER_ID value in the registration cache
535+ * not matching the BUFFER_ID of the buffer we are checking. Return false
536+ * if the registration is still good.
537+ */
538+ static bool mca_rcache_accelerator_previously_freed_memory (mca_rcache_base_registration_t * reg )
539+ {
540+ int res ;
541+ opal_accelerator_buffer_id_t buf_id ;
542+ unsigned char * dbuf = reg -> base ;
543+ opal_accelerator .get_buffer_id (MCA_ACCELERATOR_NO_DEVICE_ID , dbuf , & buf_id );
544+ if (OPAL_UNLIKELY (res != OPAL_SUCCESS )) {
545+ return true;
546+ }
547+ if (buf_id != reg -> gpu_bufID ) {
548+ return true;
549+ } else {
550+ return false;
551+ }
552+ }
553+
541554/* Make sure this registration request is not stale. In other words, ensure
542555 * that we do not have a cuMemAlloc, cuMemFree, cuMemAlloc state. If we do
543556 * kick out the regisrations and deregister. This function needs to be called
544557 * with the rcache->vma_module->vma_lock held. */
545- #if OPAL_CUDA_GDR_SUPPORT
546-
547- static int check_for_cuda_freed_memory (mca_rcache_base_module_t * rcache , void * addr , size_t size )
558+ static int check_for_accelerator_freed_memory (mca_rcache_base_module_t * rcache , void * addr , size_t size )
548559{
549- unsigned long long buf_id ;
550560 mca_rcache_grdma_module_t * rcache_grdma = (mca_rcache_grdma_module_t * ) rcache ;
551561 mca_rcache_base_registration_t * reg ;
552562
@@ -556,7 +566,7 @@ static int check_for_cuda_freed_memory(mca_rcache_base_module_t *rcache, void *a
556566 }
557567
558568 /* If not previously freed memory, just return 0 */
559- if (!(mca_common_cuda_previously_freed_memory (reg ))) {
569+ if (!(mca_rcache_accelerator_previously_freed_memory (reg ))) {
560570 return OPAL_SUCCESS ;
561571 }
562572
@@ -566,7 +576,6 @@ static int check_for_cuda_freed_memory(mca_rcache_base_module_t *rcache, void *a
566576 return mca_rcache_base_vma_iterate (rcache_grdma -> cache -> vma_module , addr , size , true, gc_add ,
567577 NULL );
568578}
569- #endif /* OPAL_CUDA_GDR_SUPPORT */
570579
571580static void mca_rcache_grdma_finalize (mca_rcache_base_module_t * rcache )
572581{
0 commit comments