Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
DistributedDataShared.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
20#include <functional>
21#include <map>
22#include <utility>
23#include <vector>
24
25namespace shambase {
26
51 template<class T>
53
54 std::multimap<std::pair<u64, u64>, T> data;
55
56 using iterator = typename std::multimap<std::pair<u64, u64>, T>::iterator;
57
58 public:
64 inline std::multimap<std::pair<u64, u64>, T> &get_native() { return data; }
65
67 inline const std::multimap<std::pair<u64, u64>, T> &get_native() const { return data; }
68
70 inline auto begin() { return data.begin(); }
72 inline auto end() { return data.end(); }
74 inline auto begin() const { return data.begin(); }
76 inline auto end() const { return data.end(); }
78 inline auto cbegin() const { return data.cbegin(); }
80 inline auto cend() const { return data.cend(); }
81
95 inline iterator add_obj(u64 left_id, u64 right_id, T &&obj) {
96 return data.emplace(std::pair<u64, u64>{left_id, right_id}, std::forward<T>(obj));
97 }
98
110 inline void for_each(std::function<void(u64, u64, T &)> &&f) {
111 for (auto &[id, obj] : data) {
112 f(id.first, id.second, obj);
113 }
114 }
115
117 inline void for_each(std::function<void(u64, u64, const T &)> &&f) const {
118 for (auto &[id, obj] : data) {
119 f(id.first, id.second, obj);
120 }
121 }
122
139 inline void tranfer_all(std::function<bool(u64, u64)> cd, DistributedDataShared &other) {
140
141 std::vector<std::pair<u64, u64>> occurences;
142
143 // whoa i forgot the & here and triggered the copy constructor of every patch
144 // like do not forget it or it will be a disaster waiting to come
145 // i did throw up a 64 GPUs run because of that
146 for (auto &[k, v] : data) {
147 if (cd(k.first, k.second)) {
148 occurences.push_back(k);
149 }
150 }
151
152 for (auto p : occurences) {
153 auto ext = data.extract(p);
154 other.data.insert(std::move(ext));
155 }
156 }
157
171 inline bool has_key(u64 left_id, u64 right_id) const {
172 return (data.find({left_id, right_id}) != data.end());
173 }
174
180 inline u64 get_element_count() const { return data.size(); }
181
200 template<class Tmap>
201 inline DistributedDataShared<Tmap> map(std::function<Tmap(u64, u64, T &)> map_func) {
203 for_each([&](u64 left, u64 right, T &ref) {
204 ret.add_obj(left, right, map_func(left, right, ref));
205 });
206 return ret;
207 }
208
210 template<class Tmap>
212 std::function<Tmap(u64, u64, const T &)> map_func) const {
214 for_each([&](u64 left, u64 right, const T &ref) {
215 ret.add_obj(left, right, map_func(left, right, ref));
216 });
217 return ret;
218 }
219
223 inline void reset() { data.clear(); }
224
230 inline bool is_empty() const { return data.empty(); }
231 };
232
233} // namespace shambase
std::uint64_t u64
64 bit unsigned integer
Container for objects shared between two distributed data elements.
DistributedDataShared< Tmap > map(std::function< Tmap(u64, u64, T &)> map_func)
Transform all objects to a new type using a mapping function.
DistributedDataShared< Tmap > map(std::function< Tmap(u64, u64, const T &)> map_func) const
const version
u64 get_element_count() const
Get the total number of objects in the container.
bool has_key(u64 left_id, u64 right_id) const
Check if a patch pair exists in the container.
void for_each(std::function< void(u64, u64, T &)> &&f)
Apply a function to all stored objects.
auto cbegin() const
iterator forwarding
bool is_empty() const
Check if the container is empty.
auto cend() const
iterator forwarding
void for_each(std::function< void(u64, u64, const T &)> &&f) const
const version
iterator add_obj(u64 left_id, u64 right_id, T &&obj)
Add an object associated with a patch pair.
std::multimap< std::pair< u64, u64 >, T > & get_native()
Get direct access to the underlying multimap container.
auto end() const
iterator forwarding
void reset()
Clear all objects from the container.
const std::multimap< std::pair< u64, u64 >, T > & get_native() const
const version
auto begin() const
iterator forwarding
void tranfer_all(std::function< bool(u64, u64)> cd, DistributedDataShared &other)
Transfer objects to another container based on a condition.
namespace for basic c++ utilities
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.