File: test_rotg.cc

package info (click to toggle)
blaspp 2024.10.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,636 kB
  • sloc: cpp: 29,332; ansic: 8,448; python: 2,192; xml: 182; perl: 101; makefile: 53; sh: 7
file content (169 lines) | stat: -rw-r--r-- 5,426 bytes parent folder | download
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
// 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 "test.hh"
#include "cblas_wrappers.hh"
#include "lapack_wrappers.hh"
#include "print_matrix.hh"

// -----------------------------------------------------------------------------
template <typename T>
void test_rotg_work( Params& params, bool run )
{
    using namespace testsweeper;
    using std::abs;
    using std::real;
    using std::imag;
    using real_t   = blas::real_type< T >;

    // get & mark input values
    int64_t n = params.dim.n();
    int verbose = params.verbose();

    // mark non-standard output values
    params.ref_time();
    params.error2();
    params.error3();

    // adjust header to msec
    params.time.name( "time (ms)" );
    params.ref_time.name( "ref time (ms)" );
    params.ref_time.width( 13 );

    if (! run)
        return;

    // setup
    std::vector<T> a( n ), aref( n ), a_in( n );
    std::vector<T> b( n ), bref( n ), b_in( n );
    std::vector<T> s( n ), sref( n );
    std::vector<real_t> c( n ), cref( n );

    int64_t idist = 3;
    int iseed[4] = { 0, 0, 0, 1 };
    lapack_larnv( idist, iseed, n, &a[0] );
    lapack_larnv( idist, iseed, n, &b[0] );
    aref = a;
    bref = b;

    if (verbose >= 2) {
        printf( "a_in  = " );  print_vector( n, &a[0], 1 );
        printf( "b_in  = " );  print_vector( n, &b[0], 1 );
    }

    // Save some data to check later
    if (params.check() == 'y') {
        cblas_copy( n, &a[0], 1, &a_in[0], 1 );
        cblas_copy( n, &b[0], 1, &b_in[0], 1 );
    }

    // run test
    testsweeper::flush_cache( params.cache() );
    double time = get_wtime();
    for (int64_t i = 0; i < n; ++i) {
        blas::rotg( &a[i], &b[i], &c[i], &s[i] );
    }
    time = get_wtime() - time;
    params.time() = time * 1000;  // msec

    if (verbose >= 2) {
        printf( "a_out = " );  print_vector( n, &a[0], 1 );
        printf( "b_out = " );  print_vector( n, &b[0], 1 );
        printf( "c     = " );  print_vector( n, &c[0], 1 );
        printf( "s     = " );  print_vector( n, &s[0], 1 );
    }

    if (params.check() == 'y') {
        // run reference
        testsweeper::flush_cache( params.cache() );
        time = get_wtime();
        for (int64_t i = 0; i < n; ++i) {
            cblas_rotg( &aref[i], &bref[i], &cref[i], &sref[i] );
        }
        time = get_wtime() - time;
        params.ref_time() = time * 1000;  // msec

        if (verbose >= 2) {
            printf( "a_ref = " );  print_vector( n, &aref[0], 1 );
            printf( "b_ref = " );  print_vector( n, &bref[0], 1 );
            printf( "c_ref = " );  print_vector( n, &cref[0], 1 );
            printf( "s_ref = " );  print_vector( n, &sref[0], 1 );
        }

        // get max error of all outputs
        cblas_axpy( n, -1.0, &a[0], 1, &aref[0], 1 );
        cblas_axpy( n, -1.0, &b[0], 1, &bref[0], 1 );
        cblas_axpy( n, -1.0, &c[0], 1, &cref[0], 1 );
        cblas_axpy( n, -1.0, &s[0], 1, &sref[0], 1 );

        int64_t ia = cblas_iamax( n, &aref[0], 1 );
        int64_t ib = cblas_iamax( n, &bref[0], 1 );
        int64_t ic = cblas_iamax( n, &cref[0], 1 );
        int64_t is = cblas_iamax( n, &sref[0], 1 );

        real_t error = blas::max(
            abs( aref[ ia ] ),
            abs( bref[ ib ] ),
            abs( cref[ ic ] ),
            abs( sref[ is ] )
        );

        // error is normally 0, but allow for some rounding just in case.
        real_t u = 0.5 * std::numeric_limits< real_t >::epsilon();
        params.error() = error;
        params.okay() = (error < 10*u);

        // Applying the rotations $\begin{bmatrix} c & s \\ -s & c \end{bmatrix}$
        // to the vectors $[ a_i, b_i ]^T$
        // Expected output: $[ \sqrt{ a_i^2 + b_i^2 } & 0 ]$

        #define ABSSQ(t_) real(t_)*real(t_) + imag(t_)*imag(t_)

        std::vector<real_t> diffNorm2( n );
        for (int64_t i = 0; i < n; ++i) {
            diffNorm2[i] = ABSSQ( a_in[i] ) + ABSSQ( b_in[i] );
            blas::rot( 1, &a_in[i], 1, &b_in[i], 1, c[i], s[i] );
            diffNorm2[i] -= ABSSQ( a_in[i] );
        }

        #undef ABSSQ

        params.error2() = cblas_nrm2( n, &b_in[0], 1 );
        if (verbose >= 2) {
            printf( "null vector = " );  print_vector( n, &b_in[0], 1 );
        }

        params.error3() = cblas_nrm2( n, &diffNorm2[0], 1 );
        if (verbose >= 2) {
            printf( "null vector = " );  print_vector( n, &diffNorm2[0], 1 );
        }
    }
}

// -----------------------------------------------------------------------------
void test_rotg( Params& params, bool run )
{
    switch (params.datatype()) {
        case testsweeper::DataType::Single:
            test_rotg_work< float >( params, run );
            break;

        case testsweeper::DataType::Double:
            test_rotg_work< double >( params, run );
            break;

        case testsweeper::DataType::SingleComplex:
            test_rotg_work< std::complex<float> >( params, run );
            break;

        case testsweeper::DataType::DoubleComplex:
            test_rotg_work< std::complex<double> >( params, run );
            break;

        default:
            throw std::exception();
            break;
    }
}