Skip to content

BLAS 1::sum

Nathan Ellingwood edited this page Jun 24, 2020 · 1 revision

KokkosBlas::sum()

Header File: KokkosBlas1_sum.hpp

Usage: sum = KokkosBlas::sum(x); KokkosBlas::sum(r,x);

Return the sum of elements of x, or replace entry of r with sum of corresponding multi-vector entries of x.

Single Vector Interface

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

Parameters:

  • XVector: a rank-1 Kokkos::View

Requirements:

  • x.rank == 1

Single and MultiVector Interface

template<class ReturnVector, class XVector>
void sum (const ReturnVector& r, const XVector& x);

Parameters:

  • ReturnVector: a rank-0 or rank-1 Kokkos::View
  • XVector: a rank-1 or rank-2 Kokkos::View

Requirements:

  • x.rank == r.rank + 1
  • r.extent(0) == x.extent(1)
  • ReturnVector::non_const_value_type == ReturnVector::value_type
  • ReturnVector::value_type == XVector::size_type

Example

#include<Kokkos_Core.hpp>
#include<Kokkos_Random.hpp>
#include<KokkosBlas1_sum.hpp>

int main(int argc, char* argv[]) {
   Kokkos::initialize();
   {
     int N = atoi(argv[1]);

     using ViewType = Kokkos::View<double*>;
     using Scalar   = typename ViewType::non_const_value_type;
     using AT       = Kokkos::Details::ArithTraits<Scalar>;
     using mag_type = typename AT::mag_type;
     using size_type= typename ViewType::size_type;

     ViewType x("X",N);

     Kokkos::Random_XorShift64_Pool<typename ViewType::device_type::execution_space> rand_pool(13718);
     Kokkos::fill_random(x,rand_pool,Scalar(10));

     typename ViewType::HostMirror h_x = Kokkos::create_mirror_view(x);
     Kokkos::deep_copy(h_x,x);

     Scalar sum = KokkosBlas::sum(x);

     Scalar expected_result = 0;
     for(int i=0; i<N; i++) {      
       expected_result += h_x(i);
     }

     printf("Sum of X: %lf, Expected: %lf\n",sum, expected_result);
   }
   Kokkos::finalize();
}
Clone this wiki locally