11#include <kernel/util/type_traits.hpp>
12#include <kernel/lafem/container.hpp>
13#include <kernel/lafem/dense_vector.hpp>
14#include <kernel/util/tiny_algebra.hpp>
15#include <kernel/util/math.hpp>
16#include <kernel/lafem/arch/max_abs_index.hpp>
17#include <kernel/lafem/arch/min_abs_index.hpp>
18#include <kernel/lafem/arch/max_index.hpp>
19#include <kernel/lafem/arch/min_index.hpp>
20#include <kernel/lafem/arch/max_rel_diff.hpp>
21#include <kernel/adjacency/permutation.hpp>
31 template<
typename DT_,
int BlockSize_, Perspective perspective_>
32 struct SparseVectorBlockedPerspectiveHelper
34 typedef Tiny::Vector<DT_, BlockSize_>
Type;
37 template<
typename DT_,
int BlockSize_>
38 struct SparseVectorBlockedPerspectiveHelper<DT_, BlockSize_,
Perspective::
pod>
67 template <
typename DT_,
typename IT_,
int BlockSize_>
71 template <
typename T1_,
typename T2_>
72 static void _insertion_sort(T1_ * key, T2_ * val1,
Index size)
81 while (j > 0 && key[j-1] > swap_key)
92 Index & _used_elements()
97 Index & _allocated_elements()
107 bool _remove_element(IT_ ind)
109 IT_* pindices = this->
_indices.at(0);
110 IT_* ptr = std::find(pindices, pindices + _used_elements(), ind);
111 if(ptr == pindices + _used_elements())
116 *ptr = std::numeric_limits<IT_>::max();
151 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
155 deserialize<DT2_,IT2_>(input);
199 this->
_scalar_index.push_back(Math::min<Index>(size_in, 1000));
222 this->
_scalar_index.push_back(Math::min<Index>(size_in, 1000));
225 this->
_elements.push_back(elements_in.template elements<Perspective::pod>());
226 this->
_elements_size.push_back(elements_in.template size<Perspective::pod>());
259 this->
move(std::forward<SparseVectorBlocked>(other));
275 t.
clone(*
this, clone_mode);
287 template<
typename DT2_,
typename IT2_>
303 template <
typename DT2_,
typename IT2_>
318 template <Perspective perspective_ = Perspective::native>
319 auto elements() const -> const typename Intern::SparseVectorBlockedPerspectiveHelper<DT_, BlockSize_, perspective_>::Type *
326 return (
const typename Intern::SparseVectorBlockedPerspectiveHelper<DT_, BlockSize_, perspective_>::Type *)(this->
_elements.at(0));
331 template <Perspective perspective_ = Perspective::native>
332 auto elements() ->
typename Intern::SparseVectorBlockedPerspectiveHelper<DT_, BlockSize_, perspective_>::Type *
339 return (
typename Intern::SparseVectorBlockedPerspectiveHelper<DT_, BlockSize_, perspective_>::Type *)(this->
_elements.at(0));
373 template <Perspective perspective_ = Perspective::native>
393 MemoryPool::synchronize();
444 _used_elements() = 1;
461 DT_ * elements_new(MemoryPool::template allocate_memory<DT_>(
463 MemoryPool::template set_memory<DT_>(elements_new, DT_(4711),
497 if(_used_elements() ==
Index(0))
505 _insertion_sort(pindices, pelements, _used_elements());
508 for (
Index i(1) ; i < _used_elements() ; ++i)
510 if (pindices[i-1] == pindices[i])
512 pindices[i-1] = std::numeric_limits<IT_>::max();
517 _insertion_sort(pindices, pelements, _used_elements());
519 while (pindices[_used_elements() - 1 - junk] == std::numeric_limits<IT_>::max() && junk < _used_elements())
521 _used_elements() -= junk;
532 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
539 template<
typename IndexContainer>
540 bool remove_elements(
const IndexContainer& inds)
545 success &= _remove_element(
IndexType(i));
552 return _remove_element(ind);
557 const IT_* pindices = this->
_indices.at(0);
571 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
585 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
586 if (! file.is_open())
587 XABORTM(
"Unable to open Vector file " + filename);
607 XABORTM(
"Filemode not supported!");
619 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
621 bin = std::ofstream::out;
623 char* buff =
nullptr;
629 file.open(filename.c_str(), bin);
631 XABORTM(
"Unable to open Matrix file " + filename);
650 file <<
"%%MatrixMarket matrix coordinate real general\n";
651 file << this->
size()*BlockSize <<
" " << 1 <<
" " << this->
used_elements()*BlockSize <<
"\n";
654 const IT_ * pind(this->
indices());
655 const DT_ * pval(this->elements<Perspective::pod>());
669 XABORTM(
"Filemode not supported!");
685 Index max_abs_index = Arch::MaxAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
686 ASSERT(max_abs_index < this->
template used_elements<Perspective::pod>());
687 DT_ result(this->
template elements<Perspective::pod>()[max_abs_index]);
691 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
705 Index min_abs_index = Arch::MinAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
706 ASSERT(min_abs_index < this->
template used_elements<Perspective::pod>());
707 DT_ result(this->
template elements<Perspective::pod>()[min_abs_index]);
711 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
725 Index max_index = Arch::MaxIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
726 ASSERT(max_index < this->
template used_elements<Perspective::pod>());
727 DT_ result(this->
template elements<Perspective::pod>()[max_index]);
730 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
744 Index min_index = Arch::MinIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
745 ASSERT(min_index < this->
template used_elements<Perspective::pod>());
746 DT_ result(this->
template elements<Perspective::pod>()[min_index]);
749 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
766 x.template elements<Perspective::pod>(), this->template used_elements<Perspective::pod>());
769 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
779 if (perm.
size() == 0)
782 XASSERTM(perm.
size() == this->size(),
"Container size does not match permutation size");
787 const Index *
const inv_pos(inv.get_perm_pos());
791 target(inv_pos[col], (*
this)(col));
803 template <Perspective perspective_ = Perspective::native>
857 return "SparseVectorBlocked";
880 const IT_* a = this->
indices();
881 const IT_* b = other.indices();
888 for(
Index i(0); i < n; ++i)
910 for (
int j(0) ; j < BlockSize_ ; ++j)
#define ASSERT(expr)
Debug-Assertion macro definition.
#define XABORTM(msg)
Abortion macro definition with custom message.
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Index size() const
returns the size of the permutation
Permutation inverse() const
Computes the inverse permutation.
std::vector< DT_ * > _elements
List of pointers to all datatype dependent arrays.
bool empty() const
Checks whether the container is empty.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Index size() const
Returns the containers size.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
std::vector< IT_ * > _indices
List of pointers to all IT_ dependent arrays.
void clone(const Container &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
void move(Container &&other)
Assignment move operation.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Blocked Dense data vector class template.
Index size() const
The number of elements.
Dense data vector class template.
DT_ * elements()
Get a pointer to the data array.
Config class for serialize parameter.
Sparse vector class template.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
Tiny::Vector< DT_, BlockSize_ > ValueType
Our value type.
SparseVectorBlocked(Index size_in, DenseVectorBlocked< DT_, IT_, BlockSize_ > &elements_in, DenseVector< IT_, IT_ > &indices_in, bool is_sorted=true)
Constructor.
static String name()
Returns a descriptive string.
DT_ min_element() const
Retrieve the minimum value of this vector.
Index sorted() const
Retrieve status of element sorting.
Index size() const
The number of elements.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
SparseVectorBlocked(Index size_in)
Constructor.
void clone(const SparseVectorBlocked< DT2_, IT2_, BlockSize_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
static constexpr int BlockSize
Our size of a single block.
Index used_elements() const
Retrieve non zero element count.
friend std::ostream & operator<<(std::ostream &lhs, const SparseVectorBlocked &b)
SparseVectorBlocked streaming operator.
IT_ const * indices() const
Get a pointer to the non zero indices array.
void operator()(Index index, const ValueType &val)
Set specific vector element.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
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(std::vector< char > input)
Constructor.
DT_ DataType
Our datatype.
SparseVectorBlocked(FileMode mode, const String &filename)
Constructor.
SparseVectorBlocked(FileMode mode, std::istream &file)
Constructor.
void permute(Adjacency::Permutation &perm)
Permutate vector according to the given Permutation.
SparseVectorBlocked & operator=(SparseVectorBlocked &&other)
Assignment move operator.
IT_ IndexType
Our indextype.
SparseVectorBlocked clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
void convert(const SparseVectorBlocked< DT2_, IT2_, BlockSize_ > &other)
Conversion method.
SparseVectorBlocked()
Constructor.
Index allocated_elements() const
Retrieve amount of allocated elements.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
bool same_layout(const SparseVectorBlocked &other) const
Checks whether the layout of this vector is identical to another sparse vector.
const ValueType operator()(Index index) const
Retrieve specific vector element.
IT_ * indices()
Get a pointer to the non zero indices array.
DT_ max_element() const
Retrieve the maximum value of this vector.
auto elements() const -> const typename Intern::SparseVectorBlockedPerspectiveHelper< DT_, BlockSize_, perspective_ >::Type *
Retrieve a pointer to the data array.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
SparseVectorBlocked(SparseVectorBlocked &&other)
Move Constructor.
void sort()
Sorts the vector.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
auto elements() -> typename Intern::SparseVectorBlockedPerspectiveHelper< DT_, BlockSize_, perspective_ >::Type *
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
Index alloc_increment() const
Retrieve allocation incrementation value.
static void copy(DT_ *dest, const DT_ *src, const Index count)
Copy memory area from src to dest.
static void set_memory(DT_ *address, const DT_ val, const Index count=1)
set memory to specific value
static void release_memory(void *address)
release memory or decrease reference counter
static void increase_memory(void *address)
increase memory counter
String class implementation.
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
Tiny Vector class template.
T_ v[s_]
actual vector data
constexpr std::size_t FileOutStreamBufferSize
OutStreamBufferSize.
T_ abs(T_ x)
Returns the absolute value.
Type
bitmask for zfp header
String stringify(const T_ &item)
Converts an item into a String.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
std::uint64_t Index
Index data type.