75 std::vector<unsigned char>
pack()
const
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));
83 put(std::uint32_t(0x424c5001)); put(std::uint32_t(
sizeof(
Scalar)));
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)
91 for (std::size_t j=0; j<
neighbors[k].size(); ++j)
100 std::size_t expectedNodes)
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);
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");
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)
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)
132 plan.neighbors[k].push_back(get.template
operator()<std::int32_t>());
133 plan.weights[k].push_back(get.template
operator()<
Scalar>());
136 if (pos!=bytes.size())
throw std::runtime_error(
"Trailing BDDC lifting plan data");
142 std::vector<quantizedScalar>
forward(std::vector<quantizedScalar>
const& data,
bool zigzag)
const
144 using U = std::make_unsigned_t<quantizedScalar>;
145 int const n =
static_cast<int>(
nodeCount);
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);
158 for (
int comp = 0; comp < m; ++comp)
161 for (
int p = 0; p < n; ++p) x[p] = static_cast<U>(data[p * m + comp]);
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)
166 long double pred = 0, wsum = 0;
167 for (
int t = 0; t < static_cast<int>(
neighbors[k].size()); ++t)
170 wsum +=
static_cast<long double>(
weights[k][t]);
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())));
175 transformed[base + nc + k] =
static_cast<quantizedScalar
>(encodeDetail(
detail));
181 std::vector<quantizedScalar>
inverse(std::vector<quantizedScalar>
const& data,
bool zigzag)
const
183 using U = std::make_unsigned_t<quantizedScalar>;
184 int const n =
static_cast<int>(
nodeCount);
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;
195 for (
int comp = 0; comp < m; ++comp)
197 std::vector<U> x(n, U(0));
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)
202 long double pred = 0, wsum = 0;
203 for (
int t = 0; t < static_cast<int>(
neighbors[k].size()); ++t)
206 wsum +=
static_cast<long double>(
weights[k][t]);
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);
212 for (
int p = 0; p < n; ++p) reconstructed[p * m + comp] = static_cast<quantizedScalar>(x[p]);
214 return reconstructed;