KASKADE 7 development version
Loading...
Searching...
No Matches
bddcLiftingPlan.hh
Go to the documentation of this file.
1
2/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3/* */
4/* This file is part of the library KASKADE 7 */
5/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
6/* */
7/* Copyright (C) 2026 Zuse Institute Berlin */
8/* */
9/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
10/* see $KASKADE/academic.txt */
11/* */
12/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14#ifndef KASKADE_BDDC_LIFTING_PLAN_HH
15#define KASKADE_BDDC_LIFTING_PLAN_HH
16
17#include <algorithm>
18#include <cmath>
19#include <cstdint>
20#include <cstring>
21#include <limits>
22#include <stdexcept>
23#include <type_traits>
24#include <vector>
25
26namespace Kaskade::BDDC
27{
37template<class Scalar>
39{
40 std::size_t nodeCount = 0;
41 std::vector<int> coarse, fine;
42 std::vector<std::vector<int>> neighbors;
43 std::vector<std::vector<Scalar>> weights;
44
45 void validate() const
46 {
47 if (nodeCount > static_cast<std::size_t>(std::numeric_limits<int>::max()) ||
48 coarse.size()+fine.size() != nodeCount || neighbors.size() != fine.size() ||
49 weights.size() != fine.size())
50 throw std::runtime_error("Invalid BDDC lifting partition size");
51 std::vector<int> kind(nodeCount,0);
52 auto mark = [&](std::vector<int> const& indices, int type) {
53 for (int i: indices)
54 {
55 if (i < 0 || static_cast<std::size_t>(i) >= nodeCount || kind[i])
56 throw std::runtime_error("Invalid BDDC lifting partition index");
57 kind[i] = type;
58 }
59 };
60 mark(coarse,1); mark(fine,2);
61 for (std::size_t k=0; k<fine.size(); ++k)
62 {
63 if (neighbors[k].empty() || neighbors[k].size()!=weights[k].size())
64 throw std::runtime_error("Invalid BDDC lifting predictor size");
65 for (std::size_t j=0; j<neighbors[k].size(); ++j)
66 {
67 int i=neighbors[k][j];
68 if (i<0 || static_cast<std::size_t>(i)>=nodeCount || kind[i]!=1 ||
69 !std::isfinite(weights[k][j]) || weights[k][j]<=Scalar(0))
70 throw std::runtime_error("Invalid BDDC lifting predictor");
71 }
72 }
73 }
74
75 std::vector<unsigned char> pack() const
76 {
77 validate();
78 std::vector<unsigned char> bytes;
79 auto put = [&](auto value) {
80 auto n=bytes.size(); bytes.resize(n+sizeof(value));
81 std::memcpy(bytes.data()+n,&value,sizeof(value));
82 };
83 put(std::uint32_t(0x424c5001)); put(std::uint32_t(sizeof(Scalar)));
84 put(std::uint64_t(nodeCount)); put(std::uint64_t(coarse.size()));
85 put(std::uint64_t(fine.size()));
86 for (int i: coarse) put(std::int32_t(i));
87 for (int i: fine) put(std::int32_t(i));
88 for (std::size_t k=0; k<fine.size(); ++k)
89 {
90 put(std::uint64_t(neighbors[k].size()));
91 for (std::size_t j=0; j<neighbors[k].size(); ++j)
92 {
93 put(std::int32_t(neighbors[k][j])); put(weights[k][j]);
94 }
95 }
96 return bytes;
97 }
98
99 static IntegerLiftingPlan unpack(std::vector<unsigned char> const& bytes,
100 std::size_t expectedNodes)
101 {
102 std::size_t pos=0;
103 auto get = [&]<class T>() {
104 if (bytes.size()-pos<sizeof(T))
105 throw std::runtime_error("Truncated BDDC lifting plan");
106 T value; std::memcpy(&value,bytes.data()+pos,sizeof(T)); pos+=sizeof(T);
107 return value;
108 };
109 if (get.template operator()<std::uint32_t>()!=0x424c5001 ||
110 get.template operator()<std::uint32_t>()!=sizeof(Scalar))
111 throw std::runtime_error("Unsupported BDDC lifting plan format");
113 auto n=get.template operator()<std::uint64_t>();
114 auto nc=get.template operator()<std::uint64_t>();
115 auto nf=get.template operator()<std::uint64_t>();
116 if (n!=expectedNodes || n>std::numeric_limits<int>::max() || nc>n || nf!=n-nc ||
117 n>(bytes.size()-pos)/sizeof(std::int32_t))
118 throw std::runtime_error("BDDC lifting interface size mismatch");
119 plan.nodeCount=n;
120 for (std::uint64_t k=0; k<nc; ++k)
121 plan.coarse.push_back(get.template operator()<std::int32_t>());
122 for (std::uint64_t k=0; k<nf; ++k)
123 plan.fine.push_back(get.template operator()<std::int32_t>());
124 plan.neighbors.resize(nf); plan.weights.resize(nf);
125 for (std::size_t k=0; k<nf; ++k)
126 {
127 auto count=get.template operator()<std::uint64_t>();
128 if (count>(bytes.size()-pos)/(sizeof(std::int32_t)+sizeof(Scalar)))
129 throw std::runtime_error("Truncated BDDC lifting predictor");
130 for (std::uint64_t j=0; j<count; ++j)
131 {
132 plan.neighbors[k].push_back(get.template operator()<std::int32_t>());
133 plan.weights[k].push_back(get.template operator()<Scalar>());
134 }
135 }
136 if (pos!=bytes.size()) throw std::runtime_error("Trailing BDDC lifting plan data");
137 plan.validate();
138 return plan;
139 }
140
141 template<int m, class quantizedScalar>
142 std::vector<quantizedScalar> forward(std::vector<quantizedScalar> const& data, bool zigzag) const
143 {
144 using U = std::make_unsigned_t<quantizedScalar>;
145 int const n = static_cast<int>(nodeCount);
146 if (data.size() != nodeCount * m)
147 throw std::runtime_error("BDDC lifting plan/data size mismatch");
148 if (n <= 1) return data;
149 int const nc = static_cast<int>(coarse.size());
150 std::vector<quantizedScalar> transformed(data.size(), quantizedScalar(0));
151 auto encodeDetail = [zigzag](U d) -> U {
152 if (!zigzag) return d;
153 using S = std::make_signed_t<U>;
154 if (static_cast<S>(d) >= 0) return d << 1;
155 U magnitude = U(0) - d;
156 return (magnitude << 1) - U(1);
157 };
158 for (int comp = 0; comp < m; ++comp)
159 {
160 std::vector<U> x(n);
161 for (int p = 0; p < n; ++p) x[p] = static_cast<U>(data[p * m + comp]);
162 int base = comp * n;
163 for (int k = 0; k < nc; ++k) transformed[base + k] = static_cast<quantizedScalar>(x[coarse[k]]);
164 for (int k = 0; k < static_cast<int>(fine.size()); ++k)
165 {
166 long double pred = 0, wsum = 0;
167 for (int t = 0; t < static_cast<int>(neighbors[k].size()); ++t)
168 {
169 pred += static_cast<long double>(weights[k][t]) * x[neighbors[k][t]];
170 wsum += static_cast<long double>(weights[k][t]);
171 }
172 pred = std::round(pred / std::max(wsum, 1e-12L));
173 pred = std::max(0.0L, std::min(pred, static_cast<long double>(std::numeric_limits<U>::max())));
174 U detail = x[fine[k]] - static_cast<U>(pred);
175 transformed[base + nc + k] = static_cast<quantizedScalar>(encodeDetail(detail));
176 }
177 }
178 return transformed;
179 }
180 template<int m, class quantizedScalar>
181 std::vector<quantizedScalar> inverse(std::vector<quantizedScalar> const& data, bool zigzag) const
182 {
183 using U = std::make_unsigned_t<quantizedScalar>;
184 int const n = static_cast<int>(nodeCount);
185 if (data.size() != nodeCount * m)
186 throw std::runtime_error("BDDC lifting plan/data size mismatch");
187 if (n <= 1) return data;
188 int const nc = static_cast<int>(coarse.size());
189 std::vector<quantizedScalar> reconstructed(data.size(), quantizedScalar(0));
190 auto decodeDetail = [zigzag](U u) -> U {
191 if (!zigzag) return u;
192 U magnitude = (u >> 1) + (u & U(1));
193 return (u & U(1)) ? U(0) - magnitude : magnitude;
194 };
195 for (int comp = 0; comp < m; ++comp)
196 {
197 std::vector<U> x(n, U(0));
198 int base = comp * n;
199 for (int k = 0; k < nc; ++k) x[coarse[k]] = static_cast<U>(data[base + k]);
200 for (int k = 0; k < static_cast<int>(fine.size()); ++k)
201 {
202 long double pred = 0, wsum = 0;
203 for (int t = 0; t < static_cast<int>(neighbors[k].size()); ++t)
204 {
205 pred += static_cast<long double>(weights[k][t]) * x[neighbors[k][t]];
206 wsum += static_cast<long double>(weights[k][t]);
207 }
208 pred = std::round(pred / std::max(wsum, 1e-12L));
209 pred = std::max(0.0L, std::min(pred, static_cast<long double>(std::numeric_limits<U>::max())));
210 x[fine[k]] = decodeDetail(static_cast<U>(data[base + nc + k])) + static_cast<U>(pred);
211 }
212 for (int p = 0; p < n; ++p) reconstructed[p * m + comp] = static_cast<quantizedScalar>(x[p]);
213 }
214 return reconstructed;
215 }
216};
217}
218#endif
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
Producer-owned, interface-relative integer prediction plan.
std::vector< quantizedScalar > forward(std::vector< quantizedScalar > const &data, bool zigzag) const
static IntegerLiftingPlan unpack(std::vector< unsigned char > const &bytes, std::size_t expectedNodes)
std::vector< quantizedScalar > inverse(std::vector< quantizedScalar > const &data, bool zigzag) const
std::vector< std::vector< int > > neighbors
std::vector< std::vector< Scalar > > weights
std::vector< unsigned char > pack() const