-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtrmv.cc
231 lines (206 loc) · 6.72 KB
/
trmv.cc
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
#include "blas/fortran.h"
#include "blas.hh"
#include "blas_internal.hh"
#include "blas/counter.hh"
#include <limits>
#include <string.h>
namespace blas {
//==============================================================================
namespace internal {
//------------------------------------------------------------------------------
/// Low-level overload wrapper calls Fortran, float version.
/// @ingroup trmv_internal
inline void trmv(
char uplo,
char trans,
char diag,
blas_int n,
float const* A, blas_int lda,
float* x, blas_int incx )
{
BLAS_strmv( &uplo, &trans, &diag, &n, A, &lda, x, &incx );
}
//------------------------------------------------------------------------------
/// Low-level overload wrapper calls Fortran, double version.
/// @ingroup trmv_internal
inline void trmv(
char uplo,
char trans,
char diag,
blas_int n,
double const* A, blas_int lda,
double* x, blas_int incx )
{
BLAS_dtrmv( &uplo, &trans, &diag, &n, A, &lda, x, &incx );
}
//------------------------------------------------------------------------------
/// Low-level overload wrapper calls Fortran, complex<float> version.
/// @ingroup trmv_internal
inline void trmv(
char uplo,
char trans,
char diag,
blas_int n,
std::complex<float> const* A, blas_int lda,
std::complex<float>* x, blas_int incx )
{
BLAS_ctrmv( &uplo, &trans, &diag, &n,
(blas_complex_float*) A, &lda,
(blas_complex_float*) x, &incx );
}
//------------------------------------------------------------------------------
/// Low-level overload wrapper calls Fortran, complex<double> version.
/// @ingroup trmv_internal
inline void trmv(
char uplo,
char trans,
char diag,
blas_int n,
std::complex<double> const* A, blas_int lda,
std::complex<double>* x, blas_int incx )
{
BLAS_ztrmv( &uplo, &trans, &diag, &n,
(blas_complex_double*) A, &lda,
(blas_complex_double*) x, &incx );
}
} // namespace internal
//==============================================================================
namespace impl {
//------------------------------------------------------------------------------
/// Mid-level templated wrapper checks and converts arguments,
/// then calls low-level wrapper.
/// @ingroup trmv_internal
///
template <typename scalar_t>
void trmv(
blas::Layout layout,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t n,
scalar_t const* A, int64_t lda,
scalar_t* x, int64_t incx )
{
// check arguments
blas_error_if( layout != Layout::ColMajor &&
layout != Layout::RowMajor );
blas_error_if( uplo != Uplo::Lower &&
uplo != Uplo::Upper );
blas_error_if( trans != Op::NoTrans &&
trans != Op::Trans &&
trans != Op::ConjTrans );
blas_error_if( diag != Diag::NonUnit &&
diag != Diag::Unit );
blas_error_if( n < 0 );
blas_error_if( lda < n );
blas_error_if( incx == 0 );
#ifdef BLAS_HAVE_PAPI
// PAPI instrumentation
counter::trmv_type element;
memset( &element, 0, sizeof( element ) );
element = { uplo, trans, diag, n };
counter::insert( element, counter::Id::trmv );
double gflops = 1e9 * blas::Gflop< scalar_t >::trmv( n );
counter::inc_flop_count( (long long int)gflops );
#endif
// convert arguments
blas_int n_ = to_blas_int( n );
blas_int lda_ = to_blas_int( lda );
blas_int incx_ = to_blas_int( incx );
blas::Op trans2 = trans;
if (layout == Layout::RowMajor) {
// swap lower <=> upper
// A => A^T; A^T => A; A^H => A
uplo = (uplo == Uplo::Lower ? Uplo::Upper : Uplo::Lower);
trans2 = (trans == Op::NoTrans ? Op::Trans : Op::NoTrans);
if constexpr (is_complex_v<scalar_t>) {
if (trans == Op::ConjTrans) {
// conjugate x (in-place)
int64_t ix = (incx > 0 ? 0 : (-n + 1)*incx);
for (int64_t i = 0; i < n; ++i) {
x[ ix ] = conj( x[ ix ] );
ix += incx;
}
}
}
}
char uplo_ = to_char( uplo );
char trans_ = to_char( trans2 );
char diag_ = to_char( diag );
// call low-level wrapper
internal::trmv( uplo_, trans_, diag_, n_, A, lda_, x, incx_ );
if constexpr (is_complex_v<scalar_t>) {
if (layout == Layout::RowMajor && trans == Op::ConjTrans) {
// conjugate x (in-place)
int64_t ix = (incx > 0 ? 0 : (-n + 1)*incx);
for (int64_t i = 0; i < n; ++i) {
x[ ix ] = conj( x[ ix ] );
ix += incx;
}
}
}
}
} // namespace impl
//==============================================================================
// High-level overloaded wrappers call mid-level templated wrapper.
//------------------------------------------------------------------------------
/// CPU, float version.
/// @ingroup trmv
void trmv(
blas::Layout layout,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t n,
float const* A, int64_t lda,
float* x, int64_t incx )
{
impl::trmv( layout, uplo, trans, diag, n, A, lda, x, incx );
}
//------------------------------------------------------------------------------
/// CPU, double version.
/// @ingroup trmv
void trmv(
blas::Layout layout,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t n,
double const* A, int64_t lda,
double* x, int64_t incx )
{
impl::trmv( layout, uplo, trans, diag, n, A, lda, x, incx );
}
//------------------------------------------------------------------------------
/// CPU, complex<float> version.
/// @ingroup trmv
void trmv(
blas::Layout layout,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t n,
std::complex<float> const* A, int64_t lda,
std::complex<float>* x, int64_t incx )
{
impl::trmv( layout, uplo, trans, diag, n, A, lda, x, incx );
}
//------------------------------------------------------------------------------
/// CPU, complex<double> version.
/// @ingroup trmv
void trmv(
blas::Layout layout,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t n,
std::complex<double> const* A, int64_t lda,
std::complex<double>* x, int64_t incx )
{
impl::trmv( layout, uplo, trans, diag, n, A, lda, x, incx );
}
} // namespace blas