KokkosBlas::swap

Defined in header: KokkosBlas1_swap.hpp

template <class execution_space, class XVector, class YVector>
void swap(execution_space const& space, XVector const& x, YVector const& y);

template <class XVector, class YVector>
void swap(const XVector& x, const YVector& y);

Exchanges the values of x with corresponding values of y.

  1. iterates over the extents of x, exchange entries of x and y on the space instance

  2. iterates over the extents of x, exchange entries of x and y on the default instance of typename XVector::execution_space

The function will throw a runtime exception if x.extent(0) != y.extent(0) || x.extent(1) != y.extent(1)

Parameters

space:

execution space instance

x:

vector(s) to swap with y

y:

vector(s) to sawp with x

Type Requirements

  • execution_space must be a Kokkos execution space

  • XVector must be a Kokkos View of rank 1 that satisfies

    • std::is_same_v<typename XVector::value_type, typename XVector::non_const_value_type == true

    • Kokkos::SpaceAccessibility<execution_space, typename XVector::memory_space>::accessible == true

  • YVector must be a Kokkos View of rank 1 that satisfies

    • std::is_same_v<typename YVector::value_type, typename YVector::non_const_value_type == true

    • Kokkos::SpaceAccessibility<execution_space, typename YVector::memory_space>::accessible == true

Example

#include <iostream>
#include <Kokkos_Core.hpp>
#include <KokkosBlas1_swap.hpp>

int main(void) {
  Kokkos::initialize();
  {
    Kokkos::View<double*> x("x", 5), y("y", 5);
    auto h_x = Kokkos::create_mirror_view(x);
    auto h_y = Kokkos::create_mirror_view(y);
    h_x(0)   = 0.0;
    h_x(1)   = 1.0;
    h_x(2)   = 2.0;
    h_x(3)   = 3.0;
    h_x(4)   = 4.0;
    h_y(0)   = 9.0;
    h_y(1)   = 8.0;
    h_y(2)   = 7.0;
    h_y(3)   = 6.0;
    h_y(4)   = 5.0;

    std::cout << "orignal values:\n"
              << "   x: {" << h_x(0) << ", " << h_x(1) << ", " << h_x(2) << ", " << h_x(3) << ", " << h_x(4) << "}\n"
              << "   y: {" << h_y(0) << ", " << h_y(1) << ", " << h_y(2) << ", " << h_y(3) << ", " << h_y(4) << "}"
              << std::endl;
    Kokkos::deep_copy(x, h_x);
    Kokkos::deep_copy(y, h_y);

    KokkosBlas::swap(x, y);
    Kokkos::deep_copy(h_x, x);
    Kokkos::deep_copy(h_y, y);

    std::cout << "final values:\n"
              << "   x: {" << h_x(0) << ", " << h_x(1) << ", " << h_x(2) << ", " << h_x(3) << ", " << h_x(4) << "}\n"
              << "   y: {" << h_y(0) << ", " << h_y(1) << ", " << h_y(2) << ", " << h_y(3) << ", " << h_y(4) << "}"
              << std::endl;
  }
  Kokkos::finalize();
}

output:

orignal values:
   x: {0, 1, 2, 3, 4}
   y: {9, 8, 7, 6, 5}
final values:
   x: {9, 8, 7, 6, 5}
   y: {0, 1, 2, 3, 4}