KokkosBlas::axpy¶
Defined in header: KokkosBlas1_axpby.hpp
template <class execution_space, class AV, class XMV, class YMV>
void axpy(const execution_space& space, const AV& a, const XMV& X, const YMV& Y)
template <class AV, class XMV, class YMV>
void axpy(const AV& a, const XMV& X, const YMV& Y)
Add entries of X scaled by coefficient a to entries of Y: Y += a*X
iterate over the entries of
Yand add the corresponding entries ofXscaled byausing the resources ofspaceiterate over the entries of
Yand add the corresponding entries ofXscaled byausing the resources of the default instance oftypename XMV::execution_space
amay each be any of the following:a scalar value
a rank-0 host-accessible
Kokkos::Viewa rank-0 device-accessible
Kokkos::Viewa rank-1 device-accessible
Kokkos::Viewwith extent 1 (the coefficient will be applied to all columns)a rank-1 device-accessible
Kokkos::Viewwith extentY.extent(1)(one coefficient per column)
- The function will throw a runtime exception if any of the following conditions are not met:
Y.extent(0) == X.extent(0) && Y.extent(1) == X.extent(1)
- If
ais a rank-1View: a.extent(0) != 1 && a.extent(0) != X.extent(1)
Parameters¶
- space:
execution space instance
- a:
scaling factor
- X, Y:
vector(s) of input and output values respectively
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 == true
YMV must be a Kokkos View that satisfies
YMV::rank == XMV::rankstd::is_same_v<typename YMV::value_type, typename YMV::non_const_value_type> == trueKokkos::SpaceAccessibility<execution_space, typename YMV::memory_space>::accessible == true
AV must be a scalar or a Kokkos View that satisfies one of the following:
AV::rank == 0AV::rank == 1 && Kokkos::SpaceAccessibility<execution_space, typename YMV::memory_space>::accessible
Example¶
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <Kokkos_Core.hpp>
#include <KokkosBlas1_axpby.hpp>
#include <iostream>
int main(int argc, char* argv[]) {
Kokkos::initialize(argc, argv);
{
int N = 10;
if (argc > 1) {
N = atoi(argv[1]);
}
Kokkos::View<double*> x("X", N);
Kokkos::View<double*> y("Y", N);
Kokkos::deep_copy(x, 3.0);
Kokkos::deep_copy(y, 2.0);
double alpha = 1.5;
KokkosBlas::axpy(alpha, x, y);
double sum = 0.0;
Kokkos::parallel_reduce(
"CheckValue", N, KOKKOS_LAMBDA(const int& i, double& lsum) { lsum += y(i); }, sum);
const double expected = N * (2.0 + 1.5 * 3.0);
std::cout << "Sum: " << sum << ", Expected: " << expected << ", Diff: " << sum - expected << std::endl;
}
Kokkos::finalize();
}
output:
Sum: 65, Expected: 65, Diff: 0