Mundy: Multibody Nonlocal Dynamics Version of the Day
Loading...
Searching...
No Matches
mundy::host_ptr< T > Class Template Reference

A reference-counted shared handle to a host-resident T whose handle is trivially copyable onto a device. More...

#include <host_ptr.hpp>

Inheritance diagram for mundy::host_ptr< T >:
[legend]

Public Types

Type aliases
using element_type = T

Public Member Functions

Constructors and destructor
 host_ptr ()=default
 Construct an empty handle that owns and points to nothing.
 host_ptr (std::nullptr_t) noexcept
 Construct an empty handle from nullptr (mirrors std::shared_ptr).
template<typename U>
requires std::is_convertible_v<U*, T*>
 host_ptr (U *ptr)
 Take ownership of ptr, deleting it with delete when the last reference drops.
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
 host_ptr (U *ptr, Deleter deleter)
 Take ownership of ptr, invoking deleter(ptr) when the last reference drops.
template<typename Deleter>
 host_ptr (std::nullptr_t, Deleter deleter)
 Own no object but carry a deleter to invoke with nullptr (mirrors std::shared_ptr).
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
 host_ptr (std::unique_ptr< U, Deleter > &&owner)
 Adopt the object owned by a std::unique_ptr.
 host_ptr (const T &value)
 Construct a fresh T by copying value (convenience beyond std::shared_ptr).
 host_ptr (T &&value)
 Construct a fresh T by moving value (convenience beyond std::shared_ptr).
template<typename... Args>
 host_ptr (std::in_place_t, Args &&... args)
 Construct a fresh T in place, forwarding args to its constructor.
 host_ptr (const host_ptr &)=default
 Shallow copy: shares ownership. Trivially device-copyable.
 host_ptr (host_ptr &&)=default
template<typename U>
requires std::is_convertible_v<U*, T*>
 host_ptr (const host_ptr< U > &other)
 Converting copy: host_ptr<Base> from host_ptr<Derived> (shares ownership, retypes the pointer).
template<typename U>
requires std::is_convertible_v<U*, T*>
 host_ptr (host_ptr< U > &&other)
 Converting move.
template<typename U>
 host_ptr (const host_ptr< U > &other, T *ptr)
 Aliasing constructor: shares other's ownership but points at ptr (which need not be other's object).
 ~host_ptr ()
 Release this handle's ownership; runs the disposer if this was the last reference.
Assignment (host-only; copy-and-swap, so the prior value is released exactly once)
host_ptroperator= (const host_ptr &other)
host_ptroperator= (host_ptr &&other) noexcept
template<typename U>
requires std::is_convertible_v<U*, T*>
host_ptroperator= (const host_ptr< U > &other)
template<typename U>
requires std::is_convertible_v<U*, T*>
host_ptroperator= (host_ptr< U > &&other)
host_ptroperator= (std::nullptr_t) noexcept
Modifiers (host-only)
void reset () noexcept
 Become empty, releasing ownership (runs the disposer if this was the last reference).
template<typename U>
requires std::is_convertible_v<U*, T*>
void reset (U *ptr)
 Replace the managed object with ownership of ptr (deleted with delete).
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
void reset (U *ptr, Deleter deleter)
 Replace the managed object with ownership of ptr and a custom deleter.
template<typename... Args>
void emplace (Args &&... args)
 Replace the managed object with a fresh T constructed from args (convenience).
void swap (host_ptr &other) noexcept
 Swap ownership and pointer with another handle.

Observers (host-only)

template<typename U>
class host_ptr
T & operator* () const noexcept
 Reference to the pointed-to value. Undefined if this handle points to nothing.
T * operator-> () const noexcept
 Pointer to the pointed-to value (null if this handle points to nothing).
T * get () const noexcept
 Pointer to the pointed-to value (null if this handle points to nothing). Mirrors std::shared_ptr::get.
long use_count () const noexcept
 Number of host_ptr instances sharing ownership (0 if empty).
 operator bool () const noexcept
 True if this handle points to an object.
template<typename U>
bool owner_before (const host_ptr< U > &other) const noexcept
 Ownership-based ordering (by control block, like std::shared_ptr::owner_before).

Detailed Description

template<typename T>
class mundy::host_ptr< T >

host_ptr<T> is, semantically, a std::shared_ptr<T> that survives being captured by value into a device kernel: were device support not a concern, a plain std::shared_ptr<T> would be used instead. It mirrors std::shared_ptr in ownership model and interface — owning raw-pointer construction, custom deleters, unique_ptr adoption, the aliasing constructor, DerivedBase conversions, the pointer casts, ordering/hashing/owner_before, and reset — with the single deliberate omission of weak_ptr/enable_shared_from_this.

How the device-copyability works
A host_ptr is a Kokkos::View (the device-copyable, reference-counted allocation) holding a small type-erased control block, plus a typed T* ptr_. The View supplies the reference count — atomic on the host, with tracking disabled inside kernels, so a copy captured into a kernel is a cheap non-owning handle and the owner must outlive the kernel. The hand-rolled control block supplies only type-erased destruction (the disposer), so deleters, adoption, aliasing, and polymorphism all work without std::shared_ptr. The value lives in host memory and is accessible only on the host: the accessors are not KOKKOS_FUNCTION, so dereferencing on the device is a compile error.
Template Parameters
THost-resident pointed-to type.

Member Typedef Documentation

◆ element_type

template<typename T>
using mundy::host_ptr< T >::element_type = T

Constructor & Destructor Documentation

◆ host_ptr() [1/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( )
default

Device-callable so a host_ptr member can sit inside a KOKKOS_DEFAULTED-constructible, device-capturable class.

◆ host_ptr() [2/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( std::nullptr_t )
inlinenoexcept

◆ host_ptr() [3/14]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
mundy::host_ptr< T >::host_ptr ( U * ptr)
inlineexplicit

◆ host_ptr() [4/14]

template<typename T>
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
mundy::host_ptr< T >::host_ptr ( U * ptr,
Deleter deleter )
inline

◆ host_ptr() [5/14]

template<typename T>
template<typename Deleter>
mundy::host_ptr< T >::host_ptr ( std::nullptr_t ,
Deleter deleter )
inline

◆ host_ptr() [6/14]

template<typename T>
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
mundy::host_ptr< T >::host_ptr ( std::unique_ptr< U, Deleter > && owner)
inline

◆ host_ptr() [7/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( const T & value)
inlineexplicit

◆ host_ptr() [8/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( T && value)
inlineexplicit

◆ host_ptr() [9/14]

template<typename T>
template<typename... Args>
mundy::host_ptr< T >::host_ptr ( std::in_place_t ,
Args &&... args )
inlineexplicit

◆ host_ptr() [10/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( const host_ptr< T > & )
default

◆ host_ptr() [11/14]

template<typename T>
mundy::host_ptr< T >::host_ptr ( host_ptr< T > && )
default

◆ host_ptr() [12/14]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
mundy::host_ptr< T >::host_ptr ( const host_ptr< U > & other)
inline

◆ host_ptr() [13/14]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
mundy::host_ptr< T >::host_ptr ( host_ptr< U > && other)
inline

◆ host_ptr() [14/14]

template<typename T>
template<typename U>
mundy::host_ptr< T >::host_ptr ( const host_ptr< U > & other,
T * ptr )
inline

◆ ~host_ptr()

template<typename T>
mundy::host_ptr< T >::~host_ptr ( )
inline

Member Function Documentation

◆ operator=() [1/5]

template<typename T>
host_ptr & mundy::host_ptr< T >::operator= ( const host_ptr< T > & other)
inline

◆ operator=() [2/5]

template<typename T>
host_ptr & mundy::host_ptr< T >::operator= ( host_ptr< T > && other)
inlinenoexcept

◆ operator=() [3/5]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
host_ptr & mundy::host_ptr< T >::operator= ( const host_ptr< U > & other)
inline

◆ operator=() [4/5]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
host_ptr & mundy::host_ptr< T >::operator= ( host_ptr< U > && other)
inline

◆ operator=() [5/5]

template<typename T>
host_ptr & mundy::host_ptr< T >::operator= ( std::nullptr_t )
inlinenoexcept

◆ reset() [1/3]

template<typename T>
void mundy::host_ptr< T >::reset ( )
inlinenoexcept

◆ reset() [2/3]

template<typename T>
template<typename U>
requires std::is_convertible_v<U*, T*>
void mundy::host_ptr< T >::reset ( U * ptr)
inline

◆ reset() [3/3]

template<typename T>
template<typename U, typename Deleter>
requires std::is_convertible_v<U*, T*>
void mundy::host_ptr< T >::reset ( U * ptr,
Deleter deleter )
inline

◆ emplace()

template<typename T>
template<typename... Args>
void mundy::host_ptr< T >::emplace ( Args &&... args)
inline

◆ swap()

template<typename T>
void mundy::host_ptr< T >::swap ( host_ptr< T > & other)
inlinenoexcept

◆ operator*()

template<typename T>
T & mundy::host_ptr< T >::operator* ( ) const
inlinenoexcept

◆ operator->()

template<typename T>
T * mundy::host_ptr< T >::operator-> ( ) const
inlinenoexcept

◆ get()

template<typename T>
T * mundy::host_ptr< T >::get ( ) const
inlinenoexcept

◆ use_count()

template<typename T>
long mundy::host_ptr< T >::use_count ( ) const
inlinenoexcept

◆ operator bool()

template<typename T>
mundy::host_ptr< T >::operator bool ( ) const
inlineexplicitnoexcept

◆ owner_before()

template<typename T>
template<typename U>
bool mundy::host_ptr< T >::owner_before ( const host_ptr< U > & other) const
inlinenoexcept

◆ host_ptr

template<typename T>
template<typename U>
friend class host_ptr
friend