Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CommunicationBufferImpl.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
23#include <stdexcept>
24#include <variant>
25
26namespace shamcomm {
27
40
41 inline CommunicationProtocol get_protocol(sham::Device &device) {
42 if (device.mpi_prop.is_mpi_direct_capable) {
43 return DirectGPU;
44 } else {
45 return CopyToHost;
46 }
47 }
48
49 namespace details {
50
51 template<CommunicationProtocol comm_mode>
53
54 template<>
56
57 std::shared_ptr<sham::DeviceScheduler> dev_sched;
58
60
61 public:
63 u64 bytelen, std::shared_ptr<sham::DeviceScheduler> dev_sched)
64 : dev_sched(dev_sched), usm_buf(bytelen, dev_sched) {}
65
68 std::shared_ptr<sham::DeviceScheduler> dev_sched)
69 : dev_sched(dev_sched),
70 usm_buf(std::forward<sham::DeviceBuffer<u8, sham::host>>(moved_obj)) {}
71
72 inline std::unique_ptr<CommunicationBuffer> duplicate_to_ptr() {
73 std::unique_ptr<CommunicationBuffer> ret
74 = std::make_unique<CommunicationBuffer>(usm_buf.copy(), dev_sched);
75 return ret;
76 }
77
78 inline u64 get_size() { return usm_buf.get_size(); }
79
80 inline sham::DeviceBuffer<u8> copy_back_usm() {
81 return usm_buf.copy_to<sham::device>();
82 }
83 static sham::DeviceBuffer<u8> convert_usm(CommunicationBuffer &&buf) {
84 return buf.usm_buf.copy_to<sham::device>();
85 }
86
87 u8 *get_ptr() {
88 sham::EventList depends_list;
89 u8 *ptr = usm_buf.get_write_access(depends_list);
90 depends_list.wait_and_throw();
91 usm_buf.complete_event_state(sycl::event{});
92
93 return ptr;
94 }
95 };
96
97 template<>
99
100 std::shared_ptr<sham::DeviceScheduler> dev_sched;
101
103
104 public:
105 inline CommunicationBuffer(
106 u64 bytelen, std::shared_ptr<sham::DeviceScheduler> dev_sched)
107 : dev_sched(dev_sched), usm_buf(bytelen, dev_sched) {}
108
109 inline CommunicationBuffer(
111 std::shared_ptr<sham::DeviceScheduler> dev_sched)
112 : dev_sched(dev_sched),
113 usm_buf(std::forward<sham::DeviceBuffer<u8, sham::device>>(moved_obj)) {}
114
115 inline std::unique_ptr<CommunicationBuffer> duplicate_to_ptr() {
116 std::unique_ptr<CommunicationBuffer> ret
117 = std::make_unique<CommunicationBuffer>(usm_buf.copy(), dev_sched);
118 return ret;
119 }
120
121 inline sham::DeviceBuffer<u8> copy_back_usm() {
122 return usm_buf.copy_to<sham::device>();
123 }
124
125 inline u64 get_size() { return usm_buf.get_size(); }
126
127 static sham::DeviceBuffer<u8> convert_usm(CommunicationBuffer &&buf) {
128 return std::move(buf.usm_buf);
129 }
130
131 u8 *get_ptr() {
132 sham::EventList depends_list;
133 u8 *ptr = usm_buf.get_write_access(depends_list);
134 depends_list.wait_and_throw();
135 usm_buf.complete_event_state(sycl::event{});
136
137 return ptr;
138 }
139 };
140 } // namespace details
141
142} // namespace shamcomm
This file contains the declaration of the USMPtrHolder class.
std::uint8_t u8
8 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
DeviceBuffer< T, new_target > copy_to() const
Copy the content of the buffer to a new buffer with a different USM target.
size_t get_size() const
Gets the number of elements in the buffer.
DeviceBuffer< T, target > copy() const
Copy the current buffer.
Represents a SYCL device.
Definition Device.hpp:147
Class to manage a list of SYCL events.
Definition EventList.hpp:31
void wait_and_throw()
Wait for all events in the list to be finished and throw an exception if one has occurred.
Definition EventList.hpp:72
This header file contains utility functions related to exception handling in the code.
Namespace for internal details of the logs module.
@ device
Device memory.
namespace for communication related stuff
@ CopyToHost
copy data to the host and then perform the call
@ DirectGPU
copy data straight from the GPU