| 1 |
/* |
| 2 |
* Copyright (C) the FFLAS-FFPACK group |
| 3 |
* Written by Pascal Giorgi <[email protected]> |
| 4 |
* |
| 5 |
* This file is Free Software and part of FFLAS-FFPACK. |
| 6 |
* |
| 7 |
* ========LICENCE======== |
| 8 |
* This file is part of the library FFLAS-FFPACK. |
| 9 |
* |
| 10 |
* FFLAS-FFPACK is free software: you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public |
| 12 |
* License as published by the Free Software Foundation; either |
| 13 |
* version 2.1 of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This library is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this library; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
* ========LICENCE======== |
| 24 |
*. |
| 25 |
*/ |
| 26 |
#include <iomanip> |
| 27 |
#include <iostream> |
| 28 |
#include <random> |
| 29 |
|
| 30 |
#include "fflas-ffpack/utils/args-parser.h" |
| 31 |
#include "fflas-ffpack/utils/test-utils.h" |
| 32 |
#include "fflas-ffpack/utils/fflas_io.h" |
| 33 |
#include "fflas-ffpack/utils/fflas_memory.h" |
| 34 |
|
| 35 |
#include <givaro/modular.h> |
| 36 |
#include <recint/rint.h> |
| 37 |
|
| 38 |
#include "fflas-ffpack/fflas/fflas_transpose.h" |
| 39 |
|
| 40 |
using namespace std; |
| 41 |
using namespace FFLAS; |
| 42 |
using namespace FFPACK; |
| 43 |
|
| 44 |
using Givaro::Modular; |
| 45 |
using Givaro::ModularBalanced; |
| 46 |
|
| 47 |
/******************************************************************************/ |
| 48 |
template <typename Elt> |
| 49 |
class Bench { |
| 50 |
public: |
| 51 |
using Field = Modular<Elt>; |
| 52 |
using Elt_ptr = typename Field::Element_ptr; |
| 53 |
using Residu = typename Field::Residu_t; |
| 54 |
template <bool B, class T = void> |
| 55 |
using enable_if_t = typename std::enable_if<B, T>::type; |
| 56 |
template <typename Simd> |
| 57 |
using is_same_element = typename Simd::template is_same_element<Field>; |
| 58 |
template <typename E> |
| 59 |
using enable_if_no_simd_t = enable_if_t<Simd<E>::vect_size == 1>; |
| 60 |
template <typename E> |
| 61 |
using enable_if_simd128_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 16>; |
| 62 |
template <typename E> |
| 63 |
using enable_if_simd256_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 32>; |
| 64 |
template <typename E> |
| 65 |
using enable_if_simd512_t = enable_if_t<sizeof(E)*Simd<E>::vect_size == 64>; |
| 66 |
|
| 67 |
/* ctor */ |
| 68 |
Bench (size_t m, size_t n, size_t iters, bool inplace) |
| 69 |
: F(cardinality()), m(m), n(n), iters(iters), inplace(inplace) { |
| 70 |
} |
| 71 |
|
| 72 |
/* */ |
| 73 |
template <typename _E = Elt, |
| 74 |
enable_if_t<!is_same<_E, Givaro::Integer>::value>* = nullptr> |
| 75 |
static Residu |
| 76 |
cardinality () { |
| 77 |
return Field::maxCardinality(); |
| 78 |
} |
| 79 |
|
| 80 |
template <typename _E = Elt, |
| 81 |
enable_if_t<is_same<_E, Givaro::Integer>::value>* = nullptr> |
| 82 |
static Residu |
| 83 |
cardinality () { |
| 84 |
/* Bench Givaro::Integer with a large (=more than a word) modulus */ |
| 85 |
return Givaro::Integer ("0x100000000000000000000000000000000"); |
| 86 |
} |
| 87 |
|
| 88 |
/* main test function */ |
| 89 |
template <typename Simd = NoSimd<Elt>, |
| 90 |
enable_if_t<is_same_element<Simd>::value>* = nullptr> |
| 91 |
void doBenchs () { |
| 92 |
Elt_ptr A = nullptr, B = nullptr; |
| 93 |
const size_t lda = n; |
| 94 |
const size_t ldb = m; |
| 95 |
|
| 96 |
if (inplace) { |
| 97 |
A = B = fflas_new (F, m, lda); |
| 98 |
} |
| 99 |
else { |
| 100 |
A = fflas_new (F, m, lda); |
| 101 |
B = fflas_new (F, n, ldb); |
| 102 |
finit (F, n, m, B, ldb); |
| 103 |
} |
| 104 |
|
| 105 |
finit (F, m, n, A, lda); |
| 106 |
for (size_t i = 0; i < m; i++) |
| 107 |
for (size_t j = 0; j < n; j++) |
| 108 |
F.assign (A[i*lda+j], (uint64_t) (i << 16) + j); |
| 109 |
|
| 110 |
Givaro::Timer chrono; |
| 111 |
chrono.clear(); chrono.start(); |
| 112 |
for (size_t i = 0; i < iters; i++) { |
| 113 |
ftranspose<Field, Simd> (F, m, n, A, lda, B, ldb); |
| 114 |
} |
| 115 |
chrono.stop(); |
| 116 |
double time = chrono.usertime(); |
| 117 |
|
| 118 |
fflas_delete (A); |
| 119 |
if (!inplace) { |
| 120 |
fflas_delete (B); |
| 121 |
} |
| 122 |
|
| 123 |
/* print results */ |
| 124 |
/* mul by 2: 1 read + 1 write per element of the matrix */ |
| 125 |
double throughput = (2*iters*m*n*sizeof(Elt)) / (time * (1 << 30)); |
| 126 |
std::cout << F.type_string() |
| 127 |
<< string (27-F.type_string().size(), '.') |
| 128 |
<< Simd::type_string() |
| 129 |
<< string (26-Simd::type_string().size(), '.') |
| 130 |
<< (inplace ? "inplace " : "....... ") |
| 131 |
<< setw (7) << fixed << setprecision(2) |
| 132 |
<< 1000*time / ((double) iters) << "ms " |
| 133 |
<< setw (5) << fixed << setprecision(2) |
| 134 |
<< throughput << "GB/s" << endl; |
| 135 |
} |
| 136 |
|
| 137 |
/* run tests: call doBenchs for best Simd structs or all available Simd |
| 138 |
* structs, depending on parameter. |
| 139 |
*/ |
| 140 |
template <typename _E = Elt, |
| 141 |
enable_if_t<is_same<_E, Elt>::value>* = nullptr, |
| 142 |
enable_if_no_simd_t<_E>* = nullptr> |
| 143 |
void run (bool allsimd) { |
| 144 |
doBenchs (); |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef __FFLASFFPACK_HAVE_SSE4_1_INSTRUCTIONS |
| 148 |
template <typename _E = Elt, |
| 149 |
enable_if_t<is_same<_E, Elt>::value>* = nullptr, |
| 150 |
enable_if_t<Simd<_E>::vect_size != 1>* = nullptr, |
| 151 |
enable_if_simd128_t<_E>* = nullptr> |
| 152 |
void run (bool allsimd) { |
| 153 |
if (allsimd) { |
| 154 |
doBenchs (); |
| 155 |
} |
| 156 |
doBenchs<Simd128<Elt>> (); |
| 157 |
} |
| 158 |
#endif |
| 159 |
|
| 160 |
#ifdef __FFLASFFPACK_HAVE_AVX_INSTRUCTIONS |
| 161 |
template <typename _E = Elt, |
| 162 |
enable_if_t<is_same<_E, Elt>::value>* = nullptr, |
| 163 |
enable_if_t<Simd<_E>::vect_size != 1>* = nullptr, |
| 164 |
enable_if_simd256_t<_E>* = nullptr> |
| 165 |
void run (bool allsimd) { |
| 166 |
if (allsimd) { |
| 167 |
doBenchs (); |
| 168 |
doBenchs<Simd128<Elt>> (); |
| 169 |
} |
| 170 |
doBenchs<Simd256<Elt>> (); |
| 171 |
} |
| 172 |
#endif |
| 173 |
|
| 174 |
#ifdef __FFLASFFPACK_HAVE_AVX512DQ_INSTRUCTIONS |
| 175 |
template <typename _E = Elt, |
| 176 |
enable_if_t<is_same<_E, Elt>::value>* = nullptr, |
| 177 |
enable_if_t<Simd<_E>::vect_size != 1>* = nullptr, |
| 178 |
enable_if_simd512_t<_E>* = nullptr> |
| 179 |
void run (bool allsimd) { |
| 180 |
if (allsimd) { |
| 181 |
doBenchs (); |
| 182 |
doBenchs<Simd128<Elt>> (); |
| 183 |
doBenchs<Simd256<Elt>> (); |
| 184 |
} |
| 185 |
doBenchs<Simd512<Elt>> (); |
| 186 |
} |
| 187 |
|
| 188 |
/* Workaround for Simd512<(u)int32/16_t> which does not exist */ |
| 189 |
template <typename _E = Elt, |
| 190 |
enable_if_t<is_same<_E, uint32_t>::value |
| 191 |
|| is_same<_E, int32_t>::value |
| 192 |
|| is_same<_E, uint16_t>::value |
| 193 |
|| is_same<_E, int16_t>::value |
| 194 |
>* = nullptr> |
| 195 |
void run (bool allsimd) { |
| 196 |
if (allsimd) { |
| 197 |
doBenchs (); |
| 198 |
doBenchs<Simd128<Elt>> (); |
| 199 |
} |
| 200 |
doBenchs<Simd256<Elt>> (); |
| 201 |
} |
| 202 |
#endif |
| 203 |
|
| 204 |
protected: |
| 205 |
Field F; |
| 206 |
const size_t m; |
| 207 |
const size_t n; |
| 208 |
const size_t iters; |
| 209 |
const bool inplace; |
| 210 |
}; |
| 211 |
|
| 212 |
/******************************************************************************/ |
| 213 |
int main(int argc, char** argv) |
| 214 |
{ |
| 215 |
size_t iters = 20; |
| 216 |
size_t m = 6144; |
| 217 |
size_t n = 4096; |
| 218 |
bool allsimd = false; |
| 219 |
Argument as[] = { |
| 220 |
{ 'm', "-m M", "Set the row dimension of the matrix.", |
| 221 |
TYPE_INT, &m }, |
| 222 |
{ 'n', "-n N", "Set the column dimension of the matrix.", |
| 223 |
TYPE_INT, &n }, |
| 224 |
{ 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iters }, |
| 225 |
{ 'a', "-a Y/N", "benchmarks all Simd structs (default: no).", |
| 226 |
TYPE_BOOL, &allsimd }, |
| 227 |
|
| 228 |
END_OF_ARGUMENTS |
| 229 |
}; |
| 230 |
|
| 231 |
/* parse command-line */ |
| 232 |
FFLAS::parseArguments (argc, argv, as); |
| 233 |
cout << "# To rerun this bench: " << argv[0] << " -m " << m << " -n " << n |
| 234 |
<< " -i " << iters << (allsimd ? " -a" : "") <<endl; |
| 235 |
|
| 236 |
bool inplace = true; |
| 237 |
do { |
| 238 |
inplace = not inplace; |
| 239 |
cout << endl << "Benchmarking transpose on matrices of size " << m |
| 240 |
<< "x" << n << (inplace ? " [inplace variant]" : "") << ":" |
| 241 |
<< endl; |
| 242 |
|
| 243 |
Bench<double>(m,n,iters,inplace).run(allsimd); |
| 244 |
Bench<uint64_t>(m,n,iters,inplace).run(allsimd); |
| 245 |
Bench<float>(m,n,iters,inplace).run(allsimd); |
| 246 |
Bench<uint32_t>(m,n,iters,inplace).run(allsimd); |
| 247 |
Bench<uint16_t>(m,n,iters,inplace).run(allsimd); |
| 248 |
|
| 249 |
/* Bench<Givaro::Integer>(m,n,iters,inplace).run(allsimd); */ |
| 250 |
/* Bench<RecInt::ruint<6>>(m,n,iters,inplace).run(allsimd); */ |
| 251 |
/* Bench<RecInt::ruint<7>>(m,n,iters,inplace).run(allsimd); */ |
| 252 |
/* Bench<RecInt::ruint<8>>(m,n,iters,inplace).run(allsimd); */ |
| 253 |
} |
| 254 |
while (not inplace) ; |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |