|
| 1 | +//@HEADER |
| 2 | +// ************************************************************************ |
| 3 | +// |
| 4 | +// Kokkos v. 4.0 |
| 5 | +// Copyright (2022) National Technology & Engineering |
| 6 | +// Solutions of Sandia, LLC (NTESS). |
| 7 | +// |
| 8 | +// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 | +// the U.S. Government retains certain rights in this software. |
| 10 | +// |
| 11 | +// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 | +// See https://kokkos.org/LICENSE for license information. |
| 13 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 | +// |
| 15 | +//@HEADER |
| 16 | + |
| 17 | +#include <Kokkos_Core.hpp> |
| 18 | +#include <Kokkos_Random.hpp> |
| 19 | +#include <KokkosBatched_Iamax.hpp> |
| 20 | + |
| 21 | +using ExecutionSpace = Kokkos::DefaultExecutionSpace; |
| 22 | + |
| 23 | +/// \brief Example of batched iamax |
| 24 | +/// Finds the index of the first element having maximum absolute value. |
| 25 | +/// X0: [1, 2, 0] -> 1 |
| 26 | +/// X1: [-5, 4, 3] -> 0 |
| 27 | +/// X2: [0, -1, -1] -> 1 |
| 28 | +/// |
| 29 | +int main(int /*argc*/, char** /*argv*/) { |
| 30 | + Kokkos::initialize(); |
| 31 | + { |
| 32 | + using View2DType = Kokkos::View<double**, ExecutionSpace>; |
| 33 | + using Idx1DType = Kokkos::View<int*, ExecutionSpace>; |
| 34 | + const int Nb = 10, n = 3; |
| 35 | + |
| 36 | + // Batched vectors |
| 37 | + View2DType x0("x0", Nb, n), x1("x1", Nb, n), x2("x2", Nb, n); |
| 38 | + |
| 39 | + // Max indices |
| 40 | + Idx1DType iamax0("iamax0", Nb), iamax1("iamax1", Nb), iamax2("iamax2", Nb); |
| 41 | + |
| 42 | + // Initialize x0, x1, x2 |
| 43 | + auto h_x0 = Kokkos::create_mirror_view(x0); |
| 44 | + auto h_x1 = Kokkos::create_mirror_view(x1); |
| 45 | + auto h_x2 = Kokkos::create_mirror_view(x2); |
| 46 | + |
| 47 | + for (int ib = 0; ib < Nb; ib++) { |
| 48 | + h_x0(ib, 0) = 1.0; |
| 49 | + h_x0(ib, 1) = 2.0; |
| 50 | + h_x0(ib, 2) = 0.0; |
| 51 | + |
| 52 | + h_x1(ib, 0) = -5.0; |
| 53 | + h_x1(ib, 1) = 4.0; |
| 54 | + h_x1(ib, 2) = 3.0; |
| 55 | + |
| 56 | + h_x2(ib, 0) = 0.0; |
| 57 | + h_x2(ib, 1) = -1.0; |
| 58 | + h_x2(ib, 2) = -1.0; |
| 59 | + } |
| 60 | + Kokkos::deep_copy(x0, h_x0); |
| 61 | + Kokkos::deep_copy(x1, h_x1); |
| 62 | + Kokkos::deep_copy(x2, h_x2); |
| 63 | + |
| 64 | + // Find max indices |
| 65 | + ExecutionSpace exec; |
| 66 | + using policy_type = Kokkos::RangePolicy<ExecutionSpace, Kokkos::IndexType<int>>; |
| 67 | + policy_type policy{exec, 0, Nb}; |
| 68 | + Kokkos::parallel_for( |
| 69 | + "iamax", policy, KOKKOS_LAMBDA(int ib) { |
| 70 | + auto sub_x0 = Kokkos::subview(x0, ib, Kokkos::ALL); |
| 71 | + auto sub_x1 = Kokkos::subview(x1, ib, Kokkos::ALL); |
| 72 | + auto sub_x2 = Kokkos::subview(x2, ib, Kokkos::ALL); |
| 73 | + |
| 74 | + // Find max indices |
| 75 | + iamax0(ib) = KokkosBatched::SerialIamax::invoke(sub_x0); |
| 76 | + iamax1(ib) = KokkosBatched::SerialIamax::invoke(sub_x1); |
| 77 | + iamax2(ib) = KokkosBatched::SerialIamax::invoke(sub_x2); |
| 78 | + }); |
| 79 | + |
| 80 | + // Confirm that the results are correct |
| 81 | + auto h_iamax0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, iamax0); |
| 82 | + auto h_iamax1 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, iamax1); |
| 83 | + auto h_iamax2 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, iamax2); |
| 84 | + bool correct = true; |
| 85 | + for (int ib = 0; ib < Nb; ib++) { |
| 86 | + if (h_iamax0(ib) != 1) correct = false; |
| 87 | + if (h_iamax1(ib) != 0) correct = false; |
| 88 | + if (h_iamax2(ib) != 1) correct = false; |
| 89 | + } |
| 90 | + |
| 91 | + if (correct) { |
| 92 | + std::cout << "iamax works correctly!" << std::endl; |
| 93 | + } |
| 94 | + } |
| 95 | + Kokkos::finalize(); |
| 96 | +} |
0 commit comments