1 /* Copyright (c) FFLAS-FFPACK
2  * Written by Philippe LEDENT <philippe.ledent@etu.univ-grenoble-alpes.fr>
3  * ========LICENCE========
4  * This file is part of the library FFLAS-FFPACK.
5  *
6  * FFLAS-FFPACK is free software: you can redistribute it and/or modify
7  * it under the terms of the  GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  * ========LICENCE========
20  */
21 
22 // declare that the call to openblas_set_numthread will be made here, hence don't do it
23 // everywhere in the call stack
24 #define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET 1
25 
26 #include "fflas-ffpack/fflas-ffpack-config.h"
27 #include <iostream>
28 #include <givaro/modular.h>
29 #include <givaro/givrational.h>
30 
31 #include "fflas-ffpack/fflas-ffpack.h"
32 #include "fflas-ffpack/utils/timer.h"
33 #include "fflas-ffpack/utils/fflas_io.h"
34 #include "fflas-ffpack/paladin/parallel.h"
35 #include "fflas-ffpack/paladin/fflas_plevel1.h"
36 #include "fflas-ffpack/utils/args-parser.h"
37 
38 
39 using namespace std;
40 using namespace FFLAS;
41 using namespace FFPACK;
42 
43 template<class Field>
run_with_field(int q,size_t iter,size_t N,const size_t BS,const size_t p,const size_t threads)44 typename Field::Element run_with_field(int q, size_t iter, size_t N, const size_t BS, const size_t p, const size_t threads){
45     Field F(q);
46     typename Field::RandIter G(F, BS);
47 
48     typename Field::Element_ptr A, B;
49     typename Field::Element d; F.init(d);
50 
51 #ifdef __GIVARO_USE_OPENMP
52     Givaro::OMPTimer chrono, time;
53 #else
54     Givaro::Timer chrono, time;
55 #endif
56     time.clear();
57 
58     for (size_t i=0;i<iter;++i){
59         A = fflas_new(F, N);
60         B = fflas_new(F, N);
61 
62         PAR_BLOCK { pfrand(F, G, N, 1, A); pfrand(F, G, N, 1, B); }
63 
64         //         FFLAS::WriteMatrix(std::cerr, F, 1, N, A, 1);
65         //         FFLAS::WriteMatrix(std::cerr, F, 1, N, B, 1);
66 
67         F.assign(d, F.zero);
68 
69 
70         FFLAS::ParSeqHelper::Parallel<
71         FFLAS::CuttingStrategy::Block,
72         FFLAS::StrategyParameter::Threads> ParHelper(threads);
73 
74         chrono.clear();
75         if (p){
76             chrono.start();
77             F.assign(d, fdot(F, N, A, 1U, B, 1U, ParHelper));
78             chrono.stop();
79         } else {
80             chrono.start();
81             F.assign(d, fdot(F, N, A, 1U, B, 1U, FFLAS::ParSeqHelper::Sequential()));
82             chrono.stop();
83         }
84 
85 
86         // std::cerr << chrono
87         //           << " Gfops: " << ((double(2*N)/1000.)/1000.)/(1000.*chrono.realtime())
88         //           << std::endl;
89 
90         time+=chrono;
91         FFLAS::fflas_delete(A);
92         FFLAS::fflas_delete(B);
93     }
94     // -----------
95     // Standard output for benchmark
96     std::cout << "Time: " << time.realtime()/iter
97     << " Gfops: " << ((double(2*N)/1000.)/1000.)/(1000.*time.realtime())* double(iter);
98 
99     // 	F.write(std::cerr, d) << std::endl;
100     return d;
101 }
102 
main(int argc,char ** argv)103 int main(int argc, char** argv) {
104 
105 #ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS
106     openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS);
107 #endif
108 
109     size_t iter = 20; // to get nonzero time
110     size_t N    = 5000;
111     size_t BS   = 5000;
112     int q		= 131071101;
113     size_t p	=0;
114     size_t maxallowed_threads; PAR_BLOCK { maxallowed_threads=NUM_THREADS; }
115     size_t threads=maxallowed_threads;
116 
117     Argument as[] = {
118         { 'n', "-n N", "Set the dimension of the matrix C.",TYPE_INT , &N },
119         { 'q', "-q Q", "Set the field characteristic (0 for the integers).",         TYPE_INT , &q },
120         { 'i', "-i R", "Set number of repetitions.",		TYPE_INT , &iter },
121         { 'b', "-b B", "Set the bitsize of the random elements.",         TYPE_INT , &BS},
122         { 'p', "-p P", "0 for sequential, 1 for parallel.",	TYPE_INT , &p },
123         { 't', "-t T", "number of virtual threads to drive the partition.", TYPE_INT , &threads },
124         END_OF_ARGUMENTS
125     };
126 
127     FFLAS::parseArguments(argc,argv,as);
128 
129     if (q > 0){
130         BS = Givaro::Integer(q).bitsize();
131         double d = run_with_field<Givaro::ModularBalanced<double> >(q, iter, N, BS, p, threads);
132         std::cout << " d: " << d;
133     } else {
134         auto d = run_with_field<Givaro::ZRing<Givaro::Integer> > (q, iter, N, BS, p, threads);
135         std::cout << " size: " << logtwo(d>0?d:-d);
136     }
137 
138     FFLAS::writeCommandString(std::cout, as) << std::endl;
139     return 0;
140 }
141 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
142 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
143