KokkosBlas::sum

Defined in header: KokkosBlas1_sum.hpp

template <class execution_space, class XVector>
requires Kokkos::is_execution_space_v<execution_space>
typename XVector::non_const_value_type sum(const execution_space& space, const XVector& x);

template <class XVector>
typename XVector::non_const_value_type sum(const XVector& x);

template <class execution_space, class RV, class XMV>
void sum(const execution_space& space, const RV& R, const XMV& X);

template <class RV, class XMV>
requires Kokkos::is_view_v<RV>
void sum(const RV& R, const XMV& X);

computes the sum of the elements in vector(s) X and stores the result in R. When the sum is returned by the function, the function is blocking and will complete before returning.

\[R_i = \sum_j X_{ij}\]
  1. Computes the sum of the entries in X using space instance and returns them.

  2. Computes the sum of the entries in X using the default instance of typename XVector::execution_space and returns them.

  3. Computes the sum of the entries in X column wise and store the results in R using space instance.

  4. Computes the sum of the entries in X column wise and store the results in R using the default instance of typename XVector::execution_space.

  1. and 4. will throw a runtime exception if X.extent(1) != R.extent(0) when X has rank 2.

Parameters

space:

execution space instance

R:

output vector storing the sum along the column(s) of X

X:

input vector(s)

Type Requirements

  • execution_space must be a Kokkos execution space

  • XMV must be a Kokkos View of rank 1 or 2, that satisfies

    • Kokkos::SpaceAccessibility<execution_space, typename XMV::memory_space>::accessible

  • RV must be a Kokkos View of rank 0 or 1, that satisfies

    • !std::is_const_v<typename RV::value_type>

    • RV::rank == XMV::rank - 1

Example

 1#include <iostream>
 2#include <Kokkos_Core.hpp>
 3#include <KokkosBlas1_sum.hpp>
 4
 5int main(void) {
 6  Kokkos::initialize();
 7  {
 8    Kokkos::View<double*> x("x", 5);
 9    auto h_x = Kokkos::create_mirror_view(x);
10    h_x(0)   = 0.0;
11    h_x(1)   = -1.0;
12    h_x(2)   = -2.0;
13    h_x(3)   = 3.0;
14    h_x(4)   = -4.0;
15    Kokkos::deep_copy(x, h_x);
16
17    std::cout << "orignal values:\n"
18              << "   x: {" << h_x(0) << ", " << h_x(1) << ", " << h_x(2) << ", " << h_x(3) << ", " << h_x(4) << "}"
19              << std::endl;
20
21    auto res = KokkosBlas::sum(x);
22
23    std::cout << "result:\n"
24              << "   " << res << std::endl;
25  }
26  Kokkos::finalize();
27}

output:

orignal values:
   x: {0, -1, -2, 3, -4}
result:
   -4