int main (int argc, char **argv) { static size_t n = 10000; static unsigned int iterations = 1; static Argument args[] = { { 'n', "-n N", "Set dimension of test vectors to NxN.", TYPE_INT, &n }, { 'i', "-i I", "Perform each test for I iterations.", TYPE_INT, &iterations }, END_OF_ARGUMENTS }; parseArguments (argc, argv, args); commentator().start("Givaro::Modular<float> field test suite", "Givaro::Modular<float>"); bool pass = true; Givaro::Modular<float> F2 (2); Givaro::Modular<float> F5 (5); Givaro::Modular<float> F7 (7); Givaro::IntPrimeDom IPD; integer k = Givaro::Modular<float>::maxCardinality()+1; IPD.prevprime(k, k); Givaro::Modular<float> Fmax(k); IPD.prevprime(k, k/2); Givaro::Modular<float> Fmid(k); // Make sure some more detailed messages get printed commentator().getMessageClass (INTERNAL_DESCRIPTION).setMaxDepth (4); commentator().getMessageClass (INTERNAL_DESCRIPTION).setMaxDetailLevel (Commentator::LEVEL_UNIMPORTANT); std::ostream& report = commentator().report(); report << "Field F2" << std::endl; if (!runFieldTests (F2, "Givaro::Modular<float>", iterations, n, false)) pass = false; report << "Field F5" << std::endl; if (!runFieldTests (F5, "Givaro::Modular<float>", iterations, n, false)) pass = false; report << "Field F7" << std::endl; if (!runFieldTests (F7, "Givaro::Modular<float>", iterations, n, false)) pass = false; report << "Field Fmax" << std::endl; if (!runFieldTests (Fmax, "Givaro::Modular<float>", iterations, n, false)) pass = false; report << "Field Fmid" << std::endl; if (!runFieldTests (Fmid, "Givaro::Modular<float>", iterations, n, false)) pass = false; commentator().stop("Givaro::Modular<float> field test suite"); return pass ? 0 : -1; }
int tmain(){ srand( (int)seed); srand48(seed); Givaro::Integer::seeding(seed); typedef Givaro::Modular<Ints> Field; Givaro::Integer p; FFLAS::Timer chrono, TimFreivalds; double time=0.,timev=0.; #ifdef BENCH_FLINT double timeFlint=0.; #endif for (size_t loop=0;loop<iters;loop++){ Givaro::Integer::random_exact_2exp(p, b); Givaro::IntPrimeDom IPD; IPD.nextprimein(p); Ints ip; Givaro::Caster<Ints,Givaro::Integer>(ip,p); Givaro::Caster<Givaro::Integer,Ints>(p,ip); // to check consistency Field F(ip); size_t lda,ldb,ldc; lda=k; ldb=n; ldc=n; typename Field::RandIter Rand(F,seed); typename Field::Element_ptr A,B,C; A= FFLAS::fflas_new(F,m,lda); B= FFLAS::fflas_new(F,k,ldb); C= FFLAS::fflas_new(F,m,ldc); // for (size_t i=0;i<m;++i) // for (size_t j=0;j<k;++j) // Rand.random(A[i*lda+j]); // for (size_t i=0;i<k;++i) // for (size_t j=0;j<n;++j) // Rand.random(B[i*ldb+j]); // for (size_t i=0;i<m;++i) // for (size_t j=0;j<n;++j) // Rand.random(C[i*ldc+j]); PAR_BLOCK { FFLAS::pfrand(F,Rand, m,k,A,m/size_t(MAX_THREADS)); } PAR_BLOCK { FFLAS::pfrand(F,Rand, k,n,B,k/MAX_THREADS); } PAR_BLOCK { FFLAS::pfzero(F, m,n,C,m/MAX_THREADS); } Ints alpha,beta; alpha=F.one; beta=F.zero; #ifdef BENCH_FLINT // FLINT MUL // fmpz_t modp,tmp; fmpz_init(modp); fmpz_init(tmp); fmpz_set_mpz(modp, *(reinterpret_cast<const mpz_t*>(&p))); fmpz_mat_t AA,BB,CC,DD; fmpz_mat_init (AA, m, k); fmpz_mat_init (BB, k, n); fmpz_mat_init (CC, m, n); fmpz_mat_init (DD, m, n); fmpz_t aalpha, bbeta; fmpz_set_mpz(aalpha,*(reinterpret_cast<const mpz_t*>(&alpha))); fmpz_set_mpz(bbeta,*(reinterpret_cast<const mpz_t*>(&beta))); for (size_t i=0;i<m;++i) for (size_t j=0;j<k;++j) fmpz_set_mpz(fmpz_mat_entry(AA,i,j),*(reinterpret_cast<const mpz_t*>(A+i*lda+j))); for (size_t i=0;i<k;++i) for (size_t j=0;j<n;++j) fmpz_set_mpz(fmpz_mat_entry(BB,i,j),*(reinterpret_cast<const mpz_t*>(B+i*ldb+j))); for (size_t i=0;i<m;++i) for (size_t j=0;j<n;++j) fmpz_set_mpz(fmpz_mat_entry(CC,i,j),*(reinterpret_cast<const mpz_t*>(C+i*ldc+j))); chrono.clear();chrono.start(); // DD= A.B fmpz_mat_mul(DD,AA,BB); // CC = beta.C fmpz_mat_scalar_mul_fmpz(CC,CC,bbeta); // CC = CC + DD.alpha fmpz_mat_scalar_addmul_fmpz(CC,DD,aalpha); // CC = CC mod p for (size_t i=0;i<m;++i) for (size_t j=0;j<n;++j) fmpz_mod(fmpz_mat_entry(CC,i,j),fmpz_mat_entry(CC,i,j),modp); chrono.stop(); timeFlint+=chrono.usertime(); fmpz_mat_clear(AA); fmpz_mat_clear(BB); #endif //END FLINT CODE // using FFLAS::CuttingStrategy::Recursive; using FFLAS::StrategyParameter::TwoDAdaptive; // RNS MUL_LA chrono.clear();chrono.start(); // PAR_BLOCK{ // FFLAS::fgemm(F,FFLAS::FflasNoTrans,FFLAS::FflasNoTrans,m,n,k,alpha,A,lda,B,ldb,beta,C,ldc, SPLITTER(NUM_THREADS,Recursive,TwoDAdaptive) ); // } { FFLAS::fgemm(F,FFLAS::FflasNoTrans,FFLAS::FflasNoTrans,m,n,k,alpha,A,lda,B,ldb,beta,C,ldc,FFLAS::ParSeqHelper::Sequential()); } chrono.stop(); time+=chrono.realtime(); TimFreivalds.start(); bool pass = FFLAS::freivalds(F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, m,n,k, alpha, A, k, B, n, C,n); TimFreivalds.stop(); timev+=TimFreivalds.usertime(); if (!pass) { std::cout<<"FAILED"<<std::endl; std::cout << "p:=" << p << ';'<<std::endl; write_matrix(std::cout<<"A:=",p,m,k,A,lda)<<';'<<std::endl; write_matrix(std::cout<<"B:=",p,k,n,B,ldb)<<';'<<std::endl; write_matrix(std::cout<<"C:=",p,m,n,C,ldc)<<';'<<std::endl; } FFLAS::fflas_delete(A); FFLAS::fflas_delete(B); FFLAS::fflas_delete(C); } double Gflops=(2.*double(m)/1000.*double(n)/1000.*double(k)/1000.0) / time * double(iters); // Gflops*=p.bitsize()/16.; cout<<typeid(Ints).name() << " | Time: "<< (time/double(iters)) << " (total:" << time <<") Gflops: "<<Gflops<<" | perword: "<< (Gflops*double(p.bitsize()))/64. ; FFLAS::writeCommandString(std::cout << '|' << p << " (" << p.bitsize()<<")|", as) << " | Freivalds: "<< timev/double(iters) << std::endl; #ifdef BENCH_FLINT cout<<"Time FLINT: "<<timeFlint<<endl; #endif return 0; }
int main(int argc, char** argv){ FFLAS::Timer tim; Givaro::IntPrimeDom IPD; uint64_t p; size_t M, N ; bool keepon = true; Givaro::Integer _p,tmp; Field::Element zero,one; cerr<<setprecision(10); size_t TMAX = 100; size_t PRIMESIZE = 23; if (argc > 1 ) TMAX = atoi(argv[1]); if (argc > 2 ) PRIMESIZE = atoi(argv[2]); FFLAS::FFLAS_TRANSPOSE ta; FFLAS::FFLAS_DIAG diag; size_t lda; Field::Element * A, *Abis, *X,* U, *L; size_t *P, *Q; while (keepon){ srandom(_p); do{ // max = Integer::random(2); _p = random();//max % (2<<30); IPD.prevprime( tmp, (_p% (1<<PRIMESIZE)) ); p = tmp; }while( (p <= 2) ); Field F( p); F.init(zero,0.0); F.init(one,1.0); Field::RandIter RValue( F ); do{ M = (size_t) random() % TMAX; N = (size_t) random() % TMAX; } while ((M == 0) || (N == 0)); lda = N; if (random()%2) diag = FFLAS::FflasUnit; else diag = FFLAS::FflasNonUnit; if (random()%2){ ta = FFLAS::FflasTrans; L = FFLAS::fflas_new<Field::Element>(M*N); U = FFLAS::fflas_new<Field::Element>(N*N); P = FFLAS::fflas_new<size_t>(M); Q = FFLAS::fflas_new<size_t>(N); for (size_t i=0; i<M; ++i) P[i] = 0; for (size_t i=0; i<N; ++i) Q[i] = 0; } else{ ta = FFLAS::FflasNoTrans; L = FFLAS::fflas_new<Field::Element>(M*M); U = FFLAS::fflas_new<Field::Element>(M*N); P = FFLAS::fflas_new<size_t>(N); Q = FFLAS::fflas_new<size_t>(M); for (size_t i=0; i<N; ++i) P[i] = 0; for (size_t i=0; i<M; ++i) Q[i] = 0; } size_t R=0; Field::Element * G = FFLAS::fflas_new<Field::Element>(M*M); Field::Element * H = FFLAS::fflas_new<Field::Element>(M*N); size_t t; do{ t = (size_t) random() % 10; } while ((!t)||(t==1)); for (size_t i=0; i<M; ++i) if (!(random() % t)) for (size_t j=0; j < M; ++j) RValue.random (*(G+i*M+j)); else for (size_t j=0; j < M; ++j) F.assign(*(G+i*M+j), zero); for (size_t j=0; j < N; ++j) if (!(random() % t)) for (size_t i=0; i<M; ++i) RValue.random (*(H+i*N+j)); else for (size_t i=0; i<M; ++i) F.assign(*(H+i*N+j), zero); // write_field(F,cerr<<"G = "<<endl,G,M,M,M); // write_field(F,cerr<<"H = "<<endl,H,M,N,N); A = FFLAS::fflas_new<Field::Element>(M*N); FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, M, N, M, one, G, M, H, N, zero, A, N); FFLAS::fflas_delete( G); FFLAS::fflas_delete( H); Abis = FFLAS::fflas_new<Field::Element>(M*N); for (size_t i=0; i<M*N; ++i) *(Abis+i) = *(A+i); X = FFLAS::fflas_new<Field::Element>(M*N); cout <<"p = "<<(size_t)p<<" M = "<<M <<" N = "<<N <<((diag==FFLAS::FflasUnit)?" Unit ":" Non Unit ") <<((ta==FFLAS::FflasNoTrans)?"LQUP ( A ) ":"LQUP ( A^T ) ") <<"...."; tim.clear(); tim.start(); R = FFPACK::LUdivine (F, diag, ta, M, N, A, lda, P, Q); tim.stop(); //write_field(F,cerr<<"Result = "<<endl,Abis,M,N,lda); if (ta == FFLAS::FflasNoTrans){ for (size_t i=0; i<R; ++i){ for (size_t j=0; j<i; ++j) F.assign ( *(U + i*N + j), zero); for (size_t j=i+1; j<N; ++j) F.assign (*(U + i*N + j), *(A+ i*N+j)); } for (size_t i=R;i<M; ++i) for (size_t j=0; j<N; ++j) F.assign(*(U+i*N+j), zero); for ( size_t i=0; i<M; ++i ){ size_t j=0; for (; j< ((i<R)?i:R) ; ++j ) F.assign( *(L + i*M+j), *(A+i*N+j)); for (; j<M; ++j ) F.assign( *(L+i*M+j), zero); } //write_field(F,cerr<<"L = "<<endl,L,M,M,M); //write_field(F,cerr<<"U = "<<endl,U,M,N,N); FFPACK::applyP( F, FFLAS::FflasRight, FFLAS::FflasNoTrans, M,0,(int) R, L, M, Q); for ( size_t i=0; i<M; ++i ) F.assign(*(L+i*(M+1)), one); if (diag == FFLAS::FflasNonUnit) for ( size_t i=0; i<R; ++i ) F.assign (*(U+i*(N+1)), *(A+i*(lda+1))); else{ for (size_t i=0; i<R; ++i ){ *(L+Q[i]*(M+1)) = *(A+Q[i]*lda+i); F.assign (*(U+i*(N+1)),one); } } FFPACK::applyP (F, FFLAS::FflasRight, FFLAS::FflasNoTrans, M,0,(int) R, U, N, P); FFPACK::applyP (F, FFLAS::FflasLeft, FFLAS::FflasTrans, N,0,(int) R, U, N, Q); FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, M,N,M, 1.0, L,M, U,N, 0.0, X,N); //FFLAS::fflas_delete( A); } else { for (size_t i=0; i<R; ++i){ for (size_t j=0; j<i; ++j) F.assign ( *(L + i + j*N), zero); for (size_t j=i+1; j<M; ++j) F.assign (*(L + i + j*N), *(A+ i+j*N)); } for (size_t i=R;i<N; ++i) for (size_t j=0; j<M; ++j) F.assign(*(L+i+j*N), zero); for ( size_t i=0; i<N; ++i ){ size_t j=0; for (; j< ((i<R)?i:R) ; ++j ) F.assign( *(U + i+j*N), *(A+i+j*N)); for (; j<N; ++j ) F.assign( *(U+i+j*N), zero); } FFPACK::applyP( F, FFLAS::FflasLeft, FFLAS::FflasTrans, N,0,(int) R, U, N, Q); for (size_t i=0; i<N; ++i) F.assign (*(U+i*(N+1)),one); if (diag == FFLAS::FflasNonUnit) for ( size_t i=0; i<R; ++i ) F.assign (*(L+i*(N+1)), *(A+i*(lda+1))); else{ for ( size_t i=0; i<R; ++i ){ *(U+Q[i]*(N+1)) = *(A+Q[i]+i*N); F.assign (*(L+i*(N+1)),one); } } // write_field(F,cerr<<"L = "<<endl,L,M,N,N); // write_field(F,cerr<<"U = "<<endl,U,N,N,N); FFPACK::applyP (F, FFLAS::FflasLeft, FFLAS::FflasTrans, N,0,(int) R, L, N, P); FFPACK::applyP (F, FFLAS::FflasRight, FFLAS::FflasNoTrans, M,0,(int) R, L, N, Q); FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, M,N,N, 1.0, L,N, U,N, 0.0, X,N); } for (size_t i=0; i<M; ++i) for (size_t j=0; j<N; ++j) if (!F.areEqual (*(Abis+i*N+j), *(X+i*N+j))){ cerr<<"error for i,j="<<i<<" "<<j<<" "<<*(Abis+i*N+j)<<" "<<*(X+i*N+j)<<endl; keepon = false; } //write_field(F,cerr<<"X = "<<endl,X,m,n,n); //write_field(F,cerr<<"B = "<<endl,B,m,n,n); if (keepon){ cout<<"R = "<<R <<" Passed " <<(double(M*M)/1000.0*(double(N)-double(M)/3.0)/tim.usertime()/1000.0)<<"Mfops"<<endl; FFLAS::fflas_delete( A); FFLAS::fflas_delete( L); FFLAS::fflas_delete( U); FFLAS::fflas_delete( Abis); FFLAS::fflas_delete( X); FFLAS::fflas_delete( P); FFLAS::fflas_delete( Q); } else{ cerr<<"Abis = "<<endl; write_field( F, cerr, Abis, (int) M, (int) N, (int) N ); cerr<<"X = "<<endl; write_field( F, cerr, X, (int) M, (int) N, (int) N ); } } cout<<endl; cerr<<"FAILED with p = "<<(size_t)p<<" M = "<<M<<" N = "<<N <<" trans = "<<ta<<" diag = "<<diag<<endl; cerr<<"A:"<<endl; cerr<<M<<" "<<N<<" M"<<endl; for (size_t i=0; i<M; ++i) for (size_t j=0; j<N; ++j) if (*(Abis+i*lda+j)) cerr<<i+1<<" "<<j+1<<" "<<((int) *(Abis+i*lda+j) )<<endl; cerr<<"0 0 0"<<endl<<endl; FFLAS::fflas_delete( A); FFLAS::fflas_delete( Abis); FFLAS::fflas_delete( L); FFLAS::fflas_delete( U); FFLAS::fflas_delete( X); FFLAS::fflas_delete( P); FFLAS::fflas_delete( Q); }
int main(int argc, char** argv){ FFLAS::Timer tim; Givaro::IntPrimeDom IPD; uint64_t p; size_t M, N, K ; bool keepon = true; Givaro::Integer _p,tmp; Field::Element zero,one; cerr<<setprecision(10); size_t TMAX = 300; size_t PRIMESIZE = 23; if (argc > 1 ) TMAX = atoi(argv[1]); if (argc > 2 ) PRIMESIZE = atoi(argv[2]); FFLAS::FFLAS_TRANSPOSE trans; FFLAS::FFLAS_SIDE side; FFLAS::FFLAS_UPLO uplo; FFLAS::FFLAS_DIAG diag; size_t lda, ldb; Field::Element * A, *Abis, *B,* Bbis; Field::Element alpha; while (keepon){ srandom(_p); do{ // max = Integer::random(2); _p = random();//max % (2<<30); IPD.prevprime( tmp, (_p% (1<<PRIMESIZE)) ); p = tmp; }while( (p <= 2) ); Field F (p); F.init (zero,0.0); F.init (one,1.0); Field::RandIter RValue (F); do{ M = (size_t) random() % TMAX; N = (size_t) random() % TMAX; } while ((M == 0) || (N == 0)); ldb = N; if (random()%2) trans = FFLAS::FflasNoTrans; else trans = FFLAS::FflasTrans; if (random()%2) diag = FFLAS::FflasUnit; else diag = FFLAS::FflasNonUnit; if (random()%2){ side = FFLAS::FflasLeft; K = M; lda = M; } else { side = FFLAS::FflasRight; K = N; lda = N; } if (random()%2) uplo = FFLAS::FflasUpper; else uplo = FFLAS::FflasLower; while (F.isZero(RValue.random (alpha))); A = FFLAS::fflas_new<Field::Element>(K*K); B = FFLAS::fflas_new<Field::Element>(M*N); Abis = FFLAS::fflas_new<Field::Element>(K*K); Bbis = FFLAS::fflas_new<Field::Element>(M*N); for (size_t i = 0; i < M; ++i) for (size_t j = 0; j < N; ++j){ RValue.random (*(B + i*N + j)); *(Bbis + i*N + j) = *(B + i*N + j); } for (size_t i = 0; i < K; ++i) for (size_t j = 0; j < K; ++j) *(Abis + i*K + j) = RValue.random (*(A + i*K + j)); for (size_t i = 0; i < K; ++i){ while (F.isZero(RValue.random (*(A + i*(K+1))))); *(Abis + i*(K +1)) = *(A + i*(K+1)); } cout <<"p = "<<(size_t)p <<" M = "<<M <<" N = "<<N <<((side==FFLAS::FflasLeft)?" Left ":" Right ") <<((uplo==FFLAS::FflasLower)?" Lower ":" Upper ") <<((trans==FFLAS::FflasTrans)?" Trans ":" NoTrans ") <<((diag==FFLAS::FflasUnit)?" Unit ":" NonUnit ") <<"...."; tim.clear(); tim.start(); FFLAS::ftrsm (F, side, uplo, trans, diag, M, N, alpha, A, lda, B, ldb); tim.stop(); // Verification Field::Element invalpha; F.inv(invalpha, alpha); FFLAS::ftrmm (F, side, uplo, trans, diag, M, N, invalpha, A, K, B, N); for (size_t i = 0;i < M;++i) for (size_t j = 0;j < N; ++j) if ( !F.areEqual (*(Bbis + i*N+ j ), *(B + i*N + j))){ cerr<<endl <<"Bbis ["<<i<<", "<<j<<"] = "<<(*(Bbis + i*N + j)) <<" ; B ["<<i<<", "<<j<<"] = "<<(*(B + i*N + j)); keepon = false; } for (size_t i = 0;i < K; ++i) for (size_t j = 0;j < K; ++j) if ( !F.areEqual (*(A + i*K + j), *(Abis + i*K + j))){ cerr<<endl <<"A ["<<i<<", "<<j<<"] = "<<(*(A + i*K + j)) <<" ; Abis ["<<i<<", "<<j<<"] = "<<(*(Abis + i*K + j)); keepon = false; } if (keepon) { cout<<" Passed " <<double(M*N)/1000000.0*double(K)/tim.usertime()<<" Mfops"<<endl; FFLAS::fflas_delete( B); FFLAS::fflas_delete( Bbis); FFLAS::fflas_delete( A); FFLAS::fflas_delete( Abis); } else { cerr<<endl; write_field (F, cerr<<"A = "<<endl, Abis, (int) K,(int) K,(int) K); write_field (F, cerr<<"B = "<<endl, Bbis, (int) M,(int) N,(int) N); } } cout<<endl; cerr<<"FAILED with p = "<<(size_t)p <<" M = "<<M <<" N = "<<N <<" alpha = "<<alpha <<((side==FFLAS::FflasLeft)?" Left ":" Right ") <<((uplo==FFLAS::FflasLower)?" Lower ":" Upper ") <<((trans==FFLAS::FflasTrans)?" Trans ":" NoTrans ") <<((diag==FFLAS::FflasUnit)?" Unit ":" NonUnit ") <<endl; cerr<<"A:"<<endl; cerr<<K<<" "<<K<<" M"<<endl; for (size_t i=0; i<K; ++i) for (size_t j=0; j<K; ++j) if ((*(Abis + i*lda + j))) cerr<<i+1<<" "<<j+1<<" " <<((int) *(Abis+i*lda+j) ) <<endl; cerr<<"0 0 0"<<endl<<endl; cerr<<"B:"<<endl; cerr<<M<<" "<<N<<" M"<<endl; for (size_t i=0; i<M; ++i) for (size_t j=0; j<N; ++j) if ((*(Bbis + i*ldb + j))) cerr<<i+1<<" "<<j+1<<" " <<((int) *(Bbis+i*ldb+j) ) <<endl; cerr<<"0 0 0"<<endl<<endl; FFLAS::fflas_delete( A); FFLAS::fflas_delete( Abis); FFLAS::fflas_delete( B); FFLAS::fflas_delete( Bbis); }