Skip to content

BLAS 1::iamax

Vinh Dang edited this page Feb 18, 2020 · 12 revisions

KokkosBlas::iamax()

Header File: KokkosBlas1_iamax.hpp

Usage: max_loc = KokkosBlas::iamax(x); KokkosBlas::iamax(r,x);

Return the (smallest) index of the element of the maximum magnitude of x.

Single Vector Interface

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

Parameters:

  • XVector: a rank-1 Kokkos::View

Requirements:

  • x.rank == 1

Single and MultiVector Interface

template<class ReturnVector, class XVector>
void iamax (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_iamax.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);

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

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

   Kokkos::deep_copy(h_x,x);

   size_type max_loc = KokkosBlas::iamax(x);

   mag_type expected_result = Kokkos::Details::ArithTraits<mag_type>::min();
   size_type expected_max_loc = 0;
   for(int i=0; i<N; i++) {      
     mag_type val = AT::abs(h_x(i));
     if(val > expected_result) { expected_result = val; expected_max_loc = i+1;}
   }

   printf("Iamax of X: %i, Expected: %i\n",max_loc,expected_max_loc);

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