From fd0dd9aa0b2331df7722d27ab92193ab3527a0ea Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Tue, 17 Dec 2024 09:15:34 -0700 Subject: [PATCH 1/3] Add iamax, nrm1, nrm2 examples Signed-off-by: Carl Pearson --- example/wiki/blas/CMakeLists.txt | 15 +++++++ example/wiki/blas/KokkosBlas1_wiki_iamax.cpp | 43 ++++++++++++++++++++ example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp | 16 ++++++++ example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp | 17 ++++++++ 4 files changed, 91 insertions(+) create mode 100644 example/wiki/blas/KokkosBlas1_wiki_iamax.cpp create mode 100644 example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp create mode 100644 example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp diff --git a/example/wiki/blas/CMakeLists.txt b/example/wiki/blas/CMakeLists.txt index 245957bc89..48ba85f4d6 100644 --- a/example/wiki/blas/CMakeLists.txt +++ b/example/wiki/blas/CMakeLists.txt @@ -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 diff --git a/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp b/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp new file mode 100644 index 0000000000..9e2e3e42b1 --- /dev/null +++ b/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + Kokkos::initialize(); + { + int N = 100; + if (argc >= 2) { + N = atoi(argv[1]); + } + + using ViewType = Kokkos::View; + using Scalar = typename ViewType::non_const_value_type; + using AT = Kokkos::ArithTraits; + 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 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::ArithTraits::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(); +} diff --git a/example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp b/example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp new file mode 100644 index 0000000000..99d147236f --- /dev/null +++ b/example/wiki/blas/KokkosBlas1_wiki_nrm1.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +int main(void) { + Kokkos::initialize(); + { + Kokkos::View 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(); +} diff --git a/example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp b/example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp new file mode 100644 index 0000000000..5715c94dcb --- /dev/null +++ b/example/wiki/blas/KokkosBlas1_wiki_nrm2.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +int main(void) { + Kokkos::initialize(); + { + Kokkos::View 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(); +} From e4827e59b1be9eab68f37580264bd1638ad293a6 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Tue, 17 Dec 2024 09:34:09 -0700 Subject: [PATCH 2/3] iamax example: printf -> iostream Signed-off-by: Carl Pearson --- example/wiki/blas/KokkosBlas1_wiki_iamax.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp b/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp index 9e2e3e42b1..382db24848 100644 --- a/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp +++ b/example/wiki/blas/KokkosBlas1_wiki_iamax.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -37,7 +38,7 @@ int main(int argc, char* argv[]) { } } - printf("Iamax of X: %i, Expected: %i\n", max_loc, expected_max_loc); + std::cout << "Iamax of X: " << max_loc << ", Expected: " << expected_max_loc << std::endl; } Kokkos::finalize(); } From c6d6ccba3d260ce01a8a2f38e5c6d8114709c2ad Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Tue, 17 Dec 2024 09:41:26 -0700 Subject: [PATCH 3/3] add dot wiki example Signed-off-by: Carl Pearson --- example/wiki/blas/CMakeLists.txt | 5 +++++ example/wiki/blas/KokkosBlas1_wiki_dot.cpp | 24 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 example/wiki/blas/KokkosBlas1_wiki_dot.cpp diff --git a/example/wiki/blas/CMakeLists.txt b/example/wiki/blas/CMakeLists.txt index 48ba85f4d6..5061c54b7d 100644 --- a/example/wiki/blas/CMakeLists.txt +++ b/example/wiki/blas/CMakeLists.txt @@ -3,6 +3,11 @@ KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../../../test_common) +KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST( + wiki_blas1_dot + SOURCES KokkosBlas1_wiki_dot.cpp + ) + KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST( wiki_blas1_iamax SOURCES KokkosBlas1_wiki_iamax.cpp diff --git a/example/wiki/blas/KokkosBlas1_wiki_dot.cpp b/example/wiki/blas/KokkosBlas1_wiki_dot.cpp new file mode 100644 index 0000000000..fc0d258ac3 --- /dev/null +++ b/example/wiki/blas/KokkosBlas1_wiki_dot.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + Kokkos::initialize(); + { + int N = 100; + if (argc >= 2) { + N = atoi(argv[1]); + } + + Kokkos::View x("X", N); + Kokkos::View y("Y", N); + Kokkos::deep_copy(x, 3.0); + Kokkos::deep_copy(y, 2.0); + + double x_y = KokkosBlas::dot(x, y); + + std::cout << "X_dot_Y: " << x_y << " Expected: " << 1.0 * N * (3.0 * 2.0) + << " Diff: " << x_y - 1.0 * N * (3.0 * 2.0) << std::endl; + } + Kokkos::finalize(); +}