| 1 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 | //-------------------------------------------------------------------------- |
|---|
| 3 | // Test for invert : 1 computation |
|---|
| 4 | // |
|---|
| 5 | //-------------------------------------------------------------------------- |
|---|
| 6 | // Clement Pernet |
|---|
| 7 | //------------------------------------------------------------------------- |
|---|
| 8 | |
|---|
| 9 | #define DEBUG 1 |
|---|
| 10 | #define TIME 1 |
|---|
| 11 | using namespace std; |
|---|
| 12 | |
|---|
| 13 | #include <iomanip> |
|---|
| 14 | #include <iostream> |
|---|
| 15 | #include "fflas-ffpack/modular-balanced.h" |
|---|
| 16 | #include "timer.h" |
|---|
| 17 | #include "Matio.h" |
|---|
| 18 | #include "fflas-ffpack/ffpack.h" |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | typedef ModularBalanced<float> Field; |
|---|
| 22 | |
|---|
| 23 | int main(int argc, char** argv){ |
|---|
| 24 | |
|---|
| 25 | int n; |
|---|
| 26 | int nbit=atoi(argv[3]); // number of times the product is performed |
|---|
| 27 | cerr<<setprecision(10); |
|---|
| 28 | Field::Element zero, one; |
|---|
| 29 | |
|---|
| 30 | if (argc != 4) { |
|---|
| 31 | cerr<<"Usage : test-invert <p> <A> <<i>" |
|---|
| 32 | <<endl |
|---|
| 33 | <<" to invert A mod p (i computations)" |
|---|
| 34 | <<endl; |
|---|
| 35 | exit(-1); |
|---|
| 36 | } |
|---|
| 37 | Field F(atof(argv[1])); |
|---|
| 38 | F.init(zero,0.0); |
|---|
| 39 | F.init(one,1.0); |
|---|
| 40 | Field::Element * A; |
|---|
| 41 | A = read_field(F,argv[2],&n,&n); |
|---|
| 42 | |
|---|
| 43 | Timer tim,t; t.clear();tim.clear(); |
|---|
| 44 | int nullity=0; |
|---|
| 45 | |
|---|
| 46 | for(int i = 0;i<nbit;++i){ |
|---|
| 47 | t.clear(); |
|---|
| 48 | t.start(); |
|---|
| 49 | FFPACK::Invert (F, n, A, n, nullity); |
|---|
| 50 | t.stop(); |
|---|
| 51 | tim+=t; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | #if DEBUG |
|---|
| 55 | Field::Element *Ab = read_field(F,argv[2],&n,&n); |
|---|
| 56 | Field::Element *I = new Field::Element[n*n]; |
|---|
| 57 | FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, n, n, n, |
|---|
| 58 | 1.0, Ab, n, A, n, 0.0, I, n); |
|---|
| 59 | bool wrong = false; |
|---|
| 60 | |
|---|
| 61 | for (int i=0;i<n;++i) |
|---|
| 62 | for (int j=0;j<n;++j) |
|---|
| 63 | if ( ((i!=j) && !F.areEqual(*(I+i*n+j),zero)) |
|---|
| 64 | ||((i==j) &&!F.areEqual(*(I+i*n+j),one))) |
|---|
| 65 | wrong = true; |
|---|
| 66 | |
|---|
| 67 | if ( wrong ){ |
|---|
| 68 | if (nullity > 0) |
|---|
| 69 | cerr<<"Matrix is singular over Z/"<<argv[1]<<"Z"<<endl; |
|---|
| 70 | else{ |
|---|
| 71 | cerr<<"FAIL"<<endl; |
|---|
| 72 | write_field (F,cerr<<"A="<<endl,Ab,n,n,n); |
|---|
| 73 | write_field (F,cerr<<"A^-1="<<endl,A,n,n,n); |
|---|
| 74 | write_field (F,cerr<<"I="<<endl,I,n,n,n); |
|---|
| 75 | } |
|---|
| 76 | } else { |
|---|
| 77 | cerr<<"PASS"<<endl; |
|---|
| 78 | } |
|---|
| 79 | delete[] I; |
|---|
| 80 | delete[] Ab; |
|---|
| 81 | |
|---|
| 82 | #endif |
|---|
| 83 | delete[] A; |
|---|
| 84 | |
|---|
| 85 | #if TIME |
|---|
| 86 | double mflops = 2*(n*n/1000000.0)*nbit*n/tim.usertime(); |
|---|
| 87 | cerr<<"n = "<<n<<" Inversion over Z/"<<atoi(argv[1])<<"Z : t= " |
|---|
| 88 | << tim.usertime()/nbit |
|---|
| 89 | << " s, Mffops = "<<mflops |
|---|
| 90 | << endl; |
|---|
| 91 | |
|---|
| 92 | cout<<n<<" "<<mflops<<" "<<tim.usertime()/nbit<<endl; |
|---|
| 93 | #endif |
|---|
| 94 | } |
|---|