Debian Codegraph @

benchmarks/benchmark-dgemm.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Clément Pernet <[email protected]>
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 //#include "goto-def.h"
23
24 #include "fflas-ffpack/fflas-ffpack-config.h"
25 #include <iostream>
26 #include <givaro/modular.h>
27
28 #include "fflas-ffpack/config-blas.h"
29 #include "fflas-ffpack/fflas/fflas.h"
30 #include "fflas-ffpack/utils/timer.h"
31 #include "fflas-ffpack/utils/fflas_io.h"
32 #include "fflas-ffpack/utils/args-parser.h"
33
34 #ifdef __FFLASFFPACK_USE_OPENMP
35 typedef FFLAS::OMPTimer TTimer;
36 #else
37 typedef FFLAS::Timer TTimer;
38 #endif
39
40 #ifndef __SGEMM__
41 typedef double Floats;
42 #define CBLAS_GEMM cblas_dgemm
43 #else
44 typedef float Floats;
45 #define CBLAS_GEMM cblas_sgemm
46 #endif
47
48
49 using namespace std;
50
51 int main(int argc, char** argv) {
52
53 size_t iter = 1;
54 int q = 1009;
55 size_t n = 2000;
56 std::string file1 = "";
57 std::string file2 = "";
58
59 Argument as[] = {
60 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT , &q },
61 { 'n', "-n N", "Set the dimension of the matrix.", TYPE_INT , &n },
62 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iter },
63 { 'f', "-f FILE", "Set the first input file (empty for random).", TYPE_STR , &file1 },
64 { 'g', "-g FILE", "Set the second input file (empty for random).", TYPE_STR , &file2 },
65 END_OF_ARGUMENTS
66 };
67
68 FFLAS::parseArguments(argc,argv,as);
69
70
71 typedef Givaro::ModularBalanced<Floats> Field;
72 typedef Field::Element Element;
73
74 Field F(q);
75
76 TTimer chrono;
77 double time=0.0;// time2=0.0;
78
79 Element * A, * B, * C;
80
81 if (iter>1) {
82 if (!file1.empty()){
83 FFLAS::ReadMatrix (file1.c_str(),F,n,n,A);
84 }
85 else{
86 Field::RandIter G(F);
87 A = FFLAS::fflas_new<Element>(n*n);
88 #pragma omp parallel for
89 for (size_t i=0; i<n; ++i)
90 for (size_t j=0; j<n; ++j)
91 G.random(*(A+i*n+j));
92 }
93
94 if (!file2.empty()){
95 FFLAS::ReadMatrix (file2.c_str(),F,n,n,B);
96 }
97 else{
98 Field::RandIter G(F);
99 B = FFLAS::fflas_new<Element>(n*n);
100 #pragma omp parallel for
101 for (size_t i=0; i<n; ++i)
102 for (size_t j=0; j<n; ++j)
103 G.random(*(B+i*n+j));
104 }
105
106 C = FFLAS::fflas_new<Element>(n*n);
107
108 CBLAS_GEMM (CblasRowMajor, CblasNoTrans, CblasNoTrans, n,n,n, F.one,
109 A, n, B, n, F.zero, C,n);
110
111 FFLAS::fflas_delete( A);
112 FFLAS::fflas_delete( B);
113 FFLAS::fflas_delete( C);
114 }
115
116 for (size_t it=0;it<iter;++it){
117
118 if (!file1.empty()){
119 FFLAS::ReadMatrix (file1.c_str(),F,n,n,A);
120 }
121 else{
122 Field::RandIter G(F);
123 A = FFLAS::fflas_new<Element>(n*n);
124 #pragma omp parallel for
125 for (size_t i=0; i<n; ++i)
126 for (size_t j=0; j<n; ++j)
127 G.random(*(A+i*n+j));
128 }
129
130 if (!file2.empty()){
131 FFLAS::ReadMatrix (file2.c_str(),F,n,n,B);
132 }
133 else{
134 Field::RandIter G(F);
135 B = FFLAS::fflas_new<Element>(n*n);
136 #pragma omp parallel for
137 for (size_t i=0; i<n; ++i)
138 for (size_t j=0; j<n; ++j)
139 G.random(*(B+i*n+j));
140 }
141
142 C = FFLAS::fflas_new<Element>(n*n);
143
144 chrono.clear();
145 chrono.start();
146 CBLAS_GEMM (CblasRowMajor, CblasNoTrans, CblasNoTrans, n,n,n, F.one,
147 A, n, B, n, F.zero, C,n);
148 chrono.stop();
149 time+=chrono.usertime();
150
151 FFLAS::fflas_delete( A);
152 FFLAS::fflas_delete( B);
153 FFLAS::fflas_delete( C);
154 }
155
156 // -----------
157 // Standard output for benchmark - Alexis Breust 2014/11/14
158 std::cout << "Time: " << time / double(iter)
159 << " Gfops: " << (2.*double(n)/1000.*double(n)/1000.*double(n)/1000.0) / time * double(iter);
160 FFLAS::writeCommandString(std::cout, as) << std::endl;
161
162 return 0;
163 }
164
165 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
166 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s