| 1 |
/* |
| 2 |
* Copyright (C) FFLAS-FFPACK |
| 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 |
|
| 27 |
// declare that the call to openblas_set_numthread will be made here, hence don't do it |
| 28 |
// everywhere in the call stack |
| 29 |
#define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET 1 |
| 30 |
|
| 31 |
|
| 32 |
#if not defined(MG_DEFAULT) |
| 33 |
#define MG_DEFAULT MG_ACTIVE |
| 34 |
#endif |
| 35 |
#if not defined(STD_RECINT_SIZE) |
| 36 |
#define STD_RECINT_SIZE 8 |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "fflas-ffpack/fflas-ffpack-config.h" |
| 40 |
#include <iostream> |
| 41 |
#include <typeinfo> |
| 42 |
#include <vector> |
| 43 |
#include <string> |
| 44 |
using namespace std; |
| 45 |
|
| 46 |
#include "fflas-ffpack/fflas/fflas.h" |
| 47 |
#include "fflas-ffpack/utils/fflas_io.h" |
| 48 |
#include "fflas-ffpack/utils/timer.h" |
| 49 |
#include "fflas-ffpack/utils/args-parser.h" |
| 50 |
#include "givaro/modular-integer.h" |
| 51 |
#include "givaro/givcaster.h" |
| 52 |
#include "fflas-ffpack/paladin/parallel.h" |
| 53 |
#ifdef BENCH_RECINT |
| 54 |
#include "recint/recint.h" |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef BENCH_FLINT |
| 58 |
#define __GMP_BITS_PER_MP_LIMB 64 |
| 59 |
extern "C" { |
| 60 |
#include "flint/longlong.h" |
| 61 |
#include "flint/long_extras.h" |
| 62 |
#include "flint/fmpz_mat.h" |
| 63 |
#include "flint/fmpz.h" |
| 64 |
#include "flint/flint.h" |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
static size_t iters = 3 ; |
| 70 |
static Givaro::Integer q = -1 ; |
| 71 |
static unsigned long b = 512 ; |
| 72 |
static size_t m = 512 ; |
| 73 |
static size_t k = 512 ; |
| 74 |
static size_t n = 512 ; |
| 75 |
static int nbw = -1 ; |
| 76 |
static size_t seed= time(NULL); |
| 77 |
static Argument as[] = { |
| 78 |
{ 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER , &q }, |
| 79 |
{ 'b', "-b B", "Set the bitsize of the random characteristic.", TYPE_INT , &b }, |
| 80 |
{ 'm', "-m M", "Set the dimension m of the matrix.", TYPE_INT , &m }, |
| 81 |
{ 'k', "-k K", "Set the dimension k of the matrix.", TYPE_INT , &k }, |
| 82 |
{ 'n', "-n N", "Set the dimension n of the matrix.", TYPE_INT , &n }, |
| 83 |
{ 'w', "-w N", "Set the number of winograd levels (-1 for random).", TYPE_INT , &nbw }, |
| 84 |
{ 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iters }, |
| 85 |
{ 's', "-s S", "Sets seed.", TYPE_INT , &seed }, |
| 86 |
END_OF_ARGUMENTS |
| 87 |
}; |
| 88 |
|
| 89 |
template<typename Ints> |
| 90 |
int tmain(){ |
| 91 |
srand( (int)seed); |
| 92 |
srand48(seed); |
| 93 |
Givaro::Integer::seeding(seed); |
| 94 |
|
| 95 |
typedef Givaro::Modular<Ints> Field; |
| 96 |
Givaro::Integer p; |
| 97 |
FFLAS::Timer chrono, TimFreivalds; |
| 98 |
double time=0.,timev=0.; |
| 99 |
#ifdef BENCH_FLINT |
| 100 |
double timeFlint=0.; |
| 101 |
#endif |
| 102 |
for (size_t loop=0;loop<iters;loop++){ |
| 103 |
Givaro::Integer::random_exact_2exp(p, b); |
| 104 |
Givaro::IntPrimeDom IPD; |
| 105 |
IPD.nextprimein(p); |
| 106 |
Ints ip; Givaro::Caster<Ints,Givaro::Integer>(ip,p); |
| 107 |
Givaro::Caster<Givaro::Integer,Ints>(p,ip); // to check consistency |
| 108 |
|
| 109 |
Field F(ip); |
| 110 |
size_t lda,ldb,ldc; |
| 111 |
lda=k; |
| 112 |
ldb=n; |
| 113 |
ldc=n; |
| 114 |
|
| 115 |
typename Field::RandIter Rand(F,seed); |
| 116 |
typename Field::Element_ptr A,B,C; |
| 117 |
A= FFLAS::fflas_new(F,m,lda); |
| 118 |
B= FFLAS::fflas_new(F,k,ldb); |
| 119 |
C= FFLAS::fflas_new(F,m,ldc); |
| 120 |
|
| 121 |
// for (size_t i=0;i<m;++i) |
| 122 |
// for (size_t j=0;j<k;++j) |
| 123 |
// Rand.random(A[i*lda+j]); |
| 124 |
// for (size_t i=0;i<k;++i) |
| 125 |
// for (size_t j=0;j<n;++j) |
| 126 |
// Rand.random(B[i*ldb+j]); |
| 127 |
// for (size_t i=0;i<m;++i) |
| 128 |
// for (size_t j=0;j<n;++j) |
| 129 |
// Rand.random(C[i*ldc+j]); |
| 130 |
|
| 131 |
PAR_BLOCK { FFLAS::pfrand(F,Rand, m,k,A,m/size_t(MAX_THREADS)); } |
| 132 |
PAR_BLOCK { FFLAS::pfrand(F,Rand, k,n,B,k/MAX_THREADS); } |
| 133 |
PAR_BLOCK { FFLAS::pfzero(F, m,n,C,m/MAX_THREADS); } |
| 134 |
|
| 135 |
|
| 136 |
Ints alpha,beta; |
| 137 |
alpha=F.one; |
| 138 |
beta=F.zero; |
| 139 |
|
| 140 |
|
| 141 |
#ifdef BENCH_FLINT |
| 142 |
// FLINT MUL // |
| 143 |
fmpz_t modp,tmp; |
| 144 |
fmpz_init(modp); |
| 145 |
fmpz_init(tmp); |
| 146 |
fmpz_set_mpz(modp, *(reinterpret_cast<const mpz_t*>(&p))); |
| 147 |
fmpz_mat_t AA,BB,CC,DD; |
| 148 |
fmpz_mat_init (AA, m, k); |
| 149 |
fmpz_mat_init (BB, k, n); |
| 150 |
fmpz_mat_init (CC, m, n); |
| 151 |
fmpz_mat_init (DD, m, n); |
| 152 |
fmpz_t aalpha, bbeta; |
| 153 |
fmpz_set_mpz(aalpha,*(reinterpret_cast<const mpz_t*>(&alpha))); |
| 154 |
fmpz_set_mpz(bbeta,*(reinterpret_cast<const mpz_t*>(&beta))); |
| 155 |
|
| 156 |
for (size_t i=0;i<m;++i) |
| 157 |
for (size_t j=0;j<k;++j) |
| 158 |
fmpz_set_mpz(fmpz_mat_entry(AA,i,j),*(reinterpret_cast<const mpz_t*>(A+i*lda+j))); |
| 159 |
for (size_t i=0;i<k;++i) |
| 160 |
for (size_t j=0;j<n;++j) |
| 161 |
fmpz_set_mpz(fmpz_mat_entry(BB,i,j),*(reinterpret_cast<const mpz_t*>(B+i*ldb+j))); |
| 162 |
for (size_t i=0;i<m;++i) |
| 163 |
for (size_t j=0;j<n;++j) |
| 164 |
fmpz_set_mpz(fmpz_mat_entry(CC,i,j),*(reinterpret_cast<const mpz_t*>(C+i*ldc+j))); |
| 165 |
chrono.clear();chrono.start(); |
| 166 |
// DD= A.B |
| 167 |
fmpz_mat_mul(DD,AA,BB); |
| 168 |
// CC = beta.C |
| 169 |
fmpz_mat_scalar_mul_fmpz(CC,CC,bbeta); |
| 170 |
// CC = CC + DD.alpha |
| 171 |
fmpz_mat_scalar_addmul_fmpz(CC,DD,aalpha); |
| 172 |
// CC = CC mod p |
| 173 |
for (size_t i=0;i<m;++i) |
| 174 |
for (size_t j=0;j<n;++j) |
| 175 |
fmpz_mod(fmpz_mat_entry(CC,i,j),fmpz_mat_entry(CC,i,j),modp); |
| 176 |
|
| 177 |
chrono.stop(); |
| 178 |
timeFlint+=chrono.usertime(); |
| 179 |
fmpz_mat_clear(AA); |
| 180 |
fmpz_mat_clear(BB); |
| 181 |
#endif |
| 182 |
//END FLINT CODE // |
| 183 |
using FFLAS::CuttingStrategy::Recursive; |
| 184 |
using FFLAS::StrategyParameter::TwoDAdaptive; |
| 185 |
// RNS MUL_LA |
| 186 |
chrono.clear();chrono.start(); |
| 187 |
// PAR_BLOCK{ |
| 188 |
// FFLAS::fgemm(F,FFLAS::FflasNoTrans,FFLAS::FflasNoTrans,m,n,k,alpha,A,lda,B,ldb,beta,C,ldc, SPLITTER(NUM_THREADS,Recursive,TwoDAdaptive) ); |
| 189 |
// } |
| 190 |
{ |
| 191 |
FFLAS::fgemm(F,FFLAS::FflasNoTrans,FFLAS::FflasNoTrans,m,n,k,alpha,A,lda,B,ldb,beta,C,ldc,FFLAS::ParSeqHelper::Sequential()); |
| 192 |
} |
| 193 |
|
| 194 |
chrono.stop(); |
| 195 |
time+=chrono.realtime(); |
| 196 |
|
| 197 |
TimFreivalds.start(); |
| 198 |
bool pass = FFLAS::freivalds(F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, m,n,k, alpha, A, k, B, n, C,n); |
| 199 |
TimFreivalds.stop(); |
| 200 |
timev+=TimFreivalds.usertime(); |
| 201 |
if (!pass) { |
| 202 |
std::cout<<"FAILED"<<std::endl; |
| 203 |
std::cout << "p:=" << p << ';'<<std::endl; |
| 204 |
FFLAS::WriteMatrix (std::cout<<"A:=",F,m,k,A,lda)<<';'<<std::endl; |
| 205 |
FFLAS::WriteMatrix(std::cout<<"B:=",F,k,n,B,ldb)<<';'<<std::endl; |
| 206 |
FFLAS::WriteMatrix(std::cout<<"C:=",F,m,n,C,ldc)<<';'<<std::endl; |
| 207 |
} |
| 208 |
|
| 209 |
FFLAS::fflas_delete(A); |
| 210 |
FFLAS::fflas_delete(B); |
| 211 |
FFLAS::fflas_delete(C); |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
double Gflops=(2.*double(m)/1000.*double(n)/1000.*double(k)/1000.0) / time * double(iters); |
| 216 |
// Gflops*=p.bitsize()/16.; |
| 217 |
cout << "Time: "<< (time/double(iters)) |
| 218 |
<<" Gfops: "<<Gflops |
| 219 |
<< " (total:" << time <<") " |
| 220 |
<<typeid(Ints).name() |
| 221 |
<<" | perword: "<< (Gflops*double(p.bitsize()))/64. ; |
| 222 |
|
| 223 |
FFLAS::writeCommandString(std::cout << '|' << p << " (" << p.bitsize()<<")|", as) << " | Freivalds: "<< timev/double(iters) << std::endl; |
| 224 |
|
| 225 |
#ifdef BENCH_FLINT |
| 226 |
cout<<"Time FLINT: "<<timeFlint<<endl; |
| 227 |
#endif |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
int main(int argc, char** argv){ |
| 232 |
|
| 233 |
#ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS |
| 234 |
openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS); |
| 235 |
#endif |
| 236 |
|
| 237 |
FFLAS::parseArguments(argc,argv,as); |
| 238 |
|
| 239 |
int r1 = tmain<Givaro::Integer>(); |
| 240 |
|
| 241 |
#ifdef BENCH_RECINT |
| 242 |
r1 += tmain<RecInt::rint<STD_RECINT_SIZE>>(); |
| 243 |
#endif |
| 244 |
return r1; |
| 245 |
} |
| 246 |
|
| 247 |
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 248 |
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |