KASKADE 7 development version
Loading...
Searching...
No Matches
bddcProlongationGroups.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-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_PROLONGATION_GROUPS_HH
15#define KASKADE_BDDC_PROLONGATION_GROUPS_HH
16#include <algorithm>
17#include <cmath>
18#include <cstdint>
19#include <cstring>
20#include <limits>
21#include <stdexcept>
22#include <tuple>
23#include <vector>
24
25namespace Kaskade::BDDC {
26// Experimental grouped affine prolongation header. Existing packet DOF order is retained.
27// uint32 run count, then (uint32 scalar count, float scale, float minimum)
28// per contiguous producer-group run. Header padded to transmission-slot size.
29// The same producer group scale/minimum MUST be used in all neighbor packets.
31 static std::vector<unsigned char> pack(std::vector<std::size_t> const& ids,
32 std::vector<double> const& scales,
33 std::vector<double> const& minima,
34 std::size_t slot)
35 {
36 if (!slot || ids.size()!=scales.size() || ids.size()!=minima.size() ||
37 ids.size()>std::numeric_limits<std::uint32_t>::max())
38 throw std::runtime_error("Invalid prolongation group header input");
39 std::vector<unsigned char> out(4,0);
40 std::uint32_t runs=0;
41 for (std::size_t first=0; first<ids.size();)
42 {
43 std::size_t end=first+1;
44 while (end<ids.size() && ids[end]==ids[first]) ++end;
45 float scale=static_cast<float>(scales[first]), offset=static_cast<float>(minima[first]);
46 if (!(scale>0) || !std::isfinite(scale) || !std::isfinite(offset))
47 throw std::runtime_error("Prolongation group scale/minimum must be finite float32, scale positive");
48 for (auto k=first; k<end; ++k)
49 if (scales[k]!=scales[first] || minima[k]!=minima[first])
50 throw std::runtime_error("One prolongation group has inconsistent scales");
51 std::uint32_t count=static_cast<std::uint32_t>(end-first);
52 auto pos=out.size(); out.resize(pos+12);
53 std::memcpy(out.data()+pos,&count,4);
54 std::memcpy(out.data()+pos+4,&scale,4);
55 std::memcpy(out.data()+pos+8,&offset,4);
56 ++runs; first=end;
57 }
58 std::memcpy(out.data(),&runs,4);
59 out.resize((std::max(std::size_t(8),out.size())+slot-1)/slot*slot,0);
60 return out;
61 }
62
63 static std::tuple<std::vector<double>,std::vector<double>,std::size_t>
64 unpack(unsigned char const* data, std::size_t bytes,
65 std::size_t expected, std::size_t slot)
66 {
67 if (!slot || bytes<4) throw std::runtime_error("Truncated prolongation group header");
68 std::uint32_t runs; std::memcpy(&runs,data,4);
69 if (runs>expected || runs>(bytes-4)/12)
70 throw std::runtime_error("Invalid prolongation group run count");
71 std::size_t header=(std::max(std::size_t(8),4+12*std::size_t(runs))+slot-1)/slot*slot;
72 if (header>bytes) throw std::runtime_error("Truncated prolongation group padding");
73 std::vector<double> scales, minima; scales.reserve(expected); minima.reserve(expected);
74 for (std::size_t r=0; r<runs; ++r)
75 {
76 std::uint32_t count; float scale,offset;
77 auto pos=4+12*r;
78 std::memcpy(&count,data+pos,4);
79 std::memcpy(&scale,data+pos+4,4);
80 std::memcpy(&offset,data+pos+8,4);
81 if (!count || count>expected-scales.size() || !(scale>0) ||
82 !std::isfinite(scale) || !std::isfinite(offset))
83 throw std::runtime_error("Invalid prolongation group descriptor");
84 scales.insert(scales.end(),count,double(scale));
85 minima.insert(minima.end(),count,double(offset));
86 }
87 if (scales.size()!=expected)
88 throw std::runtime_error("Prolongation group counts do not match interface");
89 return {std::move(scales),std::move(minima),header};
90 }
91};
92}
93#endif
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
static std::vector< unsigned char > pack(std::vector< std::size_t > const &ids, std::vector< double > const &scales, std::vector< double > const &minima, std::size_t slot)
static std::tuple< std::vector< double >, std::vector< double >, std::size_t > unpack(unsigned char const *data, std::size_t bytes, std::size_t expected, std::size_t slot)