12#include <kernel/util/math.hpp>
13#include <kernel/util/random.hpp>
14#include <kernel/util/statistics.hpp>
15#include <kernel/util/time_stamp.hpp>
16#include <kernel/util/tiny_algebra.hpp>
17#include <kernel/util/type_traits.hpp>
18#include <kernel/adjacency/permutation.hpp>
19#include <kernel/lafem/forward.hpp>
20#include <kernel/lafem/container.hpp>
21#include <kernel/lafem/arch/axpy_block.hpp>
22#include <kernel/lafem/arch/axpy_dense.hpp>
23#include <kernel/lafem/arch/component_invert_dense.hpp>
24#include <kernel/lafem/arch/component_product_dense.hpp>
25#include <kernel/lafem/arch/copy_stride.hpp>
26#include <kernel/lafem/arch/dot_product_block.hpp>
27#include <kernel/lafem/arch/dot_product_dense.hpp>
28#include <kernel/lafem/arch/max_rel_diff_dense.hpp>
29#include <kernel/lafem/arch/min_max_index_dense.hpp>
30#include <kernel/lafem/arch/min_max_value_block.hpp>
31#include <kernel/lafem/arch/min_max_value_dense.hpp>
32#include <kernel/lafem/arch/norm2_dense.hpp>
33#include <kernel/lafem/arch/norm2_block.hpp>
34#include <kernel/lafem/arch/scale_dense.hpp>
35#include <kernel/lafem/arch/scale_block.hpp>
36#include <kernel/lafem/arch/triple_dot_product_dense.hpp>
37#include <kernel/lafem/arch/triple_dot_product_block.hpp>
65 template <
typename DT_,
typename IT_,
int block_size_>
79 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
83 template <
typename DataType2_,
typename IndexType2_>
110 if(size_in >
Index(0))
144 if(size_in >
Index(0))
146 this->
_elements.push_back(std::forward<Memory::Arbiter>(data));
177 XASSERTM(size_in + offset_in <= dv_in.
size(),
"Ranged vector part exceeds original vector size!");
179 const std::size_t vt_size =
sizeof(DT_) * std::size_t(block_size_);
180 this->
_elements.push_back(dv_in.
_elements.front().attach(offset_in * vt_size, size_in * vt_size));
217 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
220 this->deserialize<DT2_, IT2_>(input);
236 this->
format(rng, min, max);
267 this->
move(std::forward<DenseVectorBlocked>(other));
282 t.
clone(*
this, clone_mode);
294 template<
typename DT2_,
typename IT2_>
307 template <
typename DT2_,
typename IT2_>
320 template <
typename DT2_,
typename IT2_>
323 XASSERTM(other.size() %
Index(block_size_) == 0,
"DenseVector cannot be converted to given blocksize!");
334 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
353 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
406 return Memory::TypedView<ValueType>();
413 return Memory::TypedView<ValueType>();
414 return Memory::TypedView<ValueType>(this->
_elements.at(0).view(loc, acc));
420 return Memory::TypedView<DT_>();
427 return Memory::TypedView<DT_>();
434 return Memory::TypedView<DT_>();
441 return Memory::TypedView<DT_>();
442 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
452 return "DenseVectorBlocked";
463 this->_copy_content(x, full);
474 this->_copy_content(x, full);
485 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
487 bin = std::ifstream::in;
488 std::ifstream file(filename.c_str(), bin);
489 if (! file.is_open())
490 XABORTM(
"Unable to open Vector file " + filename);
512 std::getline(file, line);
513 if (line.find(
"%%MatrixMarket matrix array real general") == String::npos)
515 XABORTM(
"Input-file is not a compatible mtx-vector-file");
519 std::getline(file, line);
521 XABORTM(
"Input-file is empty");
523 String::size_type begin(line.find_first_not_of(
" "));
524 if (line.at(begin) !=
'%')
528 String::size_type begin(line.find_first_not_of(
" "));
529 line.erase(0, begin);
530 String::size_type end(line.find_first_of(
" "));
531 String srows(line, 0, end);
532 rows = (
Index)atol(srows.c_str());
535 begin = line.find_first_not_of(
" ");
536 line.erase(0, begin);
537 end = line.find_first_of(
" ");
538 String scols(line, 0, end);
542 XABORTM(
"Input-file is no dense-vector-file");
547 DT_* pval = tmp_view.
get_w();
551 std::getline(file, line);
555 String::size_type begin(line.find_first_not_of(
" "));
556 line.erase(0, begin);
557 String::size_type end(line.find_first_of(
" "));
558 String sval(line, 0, end);
559 DT_ tval((DT_)atof(sval.c_str()));
565 this->
move(std::move(tmp));
573 std::vector<DT_> data;
578 std::getline(file, line);
579 if (line.find(
"#", 0) < line.npos)
586 std::string::size_type first_digit(line.find_first_not_of(
" "));
587 line.erase(0, first_digit);
588 std::string::size_type eol(line.length());
589 for (std::string::size_type i(0); i < eol; ++i)
591 n_z_s.append(1, line[i]);
594 DT_ n_z((DT_)atof(n_z_s.c_str()));
605 this->
move(std::move(tmp));
621 XABORTM(
"Filemode not supported!");
633 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
635 bin = std::ofstream::out;
637 char* buff =
nullptr;
643 file.open(filename.c_str(), bin);
645 XABORTM(
"Unable to open Matrix file " + filename);
664 file <<
"%%MatrixMarket matrix array real general\n";
665 file << tsize <<
" " << 1 <<
"\n";
669 for (
Index i(0); i < tsize; ++i)
671 file << elem_view(i) <<
"\n";
681 for (
Index i(0); i < tsize; ++i)
683 file << elem_view(i) <<
"\n";
696 XABORTM(
"Filemode not supported!");
711 const DT_ alpha = DT_(1))
713 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
719 FEAT_KERNEL_MARKER_START(
"DV_axpy");
723 FEAT_KERNEL_MARKER_STOP(
"DV_axpy");
727 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
741 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
747 FEAT_KERNEL_MARKER_START(
"DV_axpy");
751 FEAT_KERNEL_MARKER_STOP(
"DV_axpy");
755 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
779 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
790 XASSERTM(block_ >= 0,
"Block index has to be a positive integer!");
804 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
815 XASSERTM(block_ >= 0,
"Block index has to be a positive integer!");
824 Arch::CopyStride::template exec<DT_>(x.
elements_arbiter(), this->elements_arbiter(), 1, 0, block_size_, block_, x.
size());
829 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
855 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
866 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
878 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
889 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
901 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
926 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
942 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
943 XASSERTM(y.
size() == this->size(),
"Vector size does not match!");
953 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
967 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
977 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
991 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
1001 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1022 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1043 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1067 for(
int i = 0; i < block_size_; ++i)
1068 result[i] *= result[i];
1086 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1105 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1124 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1143 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1162 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1181 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1200 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1219 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1232 XASSERTM(x.
size() == this->size(),
"vector size mismatch!");
1240 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1266 XASSERTM(perm.
size() == this->size(),
"Container size does not match permutation size");
1271 perm.
apply(view.get_w());
1283 Index get_values(DT_ *
const pval_set)
const
1297 Index set_values(
const DT_ *
const pval_set)
1315 for (
Index i(0); i < n; ++i)
1317 lhs <<
" " << view[i];
1338 const IT_ _num_entries;
1343 _num_entries(IT_(vector.
size())),
1348 template <
typename LocalVector_,
typename Mapping_>
1349 void operator()(
const LocalVector_ & loc_vec,
const Mapping_ & mapping, DT_ alpha = DT_(1))
1352 for (
int i(0); i < mapping.get_num_local_dofs(); ++i)
1355 Index dof_idx = mapping.get_index(i);
1356 ASSERT(dof_idx < _num_entries);
1359 _data_view[dof_idx] += alpha * loc_vec[i];
1378 const Index _num_entries;
1383 _num_entries(vector.
size()),
1384 _data_view(vector.elements_view_r())
1388 template <
typename LocalVector_,
typename Mapping_>
1389 void operator()(LocalVector_ & loc_vec,
const Mapping_ & mapping, DT_ alpha = DT_(1))
1392 for (
int i(0); i < mapping.get_num_local_dofs(); ++i)
1395 Index dof_idx = mapping.get_index(i);
1396 ASSERT(dof_idx < _num_entries);
1399 loc_vec[i].axpy(alpha, _data_view(dof_idx));
#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
bool empty() const
Checks whether the permutation is empty.
void apply(Tx_ *x, bool invert=false) const
Applies In-Situ permutation.
void format()
Reset all elements of the container to a given value or zero if missing.
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.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
virtual void clear()
Free all allocated arrays.
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Gather-Axpy operation for DenseVectorBlocked.
Scatter-Axpy operation for DenseVectorBlocked.
Blocked Dense data vector class template.
DenseVectorBlocked(FileMode mode, std::istream &file)
Constructor.
bool same_layout(const DenseVectorBlocked &x) const
Checks if the structural layout of this vector matches that of another vector. This excludes comparis...
DenseVectorBlocked(Index size_in)
Constructor.
DenseVectorBlocked(DenseVectorBlocked &&other)
Move Constructor.
DenseVectorBlocked(const DenseVector< DT_, IT_ > &other)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Serialization of complete container entity.
void clone(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
void component_copy_to(DenseVector< DT_, IT_ > &x, const int block_) const
Copies the elements of a specific block of this blocked vector into a dense vector.
IT_ IndexType
Our indextype.
void scale(const DenseVectorBlocked &x, const DT_ alpha)
Calculate .
DT_ min_element() const
Retrieve the minimum value of this vector.
DataType dot(const DenseVectorBlocked &x) const
Calculate .
ValueType min_abs_element_blocked() const
Retrieve the absolute minimum value of this vector.
static String name()
Returns a descriptive string.
DenseVectorBlocked(const DenseVectorBlocked &dv_in, Index size_in, Index offset_in)
Constructor.
void copy(const DenseVectorBlocked &x, bool full=false)
Performs .
DenseVectorBlocked(std::vector< char > input)
Constructor.
void axpy(const DenseVectorBlocked &x, const DT_ alpha=DT_(1))
Calculate .
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
void component_product(const DenseVectorBlocked &x, const DenseVectorBlocked &y)
Calculate .
DataType triple_dot(const DenseVectorBlocked &x, const DenseVectorBlocked &y) const
Calculate .
static constexpr int block_size
Our size of a single block.
Index size() const
Returns the size of this vector, i.e. its number of entries.
DenseVectorBlocked(FileMode mode, const String &filename)
Constructor.
void component_copy(const DenseVector< DT_, IT_ > &x, const int block_)
Copies the elements of a dense vector into a specific block of this blocked vector.
void convert(const DenseVector< DT2_, IT2_ > &other)
Conversion method.
const Memory::Arbiter & elements_arbiter() const
Returns a reference to the element array arbiter.
DT_ norm2() const
Calculates and returns the euclidean norm of this vector.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
ValueType dot_blocked(const DenseVectorBlocked &x) const
Calculate .
DT_ max_rel_diff(const DenseVectorBlocked &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
void convert(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &other)
Conversion method.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
void permute(Adjacency::Permutation &perm)
Permute vector according to the given Permutation.
bool empty() const
Checks whether the vector is empty, i.e. if it has size 0.
ValueType max_abs_element_blocked() const
Retrieve the absolute maximum value of this vector.
ValueType min_element_blocked() const
Retrieve the minimum value of this vector.
DT_ DataType
Our datatype.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
Index size_raw() const
Returns the size of this vector in scalar data type entries, i.e. its number of entries multiplied by...
Tiny::Vector< DT_, block_size_ > ValueType
Our value type.
virtual ~DenseVectorBlocked()=default
Destructor.
friend std::ostream & operator<<(std::ostream &lhs, const DenseVectorBlocked &b)
DenseVectorBlocked streaming operator.
DenseVectorBlocked(Index size_in, DT_ value)
Constructor.
void axpy_blocked(const DenseVectorBlocked &x, const ValueType alpha)
Calculate .
ValueType triple_dot_blocked(const DenseVectorBlocked &x, const DenseVectorBlocked &y) const
Calculate .
void copy(const DenseVector< DT_, IT_ > &x, bool full=false)
Performs .
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
ValueType norm2sqr_blocked() const
Calculates and returns the squared euclidean norm of this vector.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
DenseVectorBlocked(Index size_in, Memory::Arbiter &&data)
Constructor.
void scale_blocked(const DenseVectorBlocked &x, const ValueType alpha)
Calculate .
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
DT_ norm2sqr() const
Calculates and returns the squared euclidean norm of this vector.
DT_ max_element() const
Retrieve the maximum value of this vector.
DenseVectorBlocked(Random &rng, Index size_in, DataType min, DataType max)
Constructor.
ValueType max_element_blocked() const
Retrieve the maximum value of this vector.
DenseVectorBlocked()=default
Constructor.
DenseVectorBlocked & operator=(DenseVectorBlocked &&other)
Assignment move operator.
ValueType norm2_blocked() const
Calculates and returns the euclidean norm of this vector.
void component_invert(const DenseVectorBlocked &x, const DT_ alpha=DT_(1))
Performs .
DenseVectorBlocked clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
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.
void convert_from(const T_ *ptr_src)
overload of convert_from for the identical type
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void release() const
Releases this view.
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.
Tiny Vector class template.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
T_ sqr(T_ x)
Returns the square of a value.
Access
Memory access enumeration.
@ read_write
read-write access
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.