Skip to content

BLAS 1::mult

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

KokkosBlas::mult()

Header File: KokkosBlas1_mult.hpp

Usage: KokkosBlas::mult(gamma,Y,alpha,A,X);

Element-wise multiplication of two (multi-)vectors:

Y[i] = gamma * Y[i] + alpha * A[i] * X[i]

Multiplies each value of X(i) or X(i,j) with A(i) scaled by alpha, and adds it to Y(i) or Y(i,j) after scaling the Y-value by gamma. Resulting value is assigned to Y(i) (or Y(i,j)).

Interface

template<class YMV, class AV, class XMV>
void
mult (typename YMV::const_value_type& gamma,
      const YMV& Y,
      typename AV::const_value_type& alpha,
      const AV& A,
      const XMV& X)

Parameters:

  • YMV: A rank-1 or rank-2 Kokkos::View
  • XMV: A rank-1 or rank-2 Kokkos::View
  • AV: A rank-1 Kokkos::View

Requirements:

  • YMV::value_type == YMV::non_const_value_type i.e. Y must be a non-const View
  • Y.rank == X.rank
  • Y.rank == 1 or Y.rank == 2
  • Y.extent(0) == X.extent(0)
  • Y.extent(1) == X.extent(1)
  • A.rank == 1

Example

#include<Kokkos_Core.hpp>
#include<KokkosBlas1_mult.hpp>

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

     Kokkos::View<double*> x("X",N);
     Kokkos::View<double*> y("Y",N);
     Kokkos::View<double*> a("A",N);
     Kokkos::deep_copy(x,3.0);
     Kokkos::deep_copy(y,2.0);
     Kokkos::deep_copy(a,4.0);

     const double alpha = 1.5;
     const double gamma = 1.2;

     KokkosBlas::mult(gamma,y,alpha,a,x);
     // Result per ith entry is y[i] <- gamma*y[i] + alpha*a[i]*x[i]
     //   Each entry here is: 20.4 = 1.2*2 + 1.5*3*4

     // Check results
     const double solution = gamma*2.0 + alpha*3.0*4.0;
     int error_count = 0;
     Kokkos::parallel_reduce("Error check", N, KOKKOS_LAMBDA(int i, int &update) {
         if (y(i) != solution) update += 1;
       }, error_count);

     if (error_count > 0)
       printf("Errors: %d\n", error_count);

   }
   Kokkos::finalize();
}
Clone this wiki locally