Skip to content

Commit

Permalink
Add iamax, nrm1, nrm2 examples
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Pearson <[email protected]>
  • Loading branch information
cwpearson committed Dec 17, 2024
1 parent d96c6ee commit ee4cd7e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions example/wiki/blas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../../../test_common)

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_blas1_iamax
SOURCES KokkosBlas1_wiki_iamax.cpp
)

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_blas1_nrm1
SOURCES KokkosBlas1_wiki_nrm1.cpp
)

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_blas1_nrm2
SOURCES KokkosBlas1_wiki_nrm2.cpp
)

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_blas2_ger
SOURCES KokkosBlas2_wiki_ger.cpp
Expand Down
40 changes: 40 additions & 0 deletions example/wiki/blas/KokkosBlas1_wiki_iamax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#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();
}
16 changes: 16 additions & 0 deletions example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <Kokkos_Core.hpp>
#include <KokkosBlas1_nrm1.hpp>

int main(int argc, char* argv[]) {
Kokkos::initialize();
{
Kokkos::View<double*> x("X", 100);
Kokkos::deep_copy(x, -3.0);

double x_nrm = KokkosBlas::nrm1(x);

std::cout << "X_nrm: " << x_nrm << " Expected: " << 100 * 3.0 << std::endl;
}
Kokkos::finalize();
}
17 changes: 17 additions & 0 deletions example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <cmath>
#include <iostream>
#include <Kokkos_Core.hpp>
#include <KokkosBlas1_nrm2.hpp>

int main(int argc, char* argv[]) {
Kokkos::initialize();
{
Kokkos::View<double*> x("X", 100);
Kokkos::deep_copy(x, 3.0);

double x_nrm = KokkosBlas::nrm2(x);

std::cout << "X_nrm: " << x_nrm << " Expected: " << std::sqrt(100 * 3.0 * 3.0) << std::endl;
}
Kokkos::finalize();
}

0 comments on commit ee4cd7e

Please sign in to comment.