File: test-storage-transpose.C

package info (click to toggle)
fflas-ffpack 2.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,200 kB
  • sloc: cpp: 26,712; makefile: 784; sh: 430; csh: 95; ansic: 20
file content (308 lines) | stat: -rw-r--r-- 10,650 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) the FFLAS-FFPACK group
 * Written by Pascal Giorgi <pascal.giorgi@lirmm.fr>
 *
 * This file is Free Software and part of FFLAS-FFPACK.
 *
 * ========LICENCE========
 * This file is part of the library FFLAS-FFPACK.
 *
 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 *.
 */
#include <iomanip>
#include <iostream>
#include <random>

#include "fflas-ffpack/utils/args-parser.h"
#include "fflas-ffpack/utils/test-utils.h"
#include "fflas-ffpack/utils/fflas_io.h"
#include "fflas-ffpack/utils/fflas_memory.h"

#include <givaro/modular.h>
#include <recint/rint.h>

#include "fflas-ffpack/fflas/fflas_transpose.h"

using namespace std;
using namespace FFLAS;
using namespace FFPACK;

using Givaro::Modular;
using Givaro::ModularBalanced;

/******************************************************************************/
template <typename Elt>
class Test {
public:
    using Field = Modular<Elt>;
    using Elt_ptr = typename Field::Element_ptr;
    using Residu = typename Field::Residu_t;
    template <bool B, class T = void>
    using enable_if_t = typename std::enable_if<B, T>::type;
    template <typename Simd>
    using is_same_element = typename Simd::template is_same_element<Field>;
    template <typename E>
    using enable_if_no_simd_t = enable_if_t<Simd<E>::vect_size == 1>;
    template <typename E>
    using enable_if_simd128_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 16>;
    template <typename E>
    using enable_if_simd256_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 32>;
    template <typename E>
    using enable_if_simd512_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 64>;

        /* ctor */
    Test (size_t mm, size_t nn) : F(cardinality()), _mm(mm), _nn(nn) {
    }

        /* */
    template <typename _E = Elt,
              enable_if_t<!is_same<_E, Givaro::Integer>::value>* = nullptr>
    static Residu
    cardinality () {
        return Field::maxCardinality();
    }

    template <typename _E = Elt,
              enable_if_t<is_same<_E, Givaro::Integer>::value>* = nullptr>
    static Residu
    cardinality () {
            /* Test Givaro::Integer with a large (=more than a word) modulus */
        return Givaro::Integer ("0x100000000000000000000000000000000");
    }

        /* main test function */
    template <typename Simd = NoSimd<Elt>,
              enable_if_t<is_same_element<Simd>::value>* = nullptr>
    bool test_ftranspose (size_t m, size_t n, Elt_ptr A, size_t lda,
                          Elt_ptr B, size_t ldb) {
        Elt_ptr M, Mt;
        if (A == B) {
            M = fflas_new (F, ldb, lda);
            finit (F, ldb, lda, M, lda);
            fassign (F, m, n, A, lda, M, lda);
            Mt = M;
        }
        else {
            M = A;
            Mt = B;
        }
        ftranspose<Field, Simd> (F, m, n, M, lda, Mt, ldb);

        bool ok = true;
        for (size_t i = 0; i < m; i++)
            for (size_t j = 0; j < n; j++)
                ok = ok && ((*(Mt+j*ldb+i)) == (*(A+i*lda+j)));

        if (!ok) {
            cerr << "Error, with " << m << "x" << n << " matrix"
                 << (lda == n ? "" : " with stride " + to_string(lda))
                 << (A == B ? " (inplace variant)" : "") << " over "
                 << F.type_string() << " using " << Simd::type_string()
                 << endl;
            for (size_t i = 0; i < m; i++)
                for (size_t j = 0; j < n; j++)
                    if ((*(Mt+j*ldb+i)) != (*(A+i*lda+j))) {
                        cerr << "   at index (" << j << ", " << i
                             << ") expected " << *(A+i*lda+j) << " got "
                             << *(Mt+j*ldb+i) << endl;
                    }
        }

        if (A == B) {
            fflas_delete (M);
        }
        return ok;
    }

        /* perform the tests for all sizes and types of matrices */
    template <typename Simd = NoSimd<Elt>,
              enable_if_t<is_same_element<Simd>::value>* = nullptr>
    bool doTests () {
        bool ok = true;

            /* square matrices */
        size_t nrows[] = { 3*FFLAS_TRANSPOSE_BLOCKSIZE,
                           3*FFLAS_TRANSPOSE_BLOCKSIZE+Simd::vect_size,
                           3*FFLAS_TRANSPOSE_BLOCKSIZE+Simd::vect_size+3,
                           _mm};
        for (auto m: nrows) {
            Elt_ptr M = fflas_new (F, m, m);
            Elt_ptr Mt = fflas_new (F, m, m);

            finit (F, m, m, M, m);
            for (size_t i = 0; i < m; i++)
                for (size_t j = 0; j < m; j++)
                    F.assign (M[i*m+j], (uint64_t)(i << 8) + j);
            finit (F, m, m, Mt, m);

                /* not inplace full matrix */
            ok &= test_ftranspose<Simd> (m, m, M, m, Mt, m);

                /* not inplace submatrix */
            size_t s = m - FFLAS_TRANSPOSE_BLOCKSIZE;
            ok &= test_ftranspose<Simd> (s, s, M, m, Mt, m);

                /* inplace full matrix */
            ok &= test_ftranspose<Simd> (m, m, M, m, M, m);

                /* inplace submatrix */
            s = m - Simd::vect_size;
            ok &= test_ftranspose<Simd> (s, s, M, m, M, m);

            fflas_delete (M);
            fflas_delete (Mt);
        }

            /* non square matrices */
        size_t ncols[] = { 2*FFLAS_TRANSPOSE_BLOCKSIZE,
                           4*FFLAS_TRANSPOSE_BLOCKSIZE+Simd::vect_size,
                           3*FFLAS_TRANSPOSE_BLOCKSIZE+2*Simd::vect_size+1,
                           _nn};
        for (size_t i = 0; i < 3; i++) {
            size_t m = nrows[i];
            size_t n = ncols[i];

            Elt_ptr M = fflas_new (F, m, n);
            Elt_ptr Mt = fflas_new (F, n, m);

            finit (F, m, n, M, n);
            for (size_t i = 0; i < m; i++)
                for (size_t j = 0; j < n; j++)
                    F.assign (M[i*n+j], (uint64_t)(i << 8) + j);
            finit (F, n, m, Mt, m);

                /* not inplace full matrix */
            ok &= test_ftranspose<Simd> (m, n, M, n, Mt, m);

                /* not inplace submatrix */
            size_t s = m - Simd::vect_size;
            size_t t = n - 2*Simd::vect_size+1;
            ok &= test_ftranspose<Simd> (s, t, M, n, Mt, m);

                /* inplace full matrix */
            ok &= test_ftranspose<Simd> (m, n, M, n, M, m);

            fflas_delete (M);
            fflas_delete (Mt);
        }

            /* print results */
        std::cout << F.type_string()
                  << string (36-F.type_string().size(), '.') << " "
                  << Simd::type_string()
                  << string (36-Simd::type_string().size(), '.') << " "
                  << (ok ? "PASSED" : "FAILED")
                  << endl;

        return ok;
    }

        /* run tests: call doTests for all available Simd structs */
    template <typename _E = Elt,
              enable_if_t<is_same<_E, Elt>::value>* = nullptr,
              enable_if_no_simd_t<_E>* = nullptr>
    bool run () {
        return doTests();
    }

#ifdef __FFLASFFPACK_HAVE_SSE4_1_INSTRUCTIONS
    template <typename _E = Elt,
              enable_if_t<is_same<_E, Elt>::value>* = nullptr,
              enable_if_t<Simd<_E>::vect_size != 1>* = nullptr,
              enable_if_simd128_t<_E>* = nullptr>
    bool run () {
        return doTests() & doTests<Simd128<Elt>>();
    }
#endif

#ifdef __FFLASFFPACK_HAVE_AVX_INSTRUCTIONS
    template <typename _E = Elt,
              enable_if_t<is_same<_E, Elt>::value>* = nullptr,
              enable_if_t<Simd<_E>::vect_size != 1>* = nullptr,
              enable_if_simd256_t<_E>* = nullptr>
    bool run () {
        return doTests() & doTests<Simd128<Elt>>()
            & doTests<Simd256<Elt>>();
    }
#endif

#ifdef __FFLASFFPACK_HAVE_AVX512DQ_INSTRUCTIONS
    template <typename _E = Elt,
              enable_if_t<is_same<_E, Elt>::value>* = nullptr,
              enable_if_t<Simd<_E>::vect_size != 1>* = nullptr,
              enable_if_simd512_t<_E>* = nullptr>
    bool run () {
        return doTests() & doTests<Simd128<Elt>>()
            & doTests<Simd256<Elt>>() & doTests<Simd512<Elt>>();
    }

        /* Workaround for Simd512<(u)int32/16_t> which does not exist */
    template <typename _E = Elt,
              enable_if_t<is_same<_E, uint32_t>::value
                          || is_same<_E, int32_t>::value
                          || is_same<_E, uint16_t>::value
                          || is_same<_E, int16_t>::value
                          >* = nullptr>
    bool run () {
        return doTests() & doTests<Simd128<Elt>>()
            & doTests<Simd256<Elt>>();
    }
#endif

protected:
    Field F;
    size_t _mm,_nn;
};

/******************************************************************************/
int main(int argc, char** argv)
{
    std::cout << std::setprecision(17);
    std::cerr << std::setprecision(17);

    size_t m = 1000;
    size_t n = 2000;
    
    Argument as[] = {
        { 'm', "-m M", "Set the dimension m",         TYPE_INT , &m },
        { 'n', "-n N", "Set the dimension n",         TYPE_INT , &n },
        END_OF_ARGUMENTS
    };

    parseArguments(argc,argv,as);
    bool ok = true;

    ok &= Test<float>(m,n).run();
    ok &= Test<double>(m,n).run();
    ok &= Test<uint64_t>(m,n).run();
    ok &= Test<int64_t>(m,n).run();
    ok &= Test<uint32_t>(m,n).run();
    ok &= Test<int32_t>(m,n).run();
    ok &= Test<uint16_t>(m,n).run();
    ok &= Test<int16_t>(m,n).run();
    ok &= Test<Givaro::Integer>(m,n).run();
    ok &= Test<RecInt::rint<6>>(m,n).run();
    ok &= Test<RecInt::ruint<6>>(m,n).run();
    ok &= Test<RecInt::rint<7>>(m,n).run();
    ok &= Test<RecInt::ruint<7>>(m,n).run();
    ok &= Test<RecInt::rint<8>>(m,n).run();
    ok &= Test<RecInt::ruint<8>>(m,n).run();

    return !ok;
}
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s