Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CommunicationBuffer.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
32#include "shambackends/sycl.hpp"
34#include <utility>
35
36namespace shamcomm {
37
43
44 private:
45 using int_var_t = std::variant<
46 std::unique_ptr<details::CommunicationBuffer<CopyToHost>>,
47 std::unique_ptr<details::CommunicationBuffer<DirectGPU>>>;
48
49 int_var_t _int_type;
50
51 explicit CommunicationBuffer(int_var_t &&moved_int_var)
52 : _int_type(std::move(moved_int_var)) {}
53
55
56 public:
57 inline CommunicationBuffer(u64 bytelen, sham::DeviceScheduler_ptr dev_sched) {
58 sham::Device &dev = *dev_sched->ctx->device;
59 Protocol comm_mode = get_protocol(dev);
60 if (comm_mode == CopyToHost) {
61 _int_type = std::make_unique<details::CommunicationBuffer<CopyToHost>>(
62 bytelen, dev_sched);
63 } else if (comm_mode == DirectGPU) {
64 _int_type
65 = std::make_unique<details::CommunicationBuffer<DirectGPU>>(bytelen, dev_sched);
66 } else {
68 }
69 }
70
72 sham::DeviceBuffer<u8> &&bytebuf, sham::DeviceScheduler_ptr dev_sched) {
73 sham::Device &dev = *dev_sched->ctx->device;
74 Protocol comm_mode = get_protocol(dev);
75 if (comm_mode == CopyToHost) {
76 _int_type = std::make_unique<details::CommunicationBuffer<CopyToHost>>(
77 bytebuf.copy_to<sham::host>(), dev_sched);
78 } else if (comm_mode == DirectGPU) {
79 _int_type = std::make_unique<details::CommunicationBuffer<DirectGPU>>(
80 std::forward<sham::DeviceBuffer<u8>>(bytebuf), dev_sched);
81 } else {
83 }
84 }
85
87 const sham::DeviceBuffer<u8> &bytebuf, sham::DeviceScheduler_ptr dev_sched) {
88 sham::Device &dev = *dev_sched->ctx->device;
89 Protocol comm_mode = get_protocol(dev);
90 if (comm_mode == CopyToHost) {
91 _int_type = std::make_unique<details::CommunicationBuffer<CopyToHost>>(
92 bytebuf.copy_to<sham::host>(), dev_sched);
93 } else if (comm_mode == DirectGPU) {
94 _int_type = std::make_unique<details::CommunicationBuffer<DirectGPU>>(
95 bytebuf.copy(), dev_sched);
96 } else {
98 }
99 }
100
104 inline u64 get_size() {
105 return std::visit(
106 [=](auto &&arg) {
107 return arg->get_size();
108 },
109 _int_type);
110 }
111
112 inline u8 *get_ptr() {
113 return std::visit(
114 [=](auto &&arg) {
115 return arg->get_ptr();
116 },
117 _int_type);
118 }
119
126
127 int_var_t tmp = std::visit(
128 [=](auto &&arg) {
129 return int_var_t(arg->duplicate_to_ptr());
130 },
131 _int_type);
132
133 return CommunicationBuffer(std::move(tmp));
134 }
135
141 inline std::unique_ptr<CommunicationBuffer> duplicate_to_ptr() {
142 return std::make_unique<CommunicationBuffer>(duplicate());
143 }
144
152 return std::visit(
153 [=](auto &&arg) {
154 using _t = typename std::remove_reference<decltype(*arg)>::type;
155 return _t::convert_usm(std::forward<_t>(*arg));
156 },
157 buf._int_type);
158 }
159 };
160
161 void validate_comm(std::shared_ptr<sham::DeviceScheduler> &sched);
162
163} // namespace shamcomm
std::uint8_t u8
8 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
DeviceBuffer< T, new_target > copy_to() const
Copy the content of the buffer to a new buffer with a different USM target.
DeviceBuffer< T, target > copy() const
Copy the current buffer.
Represents a SYCL device.
Definition Device.hpp:147
Shamrock communication buffers.
u64 get_size()
Gets the size of the buffer (here in bytes)
std::unique_ptr< CommunicationBuffer > duplicate_to_ptr()
duplicate the comm buffer and return a unique ptr to the copy
static sham::DeviceBuffer< u8 > convert_usm(CommunicationBuffer &&buf)
destroy the buffer and recover the held object
CommunicationBuffer duplicate()
duplicate the comm buffer and return a unique ptr to the copy
This header file contains utility functions related to exception handling in the code.
@ host
Host memory.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for communication related stuff
@ CopyToHost
copy data to the host and then perform the call
@ DirectGPU
copy data straight from the GPU