BLAS++ 2024.05.31
BLAS C++ API
Loading...
Searching...
No Matches
nrm2.hh
1// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
2// SPDX-License-Identifier: BSD-3-Clause
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
5
6#ifndef BLAS_NRM2_HH
7#define BLAS_NRM2_HH
8
9#include "blas/util.hh"
10
11#include <limits>
12
13namespace blas {
14
15// =============================================================================
32
33template <typename T>
34real_type<T>
36 int64_t n,
37 T const * x, int64_t incx )
38{
39 using std::sqrt;
40 using real_t = real_type<T>;
41
42 // check arguments
43 blas_error_if( n < 0 ); // standard BLAS returns, doesn't fail
44 blas_error_if( incx <= 0 ); // standard BLAS returns, doesn't fail
45
46 // todo: scale to avoid overflow & underflow
47 real_t result = 0;
48 if (incx == 1) {
49 // unit stride
50 for (int64_t i = 0; i < n; ++i) {
51 result += real(x[i]) * real(x[i]) + imag(x[i]) * imag(x[i]);
52 }
53 }
54 else {
55 // non-unit stride
56 int64_t ix = 0;
57 for (int64_t i = 0; i < n; ++i) {
58 result += real(x[ix]) * real(x[ix]) + imag(x[ix]) * imag(x[ix]);
59 ix += incx;
60 }
61 }
62 return sqrt( result );
63}
64
65} // namespace blas
66
67#endif // #ifndef BLAS_NRM2_HH
void nrm2(int64_t n, float const *x, int64_t incx, float *result, blas::Queue &queue)
GPU device, float version.
Definition device_nrm2.cc:84