10#include <kernel/lafem/forward.hpp>
12#include <kernel/util/type_traits.hpp>
13#include <kernel/util/math.hpp>
14#include <kernel/util/random.hpp>
15#include <kernel/lafem/container.hpp>
16#include <kernel/lafem/dense_vector_blocked.hpp>
17#include <kernel/lafem/arch/component_invert.hpp>
18#include <kernel/lafem/arch/dot_product.hpp>
19#include <kernel/lafem/arch/norm.hpp>
20#include <kernel/lafem/arch/max_abs_index.hpp>
21#include <kernel/lafem/arch/min_abs_index.hpp>
22#include <kernel/lafem/arch/max_index.hpp>
23#include <kernel/lafem/arch/min_index.hpp>
24#include <kernel/lafem/arch/max_rel_diff.hpp>
25#include <kernel/lafem/arch/scale.hpp>
26#include <kernel/lafem/arch/axpy.hpp>
27#include <kernel/lafem/arch/component_product.hpp>
28#include <kernel/adjacency/permutation.hpp>
29#include <kernel/util/statistics.hpp>
30#include <kernel/util/time_stamp.hpp>
56 template <
typename DT_,
typename IT_ = Index>
70 typedef IT_ IndexType;
78 _num_entries(vector.
size()),
83 template <
typename LocalVector_,
typename Mapping_>
84 void operator()(
const LocalVector_ & loc_vec,
const Mapping_ & mapping, DT_ alpha = DT_(1))
87 for (
int i(0); i < mapping.get_num_local_dofs(); ++i)
90 Index dof_idx = mapping.get_index(i);
91 ASSERT(dof_idx < _num_entries);
94 _data[dof_idx] += alpha * loc_vec[i];
108 typedef DT_ DataType;
109 typedef IT_ IndexType;
117 _num_entries(vector.
size()),
122 template <
typename LocalVector_,
typename Mapping_>
123 void operator()(LocalVector_ & loc_vec,
const Mapping_ & mapping, DT_ alpha = DT_(1))
126 for (
int i(0); i < mapping.get_num_local_dofs(); ++i)
129 Index dof_idx = mapping.get_index(i);
130 ASSERT(dof_idx < _num_entries);
133 loc_vec[i] += alpha * _data[dof_idx];
146 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
150 template <
typename DataType2_,
typename IndexType2_>
173 if (size_in ==
Index(0))
176 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(size_in));
192 if (size_in ==
Index(0))
195 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(size_in));
219 if (size_in ==
Index(0))
246 XASSERTM(size_in + offset_in <= dv_in.
size(),
"Ranged vector part exceeds original vector size!");
250 DT_ * te(
const_cast<DT_ *
>(dv_in.
elements()));
251 this->
_elements.push_back(te + offset_in);
304 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
308 deserialize<DT2_, IT2_>(input);
324 if (size_in ==
Index(0))
327 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(size_in));
330 this->
format(rng, min, max);
361 this->
move(std::forward<DenseVector>(other));
377 t.
clone(*
this, clone_mode);
389 template <
typename DT2_,
typename IT2_>
402 template <
typename DT2_,
typename IT2_>
415 template <
typename DT2_,
typename IT2_,
int BS2_>
420 this->
_scalar_index.push_back(other.template size<Perspective::pod>());
437 template <
typename VT_>
440 DenseVector vec(a.template size<Perspective::pod>());
442 this->
move(std::move(vec));
452 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
470 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
483 template <
int BlockSize_>
489 auto* rp = result.template elements<Perspective::native>();
490 for (
Index i(0) ; i < this->
size() ; ++i)
492 rp[i] =
typename decltype(result)
::ValueType(p[i]);
502 template <
typename VT_>
505 XASSERTM(this->
template size<Perspective::pod>() == a.template size<Perspective::pod>(),
"Vectors have not the same size!");
515 template <
typename VT_>
518 XASSERTM(this->
template size<Perspective::pod>() == a.template size<Perspective::pod>(),
"Vectors have not the same size!");
532 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
534 bin = std::ifstream::in;
535 std::ifstream file(filename.c_str(), bin);
536 if (! file.is_open())
537 XABORTM(
"Unable to open Vector file " + filename);
559 std::getline(file, line);
560 if (line.find(
"%%MatrixMarket matrix array real general") == String::npos)
562 XABORTM(
"Input-file is not a compatible mtx-vector-file");
566 std::getline(file, line);
568 XABORTM(
"Input-file is empty");
570 String::size_type begin(line.find_first_not_of(
" "));
571 if (line.at(begin) !=
'%')
575 String::size_type begin(line.find_first_not_of(
" "));
576 line.erase(0, begin);
577 String::size_type end(line.find_first_of(
" "));
578 String srows(line, 0, end);
579 rows = (
Index)atol(srows.c_str());
582 begin = line.find_first_not_of(
" ");
583 line.erase(0, begin);
584 end = line.find_first_of(
" ");
585 String scols(line, 0, end);
588 XASSERTM(cols == 1,
"Input-file is no dense-vector-file");
596 std::getline(file, line);
600 String::size_type begin(line.find_first_not_of(
" "));
601 line.erase(0, begin);
602 String::size_type end(line.find_first_of(
" "));
603 String sval(line, 0, end);
604 DT_ tval((DT_)atof(sval.c_str()));
609 this->
move(std::move(tmp));
617 std::vector<DT_> data;
622 std::getline(file, line);
623 if (line.find(
"#", 0) < line.npos)
630 std::string::size_type first_digit(line.find_first_not_of(
" "));
631 line.erase(0, first_digit);
632 std::string::size_type eol(line.length());
633 for (std::string::size_type i(0); i < eol; ++i)
635 n_z_s.append(1, line[i]);
638 DT_ n_z((DT_)atof(n_z_s.c_str()));
644 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(
Index(data.size())));
651 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_dv, file);
654 XABORTM(
"Filemode not supported!");
666 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
668 bin = std::ofstream::out;
670 char* buff =
nullptr;
676 file.open(filename.c_str(), bin);
678 XABORTM(
"Unable to open Matrix file " + filename);
700 file <<
"%%MatrixMarket matrix array real general\n";
701 file << tsize <<
" " << 1 <<
"\n";
704 for (
Index i(0); i < tsize; ++i, ++pval)
712 DT_ * temp = MemoryPool::template allocate_memory<DT_>((this->
size()));
724 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_dv, file);
727 XABORTM(
"Filemode not supported!");
736 template <Perspective = Perspective::native>
745 template <Perspective = Perspective::native>
765 MemoryPool::synchronize();
780 MemoryPool::synchronize();
790 return "DenseVector";
801 this->_copy_content(x, full);
815 const DT_ alpha = DT_(1))
817 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
825 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
845 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
864 Arch::ComponentInvert::value(this->
elements(), x.
elements(), alpha, this->size());
867 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
878 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
886 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
900 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
901 XASSERTM(y.
size() == this->size(),
"Vector size does not match!");
909 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
923 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
928 DataType result = Arch::DotProduct::value(this->
elements(), x.
elements(), this->size());
931 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
945 DT_ result = Arch::Norm2::value(this->
elements(), this->
size());
948 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
974 Index max_abs_index = Arch::MaxAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template size<Perspective::pod>());
975 ASSERT(max_abs_index < this->
template size<Perspective::pod>());
977 MemoryPool::copy(&result, this->
template elements<Perspective::pod>() + max_abs_index, 1);
981 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
995 Index min_abs_index = Arch::MinAbsIndex::value(this->
template elements<Perspective::pod>(), this->
template size<Perspective::pod>());
996 ASSERT(min_abs_index < this->
template size<Perspective::pod>());
998 MemoryPool::copy(&result, this->
template elements<Perspective::pod>() + min_abs_index, 1);
1002 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1016 Index max_index = Arch::MaxIndex::value(this->
template elements<Perspective::pod>(), this->
template size<Perspective::pod>());
1017 ASSERT(max_index < this->
template size<Perspective::pod>());
1019 MemoryPool::copy(&result, this->
template elements<Perspective::pod>() + max_index, 1);
1022 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1036 Index min_index = Arch::MinIndex::value(this->
template elements<Perspective::pod>(), this->
template size<Perspective::pod>());
1037 ASSERT(min_index < this->
template size<Perspective::pod>());
1039 MemoryPool::copy(&result, this->
template elements<Perspective::pod>() + min_index, 1);
1042 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1058 DataType max_rel_diff = Arch::MaxRelDiff::value(this->
template elements<Perspective::pod>(), x.template elements<Perspective::pod>(), this->template size<Perspective::pod>());
1061 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1076 if (this->
size() == 0 && x.
size() == 0 && this->get_elements().size() == 0 && x.
get_elements().size() == 0)
1093 if (perm.
size() == 0)
1096 XASSERTM(perm.
size() == this->size(),
"Container size does not match permutation size");
1103 void set_vec(DT_ *
const pval_set)
const
1109 void set_vec_inv(
const DT_ *
const pval_set)
1136 extern template class DenseVector<float, std::uint32_t>;
1137 extern template class DenseVector<double, std::uint32_t>;
1138 extern template class DenseVector<float, std::uint64_t>;
1139 extern template class DenseVector<double, std::uint64_t>;
#define ASSERT(expr)
Debug-Assertion macro definition.
#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
void apply(Tx_ *x, bool invert=false) const
Applies In-Situ permutation.
const std::vector< IT_ * > & get_indices() const
Returns a list of all Index arrays.
bool _foreign_memory
do we use memory that we did not allocate, nor are we allowed to free it - this mostly holds true,...
std::vector< DT_ * > _elements
List of pointers to all datatype dependent arrays.
Index used_elements() const
Returns the number of effective stored elements.
const std::vector< DT_ * > & get_elements() const
Returns a list of all data arrays.
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.
virtual void clear()
Free all allocated arrays.
void format(DT_ value=DT_(0))
Reset all elements of the container to a given value or zero if missing.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Gather-Axpy operation for DenseVector.
Scatter-Axpy operation for DenseVector.
Blocked Dense data vector class template.
Dense data vector class template.
DenseVector(Index size_in, DT_ *data)
Constructor.
void scale(const DenseVector &x, const DT_ alpha)
Calculate .
DT_ min_element() const
Retrieve the minimum value of this vector.
const DT_ operator()(Index index) const
Retrieve specific vector element.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
void convert(const DenseVector< DT2_, IT2_ > &other)
Conversion method.
DenseVector(const DenseVectorBlocked< DT_, IT_, BS_ > &other)
Constructor.
void component_product(const DenseVector &x, const DenseVector &y)
Calculate .
void copy(const DenseVector &x, bool full=false)
Performs .
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
virtual ~DenseVector()
Destructor.
DenseVector & operator=(DenseVector &&other)
Assignment move operator.
DataType dot(const DenseVector &x) const
Calculate .
DataType triple_dot(const DenseVector &x, const DenseVector &y) const
Calculate .
DT_ DataType
Our datatype.
void convert(const VT_ &a)
Conversion method.
void permute(Adjacency::Permutation &perm)
Permutate vector according to the given Permutation.
DT_ * elements()
Get a pointer to the data array.
DT_ ValueType
Our value type.
void convert(const DenseVectorBlocked< DT2_, IT2_, BS2_ > &other)
Conversion method.
bool same_layout(const DenseVector &x) const
Checks if the structural layout of this vector matches that of another vector. This excludes comparis...
IT_ IndexType
Our indextype.
DenseVector()
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Serialization of complete container entity.
DenseVector(FileMode mode, std::istream &file)
Constructor.
DT_ norm2sqr() const
Calculates and returns the squared euclid norm of this vector.
DenseVector(DenseVector &&other)
Move Constructor.
DenseVector(Index size_in, DT_ value)
Constructor.
DT_ max_rel_diff(const DenseVector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
DenseVector(Random &rng, Index size_in, DataType min, DataType max)
Constructor.
DenseVector(Index size_in)
Constructor.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
void copy_inv(VT_ &a) const
Performs .
void component_invert(const DenseVector &x, const DT_ alpha=DT_(1))
Performs .
void axpy(const DenseVector &x, const DT_ alpha=DT_(1))
Calculate .
void operator()(Index index, DT_ value)
Set specific vector element.
static String name()
Returns a descriptive string.
DenseVectorBlocked< DT_, IT_, BlockSize_ > inflate_to_blocks()
Expand DenseVector to DenseVectorBlocked.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
DT_ max_element() const
Retrieve the maximum value of this vector.
void copy(const VT_ &a)
Performs .
DenseVector clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
DenseVector(FileMode mode, String filename)
Constructor.
DenseVector(std::vector< char > input)
Constructor.
DT_ norm2() const
Calculates and returns the euclid norm of this vector.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
friend std::ostream & operator<<(std::ostream &lhs, const DenseVector &b)
DenseVector streaming operator.
void clone(const DenseVector< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
DenseVector(const DenseVector &dv_in, Index size_in, Index offset_in)
Constructor.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
Config class for serialize parameter.
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
Pseudo-Random Number Generator.
static void add_flops(Index flops)
Add an amount of flops to the global flop 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.
T_ sqr(T_ x)
Returns the square of a value.
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.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.