EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_MemoryResource.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
30#ifndef EBGEOMETRY_MEMORYRESOURCE_HPP
31#define EBGEOMETRY_MEMORYRESOURCE_HPP
32
33// Std includes
34#include <cstddef>
35
36// Our includes
37#include "EBGeometry_GPU.hpp"
39#include "EBGeometry_Macros.hpp"
40
41namespace EBGeometry {
42
52inline constexpr size_t PoolBaseAlign = 256;
53
69{
70public:
74 MemoryResource() = default;
75
79 virtual ~MemoryResource() = default;
80
85
90 operator=(const MemoryResource&) = delete;
91
101 [[nodiscard]] EBGEOMETRY_HOST
102 virtual void*
103 allocate(size_t a_bytes, size_t a_alignment) = 0;
104
112 virtual void
113 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept = 0;
114
119 [[nodiscard]] EBGEOMETRY_HOST
120 virtual bool
121 isHostAccessible() const noexcept = 0;
122
127 [[nodiscard]] EBGEOMETRY_HOST
128 virtual bool
129 isDeviceAccessible() const noexcept = 0;
130
145 virtual void
146 copy(void* a_dst,
147 const MemoryResource& a_dstResource,
148 const void* a_src,
149 const MemoryResource& a_srcResource,
150 size_t a_bytes) const noexcept;
151};
152
159{
160public:
165
169 ~HostMemoryResource() override = default;
170
177 [[nodiscard]] EBGEOMETRY_HOST
178 void*
179 allocate(size_t a_bytes, size_t a_alignment) override;
180
188 void
189 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
190
195 [[nodiscard]] EBGEOMETRY_HOST
196 bool
197 isHostAccessible() const noexcept override
198 {
199 return true;
200 }
201
206 [[nodiscard]] EBGEOMETRY_HOST
207 bool
208 isDeviceAccessible() const noexcept override
209 {
210 return false;
211 }
212};
213
220[[nodiscard]] EBGEOMETRY_HOST
221HostMemoryResource&
223
234{
235public:
240
244 ~EmulatedDeviceMemoryResource() override = default;
245
252 [[nodiscard]] EBGEOMETRY_HOST
253 void*
254 allocate(size_t a_bytes, size_t a_alignment) override;
255
263 void
264 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
265
270 [[nodiscard]] EBGEOMETRY_HOST
271 bool
272 isHostAccessible() const noexcept override
273 {
274 return true;
275 }
276
282 [[nodiscard]] EBGEOMETRY_HOST
283 bool
284 isDeviceAccessible() const noexcept override
285 {
286 return true;
287 }
288};
289
295[[nodiscard]] EBGEOMETRY_HOST
296EmulatedDeviceMemoryResource&
298
299#if defined(EBGEOMETRY_CUDA) || defined(EBGEOMETRY_HIP) || defined(EBGEOMETRY_DOXYGEN)
300
309{
310public:
315
319 ~DeviceMemoryResource() override = default;
320
328 [[nodiscard]] EBGEOMETRY_HOST
329 void*
330 allocate(size_t a_bytes, size_t a_alignment) override;
331
339 void
340 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
341
346 [[nodiscard]] EBGEOMETRY_HOST
347 bool
348 isHostAccessible() const noexcept override
349 {
350 return false;
351 }
352
357 [[nodiscard]] EBGEOMETRY_HOST
358 bool
359 isDeviceAccessible() const noexcept override
360 {
361 return true;
362 }
363
374 void
375 copy(void* a_dst,
376 const MemoryResource& a_dstResource,
377 const void* a_src,
378 const MemoryResource& a_srcResource,
379 size_t a_bytes) const noexcept override;
380};
381
390{
391public:
396
400 ~ManagedMemoryResource() override = default;
401
408 [[nodiscard]] EBGEOMETRY_HOST
409 void*
410 allocate(size_t a_bytes, size_t a_alignment) override;
411
419 void
420 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
421
426 [[nodiscard]] EBGEOMETRY_HOST
427 bool
428 isHostAccessible() const noexcept override
429 {
430 return true;
431 }
432
437 [[nodiscard]] EBGEOMETRY_HOST
438 bool
439 isDeviceAccessible() const noexcept override
440 {
441 return true;
442 }
443
454 void
455 copy(void* a_dst,
456 const MemoryResource& a_dstResource,
457 const void* a_src,
458 const MemoryResource& a_srcResource,
459 size_t a_bytes) const noexcept override;
460};
461
470{
471public:
476
480 ~PinnedMemoryResource() override = default;
481
488 [[nodiscard]] EBGEOMETRY_HOST
489 void*
490 allocate(size_t a_bytes, size_t a_alignment) override;
491
499 void
500 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
501
506 [[nodiscard]] EBGEOMETRY_HOST
507 bool
508 isHostAccessible() const noexcept override
509 {
510 return true;
511 }
512
517 [[nodiscard]] EBGEOMETRY_HOST
518 bool
519 isDeviceAccessible() const noexcept override
520 {
521 return false;
522 }
523
534 void
535 copy(void* a_dst,
536 const MemoryResource& a_dstResource,
537 const void* a_src,
538 const MemoryResource& a_srcResource,
539 size_t a_bytes) const noexcept override;
540};
541
554{
555public:
560
564 ~MappedMemoryResource() override = default;
565
572 [[nodiscard]] EBGEOMETRY_HOST
573 void*
574 allocate(size_t a_bytes, size_t a_alignment) override;
575
583 void
584 deallocate(void* a_ptr, size_t a_bytes, size_t a_alignment) noexcept override;
585
590 [[nodiscard]] EBGEOMETRY_HOST
591 bool
592 isHostAccessible() const noexcept override
593 {
594 return true;
595 }
596
601 [[nodiscard]] EBGEOMETRY_HOST
602 bool
603 isDeviceAccessible() const noexcept override
604 {
605 return true;
606 }
607
618 void
619 copy(void* a_dst,
620 const MemoryResource& a_dstResource,
621 const void* a_src,
622 const MemoryResource& a_srcResource,
623 size_t a_bytes) const noexcept override;
624};
625
633[[nodiscard]] EBGEOMETRY_HOST
636
644[[nodiscard]] EBGEOMETRY_HOST
647
655[[nodiscard]] EBGEOMETRY_HOST
658
666[[nodiscard]] EBGEOMETRY_HOST
669
670#endif // EBGEOMETRY_CUDA || EBGEOMETRY_HIP || EBGEOMETRY_DOXYGEN
671
672} // namespace EBGeometry
673
674#include "EBGeometry_MemoryResourceImplem.hpp"
675
676#endif // EBGEOMETRY_MEMORYRESOURCE_HPP
Backend-neutral GPU runtime API layer (CUDA or HIP) plus the EBGEOMETRY_GPU_CHECK macro.
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
Device memory resource: raw device allocation (GPU::memAlloc / GPU::memFree).
Definition EBGeometry_MemoryResource.hpp:309
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
bool isDeviceAccessible() const noexcept override
Device blocks are device-accessible.
Definition EBGeometry_MemoryResource.hpp:359
~DeviceMemoryResource() override=default
Destructor.
void copy(void *a_dst, const MemoryResource &a_dstResource, const void *a_src, const MemoryResource &a_srcResource, size_t a_bytes) const noexcept override
Copy between host and device memory with the GPU runtime; see MemoryResource::copy.
bool isHostAccessible() const noexcept override
Device blocks are not host-accessible.
Definition EBGeometry_MemoryResource.hpp:348
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate a block of device memory.
DeviceMemoryResource()=default
Default constructor.
Host memory that stands in for device memory: host RAM that reports itself device-accessible.
Definition EBGeometry_MemoryResource.hpp:234
~EmulatedDeviceMemoryResource() override=default
Destructor.
bool isDeviceAccessible() const noexcept override
The blocks report themselves device-accessible, which is what makes them stand in for device memory.
Definition EBGeometry_MemoryResource.hpp:284
EmulatedDeviceMemoryResource()=default
Default constructor.
bool isHostAccessible() const noexcept override
The blocks are host memory.
Definition EBGeometry_MemoryResource.hpp:272
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate an aligned block of host memory.
Host memory resource: plain aligned allocation in host RAM (std::aligned_alloc / free family).
Definition EBGeometry_MemoryResource.hpp:159
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate an aligned block of host memory.
HostMemoryResource()=default
Default constructor.
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
~HostMemoryResource() override=default
Destructor.
bool isDeviceAccessible() const noexcept override
Host blocks are not device-accessible.
Definition EBGeometry_MemoryResource.hpp:208
bool isHostAccessible() const noexcept override
Host blocks are host-accessible.
Definition EBGeometry_MemoryResource.hpp:197
Managed (unified) memory resource: one allocation reachable from host and device (GPU::memAllocManage...
Definition EBGeometry_MemoryResource.hpp:390
~ManagedMemoryResource() override=default
Destructor.
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate a block of managed (unified) memory.
void copy(void *a_dst, const MemoryResource &a_dstResource, const void *a_src, const MemoryResource &a_srcResource, size_t a_bytes) const noexcept override
Copy between host and device memory with the GPU runtime; see MemoryResource::copy.
bool isHostAccessible() const noexcept override
Managed blocks are host-accessible.
Definition EBGeometry_MemoryResource.hpp:428
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
ManagedMemoryResource()=default
Default constructor.
bool isDeviceAccessible() const noexcept override
Managed blocks are device-accessible.
Definition EBGeometry_MemoryResource.hpp:439
Mapped (zero-copy) pinned host memory resource: page-locked host memory also mapped into the device a...
Definition EBGeometry_MemoryResource.hpp:554
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
~MappedMemoryResource() override=default
Destructor.
bool isDeviceAccessible() const noexcept override
Mapped blocks are device-accessible.
Definition EBGeometry_MemoryResource.hpp:603
MappedMemoryResource()=default
Default constructor.
bool isHostAccessible() const noexcept override
Mapped blocks are host-accessible.
Definition EBGeometry_MemoryResource.hpp:592
void copy(void *a_dst, const MemoryResource &a_dstResource, const void *a_src, const MemoryResource &a_srcResource, size_t a_bytes) const noexcept override
Copy between host and device memory with the GPU runtime; see MemoryResource::copy.
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate a block of mapped (zero-copy) pinned host memory.
Abstract placement policy: allocates and frees raw, correctly-aligned byte blocks.
Definition EBGeometry_MemoryResource.hpp:69
MemoryResource(const MemoryResource &)=delete
Copy constructor. Deleted: a resource's identity is meaningful and non-copyable.
virtual void copy(void *a_dst, const MemoryResource &a_dstResource, const void *a_src, const MemoryResource &a_srcResource, size_t a_bytes) const noexcept
Copy a block between memory from two resources, one of which is this one.
MemoryResource()=default
Default constructor.
virtual ~MemoryResource()=default
Destructor.
virtual void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept=0
Release a block previously returned by allocate.
virtual bool isHostAccessible() const noexcept=0
Whether a plain host pointer dereference / std::memcpy is valid on returned blocks.
MemoryResource & operator=(const MemoryResource &)=delete
Copy assignment. Deleted: a resource's identity is meaningful and non-copyable.
virtual void * allocate(size_t a_bytes, size_t a_alignment)=0
Obtain a block of at least a_bytes bytes, aligned to a_alignment.
virtual bool isDeviceAccessible() const noexcept=0
Whether a device kernel may dereference returned blocks.
Pinned (page-locked) host memory resource: fast host-to-device staging (GPU::memAllocHost / GPU::memF...
Definition EBGeometry_MemoryResource.hpp:470
bool isDeviceAccessible() const noexcept override
Pinned blocks are not device-accessible.
Definition EBGeometry_MemoryResource.hpp:519
void * allocate(size_t a_bytes, size_t a_alignment) override
Allocate a block of page-locked host memory.
void deallocate(void *a_ptr, size_t a_bytes, size_t a_alignment) noexcept override
Free a block previously returned by allocate.
~PinnedMemoryResource() override=default
Destructor.
PinnedMemoryResource()=default
Default constructor.
bool isHostAccessible() const noexcept override
Pinned blocks are host-accessible.
Definition EBGeometry_MemoryResource.hpp:508
void copy(void *a_dst, const MemoryResource &a_dstResource, const void *a_src, const MemoryResource &a_srcResource, size_t a_bytes) const noexcept override
Copy between host and device memory with the GPU runtime; see MemoryResource::copy.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
constexpr size_t PoolBaseAlign
Block-base alignment for every Pool, in bytes.
Definition EBGeometry_MemoryResource.hpp:52
ManagedMemoryResource & managedMemoryResource() noexcept
Process-wide default managed (unified) memory resource.
DeviceMemoryResource & deviceMemoryResource() noexcept
Process-wide default device memory resource.
MappedMemoryResource & mappedMemoryResource() noexcept
Process-wide default mapped (zero-copy) pinned host memory resource.
PinnedMemoryResource & pinnedMemoryResource() noexcept
Process-wide default pinned (page-locked) host memory resource.
HostMemoryResource & hostMemoryResource() noexcept
Process-wide default host memory resource.
EmulatedDeviceMemoryResource & emulatedDeviceMemoryResource() noexcept
Process-wide emulated device memory resource.