2828#define UCT_GDR_COPY_RCACHE_OVERHEAD_AUTO 50.0e-9
2929
3030
31+ static const char * uct_gdr_copy_pin_mode_names [] = {
32+ [UCT_GDR_COPY_PIN_MODE_DEFAULT ] = "default" ,
33+ [UCT_GDR_COPY_PIN_MODE_PCIE ] = "pcie" ,
34+ [UCT_GDR_COPY_PIN_MODE_LAST ] = NULL
35+ };
36+
3137typedef struct {
3238 pthread_mutex_t lock ;
3339 unsigned refcount ;
@@ -57,6 +63,13 @@ static ucs_config_field_t uct_gdr_copy_md_config_table[] = {
5763 {"MEM_REG_GROWTH" , "0.06ns" , "Memory registration growth rate" , /* TODO take default from device */
5864 ucs_offsetof (uct_gdr_copy_md_config_t , uc_reg_cost .c ), UCS_CONFIG_TYPE_TIME },
5965
66+ {"PIN_MODE" , "default" ,
67+ "Mapping type for CPU access:\n"
68+ " default - Default mapping C2C or PCIe\n"
69+ " pcie - Force a PCIe based mapping (BAR1), may fail at registration.\n" ,
70+ ucs_offsetof (uct_gdr_copy_md_config_t , pin_mode ),
71+ UCS_CONFIG_TYPE_ENUM (uct_gdr_copy_pin_mode_names )},
72+
6073 {"" , "RCACHE_PURGE_ON_FORK=n" , NULL ,
6174 ucs_offsetof (uct_gdr_copy_md_config_t , rcache_config ),
6275 UCS_CONFIG_TYPE_TABLE (ucs_config_rcache_table )},
@@ -139,17 +152,26 @@ UCS_PROFILE_FUNC(ucs_status_t, uct_gdr_copy_mem_reg_internal,
139152 uct_gdr_copy_md_t * md = ucs_derived_of (uct_md , uct_gdr_copy_md_t );
140153 unsigned long d_ptr = ((unsigned long )(char * )address );
141154 ucs_log_level_t log_level ;
155+ uint32_t pin_gdr_flags = 0 ;
142156 int ret ;
143157
144158 ucs_assert ((address != NULL ) && (length != 0 ));
145159
146160 log_level = (flags & UCT_MD_MEM_FLAG_HIDE_ERRORS ) ? UCS_LOG_LEVEL_DEBUG :
147161 UCS_LOG_LEVEL_ERROR ;
148162
163+ #if HAVE_DECL_GDR_PIN_BUFFER_V2
164+ pin_gdr_flags = (md -> pin_mode == UCT_GDR_COPY_PIN_MODE_PCIE ) ?
165+ GDR_PIN_FLAG_FORCE_PCIE : GDR_PIN_FLAG_DEFAULT ;
166+ ret = gdr_pin_buffer_v2 (md -> gdrcpy_ctx , d_ptr , length , pin_gdr_flags ,
167+ & mem_hndl -> mh );
168+ #else
149169 ret = gdr_pin_buffer (md -> gdrcpy_ctx , d_ptr , length , 0 , 0 , & mem_hndl -> mh );
170+ #endif
150171 if (ret ) {
151- ucs_log (log_level , "gdr_pin_buffer failed. length :%lu ret:%d" ,
152- length , ret );
172+ ucs_log (log_level ,
173+ "GPU memory pin failed. length :%lu ret:%d pin_flags:%u" ,
174+ length , ret , pin_gdr_flags );
153175 goto err ;
154176 }
155177
@@ -167,9 +189,11 @@ UCS_PROFILE_FUNC(ucs_status_t, uct_gdr_copy_mem_reg_internal,
167189 goto unmap_buffer ;
168190 }
169191
170- ucs_trace ("registered memory:%p..%p length:%lu info.va:0x%" PRIx64 " bar_ptr:%p" ,
192+ ucs_trace ("registered memory:%p..%p length:%lu info.va:0x%" PRIx64
193+ " bar_ptr:%p mode:%s" ,
171194 address , UCS_PTR_BYTE_OFFSET (address , length ), length ,
172- mem_hndl -> info .va , mem_hndl -> bar_ptr );
195+ mem_hndl -> info .va , mem_hndl -> bar_ptr ,
196+ uct_gdr_copy_pin_mode_names [md -> pin_mode ]);
173197
174198 return UCS_OK ;
175199
@@ -468,6 +492,13 @@ uct_gdr_copy_md_create(uct_component_t *component,
468492 uct_gdr_copy_md_t * md ;
469493 ucs_status_t status ;
470494
495+ #if !HAVE_DECL_GDR_PIN_BUFFER_V2
496+ if (md_config -> pin_mode == UCT_GDR_COPY_PIN_MODE_PCIE ) {
497+ ucs_error ("PCIe pin mode requires GDRCopy with gdr_pin_buffer_v2" );
498+ return UCS_ERR_INVALID_PARAM ;
499+ }
500+ #endif
501+
471502 md = ucs_malloc (sizeof (* md ), "uct_gdr_copy_md_t" );
472503 if (md == NULL ) {
473504 ucs_error ("failed to allocate memory for uct_gdr_copy_md_t" );
@@ -478,6 +509,7 @@ uct_gdr_copy_md_create(uct_component_t *component,
478509 md -> reg_cost = md_config -> uc_reg_cost ;
479510 md -> super .ops = & uct_gdr_copy_md_ops ;
480511 md -> rcache = NULL ;
512+ md -> pin_mode = md_config -> pin_mode ;
481513
482514 md -> gdrcpy_ctx = gdr_open ();
483515 if (md -> gdrcpy_ctx == NULL ) {
@@ -545,6 +577,12 @@ uct_gdr_copy_md_open(uct_component_t *component, const char *md_name,
545577 md_config -> enable_rcache ) {
546578 ucs_error ("inconsistent gdr_copy rcache enable param" );
547579 status = UCS_ERR_INVALID_PARAM ;
580+ } else if (uct_gdr_copy_context .md -> pin_mode != md_config -> pin_mode ) {
581+ ucs_error ("inconsistent gdr_copy PIN_MODE: shared=%s, opening=%s" ,
582+ uct_gdr_copy_pin_mode_names
583+ [uct_gdr_copy_context .md -> pin_mode ],
584+ uct_gdr_copy_pin_mode_names [md_config -> pin_mode ]);
585+ status = UCS_ERR_INVALID_PARAM ;
548586 } else {
549587 status = UCS_OK ;
550588 }
0 commit comments