Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
vector.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
34#define GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
35
36
37#include <ginkgo/config.hpp>
38
39
40#if GINKGO_BUILD_MPI
41
42
43#include <ginkgo/core/base/dense_cache.hpp>
44#include <ginkgo/core/base/mpi.hpp>
45#include <ginkgo/core/distributed/base.hpp>
46#include <ginkgo/core/distributed/lin_op.hpp>
47#include <ginkgo/core/matrix/dense.hpp>
48
49
50namespace gko {
51namespace experimental {
52namespace distributed {
53
54
55template <typename LocalIndexType, typename GlobalIndexType>
56class Partition;
57
58
86template <typename ValueType = double>
87class Vector
88 : public EnableDistributedLinOp<Vector<ValueType>>,
89 public EnableCreateMethod<Vector<ValueType>>,
90 public ConvertibleTo<Vector<next_precision<ValueType>>>,
91 public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
92 public DistributedBase {
93 friend class EnableCreateMethod<Vector>;
95 friend class Vector<to_complex<ValueType>>;
96 friend class Vector<remove_complex<ValueType>>;
97 friend class Vector<next_precision<ValueType>>;
98
99public:
100 using EnableDistributedLinOp<Vector>::convert_to;
101 using EnableDistributedLinOp<Vector>::move_to;
104
105 using value_type = ValueType;
106 using absolute_type = remove_complex<Vector>;
107 using real_type = absolute_type;
110
117 static std::unique_ptr<Vector> create_with_config_of(
119
120
132 static std::unique_ptr<Vector> create_with_type_of(
133 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
134
147 static std::unique_ptr<Vector> create_with_type_of(
148 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
149 const dim<2>& global_size, const dim<2>& local_size, size_type stride);
150
167
170
173
185
188
191
192 void convert_to(Vector<next_precision<ValueType>>* result) const override;
193
194 void move_to(Vector<next_precision<ValueType>>* result) override;
195
196 std::unique_ptr<absolute_type> compute_absolute() const override;
197
199
204 std::unique_ptr<complex_type> make_complex() const;
205
212
217 std::unique_ptr<real_type> get_real() const;
218
223
228 std::unique_ptr<real_type> get_imag() const;
229
235
241 void fill(ValueType value);
242
253
264
275
285
296
310 array<char>& tmp) const;
311
323
337 array<char>& tmp) const;
338
348
361
371
384
393
406
416
429
440 value_type& at_local(size_type row, size_type col) noexcept;
441
445 value_type at_local(size_type row, size_type col) const noexcept;
446
461 ValueType& at_local(size_type idx) noexcept;
462
466 ValueType at_local(size_type idx) const noexcept;
467
473 value_type* get_local_values();
474
482 const value_type* get_const_local_values() const;
483
490
498 std::unique_ptr<const real_type> create_real_view() const;
499
503 std::unique_ptr<real_type> create_real_view();
504
505 size_type get_stride() const noexcept { return local_.get_stride(); }
506
517 static std::unique_ptr<const Vector> create_const(
518 std::shared_ptr<const Executor> exec, mpi::communicator comm,
520 std::unique_ptr<const local_vector_type> local_vector);
521
532 static std::unique_ptr<const Vector> create_const(
533 std::shared_ptr<const Executor> exec, mpi::communicator comm,
534 std::unique_ptr<const local_vector_type> local_vector);
535
536protected:
546 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
548
558 explicit Vector(std::shared_ptr<const Executor> exec,
560 dim<2> local_size = {});
561
577 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
578 dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
579
596 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
597 std::unique_ptr<local_vector_type> local_vector);
598
599 void resize(dim<2> global_size, dim<2> local_size);
600
601 template <typename LocalIndexType, typename GlobalIndexType>
602 void read_distributed_impl(
605
606 void apply_impl(const LinOp*, LinOp*) const override;
607
608 void apply_impl(const LinOp*, const LinOp*, const LinOp*,
609 LinOp*) const override;
610
617 virtual std::unique_ptr<Vector> create_with_same_config() const;
618
631 virtual std::unique_ptr<Vector> create_with_type_of_impl(
632 std::shared_ptr<const Executor> exec, const dim<2>& global_size,
633 const dim<2>& local_size, size_type stride) const;
634
635private:
636 local_vector_type local_;
637 ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
638 ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
639};
640
641
642} // namespace distributed
643} // namespace experimental
644
645
646namespace detail {
647
648
649template <typename TargetType>
650struct conversion_target_helper;
651
652
662template <typename ValueType>
663struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
664 using target_type = experimental::distributed::Vector<ValueType>;
665 using source_type =
666 experimental::distributed::Vector<previous_precision<ValueType>>;
667
668 static std::unique_ptr<target_type> create_empty(const source_type* source)
669 {
670 return target_type::create(source->get_executor(),
671 source->get_communicator());
672 }
673};
674
675
676} // namespace detail
677} // namespace gko
678
679
680#endif // GINKGO_BUILD_MPI
681
682
683#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:499
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:823
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
Definition lin_op.hpp:146
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition lin_op.hpp:73
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition polymorphic_object.hpp:82
A base class for distributed objects.
Definition base.hpp:60
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition partition.hpp:112
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition vector.hpp:92
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.
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...
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.
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
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:437
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition dense.hpp:875
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
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:354
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
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:373
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155