From 3180841cabdfa24d4a317b48395a6c6f46c61bee Mon Sep 17 00:00:00 2001 From: kwitaechong Date: Thu, 28 Sep 2023 23:02:22 -0400 Subject: [PATCH 1/3] normalize Vector with size 1 --- src/Picasso_BatchedLinearAlgebra.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Picasso_BatchedLinearAlgebra.hpp b/src/Picasso_BatchedLinearAlgebra.hpp index 7741f34..5710952 100644 --- a/src/Picasso_BatchedLinearAlgebra.hpp +++ b/src/Picasso_BatchedLinearAlgebra.hpp @@ -1735,6 +1735,15 @@ struct Vector return *this; } + // Normalization + KOKKOS_INLINE_FUNCTION + Vector& normalize() + { + ( *this )( 0 ) = ( *this )( 0 ) > 0 ? 1.0 : -1.0; + ; + return *this; + } + // Strides. KOKKOS_INLINE_FUNCTION int stride_0() const { return 1; } From 1a4940a8ba3ec19240d92a3c929bf92581c786ab Mon Sep 17 00:00:00 2001 From: kwitaechong Date: Mon, 2 Oct 2023 14:24:34 -0400 Subject: [PATCH 2/3] assert norm2 == 0.0 --- src/Picasso_BatchedLinearAlgebra.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Picasso_BatchedLinearAlgebra.hpp b/src/Picasso_BatchedLinearAlgebra.hpp index 5710952..a18aa7c 100644 --- a/src/Picasso_BatchedLinearAlgebra.hpp +++ b/src/Picasso_BatchedLinearAlgebra.hpp @@ -12,6 +12,8 @@ #ifndef PICASSO_BATCHEDLINEARALGEBRA_HPP #define PICASSO_BATCHEDLINEARALGEBRA_HPP +#include + #include #include @@ -1604,6 +1606,8 @@ struct Vector #pragma unroll #endif auto norm2 = Kokkos::sqrt( ~( *this ) * ( *this ) ); + assert( norm2 != 0.0 ); + for ( int i = 0; i < N; ++i ) ( *this )( i ) /= norm2; return *this; @@ -1739,8 +1743,9 @@ struct Vector KOKKOS_INLINE_FUNCTION Vector& normalize() { + assert( ( *this )( 0 ) != 0.0 ); + ( *this )( 0 ) = ( *this )( 0 ) > 0 ? 1.0 : -1.0; - ; return *this; } @@ -1907,6 +1912,8 @@ struct VectorView #pragma unroll #endif auto norm2 = Kokkos::sqrt( ~( *this ) * ( *this ) ); + assert( norm2 != 0.0 ); + for ( int i = 0; i < N; ++i ) ( *this )( i ) /= norm2; return *this; From 5b7e844cc044264bc183a2f48d7755a0c5968302 Mon Sep 17 00:00:00 2001 From: kwitaechong Date: Tue, 24 Oct 2023 10:59:25 -0400 Subject: [PATCH 3/3] check norm for vector size of 1 --- unit_test/tstBatchedLinearAlgebra.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/unit_test/tstBatchedLinearAlgebra.hpp b/unit_test/tstBatchedLinearAlgebra.hpp index e7277b0..73fd273 100644 --- a/unit_test/tstBatchedLinearAlgebra.hpp +++ b/unit_test/tstBatchedLinearAlgebra.hpp @@ -1128,6 +1128,11 @@ void vectorTest() for ( int i = 0; i < 3; ++i ) EXPECT_DOUBLE_EQ( w( i ), 32.3 ); + // Size 1 vector test + LinearAlgebra::Vector vec1 = { 4.7 }; + vec1.normalize(); + EXPECT_DOUBLE_EQ( vec1, 1.0 ); + // Size 1 vector test. // FIXME: construction of length 1 vector fails with NVCC. /*