Repository navigation
Expand file tree
/
Copy pathDecimationReductionMethod.cpp
More file actions
181 lines (150 loc) · 7.67 KB
/
Copy pathDecimationReductionMethod.cpp
File metadata and controls
181 lines (150 loc) · 7.67 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
/* Copyright (c) 2025-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 <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <cmath>
#include <numeric> // for std::accumulate
#include "dtlmod/DecimationReductionMethod.hpp"
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dtlmod_decimation_reduction, dtlmod, "DTL logging about decimation method");
namespace dtlmod {
size_t DecimationReductionMethod::ParameterizedDecimation::get_global_reduced_size() const
{
return std::accumulate(reduced_shape_.begin(), reduced_shape_.end(), var_->get_element_size(), std::multiplies<>{});
}
size_t DecimationReductionMethod::ParameterizedDecimation::get_local_reduced_size() const
{
auto start_and_count = reduced_local_start_and_count_.at(sg4::Actor::self()).second;
return std::accumulate(start_and_count.begin(), start_and_count.end(), var_->get_element_size(), std::multiplies<>{});
}
const std::pair<std::vector<size_t>, std::vector<size_t>>&
DecimationReductionMethod::ParameterizedDecimation::get_reduced_start_and_count_for(sg4::ActorPtr publisher) const
{
return reduced_local_start_and_count_.at(publisher);
}
double DecimationReductionMethod::ParameterizedDecimation::get_flop_amount_to_decimate() const
{
XBT_DEBUG("Compute decimation cost with: cost_per_element = %.2f and interpolation_method = %s", cost_per_element_,
interpolation_method_.c_str());
double amount = cost_per_element_;
auto local_size = static_cast<double>(var_->get_local_size());
int multiplier = 1;
if (interpolation_method_ == "linear")
multiplier = 2;
else if (interpolation_method_ == "quadratic")
multiplier = 4;
else if (interpolation_method_ == "cubic")
multiplier = 8;
return multiplier * amount * local_size;
}
std::vector<size_t> DecimationReductionMethod::parse_stride(std::string_view value, const Variable& var)
{
std::vector<std::string> tokens;
std::string value_str(value);
boost::split(tokens, value_str, boost::is_any_of(","), boost::token_compress_on);
if (var.get_shape().size() != tokens.size())
throw InconsistentDecimationStrideException(
XBT_THROW_POINT, "Decimation Stride and Variable Shape vectors must have the same size. Stride: " +
std::to_string(tokens.size()) + ", Shape: " + std::to_string(var.get_shape().size()));
std::vector<size_t> stride;
stride.reserve(tokens.size());
for (const auto& t : tokens) {
auto dim_stride = std::stoul(t);
if (t[0] == '-' || dim_stride == 0)
throw InconsistentDecimationStrideException(XBT_THROW_POINT, "Stride values must be strictly positive");
stride.push_back(dim_stride);
}
return stride;
}
void DecimationReductionMethod::validate_interpolation(std::string_view method, const Variable& var)
{
if (method != "linear" && method != "quadratic" && method != "cubic")
throw UnknownDecimationInterpolationException(XBT_THROW_POINT, std::string("Unknown interpolation method: ") +
std::string(method) +
" (options are: linear, cubic, or quadratic).");
if ((method == "quadratic" && var.get_shape().size() < 2) || (method == "cubic" && var.get_shape().size() < 3))
throw InconsistentDecimationInterpolationException(
XBT_THROW_POINT, "Variable has not enough dimensions to apply this interpolation method");
}
void DecimationReductionMethod::parameterize_for_variable(
const Variable& var, const std::map<std::string, std::string, std::less<>>& parameters)
{
// Start from existing values (if any) to support partial updates.
auto it = per_variable_parameterizations_.find(&var);
std::vector<size_t> stride;
std::string interpolation_method;
double cost_per_element = 1.0;
if (it != per_variable_parameterizations_.end()) {
stride = it->second->get_stride();
interpolation_method = it->second->get_interpolation_method();
cost_per_element = it->second->get_cost_per_element();
}
for (const auto& [key, value] : parameters) {
if (key == "stride")
stride = parse_stride(value, var);
else if (key == "interpolation") {
validate_interpolation(value, var);
interpolation_method = value;
} else if (key == "cost_per_element")
cost_per_element = std::stod(value);
else
throw UnknownDecimationOptionException(XBT_THROW_POINT, key);
}
if (it == per_variable_parameterizations_.end()) {
per_variable_parameterizations_.try_emplace(
&var, std::make_shared<ParameterizedDecimation>(var, stride, interpolation_method, cost_per_element));
} else {
// Update in-place to preserve per-actor state accumulated by reduce_variable().
it->second->set_stride(stride);
it->second->set_interpolation_method(interpolation_method);
it->second->set_cost_per_element(cost_per_element);
}
}
void DecimationReductionMethod::propagate_for_subscriber(const Variable& publisher_var, const Variable& subscriber_var)
{
auto it = per_variable_parameterizations_.find(&publisher_var);
// Only called for a Variable this method has been applied to, hence parameterized for. Defensive check.
if (it == per_variable_parameterizations_.end())
return; // LCOV_EXCL_LINE
auto pub_param = it->second;
auto sub_param = std::make_shared<ParameterizedDecimation>(subscriber_var, pub_param->get_stride(),
pub_param->get_interpolation_method(),
pub_param->get_cost_per_element());
sub_param->set_reduced_shape(pub_param->get_reduced_shape());
// The subscriber receives the full reduced variable, so its local region is the entire reduced shape.
const auto& reduced_shape = pub_param->get_reduced_shape();
std::vector<size_t> reduced_start(reduced_shape.size(), 0);
sub_param->set_reduced_local_start_and_count(sg4::Actor::self(), reduced_start, reduced_shape);
per_variable_parameterizations_[&subscriber_var] = std::move(sub_param);
}
void DecimationReductionMethod::reduce_variable(const Variable& var)
{
auto parameterization = per_variable_parameterizations_[&var];
auto original_shape = var.get_shape();
auto stride = parameterization->get_stride();
std::vector<size_t> reduced_shape;
size_t idx = 0;
for (auto dim_size : original_shape) {
reduced_shape.push_back(
static_cast<size_t>(std::ceil(static_cast<double>(dim_size) / static_cast<double>(stride[idx]))));
idx++;
}
parameterization->set_reduced_shape(reduced_shape);
auto self = sg4::Actor::self();
auto [start, count] = var.get_local_start_and_count(self);
std::vector<size_t> reduced_start;
std::vector<size_t> reduced_count;
for (size_t i = 0; i < original_shape.size(); i++) {
// Sanity checks that shape, start, and count have the same size have already been done
auto r_start = static_cast<size_t>(std::ceil(static_cast<double>(start[i]) / static_cast<double>(stride[i])));
size_t r_next_start = std::min(
original_shape[i],
static_cast<size_t>(std::ceil(static_cast<double>(start[i] + count[i]) / static_cast<double>(stride[i]))));
XBT_DEBUG("Dim %zu: stride = %zu, Start = %zu, r_start = %zu, Count = %zu, r_count = %zu", i, stride[i], start[i],
r_start, count[i], r_next_start - r_start);
reduced_start.push_back(r_start);
reduced_count.push_back(r_next_start - r_start);
}
parameterization->set_reduced_local_start_and_count(self, reduced_start, reduced_count);
}
} // namespace dtlmod