Repository navigation
Expand file tree
/
Copy pathCompressionReductionMethod.cpp
More file actions
167 lines (144 loc) · 8.12 KB
/
Copy pathCompressionReductionMethod.cpp
File metadata and controls
167 lines (144 loc) · 8.12 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
/* Copyright (c) 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 <algorithm>
#include <cmath>
#include <functional>
#include "dtlmod/CompressionReductionMethod.hpp"
#include "dtlmod/DTLException.hpp"
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dtlmod_compression_reduction, dtlmod, "DTL logging about compression method");
namespace dtlmod {
double CompressionReductionMethod::ParameterizedCompression::get_effective_ratio(unsigned int transaction_id) const
{
if (cfg_.ratio_variability <= 0.0)
return cfg_.compression_ratio;
// Deterministic noise from hash of (variable_name, transaction_id)
size_t seed = std::hash<std::string>{}(var_name_) ^ (std::hash<unsigned int>{}(transaction_id) << 1);
// Map to [1 - variability, 1 + variability]
double noise = 1.0 + cfg_.ratio_variability * (2.0 * (seed % 10001) / 10000.0 - 1.0);
return std::max(1.0, cfg_.compression_ratio * noise);
}
size_t CompressionReductionMethod::get_reduced_variable_global_size(const Variable& var,
unsigned int transaction_id) const
{
auto ratio = per_variable_parameterizations_.at(&var)->get_effective_ratio(transaction_id);
return static_cast<size_t>(std::ceil(static_cast<double>(var.get_global_size()) / ratio));
}
size_t CompressionReductionMethod::get_reduced_variable_local_size(const Variable& var,
unsigned int transaction_id) const
{
auto ratio = per_variable_parameterizations_.at(&var)->get_effective_ratio(transaction_id);
return static_cast<size_t>(std::ceil(static_cast<double>(var.get_local_size()) / ratio));
}
double CompressionReductionMethod::get_fidelity(const Variable& var, unsigned int /*transaction_id*/) const
{
// Compression preserves the shape but bounds the per-element error at 'accuracy'; fidelity therefore depends on
// that error bound, not on the (constant) shape or the transferred size. This is a coarse, monotone first-order
// model, not a compressor-specific quality curve: tighter bounds map to higher fidelity, saturating at 1.0.
// -log10(accuracy) / 6 gives 1.0 at accuracy 1e-6 and 0.5 at 1e-3. It is deliberately independent of the
// transaction id since 'accuracy' does not vary per transaction (unlike the compression ratio).
constexpr double reference_exponent = 6.0;
double accuracy = per_variable_parameterizations_.at(&var)->get_accuracy();
double fidelity = -std::log10(accuracy) / reference_exponent;
return std::clamp(fidelity, 0.0, 1.0);
}
double CompressionReductionMethod::get_flop_amount_to_reduce_variable(const Variable& var) const
{
auto param = per_variable_parameterizations_.at(&var);
auto num_elements = static_cast<double>(var.get_local_size() / var.get_element_size());
return param->get_compression_cost_per_element() * num_elements;
}
double CompressionReductionMethod::get_flop_amount_to_decompress_variable(const Variable& var) const
{
auto param = per_variable_parameterizations_.at(&var);
auto num_elements = static_cast<double>(var.get_local_size() / var.get_element_size());
return param->get_decompression_cost_per_element() * num_elements;
}
double CompressionReductionMethod::derive_compression_ratio(double accuracy, std::string_view profile,
double data_smoothness)
{
if (profile == "sz") {
// accuracy = absolute/relative error bound; larger = looser = more compression
// Empirical fit: ratio ≈ A × accuracy^B × (0.5 + data_smoothness)
// Calibrated to give ratio ≈ 7 at 1e-3 and ratio ≈ 2 at 1e-6.
double alpha = 24.4;
double beta = 0.181;
return std::max(1.0, alpha * std::pow(accuracy, beta) * (0.5 + data_smoothness));
}
// The "fixed" profile has no derived ratio: resolve_compression_ratio() requires an explicit one instead.
xbt_assert(profile == "zfp", "No compression ratio can be derived for the '%.*s' profile",
static_cast<int>(profile.size()), profile.data());
// ZFP-like transform-based compressor: rate = bits-per-value derived from accuracy.
// 64 bits (double) / rate gives the compression ratio.
double rate = std::max(1.0, -std::log2(accuracy) + 1.0);
return std::max(1.0, 64.0 / rate);
}
void CompressionReductionMethod::validate_compressor_profile(std::string_view profile)
{
if (profile != "fixed" && profile != "sz" && profile != "zfp")
throw UnknownCompressionOptionException(XBT_THROW_POINT, "Unknown compressor profile: " + std::string(profile) +
" (options are: fixed, sz, or zfp).");
}
double CompressionReductionMethod::resolve_compression_ratio(double ratio, bool ratio_explicitly_set, bool is_new,
std::string_view profile, double accuracy,
double data_smoothness)
{
if (ratio_explicitly_set) {
if (ratio < 1.0)
throw InconsistentCompressionRatioException(XBT_THROW_POINT, "Compression ratio must be >= 1.0");
return ratio;
}
if (is_new) {
if (profile == "fixed")
throw InconsistentCompressionRatioException(
XBT_THROW_POINT, "Compressor profile 'fixed' requires an explicit 'compression_ratio' parameter.");
return derive_compression_ratio(accuracy, profile, data_smoothness);
}
return ratio; // Keep existing ratio for partial updates without explicit ratio
}
void CompressionReductionMethod::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);
bool is_new = (it == per_variable_parameterizations_.end());
CompressionConfig cfg = is_new ? CompressionConfig{} : it->second->get_config();
bool ratio_explicitly_set = false;
for (const auto& [key, value] : parameters) {
if (key == "accuracy")
cfg.accuracy = std::stod(value);
else if (key == "compression_cost_per_element")
cfg.compression_cost_per_element = std::stod(value);
else if (key == "decompression_cost_per_element")
cfg.decompression_cost_per_element = std::stod(value);
else if (key == "compression_ratio") {
cfg.compression_ratio = std::stod(value);
ratio_explicitly_set = true;
} else if (key == "compressor") {
validate_compressor_profile(value);
cfg.compressor_profile = value;
} else if (key == "data_smoothness")
cfg.data_smoothness = std::stod(value);
else if (key == "ratio_variability")
cfg.ratio_variability = std::stod(value);
else
throw UnknownCompressionOptionException(XBT_THROW_POINT, key);
}
cfg.compression_ratio = resolve_compression_ratio(cfg.compression_ratio, ratio_explicitly_set, is_new,
cfg.compressor_profile, cfg.accuracy, cfg.data_smoothness);
XBT_DEBUG("Compression parameterization for Variable %s: profile=%s, accuracy=%.2e, ratio=%.2f, "
"compression_cost=%.2f, decompression_cost=%.2f, smoothness=%.2f, variability=%.2f",
var.get_cname(), cfg.compressor_profile.c_str(), cfg.accuracy, cfg.compression_ratio,
cfg.compression_cost_per_element, cfg.decompression_cost_per_element, cfg.data_smoothness,
cfg.ratio_variability);
// Always (re)create the parameterization — avoids field-by-field update complexity.
per_variable_parameterizations_[&var] = std::make_shared<ParameterizedCompression>(var, std::move(cfg));
}
void CompressionReductionMethod::propagate_for_subscriber(const Variable& publisher_var, const Variable& subscriber_var)
{
auto it = per_variable_parameterizations_.find(&publisher_var);
if (it != per_variable_parameterizations_.end())
per_variable_parameterizations_[&subscriber_var] =
std::make_shared<ParameterizedCompression>(subscriber_var, it->second->get_config());
}
} // namespace dtlmod