Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dot, iamax, nrm1, nrm2 examples #2467

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions example/wiki/blas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ 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
)

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
24 changes: 24 additions & 0 deletions example/wiki/blas/KokkosBlas1_wiki_dot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <Kokkos_Core.hpp>
#include <KokkosBlas1_dot.hpp>

int main(int argc, char* argv[]) {
Kokkos::initialize();
{
int N = 100;
if (argc >= 2) {
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 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();
}
44 changes: 44 additions & 0 deletions example/wiki/blas/KokkosBlas1_wiki_iamax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <Kokkos_Core.hpp>
#include <Kokkos_Random.hpp>
#include <KokkosBlas1_iamax.hpp>

int main(int argc, char* argv[]) {
Kokkos::initialize();
{
int N = 100;
if (argc >= 2) {
N = atoi(argv[1]);
}

using ViewType = Kokkos::View<double*>;
using Scalar = typename ViewType::non_const_value_type;
using AT = Kokkos::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::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;
}
}

std::cout << "Iamax of X: " << max_loc << ", Expected: " << expected_max_loc << std::endl;
}
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(void) {
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(void) {
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();
}
Loading