Skip to content

Commit

Permalink
UCP/CORE: Fix memory cache lookup retrun value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrygx committed Dec 5, 2021
1 parent 301cd84 commit 8659677
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
28 changes: 11 additions & 17 deletions src/ucp/core/ucp_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,25 +497,19 @@ ucp_memory_detect_internal(ucp_context_h context, const void *address,
}

status = ucs_memtype_cache_lookup(address, length, mem_info);
if (status != UCS_ERR_NO_ELEM) {
if (ucs_likely(status != UCS_OK)) {
ucs_assert(status == UCS_ERR_NO_ELEM);
goto out_host_mem;
}

if ((mem_info->type != UCS_MEMORY_TYPE_UNKNOWN) &&
((mem_info->sys_dev != UCS_SYS_DEVICE_ID_UNKNOWN))) {
return;
}

/* Fall thru to slow-path memory type and system device detection by UCT
* memory domains. In any case, the memory type cache is not expected to
* return HOST memory type.
*/
ucs_assert(mem_info->type != UCS_MEMORY_TYPE_HOST);
if (ucs_likely(status == UCS_ERR_NO_ELEM)) {
goto out_host_mem;
} else if ((status == UCS_ERR_UNSUPPORTED) ||
((status == UCS_OK) &&
((mem_info->type == UCS_MEMORY_TYPE_UNKNOWN) ||
(mem_info->sys_dev == UCS_SYS_DEVICE_ID_UNKNOWN)))) {
ucp_memory_detect_slowpath(context, address, length, mem_info);
} else {
ucs_assertv(status == UCS_OK, "%s (%d)", ucs_status_string(status),
status);
}

ucp_memory_detect_slowpath(context, address, length, mem_info);
/* Memory type and system device was detected successfully */
return;

out_host_mem:
Expand Down
6 changes: 5 additions & 1 deletion src/ucs/memory/memtype_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ UCS_PROFILE_FUNC(ucs_status_t, ucs_memtype_cache_lookup,
ucs_status_t status;

if (memtype_cache == NULL) {
return UCS_ERR_NO_ELEM;
return UCS_ERR_UNSUPPORTED;
}

pthread_rwlock_rdlock(&memtype_cache->lock);
Expand All @@ -345,6 +345,10 @@ UCS_PROFILE_FUNC(ucs_status_t, ucs_memtype_cache_lookup,
}
status = UCS_OK;

/* The memory type cache is not expected to return HOST memory type */
ucs_assertv(mem_info->type != UCS_MEMORY_TYPE_HOST, "%s (%d)",
ucs_memory_type_names[mem_info->type], mem_info->type);

out_unlock:
pthread_rwlock_unlock(&memtype_cache->lock);
return status;
Expand Down
4 changes: 3 additions & 1 deletion src/ucs/memory/memtype_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ struct ucs_memtype_cache {
* means the memory type is an unknown non-host
* memory, and should be detected in another way.
*
* @return Error code.
* @return UCS_OK - an element was found and the memory info is valid.
* @return UCS_ERR_NO_ELEM - an element was not found.
* @return UCS_ERR_UNSUPPORTED - the memory type cache is disabled.
*/
ucs_status_t ucs_memtype_cache_lookup(const void *address, size_t size,
ucs_memory_info_t *mem_info);
Expand Down
8 changes: 6 additions & 2 deletions src/uct/cuda/cuda_copy/cuda_copy_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ uct_cuda_copy_get_mem_type(uct_md_h md, void *address, size_t length)
ucs_status_t status;

status = ucs_memtype_cache_lookup(address, length, &mem_info);
if ((status == UCS_ERR_NO_ELEM) ||
((mem_info.type == UCS_MEMORY_TYPE_UNKNOWN))) {
if (status == UCS_ERR_NO_ELEM) {
return UCS_MEMORY_TYPE_HOST;
}

if ((status == UCS_ERR_UNSUPPORTED) ||
(mem_info.type == UCS_MEMORY_TYPE_UNKNOWN)) {
status = uct_cuda_base_detect_memory_type(md, address, length,
&mem_info.type);
if (status != UCS_OK) {
Expand Down

0 comments on commit 8659677

Please sign in to comment.