Ginkgo Generated from branch based on main. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
vector.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
6#define GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
7
8
9#include <ginkgo/config.hpp>
10
11
12#if GINKGO_BUILD_MPI
13
14
15#include <ginkgo/core/base/dense_cache.hpp>
16#include <ginkgo/core/base/mpi.hpp>
17#include <ginkgo/core/distributed/base.hpp>
18#include <ginkgo/core/distributed/lin_op.hpp>
19#include <ginkgo/core/matrix/dense.hpp>
20
21
22namespace gko {
23namespace experimental {
24namespace distributed {
25namespace detail {
26
27
28template <typename ValueType>
29class VectorCache;
30
31
32} // namespace detail
33
34
35template <typename LocalIndexType, typename GlobalIndexType>
37
38
66template <typename ValueType = double>
67class Vector
68 : public EnableDistributedLinOp<Vector<ValueType>>,
69 public ConvertibleTo<Vector<next_precision_base<ValueType>>>,
70 public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
71 public DistributedBase {
73 friend class Vector<to_complex<ValueType>>;
74 friend class Vector<remove_complex<ValueType>>;
75 friend class Vector<next_precision_base<ValueType>>;
76 friend class detail::VectorCache<ValueType>;
77
78public:
79 using EnableDistributedLinOp<Vector>::convert_to;
80 using EnableDistributedLinOp<Vector>::move_to;
83
84 using value_type = ValueType;
85 using absolute_type = remove_complex<Vector>;
86 using real_type = absolute_type;
87 using complex_type = Vector<to_complex<value_type>>;
88 using local_vector_type = gko::matrix::Dense<value_type>;
89
96 static std::unique_ptr<Vector> create_with_config_of(
98
99
111 static std::unique_ptr<Vector> create_with_type_of(
112 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
113
126 static std::unique_ptr<Vector> create_with_type_of(
127 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
128 const dim<2>& global_size, const dim<2>& local_size, size_type stride);
129
145 ptr_param<const Partition<int64, int64>> partition);
146
148 ptr_param<const Partition<int32, int64>> partition);
149
151 ptr_param<const Partition<int32, int32>> partition);
152
163 ptr_param<const Partition<int64, int64>> partition);
164
166 ptr_param<const Partition<int32, int64>> partition);
167
169 ptr_param<const Partition<int32, int32>> partition);
170
171 void convert_to(
172 Vector<next_precision_base<ValueType>>* result) const override;
173
174 void move_to(Vector<next_precision_base<ValueType>>* result) override;
175
176 std::unique_ptr<absolute_type> compute_absolute() const override;
177
179
184 std::unique_ptr<complex_type> make_complex() const;
185
192
197 std::unique_ptr<real_type> get_real() const;
198
202 void get_real(ptr_param<real_type> result) const;
203
208 std::unique_ptr<real_type> get_imag() const;
209
214 void get_imag(ptr_param<real_type> result) const;
215
221 void fill(ValueType value);
222
233
244
255
265
276
290 array<char>& tmp) const;
291
302 ptr_param<LinOp> result) const;
303
317 array<char>& tmp) const;
318
328
341
350 void compute_norm2(ptr_param<LinOp> result) const;
351
364
372 void compute_norm1(ptr_param<LinOp> result) const;
373
386
395 void compute_mean(ptr_param<LinOp> result) const;
396
408 void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
409
420 value_type& at_local(size_type row, size_type col) noexcept;
421
425 value_type at_local(size_type row, size_type col) const noexcept;
426
441 ValueType& at_local(size_type idx) noexcept;
442
446 ValueType at_local(size_type idx) const noexcept;
447
453 value_type* get_local_values();
454
462 const value_type* get_const_local_values() const;
463
470
478 std::unique_ptr<const real_type> create_real_view() const;
479
483 std::unique_ptr<real_type> create_real_view();
484
485 size_type get_stride() const noexcept { return local_.get_stride(); }
486
498 static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
500 dim<2> global_size, dim<2> local_size,
501 size_type stride);
502
514 static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
516 dim<2> global_size = {},
517 dim<2> local_size = {});
518
536 static std::unique_ptr<Vector> create(
537 std::shared_ptr<const Executor> exec, mpi::communicator comm,
538 dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
539
558 static std::unique_ptr<Vector> create(
559 std::shared_ptr<const Executor> exec, mpi::communicator comm,
560 std::unique_ptr<local_vector_type> local_vector);
561
574 static std::unique_ptr<const Vector> create_const(
575 std::shared_ptr<const Executor> exec, mpi::communicator comm,
576 dim<2> global_size,
577 std::unique_ptr<const local_vector_type> local_vector);
578
591 static std::unique_ptr<const Vector> create_const(
592 std::shared_ptr<const Executor> exec, mpi::communicator comm,
593 std::unique_ptr<const local_vector_type> local_vector);
594
595protected:
596 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
597 dim<2> global_size, dim<2> local_size, size_type stride);
598
599 explicit Vector(std::shared_ptr<const Executor> exec,
600 mpi::communicator comm, dim<2> global_size = {},
601 dim<2> local_size = {});
602
603 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
604 dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
605
606 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
607 std::unique_ptr<local_vector_type> local_vector);
608
609 void resize(dim<2> global_size, dim<2> local_size);
610
611 template <typename LocalIndexType, typename GlobalIndexType>
612 void read_distributed_impl(
614 const Partition<LocalIndexType, GlobalIndexType>* partition);
615
616 void apply_impl(const LinOp*, LinOp*) const override;
617
618 void apply_impl(const LinOp*, const LinOp*, const LinOp*,
619 LinOp*) const override;
620
627 virtual std::unique_ptr<Vector> create_with_same_config() const;
628
641 virtual std::unique_ptr<Vector> create_with_type_of_impl(
642 std::shared_ptr<const Executor> exec, const dim<2>& global_size,
643 const dim<2>& local_size, size_type stride) const;
644
645private:
646 local_vector_type local_;
647 ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
648 ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
649};
650
651
652} // namespace distributed
653} // namespace experimental
654
655
656namespace detail {
657
658
659template <typename TargetType>
660struct conversion_target_helper;
661
662
672template <typename ValueType>
673struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
674 using target_type = experimental::distributed::Vector<ValueType>;
675 using source_type =
676 experimental::distributed::Vector<previous_precision_base<ValueType>>;
677
678 static std::unique_ptr<target_type> create_empty(const source_type* source)
679 {
680 return target_type::create(source->get_executor(),
681 source->get_communicator());
682 }
683};
684
685
686} // namespace detail
687} // namespace gko
688
689
690#endif // GINKGO_BUILD_MPI
691
692
693#endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:470
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:794
Definition lin_op.hpp:117
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition lin_op.hpp:44
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition polymorphic_object.hpp:53
A base class for distributed objects.
Definition base.hpp:32
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition vector.hpp:36
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition dense.hpp:29
value_type at_local(size_type row, size_type col) const noexcept
void compute_mean(ptr_param< LinOp > result) const
Computes the column-wise mean of this (multi-)vector using a global reduction.
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, dim< 2 > local_size, size_type stride)
Creates an empty distributed vector with a specified size.
void compute_norm2(ptr_param< LinOp > result) const
Computes the Euclidean (L^2) norm of this (multi-)vector using a global reduction.
void read_distributed(const matrix_data< ValueType, int64 > &data, ptr_param< const Partition< int64, int64 > > partition)
Reads a vector from the matrix_data structure and a global row partition.
void make_complex(ptr_param< complex_type > result) const
Writes a complex copy of the original vectors to given complex vectors.
std::unique_ptr< real_type > create_real_view()
Create a real view of the (potentially) complex original multi-vector.
void compute_squared_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the square of the column-wise Euclidean ( ) norm of this (multi-)vector using a global reduc...
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size={}, dim< 2 > local_size={})
Creates an empty distributed vector with a specified size.
std::unique_ptr< real_type > get_real() const
Creates new real vectors and extracts the real part of the original vectors into that.
static std::unique_ptr< const Vector > create_const(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, std::unique_ptr< const local_vector_type > local_vector)
Creates a constant (immutable) distributed Vector from a constant local vector.
std::unique_ptr< const real_type > create_real_view() const
Create a real view of the (potentially) complex original multi-vector.
void fill(ValueType value)
Fill the distributed vectors with a given value.
static std::unique_ptr< Vector > create_with_type_of(ptr_param< const Vector > other, std::shared_ptr< const Executor > exec, const dim< 2 > &global_size, const dim< 2 > &local_size, size_type stride)
Creates an Vector with the same type as another Vector, but on a different executor and with a differ...
static std::unique_ptr< Vector > create_with_config_of(ptr_param< const Vector > other)
Creates a distributed Vector with the same size and stride as another Vector.
value_type & at_local(size_type row, size_type col) noexcept
Returns a single element of the multi-vector.
value_type * get_local_values()
Returns a pointer to the array of local values of the multi-vector.
void compute_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the Euclidean (L^2) norm of this (multi-)vector using a global reduction.
const value_type * get_const_local_values() const
Returns a pointer to the array of local values of the multi-vector.
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this (multi-)vector and conj(b) using a global reduction.
void compute_absolute_inplace() override
Compute absolute inplace on each element.
void get_real(ptr_param< real_type > result) const
Extracts the real part of the original vectors into given real vectors.
void compute_squared_norm2(ptr_param< LinOp > result) const
Computes the square of the column-wise Euclidean ( ) norm of this (multi-)vector using a global reduc...
void sub_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Subtracts b scaled by alpha from the vectors (aka: BLAS axpy).
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this (multi-)vector and b using a global reduction.
static std::unique_ptr< const Vector > create_const(std::shared_ptr< const Executor > exec, mpi::communicator comm, std::unique_ptr< const local_vector_type > local_vector)
Creates a constant (immutable) distributed Vector from a constant local vector.
void compute_mean(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise arithmetic mean of this (multi-)vector using a global reduction.
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of the original vectors.
void compute_norm1(ptr_param< LinOp > result) const
Computes the column-wise (L^1) norm of this (multi-)vector.
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, std::unique_ptr< local_vector_type > local_vector)
Creates a distributed vector from local vectors with a specified size.
void get_imag(ptr_param< real_type > result) const
Extracts the imaginary part of the original vectors into given real vectors.
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of this (multi-)vector and b using a global reduction.
std::unique_ptr< real_type > get_imag() const
Creates new real vectors and extracts the imaginary part of the original vectors into that.
void inv_scale(ptr_param< const LinOp > alpha)
Scales the vectors with the inverse of a scalar.
void add_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Adds b scaled by alpha to the vectors (aka: BLAS axpy).
void scale(ptr_param< const LinOp > alpha)
Scales the vectors with a scalar (aka: BLAS scal).
void read_distributed(const device_matrix_data< ValueType, int64 > &data, ptr_param< const Partition< int64, int64 > > partition)
Reads a vector from the device_matrix_data structure and a global row partition.
const local_vector_type * get_local_vector() const
Direct (read) access to the underlying local local_vector_type vectors.
static std::unique_ptr< Vector > create_with_type_of(ptr_param< const Vector > other, std::shared_ptr< const Executor > exec)
Creates an empty Vector with the same type as another Vector, but on a different executor.
ValueType & at_local(size_type idx) noexcept
Returns a single element of the multi-vector.
void compute_norm1(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise (L^1) norm of this (multi-)vector using a global reduction.
ValueType at_local(size_type idx) const noexcept
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, std::unique_ptr< local_vector_type > local_vector)
Creates a distributed vector from local vectors.
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of this (multi-)vector and conj(b) using a global reduction.
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition mpi.hpp:408
Dense is a matrix format which explicitly stores all values of the matrix.
Definition sparsity_csr.hpp:25
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition dense.hpp:870
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
The Ginkgo namespace.
Definition abstract_factory.hpp:20
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:260
typename detail::next_precision_base_impl< T >::type next_precision_base
Obtains the next type in the singly-linked precision list.
Definition math.hpp:421
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition math.hpp:279
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126