Skip to content

Commit 526d24c

Browse files
committed
Fold the extra query into the previous one
1 parent 39b132e commit 526d24c

1 file changed

Lines changed: 33 additions & 53 deletions

File tree

libcudacxx/include/cuda/__memory/is_pointer_accessible.h

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -205,66 +205,46 @@ _CCCL_HOST_API inline bool __is_device_accessible(
205205
{
206206
return false;
207207
}
208-
::CUpointer_attribute __attrs[4] = {
209-
::CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
210-
::CU_POINTER_ATTRIBUTE_IS_MANAGED,
211-
::CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,
212-
::CU_POINTER_ATTRIBUTE_MEMPOOL_HANDLE};
213-
auto __memory_type = static_cast<::CUmemorytype>(0);
214-
int __is_managed = 0;
215-
int __ptr_dev_id = 0;
216-
::CUmemoryPool __mempool = nullptr;
217-
void* __results[4] = {&__memory_type, &__is_managed, &__ptr_dev_id, &__mempool};
218-
const auto __status = ::cuda::__driver::__pointerGetAttributesNoThrow(__attrs, __results, __p);
219-
_CCCL_THROW_OR_RETURN(__status, "Failed to get attributes of a pointer");
220-
// (1) check if the pointer is unregistered
221-
if (__memory_type == static_cast<::CUmemorytype>(0))
222-
{
223-
return false;
224-
}
225-
// (2) check if the pointer is a device accessible pointer or managed memory
226-
if (!__is_managed && __memory_type != ::CU_MEMORYTYPE_DEVICE)
227-
{
228-
return false;
229-
}
230-
// (3) check if a memory pool is associated with the pointer
231-
if (__mempool != nullptr)
232-
{
233-
::CUmemLocation __prop{::CU_MEM_LOCATION_TYPE_DEVICE, __device.get()};
234-
::CUmemAccess_flags __pool_flags;
235-
const auto __status2 = ::cuda::__driver::__mempoolGetAccessNoThrow(__pool_flags, __mempool, &__prop);
236-
_CCCL_THROW_OR_RETURN(__status2, "Failed to get access of a memory pool");
237-
return __pool_flags & unsigned{::CU_MEM_ACCESS_FLAGS_PROT_READ};
238-
}
239-
// (4) check if the pointer is allocated on the specified device
240-
if (__ptr_dev_id == __device.get())
241-
{
242-
return true;
243-
}
244-
// (5) check if the pointer is actually accessible from the specified device
245-
if (!__is_managed)
246-
{
247-
int __result = 0;
248-
const auto __status3 = ::cuda::__driver::__deviceCanAccessPeerNoThrow(__result, __device.get(), __ptr_dev_id);
249-
_CCCL_THROW_OR_RETURN(__status3, "Failed to check if the pointer can be peer accessible from the specified device");
250-
if (!__result)
251-
{
252-
return false;
253-
}
254-
}
255-
256208
_CCCL_TRY
257209
{
258210
::cuda::__ensure_current_context __ctx_setter{__device};
259211

260-
void* __device_ptr = nullptr;
261-
const auto __ptr_status =
262-
::cuda::__driver::__pointerGetAttributeNoThrow<::CU_POINTER_ATTRIBUTE_DEVICE_POINTER>(__device_ptr, __p);
263-
if (__ptr_status == ::cudaErrorInvalidValue)
212+
::CUpointer_attribute __attrs[4] = {
213+
::CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
214+
::CU_POINTER_ATTRIBUTE_IS_MANAGED,
215+
::CU_POINTER_ATTRIBUTE_DEVICE_POINTER,
216+
::CU_POINTER_ATTRIBUTE_MEMPOOL_HANDLE};
217+
auto __memory_type = static_cast<::CUmemorytype>(0);
218+
int __is_managed = 0;
219+
void* __device_ptr = nullptr;
220+
::CUmemoryPool __mempool = nullptr;
221+
void* __results[4] = {&__memory_type, &__is_managed, &__device_ptr, &__mempool};
222+
const auto __status = ::cuda::__driver::__pointerGetAttributesNoThrow(__attrs, __results, __p);
223+
if (__status == ::cudaErrorInvalidValue)
224+
{
225+
return false;
226+
}
227+
_CCCL_THROW_OR_RETURN(__status, "Failed to get attributes of a pointer");
228+
// (1) check if the pointer is unregistered
229+
if (__memory_type == static_cast<::CUmemorytype>(0))
264230
{
265231
return false;
266232
}
267-
_CCCL_THROW_OR_RETURN(__ptr_status, "Failed to get the device pointer for the specified device");
233+
// (2) check if the pointer is a device accessible pointer or managed memory
234+
if (!__is_managed && __memory_type != ::CU_MEMORYTYPE_DEVICE)
235+
{
236+
return false;
237+
}
238+
// (3) check if a memory pool is associated with the pointer
239+
if (__mempool != nullptr)
240+
{
241+
::CUmemLocation __prop{::CU_MEM_LOCATION_TYPE_DEVICE, __device.get()};
242+
::CUmemAccess_flags __pool_flags;
243+
const auto __status2 = ::cuda::__driver::__mempoolGetAccessNoThrow(__pool_flags, __mempool, &__prop);
244+
_CCCL_THROW_OR_RETURN(__status2, "Failed to get access of a memory pool");
245+
return __pool_flags & unsigned{::CU_MEM_ACCESS_FLAGS_PROT_READ};
246+
}
247+
// (4) check if the pointer is actually accessible from the specified device
268248
return __device_ptr != nullptr;
269249
}
270250
_CCCL_CATCH_ALL

0 commit comments

Comments
 (0)