Repository navigation
Expand file tree
/
Copy pathVariable.cpp
More file actions
187 lines (161 loc) · 8.08 KB
/
Copy pathVariable.cpp
File metadata and controls
187 lines (161 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2022-2026. The SWAT Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "dtlmod/Variable.hpp"
#include "dtlmod/CompressionReductionMethod.hpp"
#include "dtlmod/DTLException.hpp"
#include "dtlmod/Stream.hpp"
#include <limits>
#include <numeric>
#include <stdexcept>
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dtlmod_variable, dtlmod, "DTL logging about Variables");
namespace dtlmod {
struct checked_multiply {
size_t operator()(size_t a, size_t b) const
{
if (b != 0 && a > std::numeric_limits<size_t>::max() / b) {
throw std::overflow_error("size_t overflow in multiplication");
}
return a * b;
}
};
////////////////////////////////////////////
///////////// PUBLIC INTERFACE /////////////
////////////////////////////////////////////
/// The global size of a Variable corresponds to the product of the number of elements in each dimension of the shape
/// vector by the element size.
size_t Variable::get_global_size() const
{
return std::accumulate(shape_.begin(), shape_.end(), element_size_, dtlmod::checked_multiply{});
}
/// The local size of a Variable corresponds to the product of the number of elements in each dimension of the count
/// vector by the element size. If variable was published to the DTL over multiple transactions, multiply the size by
/// the number of transactions.
size_t Variable::get_local_size() const
{
const auto& start_and_count = local_start_and_count_.at(sg4::Actor::self()).second;
auto total_size =
std::accumulate(start_and_count.begin(), start_and_count.end(), element_size_, dtlmod::checked_multiply{});
if (transaction_count_ > 0)
total_size *= transaction_count_;
return total_size;
}
void Variable::set_selection(const std::vector<size_t>& start, const std::vector<size_t>& count)
{
subscriber_selections_[sg4::Actor::self()] = std::make_pair(start, count);
}
void Variable::set_transaction_selection(unsigned int begin, unsigned int count)
{
subscriber_transaction_selections_[sg4::Actor::self()] = std::make_pair(begin, count);
}
void Variable::set_reduction_operation(std::shared_ptr<ReductionMethod> method,
const std::map<std::string, std::string, std::less<>>& parameters)
{
auto stream = defined_in_stream_.lock();
xbt_assert(stream, "Variable::set_reduction_operation called after its Stream has been destroyed");
// Compression is publisher-side only (check before double-reduction to give a more specific error)
if (dynamic_cast<CompressionReductionMethod*>(method.get()) && stream->get_access_mode() == Stream::Mode::Subscribe) {
throw SubscriberSideCompressionException(XBT_THROW_POINT);
}
if (is_reduced_with_ && reduction_origin_ == ReductionOrigin::Publisher &&
stream->get_access_mode() == Stream::Mode::Subscribe) {
XBT_ERROR("Subscriber %s attempted to re-reduce Variable %s, but it was already reduced on publisher side.",
sg4::Actor::self()->get_cname(), this->get_cname());
throw DoubleReductionException(
XBT_THROW_POINT,
"Variable has already been reduced by its producer; subscriber-side reduction is not allowed.");
}
method->parameterize_for_variable(*this, parameters);
method->reduce_variable(*this);
is_reduced_with_ = method;
if (reduction_origin_ == ReductionOrigin::None) {
if (stream->get_access_mode() == Stream::Mode::Publish)
reduction_origin_ = ReductionOrigin::Publisher;
else
reduction_origin_ = ReductionOrigin::Subscriber;
}
}
////////////////////////////////////////////
///////////////// INTERNALS ////////////////
////////////////////////////////////////////
/// \cond EXCLUDE_FROM_DOCUMENTATION
bool Variable::subscriber_has_a_selection(sg4::ActorPtr actor) const
{
return subscriber_selections_.find(actor) != subscriber_selections_.end();
}
bool Variable::subscriber_has_a_transaction_selection(sg4::ActorPtr actor) const
{
return subscriber_transaction_selections_.find(actor) != subscriber_transaction_selections_.end();
}
const std::pair<std::vector<size_t>, std::vector<size_t>>& Variable::get_subscriber_selection(sg4::ActorPtr actor) const
{
return subscriber_selections_.at(actor);
}
const std::pair<unsigned int, unsigned int>& Variable::get_subscriber_transaction_selection(sg4::ActorPtr actor) const
{
return subscriber_transaction_selections_.at(actor);
}
void Variable::add_transaction_metadata(unsigned int transaction_id, sg4::ActorPtr publisher,
const std::string& location)
{
if (is_reduced_with_) {
auto start_and_count = is_reduced_with_->get_reduced_start_and_count_for(*this, publisher);
metadata_->add_transaction(transaction_id, start_and_count, location, publisher);
} else
metadata_->add_transaction(transaction_id, local_start_and_count_[publisher], location, publisher);
}
std::vector<std::pair<std::string, sg_size_t>>
Variable::get_sizes_to_get_per_block(unsigned int transaction_id, const std::vector<size_t>& start,
const std::vector<size_t>& count) const
{
// Defensive check (should never trigger due to earlier validation)
xbt_assert(start.size() == count.size() && start.size() == shape_.size(),
"Internal error: dimension mismatch in get_sizes_to_get_per_block");
std::vector<std::pair<std::string, sg_size_t>> get_sizes_per_block;
// Validate transaction_id is within valid range (defense-in-depth: Transport also checks this)
if (transaction_id > metadata_->get_current_transaction()) // LCOV_EXCL_LINE
throw InvalidTransactionIdException(XBT_THROW_POINT, std::to_string(transaction_id)); // LCOV_EXCL_LINE
auto blocks = metadata_->get_blocks_for_transaction(transaction_id);
XBT_DEBUG("%zu block(s) to check for transaction %u", blocks.size(), transaction_id);
// For each block, compute the intersection between the requested region [start, start+count)
// and the available block region [block_start, block_start+block_count) in each dimension.
// Two cases of overlap are checked:
// 1. Block starts within or after the requested region: start <= block_start < start+count
// 2. Request starts within the block: block_start < start < block_start+block_count
// The size to retrieve is the product of intersection sizes across all dimensions.
// If any dimension has no overlap, nothing is retrieved from this block.
for (const auto& [block_info, location] : blocks) {
size_t size_to_get = element_size_;
auto [block_start, block_count] = block_info;
auto [where, actor] = location;
auto something_to_get = std::vector<bool>(start.size(), false);
// Determine whether some elements have to be retrieved from this particular block
for (unsigned i = 0; i < start.size(); i++) {
XBT_DEBUG("Subscriber %s checks Publisher %s", sg4::Actor::self()->get_cname(), actor->get_cname());
XBT_DEBUG("Dimension %u: wanted [%zu, %zu] vs. in block [%zu, %zu]", i, start[i], count[i], block_start[i],
block_count[i]);
size_t size_in_dim = 0;
bool element_found = false;
if (start[i] <= block_start[i] && (block_start[i] - start[i]) <= count[i]) {
size_in_dim = std::min(block_count[i], count[i] - (block_start[i] - start[i]));
element_found = true;
}
if (start[i] > block_start[i] && (start[i] - block_start[i]) <= block_count[i]) {
size_in_dim = std::min(count[i], block_count[i] - (start[i] - block_start[i]));
element_found = true;
}
if (element_found && size_in_dim > 0) {
something_to_get[i] = true;
XBT_DEBUG("Multiply size to read by %zu elements", size_in_dim);
size_to_get *= size_in_dim;
}
}
if (std::all_of(something_to_get.begin(), something_to_get.end(), [](bool v) { return v; })) {
XBT_DEBUG("Total size to read from %s: %zu)", where.c_str(), size_to_get);
get_sizes_per_block.emplace_back(where, size_to_get);
}
}
return get_sizes_per_block;
}
/// \endcond
} // namespace dtlmod