summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/blas_bindings/blas_bindings_gemv.cpp
blob: 3224383133e249e0772adeb97d900d07626c3fcd (plain)
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
// Copyright (C) 2009  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.

#include "../tester.h"
#include <dlib/matrix.h>

#ifndef DLIB_USE_BLAS
#error "BLAS bindings must be used for this test to make any sense"
#endif

namespace dlib
{
    namespace blas_bindings
    {
        // This is a little screwy.  This function is used inside the BLAS
        // bindings to count how many times each of the BLAS functions get called.
#ifdef DLIB_TEST_BLAS_BINDINGS
        int& counter_gemv() { static int counter = 0; return counter; }
#endif

    }
}

namespace  
{
    using namespace test;
    using namespace std;
    // Declare the logger we will use in this test.  The name of the logger 
    // should start with "test."
    dlib::logger dlog("test.gemv");


    class blas_bindings_gemv_tester : public tester
    {
    public:
        blas_bindings_gemv_tester (
        ) :
            tester (
                "test_gemv", // the command line argument name for this test
                "Run tests for GEMV routines.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {}

        template <typename matrix_type, typename rv_type, typename cv_type>
        void test_gemv_stuff(
            matrix_type& m,
            cv_type& cv,
            rv_type& rv
        ) const
        {
            using namespace dlib;
            using namespace dlib::blas_bindings;

            cv_type cv2;
            rv_type rv2;
            typedef typename matrix_type::type scalar_type;
            scalar_type val;

            counter_gemv() = 0;
            cv2 = m*cv;
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            cv2 = m*2*cv;
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            cv2 = m*2*trans(rv);
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            rv2 = trans(m*2*cv);
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            rv2 = rv*m;
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            rv2 = (rv + rv)*m;
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            rv2 = trans(cv)*m;
            DLIB_TEST(counter_gemv() == 1);
            dlog << dlib::LTRACE << 1;

            counter_gemv() = 0;
            rv2 = trans(cv)*trans(m) + rv*trans(m);
            DLIB_TEST(counter_gemv() == 2);
            dlog << dlib::LTRACE << 2;

            counter_gemv() = 0;
            cv2 = m*trans(trans(cv)*trans(m) + 3*rv*trans(m));
            DLIB_TEST(counter_gemv() == 3);

            // This does one dot and one gemv
            counter_gemv() = 0;
            val = trans(cv)*m*trans(rv);
            DLIB_TEST_MSG(counter_gemv() == 1, counter_gemv());

            // This does one dot and two gemv 
            counter_gemv() = 0;
            val = (trans(cv)*m)*(m*trans(rv));
            DLIB_TEST_MSG(counter_gemv() == 2, counter_gemv());

            // This does one dot and two gemv 
            counter_gemv() = 0;
            val = trans(cv)*m*trans(m)*trans(rv);
            DLIB_TEST_MSG(counter_gemv() == 2, counter_gemv());
        }


        template <typename matrix_type, typename rv_type, typename cv_type>
        void test_gemv_stuff_conj(
            matrix_type& m,
            cv_type& cv,
            rv_type& rv
        ) const
        {
            using namespace dlib;
            using namespace dlib::blas_bindings;

            cv_type cv2;
            rv_type rv2;

            counter_gemv() = 0;
            cv2 = trans(cv)*conj(m);
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            cv2 = conj(trans(m))*rv;
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            cv2 = conj(trans(m))*trans(cv);
            DLIB_TEST(counter_gemv() == 1);

            counter_gemv() = 0;
            cv2 = trans(trans(cv)*conj(2*m) + conj(3*trans(m))*rv + conj(trans(m)*3)*trans(cv));
            DLIB_TEST(counter_gemv() == 3);

        }

        void perform_test (
        )
        {
            using namespace dlib;
            typedef dlib::memory_manager<char>::kernel_1a mm;

            dlog << dlib::LINFO << "test double";
            {
                matrix<double> m = randm(4,4);
                matrix<double,0,1> cv = randm(4,1);
                matrix<double,1,0> rv = randm(1,4);
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test float";
            {
                matrix<float> m = matrix_cast<float>(randm(4,4));
                matrix<float,0,1> cv = matrix_cast<float>(randm(4,1));
                matrix<float,1,0> rv = matrix_cast<float>(randm(1,4));
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test complex<double>";
            {
                matrix<complex<double> > m = complex_matrix(randm(4,4), randm(4,4));
                matrix<complex<double>,0,1> cv = complex_matrix(randm(4,1), randm(4,1));
                matrix<complex<double>,1,0> rv = complex_matrix(randm(1,4), randm(1,4));
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test complex<float>";
            {
                matrix<complex<float> > m = matrix_cast<complex<float> >(complex_matrix(randm(4,4), randm(4,4)));
                matrix<complex<float>,0,1> cv = matrix_cast<complex<float> >(complex_matrix(randm(4,1), randm(4,1)));
                matrix<complex<float>,1,0> rv = matrix_cast<complex<float> >(complex_matrix(randm(1,4), randm(1,4)));
                test_gemv_stuff(m,cv,rv);
            }


            dlog << dlib::LINFO << "test double";
            {
                matrix<double,0,0,mm,column_major_layout> m = randm(4,4);
                matrix<double,0,1,mm,column_major_layout> cv = randm(4,1);
                matrix<double,1,0,mm,column_major_layout> rv = randm(1,4);
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test float";
            {
                matrix<float,0,0,mm,column_major_layout> m = matrix_cast<float>(randm(4,4));
                matrix<float,0,1,mm,column_major_layout> cv = matrix_cast<float>(randm(4,1));
                matrix<float,1,0,mm,column_major_layout> rv = matrix_cast<float>(randm(1,4));
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test complex<double>";
            {
                matrix<complex<double>,0,0,mm,column_major_layout > m = complex_matrix(randm(4,4), randm(4,4));
                matrix<complex<double>,0,1,mm,column_major_layout> cv = complex_matrix(randm(4,1), randm(4,1));
                matrix<complex<double>,1,0,mm,column_major_layout> rv = complex_matrix(randm(1,4), randm(1,4));
                test_gemv_stuff(m,cv,rv);
            }

            dlog << dlib::LINFO << "test complex<float>";
            {
                matrix<complex<float>,0,0,mm,column_major_layout > m = matrix_cast<complex<float> >(complex_matrix(randm(4,4), randm(4,4)));
                matrix<complex<float>,0,1,mm,column_major_layout> cv = matrix_cast<complex<float> >(complex_matrix(randm(4,1), randm(4,1)));
                matrix<complex<float>,1,0,mm,column_major_layout> rv = matrix_cast<complex<float> >(complex_matrix(randm(1,4), randm(1,4)));
                test_gemv_stuff(m,cv,rv);
            }


            print_spinner();
        }
    };

    blas_bindings_gemv_tester a;

}