KASKADE 7 development version
Loading...
Searching...
No Matches
bddcRestrictionGroups.hh
Go to the documentation of this file.
1#ifndef KASKADE_BDDC_RESTRICTION_GROUPS_HH
2#define KASKADE_BDDC_RESTRICTION_GROUPS_HH
3#include <cmath>
4#include <cstdint>
5#include <cstring>
6#include <limits>
7#include <stdexcept>
8#include <utility>
9#include <vector>
10
11namespace Kaskade::BDDC {
12// Experimental grouped restriction header. Existing packet DOF order is retained.
13// uint32 run count, then (uint32 scalar count, float scale, float zero offset)
14// per contiguous producer-group run. Header padded to transmission-slot size.
15// The same producer group scale MUST be used in all neighbor packets.
17 static std::vector<unsigned char> pack(std::vector<std::size_t> const& ids,
18 std::vector<double> const& scales,
19 std::size_t slot)
20 {
21 if (!slot || ids.size()!=scales.size() ||
22 ids.size()>std::numeric_limits<std::uint32_t>::max())
23 throw std::runtime_error("Invalid restriction group header input");
24 std::vector<unsigned char> out(4,0);
25 std::uint32_t runs=0;
26 for (std::size_t first=0; first<ids.size();)
27 {
28 std::size_t end=first+1;
29 while (end<ids.size() && ids[end]==ids[first]) ++end;
30 float scale=static_cast<float>(scales[first]), offset=0;
31 if (!(scale>0) || !std::isfinite(scale) || double(scale)!=scales[first])
32 throw std::runtime_error("Restriction group scale must be positive float32");
33 for (auto k=first; k<end; ++k)
34 if (scales[k]!=scales[first])
35 throw std::runtime_error("One restriction group has inconsistent scales");
36 std::uint32_t count=static_cast<std::uint32_t>(end-first);
37 auto pos=out.size(); out.resize(pos+12);
38 std::memcpy(out.data()+pos,&count,4);
39 std::memcpy(out.data()+pos+4,&scale,4);
40 std::memcpy(out.data()+pos+8,&offset,4);
41 ++runs; first=end;
42 }
43 std::memcpy(out.data(),&runs,4);
44 out.resize((out.size()+slot-1)/slot*slot,0);
45 return out;
46 }
47
48 static std::pair<std::vector<double>,std::size_t>
49 unpack(unsigned char const* data, std::size_t bytes,
50 std::size_t expected, std::size_t slot)
51 {
52 if (!slot || bytes<4) throw std::runtime_error("Truncated restriction group header");
53 std::uint32_t runs; std::memcpy(&runs,data,4);
54 if (runs>expected || runs>(bytes-4)/12)
55 throw std::runtime_error("Invalid restriction group run count");
56 std::size_t header=(4+12*std::size_t(runs)+slot-1)/slot*slot;
57 if (header>bytes) throw std::runtime_error("Truncated restriction group padding");
58 std::vector<double> scales; scales.reserve(expected);
59 for (std::size_t r=0; r<runs; ++r)
60 {
61 std::uint32_t count; float scale,offset;
62 auto pos=4+12*r;
63 std::memcpy(&count,data+pos,4);
64 std::memcpy(&scale,data+pos+4,4);
65 std::memcpy(&offset,data+pos+8,4);
66 if (!count || count>expected-scales.size() || !(scale>0) ||
67 !std::isfinite(scale) || offset!=0)
68 throw std::runtime_error("Invalid restriction group descriptor");
69 scales.insert(scales.end(),count,double(scale));
70 }
71 if (scales.size()!=expected)
72 throw std::runtime_error("Restriction group counts do not match interface");
73 return {std::move(scales),header};
74 }
75};
76}
77#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::size_t slot)
static std::pair< std::vector< double >, std::size_t > unpack(unsigned char const *data, std::size_t bytes, std::size_t expected, std::size_t slot)