forked from kokkos/stdBLAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorm2.cpp
More file actions
160 lines (129 loc) · 5.5 KB
/
norm2.cpp
File metadata and controls
160 lines (129 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "./gtest_fixtures.hpp"
#include <type_traits>
// FIXME (mfh 2022/06/17) Temporarily disable calling the BLAS,
// to get PR testing workflow running with mdspan tag.
#if 0
#ifdef LINALG_ENABLE_BLAS
extern "C" double dnrm2_(const int* pN,
const double* X,
const int* pINCX);
double dnrm2_wrapper(const int N, const double* X, const int INCX)
{
return dnrm2_(&N, X, &INCX);
}
#endif // LINALG_ENABLE_BLAS
#endif // 0
namespace {
using LinearAlgebra::vector_two_norm;
TEST(BLAS1_norm2, mdspan_zero)
{
// This test ensures that vectors with no entries have a norm of exactly 0
using mag_t = double;
using scalar_t = double;
using vector_t = mdspan<scalar_t, extents<std::size_t, dynamic_extent>>;
constexpr std::size_t vectorSize(0);
std::vector<scalar_t> storage(vectorSize);
vector_t x(storage.data(), vectorSize);
const auto normResult = vector_two_norm(x, mag_t{});
static_assert( std::is_same_v<std::remove_const_t<decltype(normResult)>, mag_t> );
const mag_t expectedNormResult{};
EXPECT_EQ( expectedNormResult, normResult );
const mag_t normResultPlusOne = vector_two_norm(x, mag_t(3.0));
const mag_t expectedNormResultPlusOne = mag_t(3.0);
EXPECT_EQ( expectedNormResultPlusOne, normResultPlusOne );
// Test 'auto' overload.
const auto normResultAuto = vector_two_norm(x);
static_assert( std::is_same_v<std::remove_const_t<decltype(normResultAuto)>, mag_t> );
EXPECT_EQ( expectedNormResult, normResultAuto );
}
TEST(BLAS1_norm2, mdspan_one)
{
// This test ensures that vectors with one entry have a norm of exactly the magnitude of the only element
using std::abs;
using real_t = double;
using mag_t = real_t;
using scalar_t = std::complex<real_t>;
using vector_t = mdspan<scalar_t, extents<std::size_t, dynamic_extent>>;
constexpr std::size_t vectorSize(1);
std::vector<scalar_t> storage(vectorSize);
vector_t x(storage.data(), vectorSize);
x[0] = -3;
const auto normResult = vector_two_norm(x, mag_t{});
static_assert( std::is_same_v<std::remove_const_t<decltype(normResult)>, mag_t> );
const mag_t expectedNormResult = abs( x[0] );
EXPECT_EQ( expectedNormResult, normResult );
const mag_t normResultPlusOne = vector_two_norm(x, mag_t(4.0));
const mag_t expectedNormResultPlusOne = mag_t(5.0);
EXPECT_EQ( expectedNormResultPlusOne, normResultPlusOne );
// Test 'auto' overload.
const auto normResultAuto = vector_two_norm(x);
static_assert( std::is_same_v<std::remove_const_t<decltype(normResultAuto)>, mag_t> );
EXPECT_EQ( expectedNormResult, normResultAuto );
}
TEST(BLAS1_norm2, mdspan_double)
{
using std::abs;
using std::sqrt;
using mag_t = double;
using scalar_t = double;
using vector_t = mdspan<scalar_t, extents<std::size_t, dynamic_extent>>;
constexpr std::size_t vectorSize(5);
constexpr mag_t tol =
mag_t(vectorSize) * std::numeric_limits<mag_t>::epsilon();
constexpr std::size_t storageSize = vectorSize;
std::vector<scalar_t> storage(storageSize);
vector_t x(storage.data(), vectorSize);
// Set elements in descending order so the scaling triggers
mag_t expectedNormResultSquared {};
for (std::size_t k = vectorSize; k > 1; --k) {
const scalar_t x_k = scalar_t(k);
x(k-1) = x_k;
expectedNormResultSquared += x_k * x_k;
}
const auto normResult = vector_two_norm(x, mag_t{});
static_assert( std::is_same_v<std::remove_const_t<decltype(normResult)>, mag_t> );
const mag_t expectedNormResult = sqrt(expectedNormResultSquared);
EXPECT_NEAR( expectedNormResult, normResult, tol );
// Test 'auto' overload.
const auto normResultAuto = vector_two_norm(x);
static_assert( std::is_same_v<std::remove_const_t<decltype(normResultAuto)>, mag_t> );
EXPECT_NEAR( expectedNormResult, normResultAuto, tol );
// FIXME (mfh 2022/06/17) Temporarily disable calling the BLAS,
// to get PR testing workflow running with mdspan tag.
#if 0
#ifdef LINALG_ENABLE_BLAS
const mag_t blasResult =
dnrm2_wrapper(int(vectorSize), x.data(), 1);
EXPECT_NEAR( expectedNormResult, blasResult, tol );
#endif // LINALG_ENABLE_BLAS
#endif // 0
}
TEST(BLAS1_norm2, mdspan_complex_double)
{
using real_t = double;
using mag_t = real_t;
using scalar_t = std::complex<real_t>;
using vector_t = mdspan<scalar_t, extents<std::size_t, dynamic_extent>>;
constexpr std::size_t vectorSize(5);
// Complex numbers use more arithmetic than their real analogs.
constexpr mag_t tol = 4.0 * mag_t(vectorSize) *
std::numeric_limits<mag_t>::epsilon();
constexpr std::size_t storageSize = vectorSize;
std::vector<scalar_t> storage(storageSize);
vector_t x(storage.data(), vectorSize);
mag_t expectedNormResultSquared {};
for (std::size_t k = 0; k < vectorSize; ++k) {
const scalar_t x_k(real_t(k) + 3.0, -real_t(k) - 1.0);
x(k) = x_k;
expectedNormResultSquared += abs(x_k) * abs(x_k);
}
const auto normResult = vector_two_norm(x, mag_t{});
static_assert( std::is_same_v<std::remove_const_t<decltype(normResult)>, mag_t> );
const mag_t expectedNormResult = sqrt(expectedNormResultSquared);
EXPECT_NEAR( expectedNormResult, normResult, tol );
// Test 'auto' overload.
const auto normResultAuto = vector_two_norm(x);
static_assert( std::is_same_v<std::remove_const_t<decltype(normResultAuto)>, mag_t> );
EXPECT_NEAR( expectedNormResult, normResultAuto, tol );
}
}