11#include <kernel/util/type_traits.hpp>
12#include <kernel/lafem/container.hpp>
13#include <kernel/lafem/dense_vector.hpp>
14#include <kernel/util/math.hpp>
15#include <kernel/adjacency/permutation.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>
47 template <
typename DT_,
typename IT_ = Index>
51 template <
typename T1_,
typename T2_>
52 static void _insertion_sort(T1_ * key, T2_ * val1,
Index size)
61 while (j > 0 && key[j-1] > swap_key)
72 Index & _used_elements()
77 Index & _allocated_elements()
87 bool _remove_element(IT_ ind)
89 IT_* pindices = this->
_indices.at(0);
90 IT_* ptr = std::find(pindices, pindices + _used_elements(), ind);
91 if(ptr == pindices + _used_elements())
96 *ptr = std::numeric_limits<IT_>::max();
133 this->
_scalar_index.push_back(Math::min<Index>(size_in, 1000));
156 this->
_scalar_index.push_back(Math::min<Index>(size_in, 1000));
179 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
183 deserialize<DT2_, IT2_>(input);
235 this->
move(std::forward<SparseVector>(other));
251 t.
clone(*
this, clone_mode);
263 template<
typename DT2_,
typename IT2_>
279 template <
typename DT2_,
typename IT2_>
292 template <Perspective = Perspective::native>
302 template <Perspective = Perspective::native>
346 MemoryPool::synchronize();
394 this->
_indices.at(0)[0] = IT_(index);
395 this->_used_elements() = 1;
401 this->
_elements.at(0)[this->_used_elements()] = val;
402 this->
_indices.at(0)[this->_used_elements()] = IT_(index);
403 ++this->_used_elements();
425 this->
_elements.at(0)[this->_used_elements()] = val;
426 this->
_indices.at(0)[this->_used_elements()] = IT_(index);
428 ++this->_used_elements();
443 if(_used_elements() ==
Index(0))
446 IT_ * pindices = this->
_indices.at(0);
449 _insertion_sort(pindices, pelements, _used_elements());
452 for (
Index i(1) ; i < _used_elements() ; ++i)
454 if (pindices[i-1] == pindices[i])
456 pindices[i-1] = std::numeric_limits<IT_>::max();
461 _insertion_sort(pindices, pelements, _used_elements());
463 while (pindices[_used_elements() - 1 - junk] == std::numeric_limits<IT_>::max() && junk < _used_elements())
465 _used_elements() -= junk;
469 template<
typename IndexContainer>
470 bool remove_elements(
const IndexContainer& inds)
482 return _remove_element(ind);
487 const IT_* pindices = this->
_indices.at(0);
500 Index max_abs_index = Arch::MaxAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
501 ASSERT(max_abs_index < this->
template used_elements<Perspective::pod>());
502 DT_ result(this->
template elements<Perspective::pod>()[max_abs_index]);
506 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
520 Index min_abs_index = Arch::MinAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
521 ASSERT(min_abs_index < this->
template used_elements<Perspective::pod>());
522 DT_ result(this->
template elements<Perspective::pod>()[min_abs_index]);
526 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
540 Index max_index = Arch::MaxIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
541 ASSERT(max_index < this->
template used_elements<Perspective::pod>());
542 DT_ result(this->
template elements<Perspective::pod>()[max_index]);
545 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
559 Index min_index = Arch::MinIndex::value(this->
template elements<Perspective::pod>(), this->
template used_elements<Perspective::pod>());
560 ASSERT(min_index < this->
template used_elements<Perspective::pod>());
561 DT_ result(this->
template elements<Perspective::pod>()[min_index]);
564 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
581 x.template elements<Perspective::pod>(), this->template used_elements<Perspective::pod>());
584 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
597 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
613 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
627 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
629 bin = std::ifstream::in;
630 std::ifstream file(filename.c_str(), bin);
631 if (! file.is_open())
632 XABORTM(
"Unable to open Vector file " + filename);
649 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_sv, file);
662 std::getline(file, line);
663 if (line.find(
"%%MatrixMarket matrix coordinate real general") == String::npos)
665 XABORTM(
"Input-file is not a compatible mtx-file");
669 std::getline(file,line);
671 XABORTM(
"Input-file is empty");
673 String::size_type begin(line.find_first_not_of(
" "));
674 if (line.at(begin) !=
'%')
678 String::size_type begin(line.find_first_not_of(
" "));
679 line.erase(0, begin);
680 String::size_type end(line.find_first_of(
" "));
681 String srows(line, 0, end);
682 rows = (
Index)atol(srows.c_str());
685 begin = line.find_first_not_of(
" ");
686 line.erase(0, begin);
687 end = line.find_first_of(
" ");
688 String scols(line, 0, end);
692 XABORTM(
"Input-file is no sparse-vector-file");
694 begin = line.find_first_not_of(
" ");
695 line.erase(0, begin);
696 end = line.find_first_of(
" ");
697 String snnz(line, 0, end);
698 nnz = (
Index)atol(snnz.c_str());
710 std::getline(file, line);
714 String::size_type begin(line.find_first_not_of(
" "));
715 line.erase(0, begin);
716 String::size_type end(line.find_first_of(
" "));
717 String srow(line, 0, end);
718 IT_ row((IT_)atol(srow.c_str()));
722 begin = line.find_first_not_of(
" ");
723 line.erase(0, begin);
724 end = line.find_first_of(
" ");
727 begin = line.find_first_not_of(
" ");
728 line.erase(0, begin);
729 end = line.find_first_of(
" ");
730 String sval(line, 0, end);
731 DT_ tval((DT_)atof(sval.c_str()));
742 XABORTM(
"Filemode not supported!");
754 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
756 bin = std::ofstream::out;
758 char* buff =
nullptr;
764 file.open(filename.c_str(), bin);
766 XABORTM(
"Unable to open Matrix file " + filename);
784 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_sv, file);
789 file <<
"%%MatrixMarket matrix coordinate real general\n";
793 const IT_ * pind(this->
indices());
795 for (
Index i(0) ; i < u_elem ; ++i, ++pind, ++pval)
802 XABORTM(
"Filemode not supported!");
811 template <Perspective = Perspective::native>
860 if (perm.
size() == 0)
863 XASSERTM(perm.
size() == this->size(),
"Container size does not match permutation size");
868 const Index *
const inv_pos(inv.get_perm_pos());
872 target(inv_pos[col], (*
this)(col));
876 this->
move(std::move(target));
886 return "SparseVector";
910 const IT_* a = this->
indices();
911 const IT_* b = other.indices();
918 for(
Index i(0); i < n; ++i)
948 extern template class SparseVector<float, std::uint32_t>;
949 extern template class SparseVector<double, std::uint32_t>;
950 extern template class SparseVector<float, std::uint64_t>;
951 extern template class SparseVector<double, std::uint64_t>;
#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.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Index size() const
Returns the containers size.
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.
virtual void clear()
Free all allocated arrays.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Dense data vector class template.
DT_ * elements()
Get a pointer to the data array.
Config class for serialize parameter.
Sparse vector class template.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
SparseVector()
Constructor.
SparseVector(FileMode mode, std::istream &file)
Constructor.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
static String name()
Returns a descriptive string.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
SparseVector(SparseVector &&other)
Move Constructor.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
DT_ min_element() const
Retrieve the minimum value of this vector.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
DT_ * elements()
Get a pointer to the data array.
bool same_layout(const SparseVector &other) const
Checks whether the layout of this vector is identical to another sparse vector.
void operator()(Index index, DT_ val)
Set specific vector element.
IT_ * indices()
Get a pointer to the non zero indices array.
void clone(const SparseVector< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
Index sorted() const
Retrieve status of element sorting.
DT_ DataType
Our datatype.
Index alloc_increment() const
Retrieve allocation incrementation value.
SparseVector(FileMode mode, const String &filename)
Constructor.
Index used_elements() const
Retrieve non zero element count.
SparseVector clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
void convert(const SparseVector< DT2_, IT2_ > &other)
Conversion method.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
SparseVector & operator=(SparseVector &&other)
Assignment move operator.
SparseVector(std::vector< char > input)
Constructor.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
DT_ max_element() const
Retrieve the maximum value of this vector.
SparseVector(Index size_in)
Constructor.
DT_ max_rel_diff(const SparseVector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
const DT_ operator()(Index index) const
Retrieve specific vector element.
SparseVector(Index size_in, DenseVector< DT_, IT_ > &elements_in, DenseVector< IT_, IT_ > &indices_in, bool is_sorted=true)
Constructor.
Index allocated_elements() const
Retrieve amount of allocated elements.
IT_ IndexType
Our indextype.
void permute(Adjacency::Permutation &perm)
Permutate vector according to the given Permutation.
friend std::ostream & operator<<(std::ostream &lhs, const SparseVector &b)
SparseVector streaming operator.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
static void copy(DT_ *dest, const DT_ *src, const Index count)
Copy memory area from src to dest.
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.
constexpr std::size_t FileOutStreamBufferSize
OutStreamBufferSize.
T_ abs(T_ x)
Returns the absolute value.
@ success
solving successful (convergence criterion fulfilled)
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.