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>
43 template <
typename DT_,
typename IT_ = Index>
94 Index nnze_in = map.size();
107 auto* id_p = ind_view.get_w();
108 auto* ele_p = ele_view.get_w();
110 for(
auto mp = map.cbegin(); mp != map.cend(); ++mp, ++id_p, ++ele_p)
149 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
152 this->deserialize<DT2_, IT2_>(input);
202 this->
move(std::forward<SparseVector>(other));
218 t.
clone(*
this, clone_mode);
230 template<
typename DT2_,
typename IT2_>
246 template <
typename DT2_,
typename IT2_>
397 const IT_* pindices = idx_view.
get_r();
416 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
435 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
454 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
473 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
486 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
494 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
507 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
523 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
537 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
539 bin = std::ifstream::in;
540 std::ifstream file(filename.c_str(), bin);
541 if (! file.is_open())
542 XABORTM(
"Unable to open Vector file " + filename);
561 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_sv, file);
571 std::getline(file, line);
572 if (line.find(
"%%MatrixMarket matrix coordinate real general") == String::npos)
574 XABORTM(
"Input-file is not a compatible mtx-file");
578 std::getline(file,line);
580 XABORTM(
"Input-file is empty");
582 String::size_type begin(line.find_first_not_of(
" "));
583 if (line.at(begin) !=
'%')
587 String::size_type begin(line.find_first_not_of(
" "));
588 line.erase(0, begin);
589 String::size_type end(line.find_first_of(
" "));
590 String srows(line, 0, end);
591 rows = (
Index)atol(srows.c_str());
594 begin = line.find_first_not_of(
" ");
595 line.erase(0, begin);
596 end = line.find_first_of(
" ");
597 String scols(line, 0, end);
601 XABORTM(
"Input-file is no sparse-vector-file");
603 begin = line.find_first_not_of(
" ");
604 line.erase(0, begin);
605 end = line.find_first_of(
" ");
606 String snnz(line, 0, end);
607 nnz = (
Index)atol(snnz.c_str());
616 IT_ * pind(idx_view.
get_w());
617 DT_ * pval(val_view.
get_w());
621 std::getline(file, line);
625 String::size_type begin(line.find_first_not_of(
" "));
626 line.erase(0, begin);
627 String::size_type end(line.find_first_of(
" "));
628 String srow(line, 0, end);
629 IT_ row((IT_)atol(srow.c_str()));
633 begin = line.find_first_not_of(
" ");
634 line.erase(0, begin);
635 end = line.find_first_of(
" ");
638 begin = line.find_first_not_of(
" ");
639 line.erase(0, begin);
640 end = line.find_first_of(
" ");
641 String sval(line, 0, end);
642 DT_ tval((DT_)atof(sval.c_str()));
653 XABORTM(
"Filemode not supported!");
665 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
667 bin = std::ofstream::out;
669 char* buff =
nullptr;
675 file.open(filename.c_str(), bin);
677 XABORTM(
"Unable to open Matrix file " + filename);
697 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_sv, file);
703 file <<
"%%MatrixMarket matrix coordinate real general\n";
704 file << this->
size() <<
" " << 1 <<
" " << this->
num_nzes() <<
"\n";
710 for (
Index i(0) ; i < u_elem ; ++i)
712 file << (idx_view[i]+1) <<
" " << 1 <<
" " << val_view[i] <<
"\n";
718 XABORTM(
"Filemode not supported!");
725 if (perm.
size() == 0)
728 std::map<IT_, DT_> xm;
731 const Index *
const inv_pos(ip.get_perm_pos());
737 xm.emplace(IT_(inv_pos[xi[i]]), xv[i]);
741 for(
const auto& x : xm)
756 return "SparseVector";
787 for(
Index i(0); i < n; ++i)
789 if(idx_a[i] != idx_b[i])
806 void print(std::ostream & os,
bool print_dense)
const
819 for (
Index i(0) ; i < nze ; ++i)
823 for(; k < idx_view[i]; ++k)
827 os <<
" " << idx_view[i] <<
':';
829 os <<
" " << val_view[i];
854 extern template class SparseVector<float, std::uint32_t>;
855 extern template class SparseVector<double, std::uint32_t>;
856 extern template class SparseVector<float, std::uint64_t>;
857 extern template class SparseVector<double, std::uint64_t>;
#define XABORTM(msg)
Abortion 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.
void move(Container &&source)
Assignment move operation.
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
virtual void clear()
Free all allocated arrays.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
std::vector< Index > _scalar_index
List of scalars with datatype index.
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.
Sparse vector class template.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
Index num_nzes() const
Returns the number of non-zero elements in the sparse vector.
SparseVector()
Constructor.
Memory::TypedView< DT_ > elements_view(Memory::Location loc, Memory::Access acc)
Creates a memory view for the elements array.
SparseVector(FileMode mode, std::istream &file)
Constructor.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
const Memory::Arbiter & elements_arbiter() const
Returns a const reference to the elements memory arbiter.
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 print(std::ostream &os, bool print_dense) const
Prints this vector in human readable format to an output stream.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
bool same_layout(const SparseVector &other) const
Checks whether the layout of this vector is identical to another sparse vector.
Memory::Arbiter & elements_arbiter()
Returns a reference to the elements memory arbiter.
SparseVector(Index size_in, DenseVector< DT_, IT_ > &elements_in, DenseVector< IT_, IT_ > &indices_in)
Constructor.
void clone(const SparseVector< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
Memory::TypedView< DT_ > elements_view_r(Memory::Location loc=Memory::Location::main) const
Creates a read-only memory view for the elements array for a given memory location.
DT_ DataType
Our datatype.
SparseVector(FileMode mode, const String &filename)
Constructor.
SparseVector(Index size_in, Index nnze_in)
Constructor.
Memory::TypedView< DT_ > elements_view_rw(Memory::Location loc=Memory::Location::main)
Creates a read-write memory view for the elements array for a given memory location.
SparseVector clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
void convert(const SparseVector< DT2_, IT2_ > &other)
Conversion method.
bool hollow() const
Checks whether the sparse vector does not contain any non-zero elements.
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.
SparseVector & operator=(SparseVector &&other)
Assignment move operator.
SparseVector(std::vector< char > input)
Constructor.
Memory::TypedView< IT_ > indices_view_w(Memory::Location loc=Memory::Location::main)
Creates a write-only memory view for the indices array for a given memory location.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
Index num_nzes_raw() const
Returns the raw number of non-zero elements in the sparse vector.
DT_ max_element() const
Retrieve the maximum value of this vector.
Memory::TypedView< DT_ > elements_view_w(Memory::Location loc=Memory::Location::main)
Creates a write-only memory view for the elements array for a given memory location.
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 .
Memory::TypedView< IT_ > indices_view_rw(Memory::Location loc=Memory::Location::main)
Creates a read-write memory view for the indices array for a given memory location.
Index size() const
Returns the native size of the sparse vector.
const Memory::Arbiter & indices_arbiter() const
Returns a const reference to the indices memory arbiter.
Memory::TypedView< IT_ > indices_view(Memory::Location loc, Memory::Access acc)
Creates a memory view for the indices array.
Memory::TypedView< IT_ > indices_view_r(Memory::Location loc=Memory::Location::main) const
Creates a read-only memory view for the indices array for a given memory location.
IT_ IndexType
Our indextype.
void permute(Adjacency::Permutation &perm)
Permute vector according to the given Permutation.
Memory::Arbiter & indices_arbiter()
Returns a reference to the indices memory arbiter.
friend std::ostream & operator<<(std::ostream &os, const SparseVector &b)
SparseVector streaming operator.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
bool empty() const
Checks whether the size of the sparse vector is zero.
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.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
String class implementation.
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
Access
Memory access enumeration.
@ read_write
read-write access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
std::uint64_t Index
Index data type.