cuda::is_host_accessible, cuda::is_device_accessible, cuda::is_managed#
2026-10-23
2 min read time
This page documents cuda::is_host_accessible, cuda::is_device_accessible, and cuda::is_managed, which query the accessibility of the memory referenced by a pointer.
Defined in the <cuda/memory> header.
namespace cuda {
[[nodiscard]] inline
bool is_host_accessible(const void* ptr); // (1)
[[nodiscard]] inline
bool is_device_accessible(const void* ptr, device_ref device); // (2)
[[nodiscard]] inline
bool is_managed(const void* ptr); // (3)
} // namespace cuda
Determines whether the memory referenced by ptr is accessible from the host (1), from the specified device (2), or is backed by Unified Memory (managed memory) (3).
is_device_accessible()also checks whether the memory is peer-accessible from the specifieddevice.Pinned host memory is reported as host-accessible but not device-accessible.
Parameters
ptr: A pointer to the memory location to query.device: Adevice_refthat denotes the device to query. (2)
Return value
trueif the queried property (host access, device access, or managed allocation) holds; otherwise,false.
Note
A __device__ global array or variable cannot be used directly from host code without first retrieving its address with hipGetSymbolAddress().
Prerequisites
The functions are available only in host code when the HIP runtime is available.
Exceptions
The functions throw a
cuda::cuda_errorif the underlying HIP runtime API calls fail. Note that these functions may also fail with error codes from previously launched asynchronous operations.
Undefined Behavior
The functions have undefined behavior if the pointer is not valid, for example, an already freed pointer.
Example#
#include <cassert>
#include <cuda/memory>
#include <hip/hip_runtime_api.h>
int main() {
cuda::device_ref dev{0};
void* host_ptr = nullptr;
void* device_ptr = nullptr;
void* managed_ptr = nullptr;
hipHostMalloc(&host_ptr, 1024);
hipMalloc(&device_ptr, 1024);
hipMallocManaged(&managed_ptr, 1024);
assert(cuda::is_host_accessible(host_ptr));
assert(!cuda::is_device_accessible(host_ptr, dev));
assert(cuda::is_device_accessible(device_ptr, dev));
assert(!cuda::is_host_accessible(device_ptr));
assert(cuda::is_host_accessible(managed_ptr));
assert(cuda::is_device_accessible(managed_ptr, dev));
assert(cuda::is_managed(managed_ptr));
hipHostFree(host_ptr);
hipFree(device_ptr);
hipFree(managed_ptr);
}