KASKADE 7 development version
Loading...
Searching...
No Matches
bddcSymbolHistogram.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2026 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef KASKADE_BDDC_SYMBOL_HISTOGRAM_HH
14#define KASKADE_BDDC_SYMBOL_HISTOGRAM_HH
15
16#include <cstdint>
17#include <cstdlib>
18#include <iomanip>
19#include <limits>
20#include <cmath>
21#include <filesystem>
22#include <fstream>
23#include <map>
24#include <stdexcept>
25#include <string>
26#include <tuple>
27#include <vector>
28
30{
31
38 inline bool physicalEnabled(std::uint64_t exchange)
39 {
40 auto flag=std::getenv("KASKADE_PHYSICAL_HISTOGRAM");
41 return flag && std::string(flag)=="1" && exchange==1;
42 }
43
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)
49 {
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)
63 {
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';
69 }
70 out.close();
71 }
72
85 template<class U>
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)
90 {
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)
101 {
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)}];
116 }
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';
128 out.close();
129 if (!out) throw std::runtime_error("Incomplete packet histogram: "+path.string());
130 }
131}
132#endif
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
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