stdgpu/memory.h Source File — stdgpu Latest documentation (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16#ifndef STDGPU_MEMORY_H

17#define STDGPU_MEMORY_H

18

28#include

29#include

30#include

31#include <type_traits>

32

33#include <stdgpu/config.h>

38

48template

49[[nodiscard]] T*

51

61template

62[[nodiscard]] T*

64

71template

72void

74

81template

82void

84

90{

94};

95

105template

106[[nodiscard]] T*

110

120template

121[[nodiscard]] T*

125

135template

136[[nodiscard]] T*

140

150template

151[[nodiscard]] T*

155

165template

166void

169 T* destination_host_array,

171

181template

182void

185 T* destination_device_array,

187

197template

198void

201 T* destination_host_array,

203

213template

214void

217 T* destination_device_array,

219

220namespace stdgpu

221{

222

228{

230 constexpr explicit null_object_t(int ) { }

232};

233

239

245template

247{

248public:

253

259 template <typename... Args>

261

269 template <typename ExecutionPolicy,

270 typename... Args,

271 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

273

278 const T*

280

285 T*

287

292 const T&

294

299 T&

301

306 explicit

307 operator bool() const;

308

309private:

310 std::unique_ptr<T, std::function<void(T*)>> _object;

311};

312

318{

322};

323

330template

333

339template

341{

348

353

358

363

369 template

371

378

383

390

396 [[nodiscard]] T*

398

404 void

406};

407

413template

415{

422

427

432

437

443 template

445

452

457

464

470 [[nodiscard]] T*

472

478 void

480};

481

490template

492{

494 using value_type = typename Allocator::value_type;

496 using const_pointer = typename std::pointer_traits::template rebind<

498 using void_pointer = typename std::pointer_traits::template rebind<

499 void>;

500 using const_void_pointer = typename std::pointer_traits::template rebind<

501 const void>;

503 typename std::pointer_traits::difference_type;

510 template

512 T>;

513 template

522 [[nodiscard]] static pointer

524

532 [[nodiscard]] static pointer

534

541 static void

543

553 template <typename ExecutionPolicy,

554 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

555 [[nodiscard]] static pointer

557

566 template <typename ExecutionPolicy,

567 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

568 static void

570

579 template <typename T, class... Args>

581 construct(Allocator& a, T* p, Args&&... args);

582

589 template

592

600

606 static Allocator

608};

609

617template

620

628template

631

641template <typename T, typename... Args>

644

651template

654

655#ifdef STDGPU_RUN_DOXYGEN

656

668template <typename ExecutionPolicy,

669 typename Iterator,

670 typename T,

671 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

672void

673uninitialized_fill(ExecutionPolicy&& policy, Iterator begin, Iterator end, const T& value);

674

688template <typename ExecutionPolicy,

689 typename Iterator,

690 typename Size,

691 typename T,

692 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

693Iterator

695

708template <typename ExecutionPolicy,

709 typename InputIt,

710 typename OutputIt,

711 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

712OutputIt

713uninitialized_copy(ExecutionPolicy&& policy, InputIt begin, InputIt end, OutputIt output_begin);

714

728template <typename ExecutionPolicy,

729 typename InputIt,

730 typename Size,

731 typename OutputIt,

732 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

733OutputIt

734uninitialized_copy_n(ExecutionPolicy&& policy, InputIt begin, Size n, OutputIt output_begin);

735

745template <typename ExecutionPolicy,

746 typename Iterator,

747 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

748void

749destroy(ExecutionPolicy&& policy, Iterator first, Iterator last);

750

762template <typename ExecutionPolicy,

763 typename Iterator,

764 typename Size,

765 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

766Iterator

767destroy_n(ExecutionPolicy&& policy, Iterator first, Size n);

768

769#endif

770

772namespace adl_barrier

773{

774

775template <typename ExecutionPolicy,

776 typename Iterator,

777 typename T,

778 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

779void

780uninitialized_fill(ExecutionPolicy&& policy, Iterator begin, Iterator end, const T& value);

781

782template <typename ExecutionPolicy,

783 typename Iterator,

784 typename Size,

785 typename T,

786 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

787Iterator

788uninitialized_fill_n(ExecutionPolicy&& policy, Iterator begin, Size n, const T& value);

789

790template <typename ExecutionPolicy,

791 typename InputIt,

792 typename OutputIt,

793 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

794OutputIt

795uninitialized_copy(ExecutionPolicy&& policy, InputIt begin, InputIt end, OutputIt output_begin);

796

797template <typename ExecutionPolicy,

798 typename InputIt,

799 typename Size,

800 typename OutputIt,

801 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

802OutputIt

803uninitialized_copy_n(ExecutionPolicy&& policy, InputIt begin, Size n, OutputIt output_begin);

804

805template <typename ExecutionPolicy,

806 typename Iterator,

807 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

808void

809destroy(ExecutionPolicy&& policy, Iterator first, Iterator last);

810

811template <typename ExecutionPolicy,

812 typename Iterator,

813 typename Size,

814 STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t>)>

815Iterator

816destroy_n(ExecutionPolicy&& policy, Iterator first, Size n);

817

818}

819

820using namespace adl_barrier;

822

831template

832void

834

844template

845void

847

856

865

873template

876

877}

878

879#include <stdgpu/impl/memory_detail.h>

880

881#endif

const T * operator->() const

Returns a pointer to the managed object.

T & operator*()

Returns a reference to the managed object.

device_unique_object(Args &&... args)

Creates an object on the GPU (device)

device_unique_object(ExecutionPolicy &&policy, Args &&... args)

Creates an object on the GPU (device)

const T & operator*() const

Returns a reference to the managed object.

T * operator->()

Returns a pointer to the managed object.

A resource wrapper for managing device objects with automatic scope-based object destruction.

Definition: memory.h:247

device_unique_object(null_object_t)

Creates an empty unique object.

std::ptrdiff_t index64_t

std::ptrdiff_t

Definition: cstddef.h:48

index64_t get_deallocation_count(dynamic_memory_type memory_type)

Returns the total number of registered deallocations of a specific memory type.

T * createDeviceArray(const stdgpu::index64_t count, const T default_value=T())

Creates a new device array and initializes (fills) it with the given default value.

STDGPU_HOST_DEVICE T * construct_at(T *p, Args &&... args)

Destroys the value at the given pointer.

T * copyCreateHost2HostArray(const T *host_array, const stdgpu::index64_t count, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Creates and copies the given host array to the host.

index64_t size_bytes(T *array)

Finds the size (in bytes) of the given dynamically allocated array.

void destroyHostArray(T *&host_array)

Destroys the given host array.

OutputIt uninitialized_copy_n(ExecutionPolicy &&policy, InputIt begin, Size n, OutputIt output_begin)

Copies all elements of the input range to the output range using the copy constructor.

index64_t get_allocation_count(dynamic_memory_type memory_type)

Returns the total number of registered allocations of a specific memory type.

void uninitialized_fill(ExecutionPolicy &&policy, Iterator begin, Iterator end, const T &value)

Writes the given value to into the given range using the copy constructor.

T * createHostArray(const stdgpu::index64_t count, const T default_value=T())

Creates a new host array and initializes (fills) it with the given default value.

T * copyCreateHost2DeviceArray(const T *host_array, const stdgpu::index64_t count, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Creates and copies the given host array to the device.

void deregister_memory(T *p, index64_t n, dynamic_memory_type memory_type)

Deregisters the given memory block into the internal memory size manger.

dynamic_memory_type

The types of a dynamically allocated array.

Definition: memory.h:318

void register_memory(T *p, index64_t n, dynamic_memory_type memory_type)

Registers the given memory block into the internal memory size manger.

void destroy(ExecutionPolicy &&policy, Iterator first, Iterator last)

Destroys the range of values.

Iterator uninitialized_fill_n(ExecutionPolicy &&policy, Iterator begin, Size n, const T &value)

Writes the given value to into the given range using the copy constructor.

void copyHost2HostArray(const T *source_host_array, const stdgpu::index64_t count, T *destination_host_array, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Copies the given host array to the host.

Iterator destroy_n(ExecutionPolicy &&policy, Iterator first, Size n)

Destroys the range of values.

OutputIt uninitialized_copy(ExecutionPolicy &&policy, InputIt begin, InputIt end, OutputIt output_begin)

Copies all elements of the input range to the output range using the copy constructor.

dynamic_memory_type get_dynamic_memory_type(T *array)

Determines the dynamic memory type of the given array.

void copyDevice2DeviceArray(const T *source_device_array, const stdgpu::index64_t count, T *destination_device_array, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Copies the given device array to the device.

STDGPU_HOST_DEVICE void destroy_at(T *p)

Destroys the value at the given pointer.

T * copyCreateDevice2DeviceArray(const T *device_array, const stdgpu::index64_t count, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Creates and copies the given device array to the device.

STDGPU_HOST_DEVICE T * to_address(T *p) noexcept

Converts a potential fancy pointer to a raw pointer.

void copyHost2DeviceArray(const T *source_host_array, const stdgpu::index64_t count, T *destination_device_array, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Copies the given host array to the device.

MemoryCopy

The copy check states.

Definition: memory.h:90

void copyDevice2HostArray(const T *source_device_array, const stdgpu::index64_t count, T *destination_host_array, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Copies the given device array to the host.

T * copyCreateDevice2HostArray(const T *device_array, const stdgpu::index64_t count, const MemoryCopy check_safety=MemoryCopy::RANGE_CHECK)

Creates and copies the given device array to the host.

void destroyDeviceArray(T *&device_array)

Destroys the given device array.

constexpr null_object_t null_object

A constant to indicate an uninitialized unique object.

Definition: memory.h:238

#define STDGPU_HOST_DEVICE

Platform-independent host device function annotation.

Definition: platform.h:77

typename Allocator::value_type value_type

Definition: memory.h:494

static pointer allocate(Allocator &a, index_type n, const_void_pointer hint)

Allocates a memory block of the given size.

std::false_type propagate_on_container_copy_assignment

Definition: memory.h:506

static pointer allocate(Allocator &a, index_type n)

Allocates a memory block of the given size.

std::is_empty< Allocator > is_always_equal

Definition: memory.h:509

typename std::pointer_traits< pointer >::template rebind< const value_type > const_pointer

Definition: memory.h:497

value_type * pointer

Definition: memory.h:495

typename std::pointer_traits< pointer >::template rebind< const void > const_void_pointer

Definition: memory.h:501

static Allocator select_on_container_copy_construction(const Allocator &a)

Returns a copy of the allocator.

static void deallocate(Allocator &a, pointer p, index_type n)

Deallocates the given memory block.

typename std::allocator_traits< Allocator >::template rebind_alloc< T > rebind_alloc

Definition: memory.h:512

index64_t index_type

Definition: memory.h:505

static STDGPU_HOST_DEVICE index_type max_size(const Allocator &a) noexcept

Returns the maximum size that could be theoretically allocated.

static STDGPU_HOST_DEVICE void destroy(Allocator &a, T *p)

Destroys the object value at the given pointer.

static void deallocate_filled(ExecutionPolicy &&policy, Allocator &a, pointer p, index_type n)

Deallocates the given filled memory block.

std::false_type propagate_on_container_swap

Definition: memory.h:508

std::false_type propagate_on_container_move_assignment

Definition: memory.h:507

static STDGPU_HOST_DEVICE void construct(Allocator &a, T *p, Args &&... args)

Constructs an object value at the given pointer.

Allocator allocator_type

Definition: memory.h:493

static pointer allocate_filled(ExecutionPolicy &&policy, Allocator &a, index_type n, const value_type &default_value)

Allocates and fills a memory block of the given size.

typename std::pointer_traits< pointer >::difference_type difference_type

Definition: memory.h:504

typename std::pointer_traits< pointer >::template rebind< void > void_pointer

Definition: memory.h:499

A general allocator traitor.

Definition: memory.h:492

A type to indicate an uninitialized unique object.

Definition: memory.h:228

void deallocate(T *p, index64_t n)

Deallocates the given memory block.

T value_type

Definition: memory.h:342

T * allocate(index64_t n)

Allocates a memory block of the given size.

An allocator for device memory.

Definition: memory.h:341

static constexpr dynamic_memory_type memory_type

Dynamic memory type of allocations.

Definition: memory.h:347

safe_device_allocator() noexcept=default

Default constructor.

T value_type

Definition: memory.h:416

An allocator for host memory.

Definition: memory.h:415

safe_host_allocator() noexcept=default

Default constructor.