FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
coloring_data_handler.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/adjacency/coloring.hpp>
11#include <kernel/util/memory_arbiter.hpp>
12
13// includes, system
14#include <vector>
15#include <algorithm>
16#include <numeric>
17
18namespace FEAT
19{
20 namespace Adjacency
21 {
32 {
33 public:
37 std::vector<Index> _coloring_map_offsets;
39 std::vector<Index> _coloring_map_sizes;
40
41 explicit ColoringDataHandler() = default;
42
57 template<typename ColoringType_>
58 ColoringDataHandler(const ColoringType_& coloring, int hint = -1)
59 {
60 fill_color(coloring, hint);
61 }
62
64 {
65 release_color();
66 }
67
68 ColoringDataHandler(const ColoringDataHandler&) = delete;
69
70 ColoringDataHandler& operator=(const ColoringDataHandler&) = delete;
71
72
73 ColoringDataHandler(ColoringDataHandler&& other) noexcept
74 {
75 _coloring_map = std::move(other._coloring_map);
76 _coloring_map_offsets = std::move(other._coloring_map_offsets);
77 _coloring_map_sizes = std::move(other._coloring_map_sizes);
78 other._coloring_map.release();
79 other._coloring_map_offsets.clear();
80 other._coloring_map_sizes.clear();
81 }
82
83 ColoringDataHandler& operator=(ColoringDataHandler&& other) noexcept
84 {
85 if(this == &other)
86 return *this;
87 release_color();
88 _coloring_map = std::move(other._coloring_map);
89 _coloring_map_offsets = std::move(other._coloring_map_offsets);
90 _coloring_map_sizes = std::move(other._coloring_map_sizes);
91 other._coloring_map.release();
92 other._coloring_map_offsets.clear();
93 other._coloring_map_sizes.clear();
94 return *this;
95 }
96
99 {
100 return _coloring_map_sizes.size();
101 }
102
103 Index get_color_offset(Index k) const
104 {
105 return _coloring_map_offsets.at(k);
106 }
107
108 std::vector<Index>& get_color_offsets()
109 {
111 }
112
113 const std::vector<Index>& get_color_offsets() const
114 {
116 }
117
118 Index get_color_size(Index k) const
119 {
120 return _coloring_map_sizes.at(k);
121 }
122
123 std::vector<Index>& get_color_sizes()
124 {
125 return _coloring_map_sizes;
126 }
127
128 const std::vector<Index>& get_color_sizes() const
129 {
130 return _coloring_map_sizes;
131 }
132
133 Memory::TypedView<int> color_map_view(Memory::Location mem_loc, Memory::Access mem_acc)
134 {
135 return _coloring_map.typed_view<int>(mem_loc, mem_acc);
136 }
137
138 Memory::TypedView<int> color_map_view(Memory::Location mem_loc, Memory::Access mem_acc = Memory::Access::read) const
139 {
140 return _coloring_map.typed_view<int>(mem_loc, mem_acc);
141 }
142
145 {
146 return Index(std::accumulate(_coloring_map_sizes.begin(), _coloring_map_sizes.end(), Index(0), [](Index a, Index b){return std::max(a,b);}));
147 }
148
155 void fill_color(const std::vector<int>& coloring, int hint = -1)
156 {
157 int num_colors = hint;
158 if(hint < 0)
159 {
160 num_colors = *std::max_element(coloring.begin(), coloring.end()) + 1;
161 }
162 // fill tmp vector with coloring
163 std::vector<std::vector<int>> tmp_vector;
164 tmp_vector.resize(Index(num_colors));
165 for(std::size_t i = 0; i < coloring.size(); ++i)
166 {
167 tmp_vector.at(std::size_t(coloring.at(i))).push_back(int(i));
168 }
169
170 _coloring_map = Memory::Arbiter(coloring.size() * sizeof(int));
171 _coloring_map_offsets.resize(std::size_t(num_colors));
172 _coloring_map_sizes.resize(std::size_t(num_colors));
174 Index offset(0);
175 for(Index i = 0; i < Index(num_colors); ++i)
176 {
177 Memory::memcopy_main(&coloring_map_view.get_w()[offset], tmp_vector.at(i).data(), tmp_vector.at(i).size() * sizeof(int));
178 _coloring_map_offsets.at(i) = offset;
179 _coloring_map_sizes.at(i) = tmp_vector.at(i).size();
180 offset += _coloring_map_sizes.at(i);
181 }
182 }
183
191 void fill_color(const Coloring& coloring, int hint = -1)
192 {
193 int num_colors = int(coloring.get_num_colors());
194 if(hint >= 0)
195 {
196 ASSERTM(num_colors == hint, "Hint and number of colors do not fit!");
197 }
198
199 // fill tmp vector with coloring
200 std::vector<std::vector<int>> tmp_vector;
201 tmp_vector.resize(Index(num_colors));
202 for(std::size_t i = 0; i < coloring.size(); ++i)
203 {
204 tmp_vector.at(std::size_t(coloring[i])).push_back(int(i));
205 }
206 _coloring_map = Memory::Arbiter(coloring.size() * sizeof(int));
207 _coloring_map_offsets.resize(std::size_t(num_colors));
208 _coloring_map_sizes.resize(std::size_t(num_colors));
210 Index offset(0);
211 for(Index i = 0; i < Index(num_colors); ++i)
212 {
213 Memory::memcopy_main(&coloring_map_view.get_w()[offset], tmp_vector.at(i).data(), tmp_vector.at(i).size() * sizeof(int));
214 _coloring_map_offsets.at(i) = offset;
215 _coloring_map_sizes.at(i) = tmp_vector.at(i).size();
216 offset += _coloring_map_sizes.at(i);
217 }
218 }
219
220 void release_color()
221 {
223 _coloring_map_sizes.clear();
224 _coloring_map_offsets.clear();
225 }
226
227 bool initialized() const
228 {
229 return !_coloring_map_sizes.empty();
230 }
231
232 Index get_max_size() const
233 {
234 return std::accumulate(_coloring_map_sizes.begin(), _coloring_map_sizes.end(), Index(0), [](const Index& a, const Index& b){return std::max(a, b);});
235 }
236 }; // class ColoringDataHandler
237 } // namespace Adjacency
238} // namespace FEAT
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
FEAT Kernel base header.
Datahandler for inverse coloring data.
std::vector< Index > _coloring_map_offsets
vector of coloring sizes
Index get_max_color_size() const
Get max size of all colors.
Index get_num_colors() const
Returns the number of colors.
void fill_color(const std::vector< int > &coloring, int hint=-1)
Fill in the coloring array.
Memory::Arbiter _coloring_map
vector of unified memory pointer
void fill_color(const Coloring &coloring, int hint=-1)
Fill in the coloring array.
std::vector< Index > _coloring_map_sizes
vector of coloring sizes
ColoringDataHandler(const ColoringType_ &coloring, int hint=-1)
Constructor receiving coloring data and optional hint.
Coloring object implementation.
Definition: coloring.hpp:30
auto size() const -> decltype(_coloring.size())
Returns the total number of nodes.
Definition: coloring.hpp:190
Index get_num_colors() const
Returns the number of colors.
Definition: coloring.hpp:210
Memory arbiter class.
void release()
Releases this arbiter from its associated memory block and frees it if necessary.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
TypedView< T_ > typed_view(Location mem_loc, Access access)
Maps a typed view to a memory block.
Typed memory view class.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
Access
Memory access enumeration.
@ read
read-only access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.