Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
USMPtrHolder.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
22#include "shambase/ptr.hpp"
24#include <memory>
25#include <utility>
26
27namespace sham {
28
45
58 template<USMKindTarget target>
60
61 void *usm_ptr = nullptr;
62 size_t size = 0;
63 std::shared_ptr<DeviceScheduler>
64 dev_sched;
65
66 USMPtrHolder(void *usm_ptr, size_t size, std::shared_ptr<DeviceScheduler> dev_sched)
67 : usm_ptr(usm_ptr), size(size), dev_sched(std::move(dev_sched)) {}
68
69 public:
70 void free_ptr();
71
85 static USMPtrHolder create(
86 size_t sz,
87 std::shared_ptr<DeviceScheduler> dev_sched,
88 std::optional<size_t> alignment = std::nullopt);
89
90 static USMPtrHolder create_nullptr(std::shared_ptr<DeviceScheduler> dev_sched);
91
98
102 USMPtrHolder(const USMPtrHolder &other) = delete;
103
112 USMPtrHolder(USMPtrHolder &&other) noexcept
113 : usm_ptr(std::exchange(other.usm_ptr, nullptr)), size(other.size),
114 dev_sched(other.dev_sched) {}
115
119 USMPtrHolder &operator=(const USMPtrHolder &other) = delete;
120
131 dev_sched = other.dev_sched;
132 size = other.size;
133 std::swap(usm_ptr, other.usm_ptr);
134 return *this;
135 }
136
143 template<class T>
144 inline T *ptr_cast() const {
145 if (!shambase::is_aligned<T>(usm_ptr)) {
147 "The USM pointer is not aligned with the given type");
148 }
149 return reinterpret_cast<T *>(usm_ptr);
150 }
151
160 [[nodiscard]] inline void *get_raw_ptr() const { return usm_ptr; }
161
167 [[nodiscard]] inline size_t get_bytesize() const { return size; }
168
174 [[nodiscard]] inline DeviceScheduler &get_dev_scheduler() const { return *dev_sched; }
175
181 [[nodiscard]] inline std::shared_ptr<DeviceScheduler> &get_dev_scheduler_ptr() {
182 return dev_sched;
183 }
184
190 [[nodiscard]] inline const std::shared_ptr<DeviceScheduler> &get_dev_scheduler_ptr() const {
191 return dev_sched;
192 }
193 };
194
195} // namespace sham
Class to manage the scheduling of kernels on a device.
Class for holding a USM pointer.
size_t get_bytesize() const
Get the size of the USM allocation (in byte)
DeviceScheduler & get_dev_scheduler() const
Get the SYCL context used for allocation/freeing the USM buffer.
USMPtrHolder(USMPtrHolder &&other) noexcept
Move constructor.
T * ptr_cast() const
Cast the USM pointer to the given type.
void free_ptr()
Free the held pointer.
~USMPtrHolder()
USM pointer holder destructor.
void * get_raw_ptr() const
Get the raw pointer of the USM allocation.
std::shared_ptr< DeviceScheduler > & get_dev_scheduler_ptr()
Get the SYCL context used for allocation/freeing the USM buffer.
USMPtrHolder & operator=(USMPtrHolder &&other) noexcept
Move assignment operator.
USMPtrHolder & operator=(const USMPtrHolder &other)=delete
Deleted copy assignment operator.
USMPtrHolder(const USMPtrHolder &other)=delete
Deleted copy constructor.
const std::shared_ptr< DeviceScheduler > & get_dev_scheduler_ptr() const
Get the SYCL context used for allocation/freeing the USM buffer.
static USMPtrHolder create(size_t sz, std::shared_ptr< DeviceScheduler > dev_sched, std::optional< size_t > alignment=std::nullopt)
Create a USM pointer holder.
namespace for backends this one is named only sham since shambackends is too long to write
USMKindTarget
Enum listing the different types of USM pointers allocations.
@ host
Host memory.
@ device
Device memory.
@ shared
Shared memory.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.