summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/blas_bindings/blas_bindings_gemm.cpp
blob: 83d41edd1e2a17838cf6b9c834bd01782f4ed992 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// 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_gemm() { 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.gemm");


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

        template <typename matrix_type>
        void test_gemm_stuff(
            const matrix_type& c
        ) const
        {
            using namespace dlib;
            using namespace dlib::blas_bindings;

            matrix_type b, a;
            a = c;

            counter_gemm() = 0;
            b = a*a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a/2*a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a*trans(a) + a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = (a+a)*(a+a);
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a*(a-a);
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = trans(a)*trans(a) + a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = trans(trans(trans(a)*a + a));
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a*a*a*a;
            DLIB_TEST(counter_gemm() == 3);
            b = c;

            counter_gemm() = 0;
            a = a*a*a*a;
            DLIB_TEST(counter_gemm() == 3);
            a = c;

            counter_gemm() = 0;
            a = (b + a*trans(a)*a*3*a)*trans(b);
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*trans(a)*a*3*a)*trans(b));
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*(a)*trans(a)*3*a)*trans(b));
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*(a + b)*trans(a)*3*a)*trans(b));
            DLIB_TEST_MSG(counter_gemm() == 4, counter_gemm());
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*(a*8 + b+b+b+b)*trans(a)*3*a)*trans(b));
            DLIB_TEST_MSG(counter_gemm() == 4, counter_gemm());
            a = c;
        }

        template <typename matrix_type>
        void test_gemm_stuff_conj(
            const matrix_type& c
        ) const
        {
            using namespace dlib;
            using namespace dlib::blas_bindings;

            matrix_type b, a;
            a = c;

            counter_gemm() = 0;
            b = a*conj(a);
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a*trans(conj(a)) + a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = conj(trans(a))*trans(a) + a;
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = trans(trans(trans(a)*conj(a) + conj(a)));
            DLIB_TEST(counter_gemm() == 1);

            counter_gemm() = 0;
            b = a*a*conj(a)*a;
            DLIB_TEST(counter_gemm() == 3);
            b = c;

            counter_gemm() = 0;
            a = a*trans(conj(a))*a*a;
            DLIB_TEST(counter_gemm() == 3);
            a = c;

            counter_gemm() = 0;
            a = (b + a*trans(conj(a))*a*3*a)*trans(b);
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = (trans((conj(trans(b)) + trans(a)*conj(trans(a))*a*3*a)*trans(b)));
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = ((trans(b) + trans(a)*(a)*trans(a)*3*a)*trans(conj(b)));
            DLIB_TEST(counter_gemm() == 4);
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*conj(a + b)*trans(a)*3*a)*trans(b));
            DLIB_TEST_MSG(counter_gemm() == 4, counter_gemm());
            a = c;

            counter_gemm() = 0;
            a = trans((trans(b) + trans(a)*(a*8 + b+b+b+b)*trans(a)*3*conj(a))*trans(b));
            DLIB_TEST_MSG(counter_gemm() == 4, counter_gemm());
            a = c;
        }

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

            print_spinner();

            dlog << dlib::LINFO << "test double";
            {
                matrix<double> a = randm(4,4);
                test_gemm_stuff(a);
            }

            print_spinner();
            dlog << dlib::LINFO << "test float";
            {
                matrix<float> a = matrix_cast<float>(randm(4,4));
                test_gemm_stuff(a);
            }

            print_spinner();
            dlog << dlib::LINFO << "test complex<float>";
            {
                matrix<float> a = matrix_cast<float>(randm(4,4));
                matrix<float> b = matrix_cast<float>(randm(4,4));
                matrix<complex<float> > c = complex_matrix(a,b);
                test_gemm_stuff(c);
                test_gemm_stuff_conj(c);
            }

            print_spinner();
            dlog << dlib::LINFO << "test complex<double>";
            {
                matrix<double> a = matrix_cast<double>(randm(4,4));
                matrix<double> b = matrix_cast<double>(randm(4,4));
                matrix<complex<double> > c = complex_matrix(a,b);
                test_gemm_stuff(c);
                test_gemm_stuff_conj(c);
            }


            print_spinner();

            dlog << dlib::LINFO << "test double, column major";
            {
                matrix<double,100,100,mm,column_major_layout> a = randm(100,100);
                test_gemm_stuff(a);
            }

            print_spinner();
            dlog << dlib::LINFO << "test float, column major";
            {
                matrix<float,100,100,mm,column_major_layout> a = matrix_cast<float>(randm(100,100));
                test_gemm_stuff(a);
            }

            print_spinner();
            dlog << dlib::LINFO << "test complex<double>, column major";
            {
                matrix<double,100,100,mm,column_major_layout> a = matrix_cast<double>(randm(100,100));
                matrix<double,100,100,mm,column_major_layout> b = matrix_cast<double>(randm(100,100));
                matrix<complex<double>,100,100,mm,column_major_layout > c = complex_matrix(a,b);
                test_gemm_stuff(c);
                test_gemm_stuff_conj(c);
            }

            print_spinner();

            dlog << dlib::LINFO << "test complex<float>, column major";
            {
                matrix<float,100,100,mm,column_major_layout> a = matrix_cast<float>(randm(100,100));
                matrix<float,100,100,mm,column_major_layout> b = matrix_cast<float>(randm(100,100));
                matrix<complex<float>,100,100,mm,column_major_layout > c = complex_matrix(a,b);
                test_gemm_stuff(c);
                test_gemm_stuff_conj(c);
            }

            {
                using namespace dlib;
                using namespace dlib::blas_bindings;
                array2d<double> a(100,100);
                array2d<double> b(100,100);
                matrix<double> c;
                
                counter_gemm() = 0;
                c = mat(a)*mat(b);
                DLIB_TEST(counter_gemm() == 1);

                counter_gemm() = 0;
                c = trans(2*mat(a)*mat(b));
                DLIB_TEST(counter_gemm() == 1);
            }

            {
                using namespace dlib;
                using namespace dlib::blas_bindings;
                array2d<double> a(100,100);
                array2d<double> b(100,100);
                matrix<double> aa(100,100);
                matrix<double> bb(100,100);
                matrix<double> c;
                
                counter_gemm() = 0;
                c = mat(&a[0][0],100,100)*mat(&b[0][0],100,100);
                DLIB_TEST(counter_gemm() == 1);
                set_ptrm(&c(0,0),100,100) = mat(&a[0][0],100,100)*mat(&b[0][0],100,100);
                DLIB_TEST(counter_gemm() == 2);
                set_ptrm(&c(0,0),100,100) = aa*bb;
                DLIB_TEST(counter_gemm() == 3);

                counter_gemm() = 0;
                c = trans(2*mat(&a[0][0],100,100)*mat(&b[0][0],100,100));
                DLIB_TEST(counter_gemm() == 1);
                set_ptrm(&c(0,0),100,100) = trans(2*mat(&a[0][0],100,100)*mat(&b[0][0],100,100));
                DLIB_TEST(counter_gemm() == 2);
                set_ptrm(&c(0,0),100,100) = trans(2*mat(a)*mat(b));
                DLIB_TEST(counter_gemm() == 3);
            }

            print_spinner();
        }
    };

    blas_bindings_gemm_tester a;

}