FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sparse_vector_blocked.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
11#include <kernel/util/math.hpp>
12#include <kernel/util/statistics.hpp>
13#include <kernel/util/tiny_algebra.hpp>
14#include <kernel/util/type_traits.hpp>
15#include <kernel/util/time_stamp.hpp>
16#include <kernel/adjacency/permutation.hpp>
17#include <kernel/lafem/forward.hpp>
18#include <kernel/lafem/container.hpp>
19#include <kernel/lafem/arch/min_max_value_dense.hpp>
20#include <kernel/lafem/arch/max_rel_diff_dense.hpp>
21
22namespace FEAT
23{
24 namespace LAFEM
25 {
48 template <typename DT_, typename IT_, int block_size_>
49 class SparseVectorBlocked : public Container<DT_, IT_>
50 {
51 public:
53 typedef DT_ DataType;
55 typedef IT_ IndexType;
57 static constexpr int block_size = block_size_;
60
61 private:
62 Index & _size()
63 {
64 return this->_scalar_index.at(0);
65 }
66
67 Index & _nonzeros()
68 {
69 return this->_scalar_index.at(1);
70 }
71
72 public:
79 {
80 this->_scalar_index.push_back(0);
81 this->_scalar_index.push_back(0);
82 }
83
91 explicit SparseVectorBlocked(Index size_in, Index nnze_in)
92 {
93 this->_scalar_index.push_back(size_in);
94 this->_scalar_index.push_back(nnze_in);
95 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * nnze_in * Index(block_size_)));
96 this->_elements_size.push_back(nnze_in * Index(block_size_));
97 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * nnze_in));
98 this->_indices_size.push_back(nnze_in);
99 }
100
112 DenseVector<IT_, IT_> & indices_in)
113 {
114 XASSERT(size_in != Index(0));
115 XASSERTM(indices_in.size() == elements_in.size(), "Vector size mismatch!");
116
117 this->_scalar_index.push_back(size_in);
118 this->_scalar_index.push_back(elements_in.size());
119
120 this->_elements.push_back(elements_in.elements_arbiter().attach());
121 this->_elements_size.push_back(elements_in.size() * Index(block_size_));
122 this->_indices.push_back(indices_in.elements_arbiter().attach());
123 this->_indices_size.push_back(indices_in.size());
124 }
125
132 template <typename DT2_ = DT_, typename IT2_ = IT_>
133 explicit SparseVectorBlocked(std::vector<char> input)
134 {
135 this->deserialize<DT2_,IT2_>(input);
136 }
137
146 explicit SparseVectorBlocked(FileMode mode, const String& filename)
147 {
148 this->read_from(mode, filename);
149 }
150
159 explicit SparseVectorBlocked(FileMode mode, std::istream& file)
160 {
161 this->read_from(mode, file);
162 }
163
172 Container<DT_, IT_>(std::forward<SparseVectorBlocked>(other))
173 {
174 }
175
184 {
185 this->move(std::forward<SparseVectorBlocked>(other));
186
187 return *this;
188 }
189
199 {
201 t.clone(*this, clone_mode);
202 return t;
203 }
204
213 template<typename DT2_, typename IT2_>
215 {
216 Container<DT_, IT_>::clone(other, clone_mode);
217 }
218
229 template <typename DT2_, typename IT2_>
231 {
232 this->clone(other);
233 }
234
236 Index size() const
237 {
238 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(0);
239 }
240
243 {
244 return this->size() * Index(block_size_);
245 }
246
249 {
250 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(1);
251 }
252
255 {
256 return this->num_nzes() * Index(block_size_);
257 }
258
260 bool empty() const
261 {
262 return this->_scalar_index.empty() || (this->_scalar_index.at(0) == Index(0));
263 }
264
266 bool hollow() const
267 {
268 return this->_scalar_index.empty() || (this->_scalar_index.at(1) == Index(0));
269 }
270
271 Memory::Arbiter& elements_arbiter()
272 {
273 return this->_elements.front();
274 }
275
276 const Memory::Arbiter& elements_arbiter() const
277 {
278 return this->_elements.front();
279 }
280
281 Memory::TypedView<ValueType> elements_view_r(Memory::Location loc = Memory::Location::main) const
282 {
283 if(this->_elements.empty())
284 return Memory::TypedView<ValueType>();
285 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read));
286 }
287
288 Memory::TypedView<ValueType> elements_view_w(Memory::Location loc = Memory::Location::main)
289 {
290 if(this->_elements.empty())
291 return Memory::TypedView<ValueType>();
292 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::write));
293 }
294
295 Memory::TypedView<ValueType> elements_view_rw(Memory::Location loc = Memory::Location::main)
296 {
297 if(this->_elements.empty())
298 return Memory::TypedView<ValueType>();
299 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read_write));
300 }
301
302 Memory::TypedView<ValueType> elements_view(Memory::Location loc, Memory::Access acc)
303 {
304 if(this->_elements.empty())
305 return Memory::TypedView<ValueType>();
306 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, acc));
307 }
308
309 Memory::Arbiter& indices_arbiter()
310 {
311 return this->_indices.front();
312 }
313
314 const Memory::Arbiter& indices_arbiter() const
315 {
316 return this->_indices.front();
317 }
318
319 Memory::TypedView<IT_> indices_view_r(Memory::Location loc = Memory::Location::main) const
320 {
321 if(this->_indices.empty())
322 return Memory::TypedView<IT_>();
323 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read));
324 }
325
326 Memory::TypedView<IT_> indices_view_w(Memory::Location loc = Memory::Location::main)
327 {
328 if(this->_indices.empty())
329 return Memory::TypedView<IT_>();
330 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::write));
331 }
332
333 Memory::TypedView<IT_> indices_view_rw(Memory::Location loc = Memory::Location::main)
334 {
335 if(this->_indices.empty())
336 return Memory::TypedView<IT_>();
337 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read_write));
338 }
339
340 Memory::TypedView<IT_> indices_view(Memory::Location loc, Memory::Access acc)
341 {
342 if(this->_indices.empty())
343 return Memory::TypedView<IT_>();
344 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, acc));
345 }
346
354 template <typename DT2_ = DT_, typename IT2_ = IT_>
355 void deserialize(std::vector<char> input)
356 {
357 this->template _deserialize<DT2_, IT2_>(FileMode::fm_svb, input);
358 }
359
360 bool element_exists(IndexType ind) const
361 {
362 const Memory::TypedView<IT_> idx_view = this->indices_view_r();
363 const IT_* pindices = idx_view.get_r();
364 return std::find(pindices, pindices + this->_scalar_index.at(1), ind) != pindices + this->_scalar_index.at(1);
365 }
366
377 template <typename DT2_ = DT_, typename IT2_ = IT_>
378 std::vector<char> serialize(const LAFEM::SerialConfig& config = SerialConfig()) const
379 {
380 return this->template _serialize<DT2_, IT2_>(FileMode::fm_svb, config);
381 }
382
389 void read_from(FileMode mode, const String& filename)
390 {
391 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
392 if (! file.is_open())
393 XABORTM("Unable to open Vector file " + filename);
394 read_from(mode, file);
395 file.close();
396 }
397
404 void read_from(FileMode mode, std::istream& file)
405 {
406 switch(mode)
407 {
409 case FileMode::fm_svb:
410 this->template _deserialize<double, std::uint64_t>(FileMode::fm_svb, file);
411 break;
412 default:
413 XABORTM("Filemode not supported!");
414 }
415 }
416
423 void write_out(FileMode mode, const String& filename) const
424 {
425 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
426 if(mode == FileMode::fm_mtx)
427 bin = std::ofstream::out;
428 std::ofstream file;
429 char* buff = nullptr;
430 if(mode == FileMode::fm_mtx)
431 {
432 buff = new char[LAFEM::file_out_stream_buffer_size];
433 file.rdbuf()->pubsetbuf(buff, LAFEM::file_out_stream_buffer_size);
434 }
435 file.open(filename.c_str(), bin);
436 if(! file.is_open())
437 XABORTM("Unable to open Matrix file " + filename);
438 write_out(mode, file);
439 file.close();
440 delete[] buff;
441 }
442
449 void write_out(FileMode mode, std::ostream& file) const
450 {
451 switch(mode)
452 {
454 [[fallthrough]];
455
456 case FileMode::fm_svb:
457 this->template _serialize<double, std::uint64_t>(FileMode::fm_svb, file);
458 break;
459
460 case FileMode::fm_mtx:
461 {
462
463 file << "%%MatrixMarket matrix coordinate real general\n";
464 file << this->size()*block_size << " " << 1 << " " << this->num_nzes()*block_size << "\n";
465 file << std::scientific << std::setprecision(Type::Traits<DT_>::format_precision);
466
467 const Index u_elem(this->num_nzes());
468 const Memory::TypedView<IT_> idx_view = this->indices_view_r();
469 const Memory::TypedView<ValueType> val_view = this->elements_view_r();
470 for (Index i(0) ; i < u_elem ; ++i)
471 {
472 for(int k(0); k < block_size; ++k)
473 file << (idx_view[i]*Index(block_size+k+1)) << " " << 1 << " " << val_view[i][k] << "\n";
474 }
475 break;
476 }
477
478 default:
479 XABORTM("Filemode not supported!");
480 }
481 }
482
485
491 DT_ max_abs_element() const
492 {
493 XASSERT(!this->empty());
494
495 TimeStamp ts_start;
496
497 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->num_nzes_raw(), false, true);
498
499 TimeStamp ts_stop;
500 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
501
502 return result;
503 }
504
510 DT_ min_abs_element() const
511 {
512 XASSERT(!this->empty());
513
514 TimeStamp ts_start;
515
516 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->num_nzes_raw(), true, true);
517
518 TimeStamp ts_stop;
519 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
520
521 return result;
522 }
523
529 DT_ max_element() const
530 {
531 XASSERT(!this->empty());
532
533 TimeStamp ts_start;
534
535 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->num_nzes_raw(), false, false);
536
537 TimeStamp ts_stop;
538 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
539
540 return result;
541 }
542
548 DT_ min_element() const
549 {
550 XASSERT(!this->empty());
551
552 TimeStamp ts_start;
553
554 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->num_nzes_raw(), true, false);
555
556 TimeStamp ts_stop;
557 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
558
559 return result;
560 }
561
569 {
570 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
571 XASSERT(!this->empty());
572
573 TimeStamp ts_start;
574
575 DataType result = Arch::MaxRelDiffDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), this->num_nzes_raw());
576
577 TimeStamp ts_stop;
578 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
579
580 return result;
581 }
582
584
587 {
588 if (perm.size() == 0)
589 return;
590
591 std::map<IT_, ValueType> xm;
592
593 auto ip = perm.inverse();
594 const Index * const inv_pos(ip.get_perm_pos());
595
596 Memory::TypedView<ValueType> xv(this->elements_view_rw());
597 Memory::TypedView<IT_> xi(this->indices_view_rw());
598 for (Index i(0) ; i < this->num_nzes() ; ++i)
599 {
600 xm.emplace(IT_(inv_pos[xi[i]]), xv[i]);
601 }
602
603 Index k(0);
604 for(const auto& x : xm)
605 {
606 xi[k] = x.first;
607 xv[k] = x.second;
608 ++k;
609 }
610 }
611
617 static String name()
618 {
619 return "SparseVectorBlocked";
620 }
621
631 bool same_layout(const SparseVectorBlocked& other) const
632 {
633 if(this->size() != other.size())
634 return false;
635 if(this->num_nzes() != other.num_nzes())
636 return false;
637
638 if(this->num_nzes() == Index(0))
639 return true;
640
641 // check if the indices arbiters are the same
642 if(this->indices_arbiter() == other.indices_arbiter())
643 return true;
644
645 const Memory::TypedView<IT_> idx_a = this->indices_view_r();
646 const Memory::TypedView<IT_> idx_b = other.indices_view_r();
647
648 // check all array entries
649 const Index n = this->num_nzes();
650 for(Index i(0); i < n; ++i)
651 {
652 if(idx_a[i] != idx_b[i])
653 return false;
654 }
655
656 // okay, arrays are identical
657 return true;
658 }
659
666 friend std::ostream & operator<< (std::ostream & lhs, const SparseVectorBlocked & b)
667 {
668 const Memory::TypedView<IT_> idx_view(b.indices_view_r());
669 const Memory::TypedView<ValueType> val_view(b.elements_view_r());
670 const Index n = b.size();
671 const Index nze = b.num_nzes();
672 lhs << "[";
673 Index k = 0;
674 for (Index i(0) ; i < nze ; ++i)
675 {
676 for(; k < idx_view[i]; ++k)
677 {
678 for(int d = 0; d < block_size_; ++d)
679 lhs << " " << DT_(0);
680 }
681
682 for(int d = 0; d < block_size_; ++d)
683 lhs << " " << val_view[i][d];
684 ++k;
685 }
686 for(; k < n; ++k)
687 for(int d = 0; d < block_size_; ++d)
688 lhs << " " << DT_(0);
689 lhs << "]";
690
691 return lhs;
692 }
693 }; // class SparseVectorBlocked<...>
694 } // namespace LAFEM
695} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Index size() const
returns the size of the permutation
Permutation inverse() const
Computes the inverse permutation.
Container base class.
Definition: container.hpp:224
void move(Container &&source)
Assignment move operation.
Definition: container.hpp:1007
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Definition: container.hpp:904
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Definition: container.hpp:234
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
Definition: container.hpp:232
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
Definition: container.hpp:236
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
Definition: container.hpp:230
std::vector< Index > _scalar_index
List of scalars with datatype index.
Definition: container.hpp:238
Blocked Dense data vector class template.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Dense data vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Config class for serialize parameter.
Definition: container.hpp:50
Sparse vector class template.
bool hollow() const
Checks whether the sparse vector does not contain any non-zero elements.
Index size() const
Returns the native size of the sparse vector.
SparseVectorBlocked(std::vector< char > input)
Constructor.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
static constexpr int block_size
Our size of a single block.
friend std::ostream & operator<<(std::ostream &lhs, const SparseVectorBlocked &b)
SparseVectorBlocked streaming operator.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
DT_ max_rel_diff(const SparseVectorBlocked &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
SparseVectorBlocked(Index size_in, Index nnze_in)
Constructor.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
SparseVectorBlocked(FileMode mode, const String &filename)
Constructor.
SparseVectorBlocked & operator=(SparseVectorBlocked &&other)
Assignment move operator.
bool empty() const
Checks whether the size of the sparse vector is zero.
Index num_nzes_raw() const
Returns the raw number of non-zero elements in the sparse vector.
static String name()
Returns a descriptive string.
void convert(const SparseVectorBlocked< DT2_, IT2_, block_size_ > &other)
Conversion method.
Index num_nzes() const
Returns the number of non-zero elements in the sparse vector.
Tiny::Vector< DT_, block_size_ > ValueType
Our value type.
bool same_layout(const SparseVectorBlocked &other) const
Checks whether the layout of this vector is identical to another sparse vector.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
DT_ max_element() const
Retrieve the maximum value of this vector.
SparseVectorBlocked clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
DT_ min_element() const
Retrieve the minimum value of this vector.
void permute(Adjacency::Permutation &perm)
Permute vector according to the given Permutation.
SparseVectorBlocked(SparseVectorBlocked &&other)
Move Constructor.
void clone(const SparseVectorBlocked< DT2_, IT2_, block_size_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
SparseVectorBlocked(FileMode mode, std::istream &file)
Constructor.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
SparseVectorBlocked(Index size_in, DenseVectorBlocked< DT_, IT_, block_size_ > &elements_in, DenseVector< IT_, IT_ > &indices_in)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
Index size_raw() const
Returns the raw size of the sparse vector.
Memory arbiter class.
Arbiter attach(std::size_t offset=0u, std::size_t size=full_size) const
Creates a new arbiter that is attached to the same memory block as this arbiter.
Typed memory view class.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
String class implementation.
Definition: string.hpp:47
Time stamp class.
Definition: time_stamp.hpp:54
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
Definition: time_stamp.hpp:100
Tiny Vector class template.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
Definition: base.hpp:105
Access
Memory access enumeration.
@ read_write
read-write access
@ 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.
basic Type Traits struct
Definition: type_traits.hpp:73