13#ifndef KASKADE_BDDC_SYMBOL_HISTOGRAM_HH
14#define KASKADE_BDDC_SYMBOL_HISTOGRAM_HH
40 auto flag=std::getenv(
"KASKADE_PHYSICAL_HISTOGRAM");
41 return flag && std::string(flag)==
"1" && exchange==1;
44 template<
class S,
class U>
45 void writePhysical(std::string
const& directory, std::string
const& phase,
46 std::uint64_t exchange,
int peer, std::vector<S>
const& input,
47 std::vector<S>
const& coefficients, std::vector<U>
const& quantized,
48 double minimum,
double scale,
bool split,
double constant)
50 if (input.size()!=coefficients.size() || input.size()!=quantized.size())
51 throw std::runtime_error(
"Physical histogram population mismatch");
52 auto folder=std::filesystem::path(directory)/
"physical";
53 std::filesystem::create_directories(folder);
54 auto path=folder/(phase+
"-exchange"+std::to_string(exchange)+
"-neighbor"+
55 std::to_string(peer)+
".csv");
56 if (std::filesystem::exists(path))
57 throw std::runtime_error(
"Refusing to overwrite physical histogram: "+path.string());
58 std::ofstream out(path);
59 out.exceptions(std::ios::failbit | std::ios::badbit);
60 out << std::setprecision(std::numeric_limits<double>::max_digits10);
61 out <<
"position,input,coefficient,quantized,wire_minimum,wire_scale,split,wire_constant\n";
62 for (std::size_t i=0;i<input.size();++i)
64 if (!std::isfinite(
double(input[i])) || !std::isfinite(
double(coefficients[i])))
65 throw std::runtime_error(
"Nonfinite physical histogram observation");
66 out << i <<
',' << input[i] <<
',' << coefficients[i] <<
',' << quantized[i]
67 <<
',' << float(minimum) <<
',' << float(scale) <<
',' << int(split)
68 <<
',' << constant <<
'\n';
86 void write(std::string
const& directory, std::string
const& phase,
87 std::uint64_t exchange,
int peer,
int bits, std::size_t coarseCount,
88 std::vector<U>
const& quantized, std::vector<U>
const& mapped,
89 std::vector<U>
const& lengths,
bool encoded,
bool floating,
bool integer)
91 if (directory.empty())
return;
92 if (bits<2 || bits>24 || quantized.size()!=mapped.size() ||
93 mapped.size()!=lengths.size() || coarseCount>mapped.size())
94 throw std::runtime_error(
"Invalid packet histogram input");
95 using Key=std::tuple<std::string,std::string,std::int64_t>;
96 std::map<Key,std::uint64_t> counts;
97 std::string
const inputRole=floating ?
"coefficient" :
"nodal";
98 std::string
const order=floating ? (integer ?
"both" :
"before") :
99 (integer ?
"after" :
"quantization");
100 for (std::size_t i=0;i<mapped.size();++i)
102 std::uint64_t u=
static_cast<std::uint64_t
>(mapped[i]);
103 bool const fine=integer && i>=coarseCount;
104 std::int64_t
const raw=fine ? (u%2 ? -
static_cast<std::int64_t
>(u/2)-1
105 :
static_cast<std::int64_t
>(u/2))
106 :
static_cast<std::int64_t
>(u);
107 std::string
const role=integer ? (fine ?
"detail" :
"coarse") : inputRole;
108 std::uint64_t length=0;
109 for (
auto v=u;v;v>>=1) ++length;
110 if (length!=
static_cast<std::uint64_t
>(lengths[i]))
111 throw std::runtime_error(
"Histogram bit lengths differ from encoder");
112 ++counts[{
"quantized_input",inputRole,
static_cast<std::int64_t
>(quantized[i])}];
113 ++counts[{
"raw",role,raw}];
114 ++counts[{
"unsigned",role,
static_cast<std::int64_t
>(u)}];
115 ++counts[{
"bitlength",role,
static_cast<std::int64_t
>(length)}];
117 std::filesystem::create_directories(directory);
118 auto path=std::filesystem::path(directory)/(phase+
"-exchange"+
119 std::to_string(exchange)+
"-neighbor"+std::to_string(peer)+
".csv");
120 if (std::filesystem::exists(path))
121 throw std::runtime_error(
"Refusing to overwrite packet histogram: "+path.string());
122 std::ofstream out(path);
123 if (!out)
throw std::runtime_error(
"Cannot write packet histogram: "+path.string());
124 out <<
"stage,role,symbol,count,bits,huffman_encoded,transform_order\n";
125 for (
auto const& [key,
count]:counts)
126 out << std::get<0>(key) <<
',' << std::get<1>(key) <<
',' << std::get<2>(key)
127 <<
',' <<
count <<
',' << bits <<
',' << int(encoded) <<
',' <<
order <<
'\n';
129 if (!out)
throw std::runtime_error(
"Incomplete packet histogram: "+path.string());
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
bool physicalEnabled(std::uint64_t exchange)
void write(std::string const &directory, std::string const &phase, std::uint64_t exchange, int peer, int bits, std::size_t coarseCount, std::vector< U > const &quantized, std::vector< U > const &mapped, std::vector< U > const &lengths, bool encoded, bool floating, bool integer)
Observe the actual per-neighbor encoder inputs, without modifying them.
void writePhysical(std::string const &directory, std::string const &phase, std::uint64_t exchange, int peer, std::vector< S > const &input, std::vector< S > const &coefficients, std::vector< U > const &quantized, double minimum, double scale, bool split, double constant)
int const LagrangeCubeShapeFunction< ctype, dimension, Scalar, O >::order