9#include <kernel/lafem/arch/batched_vector_apply.hpp>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/lafem/dense_vector_blocked.hpp>
13#include <kernel/lafem/tuple_vector.hpp>
14#include <kernel/global/gate.hpp>
15#include <kernel/global/vector.hpp>
17#include <kernel/util/math.hpp>
26 template<
typename VectorType_>
49 template<
typename Vector_>
53 typedef Vector_ VectorType;
54 typedef typename Vector_::DataType DataType;
69 for(std::size_t n = 0; n <= krylov_dimension; ++n)
100 virtual void correct_vector(VectorType& target,
const DataType* local_scales,
const Index num_axpy,
const DataType alpha)
const
103 for(
Index k = 0; k < num_axpy; ++k)
105 target.axpy(
_subspace[k], alpha * local_scales[k]);
130 VectorType& at(
const Index idx)
135 const VectorType& at(
const Index idx)
const
144 template<
typename Vector_>
149 typedef typename BaseClass::VectorType VectorType;
150 typedef typename BaseClass::DataType DataType;
171 for(
Index k(0); k < target_index; ++k)
173 const auto dot = this->_subspace[target_index].dot(this->_subspace[k]);
174 this->_subspace[target_index].axpy(this->_subspace[k], -dot);
175 dot_products[k] = dot;
177 return this->_subspace[target_index].norm2();
196 template<
typename Vector_>
201 typedef typename BaseClass::VectorType VectorType;
202 typedef typename BaseClass::DataType DataType;
223 void _compute_dots(
const VectorType& target,
const Index num_basis, DataType* dot_out, DataType& self_dot)
234 const DataType* dp = dv.
get_r();
235 for(
Index k(0); k < num_basis; ++k)
237 self_dot = dp[num_basis];
248 return target.norm2();
256 BaseClass(vec_ref, krylov_dimension)
263 std::vector<const VectorType*> vec_v_ptrs;
264 vec_v_ptrs.reserve(this->_subspace.size());
265 for(
const auto& v : this->_subspace)
266 vec_v_ptrs.push_back(&v);
270 ClassicGramSchmidtOrthogonalizer(
const ClassicGramSchmidtOrthogonalizer&) =
delete;
271 ClassicGramSchmidtOrthogonalizer& operator=(
const ClassicGramSchmidtOrthogonalizer&) =
delete;
272 ClassicGramSchmidtOrthogonalizer(ClassicGramSchmidtOrthogonalizer&&) =
default;
273 ClassicGramSchmidtOrthogonalizer& operator=(ClassicGramSchmidtOrthogonalizer&&) =
default;
275 virtual ~ClassicGramSchmidtOrthogonalizer()=
default;
281 const Index num_basis = target_index;
282 VectorType& target = this->_subspace[target_index];
285 DataType self_dot(0);
289 for(
Index k(0); k < num_basis; ++k)
293 this->
correct_vector(target, dot_products, num_basis, DataType(-1));
302 void correct_vector(VectorType& target,
const DataType* local_scales,
const Index num_axpy,
const DataType alpha)
const override
306 target,
_vec_arbiter.data(), local_scales, num_axpy, alpha, this->_subspace.size());
325 template<
typename Vector_>
330 typedef typename BaseClass::VectorType VectorType;
331 typedef typename BaseClass::DataType DataType;
348 _corr.resize(krylov_dimension);
371 DataType orthogonalize(DataType* dot_products,
const Index target_index)
override
374 const Index num_basis = target_index;
375 VectorType& target = this->_subspace[target_index];
378 DataType self_dot(0);
379 this->
_compute_dots(target, num_basis, dot_products, self_dot);
381 for(
Index k(0); k < num_basis; ++k)
383 this->
correct_vector(target, dot_products, num_basis, DataType(-1));
384 DataType nrm_sqr = self_dot - sum_sqr;
391 DataType self_dot2(0);
393 DataType sum_sqr2(0);
394 for(
Index k(0); k < num_basis; ++k)
396 dot_products[k] +=
_corr[k];
400 nrm_sqr = self_dot2 - sum_sqr2;
414 template<
typename VectorType_>
438 template<
typename DT_>
456 template<
typename DT_>
459 template<
typename DT_>
460 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter, VectorType_
const *
const * vectors,
Index size);
464 template<
typename LocalVector_,
typename Mirror_>
470 template<
typename DT_>
487 DT_* dots_ptr = dots_view.
get_w();
496 template<
typename DT_>
503 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter,
VectorType const *
const * vectors,
Index size)
507 std::vector<const LocalVector_*> locals;
508 locals.reserve(size);
509 for(
Index k = 0; k < size; ++k)
511 locals.push_back(&vectors[k]->local());
517 template<
typename First_,
typename... Rest_>
523 template<
typename DT_>
534 template<
typename DT_>
545 template<
typename DT_>
552 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter,
VectorType const *
const * vectors,
Index size)
555 std::vector<const First_*> firsts;
556 firsts.reserve(size);
557 for(
Index k = 0; k < size; ++k)
559 firsts.push_back(&vectors[k]->first());
566 for(
Index k = 0; k < size; ++k)
568 rests.push_back(&vectors[k]->
rest());
575 template<
typename First_>
581 template<
typename DT_>
589 template<
typename DT_>
597 template<
typename DT_>
603 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter,
VectorType const *
const * vectors,
Index size)
606 std::vector<const First_*> firsts;
607 firsts.reserve(size);
608 for(
Index k = 0; k < size; ++k)
610 firsts.push_back(&vectors[k]->first());
617 template<
typename DTT_,
typename ITT_>
623 template<
typename DT_>
628 LAFEM::Arch::BatchedDotProduct::exec<DT_, DTT_>(local_dots, target.
elements_arbiter(), target.
size(), vectors, size);
631 template<
typename DT_>
639 template<
typename DT_>
644 LAFEM::Arch::CumulativeAxpy::exec<DT_>(target.
elements_arbiter(), target.
size(), vectors, local_scales, size, alpha);
647 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter,
VectorType const *
const * vectors,
Index size)
649 for(
Index k = 0; k < size; ++k)
651 arbiter.emplace_back(&vectors[k]->elements_arbiter());
656 template<
typename DTT_,
typename ITT_,
int dim_>
662 template<
typename DT_>
668 LAFEM::Arch::BatchedDotProduct::exec<DT_, DTT_>(local_dots, target.
elements_arbiter(), target.
size_raw(), vectors, size);
671 template<
typename DT_>
680 template<
typename DT_>
685 LAFEM::Arch::CumulativeAxpy::exec<DT_>(target.
elements_arbiter(), target.
size_raw(), vectors, local_scales, size, alpha);
688 static void gather_arbiter(std::vector<Memory::Arbiter const*>& arbiter,
VectorType const *
const * vectors,
Index size)
690 for(
Index k = 0; k < size; ++k)
692 arbiter.emplace_back(&vectors[k]->elements_arbiter());
#define XASSERT(expr)
Assertion macro definition.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
int size() const
Returns the size of this communicator.
const LocalVector_ & get_freqs() const
Returns a const reference to the frequencies vector.
Global vector wrapper class template.
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gate of the vector.
const GateType * get_gate() const
Returns a const pointer to the internal gate of the vector.
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Blocked Dense data vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Index size_raw() const
Returns the size of this vector in scalar data type entries, i.e. its number of entries multiplied by...
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.
Variadic TupleVector class template.
Math Limits class template.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
void format(Location mem_loc=Location::none)
Formats the memory block to zero.
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.
Classical Gram-Schmidt with a (conditional) reorthogonalization sweep (CGS2)
std::vector< DataType > _corr
scratch for the corrective sweep's dot products, kept separate from the accumulated h[k]
void set_reortho_tol(DataType tol)
Sets the DGKS norm-ratio threshold that triggers a corrective reorthogonalization sweep.
void set_force_reortho(bool force)
Forces (or unforces) a corrective reorthogonalization sweep in every step.
bool _force_reortho
if set, always perform the corrective reorthogonalization sweep regardless of the criterion
Abstraction of the classical Gram-Schmidt procedure with a batched reduction.
DataType orthogonalize(DataType *dot_products, const Index target_index) override
Orthogonalizes v_[ti] against v_0,...v_{ti-1}.
void correct_vector(VectorType &target, const DataType *local_scales, const Index num_axpy, const DataType alpha) const override
Batched cumulative axpy: target += alpha * sum_k local_scales[k] * v[k].
std::vector< Memory::Arbiter const * > _vec_arbiter
flattened element arbiters of the persistent basis vectors, consumed by the batched kernels
void _compute_dots(const VectorType &target, const Index num_basis, DataType *dot_out, DataType &self_dot)
Computes the batched dot products of target against the leading basis vectors.
DataType _finalize_norm(const DataType nrm_sqr, const VectorType &target) const
Recovers the orthogonalized norm from the algebraic identity with a norm2 fallback.
static void batched_dot(Memory::Arbiter &local_dots, const VectorType_ &target, Memory::Arbiter const *const *vectors, Index size, Index stride)
Calculates the dot products of target with all members of vectors in a batched manner.
static void cumulative_axpy(VectorType_ &target, Memory::Arbiter const *const *vectors, DT_ const *const local_scales, Index size, DT_ alpha, Index stride)
Calculates the commulative axpy onto target with all members of vectors in a batched manner.
Base class for orthogonlizing a vector of a Krylov subspace.
virtual void correct_vector(VectorType &target, const DataType *local_scales, const Index num_axpy, const DataType alpha) const
Performs the commulative sum on vector target with the internal vector array.
virtual DataType orthogonalize(DataType *dot_products, const Index target_index)=0
Orthogonalizes v_[ti] against v_0,...v_{ti-1}.
Index get_krylov_dim() const
The krylov dimension.
std::vector< VectorType > & get_subspace()
Get a reference to the subspace vectors.
std::vector< VectorType > _subspace
The component holds the krylov subspace (and potentially additonal helper vectors)
Index _krylov_dimension
The Krylov dimension.
Simple abstraction of the modified Gram Schmidt procedure.
DataType orthogonalize(DataType *dot_products, const Index target_index) override
Orthogonalizes v_[ti] against v_0,...v_{ti-1}.
LAFEM common type definitions.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
T_ sqrt(T_ x)
Returns the square-root of a value.
T_ sqr(T_ x)
Returns the square of a value.
@ read_write
read-write access
@ rest
restriction (multigrid)
KrylovOrthogonalizerType
Enumeration of the available Krylov subspace orthogonalization strategies.
std::uint64_t Index
Index data type.