[1726] | 1 | #include "machdefs.h"
|
---|
| 2 |
|
---|
| 3 | #include <math.h>
|
---|
| 4 | #include <iostream.h>
|
---|
| 5 | #include <fstream.h>
|
---|
| 6 |
|
---|
| 7 | #include "tarrinit.h"
|
---|
| 8 | #include "array.h"
|
---|
| 9 | #include "ctimer.h"
|
---|
| 10 | #include "timing.h"
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | // --------------------------------------------------------
|
---|
| 14 | // Programme de mesure d'overhead, lie a l'utilisation des
|
---|
| 15 | // classes, de NDataBlock et TArray en particulier
|
---|
| 16 | // Attention, l'operateur * declare ds les classes
|
---|
| 17 | // ci-dessous denote l'operation multiplication element
|
---|
| 18 | // par element - Ceci n'est pas le cas pour les Tmatrix<T>
|
---|
| 19 | // --------------------------------------------------------
|
---|
| 20 |
|
---|
| 21 | // --------
|
---|
| 22 |
|
---|
| 23 | // ---------------------------------------------------
|
---|
| 24 | // Classe simple de matrice, utilisant les NDataBlock
|
---|
| 25 | // ---------------------------------------------------
|
---|
| 26 |
|
---|
| 27 | template <class T>
|
---|
| 28 | class SimpleMatrix : public AnyDataObj {
|
---|
| 29 | public:
|
---|
| 30 | SimpleMatrix(sa_size_t nr, sa_size_t nc);
|
---|
| 31 | SimpleMatrix(const SimpleMatrix<T> & m);
|
---|
| 32 | SimpleMatrix(const SimpleMatrix<T> & m, bool share);
|
---|
| 33 | virtual ~SimpleMatrix();
|
---|
| 34 |
|
---|
| 35 | virtual SimpleMatrix<T>& Set(const SimpleMatrix<T> & a);
|
---|
| 36 | inline SimpleMatrix<T>& operator = (const SimpleMatrix<T> & a)
|
---|
| 37 | { return Set(a); }
|
---|
| 38 |
|
---|
| 39 | inline T operator()(int r, int c) const
|
---|
| 40 | { return data_(r*ncol_+c); }
|
---|
| 41 | inline T& operator()(int r, int c)
|
---|
| 42 | { return data_(r*ncol_+c); }
|
---|
| 43 |
|
---|
| 44 | inline sa_size_t NRows() const {return nrow_; }
|
---|
| 45 | inline sa_size_t NCols() const {return ncol_; }
|
---|
| 46 |
|
---|
| 47 | SimpleMatrix<T>& AddElt(const SimpleMatrix<T> & b);
|
---|
| 48 | SimpleMatrix<T>& MulElt(const SimpleMatrix<T> & b);
|
---|
| 49 |
|
---|
| 50 | protected:
|
---|
| 51 | sa_size_t ncol_ , nrow_;
|
---|
| 52 | NDataBlock<T> data_;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | template <class T>
|
---|
| 56 | inline SimpleMatrix<T> operator + (const SimpleMatrix<T>& a,
|
---|
| 57 | const SimpleMatrix<T>& b)
|
---|
| 58 | {
|
---|
| 59 | SimpleMatrix<T> ret(a, false);
|
---|
| 60 | return(ret.AddElt(b));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | template <class T>
|
---|
| 64 | inline SimpleMatrix<T> operator * (const SimpleMatrix<T>& a,
|
---|
| 65 | const SimpleMatrix<T>& b)
|
---|
| 66 | {
|
---|
| 67 | SimpleMatrix<T> ret(a, false);
|
---|
| 68 | return(ret.MulElt(b));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | template <class T>
|
---|
| 72 | SimpleMatrix<T>::SimpleMatrix(sa_size_t nr, sa_size_t nc)
|
---|
| 73 | : nrow_(nr), ncol_(nc), data_(nr*nc)
|
---|
| 74 | {
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | template <class T>
|
---|
| 78 | SimpleMatrix<T>::SimpleMatrix(const SimpleMatrix & m)
|
---|
| 79 | : nrow_(m.nrow_), ncol_(m.ncol_), data_(m.data_)
|
---|
| 80 | {
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | template <class T>
|
---|
| 84 | SimpleMatrix<T>::SimpleMatrix(const SimpleMatrix & m, bool share)
|
---|
| 85 | : nrow_(m.nrow_), ncol_(m.ncol_), data_(m.data_, share)
|
---|
| 86 | {
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | template <class T>
|
---|
| 90 | SimpleMatrix<T>::~SimpleMatrix()
|
---|
| 91 | {
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | template <class T>
|
---|
| 95 | SimpleMatrix<T>& SimpleMatrix<T>::Set(const SimpleMatrix & b)
|
---|
| 96 | {
|
---|
| 97 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 98 | throw(SzMismatchError("SimpleMatrix::Set() Size(a) != Size(b)"));
|
---|
| 99 | data_ = b.data_;
|
---|
| 100 | return(*this);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | template <class T>
|
---|
| 104 | SimpleMatrix<T>& SimpleMatrix<T>::AddElt(const SimpleMatrix & b)
|
---|
| 105 | {
|
---|
| 106 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 107 | throw(SzMismatchError("SimpleMatrix::AddElt() Size(a) != Size(b)"));
|
---|
| 108 | data_ += b.data_;
|
---|
| 109 | return(*this);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | template <class T>
|
---|
| 113 | SimpleMatrix<T>& SimpleMatrix<T>::MulElt(const SimpleMatrix & b)
|
---|
| 114 | {
|
---|
| 115 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 116 | throw(SzMismatchError("SimpleMatrix::MulElt() Size(a) != Size(b)"));
|
---|
| 117 | data_ *= b.data_;
|
---|
| 118 | return(*this);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | // -------------------------------------------------
|
---|
| 124 | // Classe simple de matrice nxm , avec new double
|
---|
| 125 | // -------------------------------------------------
|
---|
| 126 |
|
---|
| 127 | class VerySimpleMatrix {
|
---|
| 128 | public:
|
---|
| 129 | VerySimpleMatrix(int nr, int nc, bool zero=true);
|
---|
| 130 | VerySimpleMatrix(const VerySimpleMatrix & m);
|
---|
| 131 | virtual ~VerySimpleMatrix();
|
---|
| 132 |
|
---|
| 133 | virtual VerySimpleMatrix& Set(const VerySimpleMatrix & a);
|
---|
| 134 | inline VerySimpleMatrix& operator = (const VerySimpleMatrix & a)
|
---|
| 135 | { return Set(a); }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | inline double operator()(int r, int c) const
|
---|
| 139 | { return a_[r*ncol_+c]; }
|
---|
| 140 | inline double& operator()(int r, int c)
|
---|
| 141 | { return a_[r*ncol_+c]; }
|
---|
| 142 |
|
---|
| 143 | inline int NRows() const {return nrow_; }
|
---|
| 144 | inline int NCols() const {return ncol_; }
|
---|
| 145 |
|
---|
| 146 | VerySimpleMatrix& AddElt(const VerySimpleMatrix & b);
|
---|
| 147 | VerySimpleMatrix& MulElt(const VerySimpleMatrix & b);
|
---|
| 148 |
|
---|
| 149 | protected:
|
---|
| 150 | int ncol_ , nrow_;
|
---|
| 151 | double* a_;
|
---|
| 152 | };
|
---|
| 153 |
|
---|
| 154 | inline
|
---|
| 155 | VerySimpleMatrix operator + (const VerySimpleMatrix& a, const VerySimpleMatrix& b)
|
---|
| 156 | {
|
---|
| 157 | VerySimpleMatrix ret(a);
|
---|
| 158 | return(ret.AddElt(b));
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | inline
|
---|
| 162 | VerySimpleMatrix operator * (const VerySimpleMatrix& a, const VerySimpleMatrix& b)
|
---|
| 163 | {
|
---|
| 164 | VerySimpleMatrix ret(a);
|
---|
| 165 | return(ret.MulElt(b));
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | VerySimpleMatrix::VerySimpleMatrix(int nr, int nc, bool zero)
|
---|
| 170 | {
|
---|
| 171 | nrow_ = nr; ncol_ = nc;
|
---|
| 172 | int l = nrow_ * ncol_;
|
---|
| 173 | a_ = new double[l];
|
---|
| 174 | if (zero)
|
---|
| 175 | for(int i=0; i<l; i++) a_[i] = 0.;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | VerySimpleMatrix::VerySimpleMatrix(const VerySimpleMatrix & m)
|
---|
| 179 | {
|
---|
| 180 | nrow_ = m.nrow_; ncol_ = m.ncol_;
|
---|
| 181 | a_ = new double[nrow_ * ncol_];
|
---|
| 182 | for(int i=0; i<nrow_ * ncol_; i++)
|
---|
| 183 | a_[i] = m.a_[i];
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | VerySimpleMatrix::~VerySimpleMatrix()
|
---|
| 187 | {
|
---|
| 188 | delete[] a_;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | VerySimpleMatrix& VerySimpleMatrix::Set(const VerySimpleMatrix & b)
|
---|
| 193 | {
|
---|
| 194 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 195 | throw(SzMismatchError("VerySimpleMatrix::Set Size(a) != Size(b)"));
|
---|
| 196 | for(int i=0; i<nrow_ * ncol_; i++)
|
---|
| 197 | a_[i] = b.a_[i];
|
---|
| 198 | return(*this);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | VerySimpleMatrix& VerySimpleMatrix::AddElt(const VerySimpleMatrix & b)
|
---|
| 202 | {
|
---|
| 203 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 204 | throw(SzMismatchError("VerySimpleMatrix::AddElt() Size(a) != Size(b)"));
|
---|
| 205 | for(int i=0; i<nrow_ * ncol_; i++)
|
---|
| 206 | a_[i] += b.a_[i];
|
---|
| 207 | return(*this);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | VerySimpleMatrix& VerySimpleMatrix::MulElt(const VerySimpleMatrix & b)
|
---|
| 211 | {
|
---|
| 212 | if ((nrow_ != b.nrow_) || (ncol_ != b.ncol_))
|
---|
| 213 | throw(SzMismatchError("VerySimpleMatrix::MulElt() Size(a) != Size(b)"));
|
---|
| 214 | for(int i=0; i<nrow_ * ncol_; i++)
|
---|
| 215 | a_[i] += b.a_[i];
|
---|
| 216 | return(*this);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | //--------------------------------------------------------
|
---|
| 221 | // classe template de matrice NxM
|
---|
| 222 | //--------------------------------------------------------
|
---|
| 223 |
|
---|
| 224 | template <class T, int L, int C>
|
---|
| 225 | class SmallMatrix {
|
---|
| 226 | public:
|
---|
| 227 | SmallMatrix() { }
|
---|
| 228 | SmallMatrix(const SmallMatrix<T,L,C> & m)
|
---|
| 229 | { for(int i=0; i<L*C; i++) data_[i] = m.data_[i]; }
|
---|
| 230 |
|
---|
| 231 | virtual ~SmallMatrix() { }
|
---|
| 232 |
|
---|
| 233 | inline SmallMatrix<T,L,C>& Set(const SmallMatrix<T,L,C> & m)
|
---|
| 234 | { for(int i=0; i<L*C; i++) data_[i] = m.data_[i]; return (*this); }
|
---|
| 235 | inline SmallMatrix<T,L,C>& operator = (const SmallMatrix<T,L,C>& a)
|
---|
| 236 | { return Set(a); }
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | inline T operator()(int r, int c) const
|
---|
| 241 | { return data_[r*C+c]; }
|
---|
| 242 | inline T& operator()(int r, int c)
|
---|
| 243 | { return data_[r*C+c]; }
|
---|
| 244 |
|
---|
| 245 | inline int NRows() const {return L; }
|
---|
| 246 | inline int NCols() const {return C; }
|
---|
| 247 |
|
---|
| 248 | inline SmallMatrix<T,L,C> & AddElt(const SmallMatrix<T,L,C> & m)
|
---|
| 249 | { for(int i=0; i<L*C; i++) data_[i] += m.data_[i]; return (*this); }
|
---|
| 250 |
|
---|
| 251 | inline SmallMatrix<T,L,C> & MulElt(const SmallMatrix<T,L,C> & m)
|
---|
| 252 | { for(int i=0; i<L*C; i++) data_[i] *= m.data_[i]; return (*this); }
|
---|
| 253 |
|
---|
| 254 | protected:
|
---|
| 255 | T data_[L*C];
|
---|
| 256 | };
|
---|
| 257 |
|
---|
| 258 | template <class T, int L, int C>
|
---|
| 259 | inline SmallMatrix<T,L,C>
|
---|
| 260 | operator + (const SmallMatrix<T,L,C>& a, const SmallMatrix<T,L,C>& b)
|
---|
| 261 | { SmallMatrix<T,L,C> ret(a); return(ret.AddElt(b)); }
|
---|
| 262 |
|
---|
| 263 | template <class T, int L, int C>
|
---|
| 264 | inline SmallMatrix<T,L,C>
|
---|
| 265 | operator * (const SmallMatrix<T,L,C>& a, const SmallMatrix<T,L,C>& b)
|
---|
| 266 | { SmallMatrix<T,L,C> ret(a); return(ret.MulElt(b)); }
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | // -------------------------------------------------------
|
---|
| 270 | // Classe simple de matrice 2x2 - sans allocation memoire
|
---|
| 271 | // -------------------------------------------------------
|
---|
| 272 |
|
---|
| 273 | class VerySimpleMatrix2x2 {
|
---|
| 274 | public:
|
---|
| 275 | inline VerySimpleMatrix2x2() { }
|
---|
| 276 | inline VerySimpleMatrix2x2(const VerySimpleMatrix2x2 & m)
|
---|
| 277 | { for(int i=0; i<4; i++) a_[i] = m.a_[i]; }
|
---|
| 278 |
|
---|
| 279 | inline ~VerySimpleMatrix2x2() { }
|
---|
| 280 |
|
---|
| 281 | inline VerySimpleMatrix2x2& Set(const VerySimpleMatrix2x2 & m)
|
---|
| 282 | { for(int i=0; i<4; i++) a_[i] = m.a_[i]; return (*this); }
|
---|
| 283 | inline VerySimpleMatrix2x2& operator = (const VerySimpleMatrix2x2 & a)
|
---|
| 284 | { return Set(a); }
|
---|
| 285 |
|
---|
| 286 | inline double operator()(int r, int c) const
|
---|
| 287 | { return a_[r*2+c]; }
|
---|
| 288 | inline double& operator()(int r, int c)
|
---|
| 289 | { return a_[r*2+c]; }
|
---|
| 290 |
|
---|
| 291 | inline int NRows() const {return 2; }
|
---|
| 292 | inline int NCols() const {return 2; }
|
---|
| 293 |
|
---|
| 294 | inline VerySimpleMatrix2x2& AddElt(const VerySimpleMatrix2x2 & m)
|
---|
| 295 | { for(int i=0; i<4; i++) a_[i] += m.a_[i]; return (*this); }
|
---|
| 296 | inline VerySimpleMatrix2x2& MulElt(const VerySimpleMatrix2x2 & m)
|
---|
| 297 | { for(int i=0; i<4; i++) a_[i] *= m.a_[i]; return (*this); }
|
---|
| 298 |
|
---|
| 299 | protected:
|
---|
| 300 | double a_[4];
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 | inline VerySimpleMatrix2x2 operator + (const VerySimpleMatrix2x2& a, const VerySimpleMatrix2x2& b)
|
---|
| 304 | { VerySimpleMatrix2x2 ret(a); return(ret.AddElt(b)); }
|
---|
| 305 |
|
---|
| 306 | inline VerySimpleMatrix2x2 operator * (const VerySimpleMatrix2x2& a, const VerySimpleMatrix2x2& b)
|
---|
| 307 | { VerySimpleMatrix2x2 ret(a); return(ret.MulElt(b)); }
|
---|
| 308 |
|
---|
| 309 | // ------------------------------------------------------------
|
---|
| 310 | // programme de test
|
---|
| 311 | // Appel: ovharr NLoop [NRow NCol]
|
---|
| 312 | // NRow = 0 ou NCol = 0 --> test 2x2 only
|
---|
| 313 | // ------------------------------------------------------------
|
---|
| 314 |
|
---|
| 315 | void add_double_n(int n, double* x1, double* x2, double* x3);
|
---|
| 316 | void mul_double_n(int n, double* x1, double* x2, double* x3);
|
---|
| 317 |
|
---|
| 318 | int main(int narg, char* arg[])
|
---|
| 319 | {
|
---|
| 320 |
|
---|
| 321 | SophyaInit();
|
---|
| 322 | InitTim(); // Initializing the CPU timer
|
---|
| 323 |
|
---|
| 324 | if (narg < 2) {
|
---|
| 325 | cout << " Missing argument/ Usage: ovharr NLoop [NRow NCol] \n "
|
---|
| 326 | << " NRow==0 OR NCol==0 ---> Test 2x2 only \n " << endl;
|
---|
| 327 | exit(1);
|
---|
| 328 | }
|
---|
| 329 | int i,j,k;
|
---|
| 330 | char buff[128];
|
---|
| 331 | int N = atoi(arg[1]);
|
---|
| 332 | int nrow = 2;
|
---|
| 333 | int ncol = 2;
|
---|
| 334 |
|
---|
| 335 | if (narg > 2) nrow = atoi(arg[2]);
|
---|
| 336 | if (narg > 3) ncol = atoi(arg[3]);
|
---|
| 337 |
|
---|
| 338 | bool fgall = true;
|
---|
| 339 | if ((nrow == 0) || (ncol == 0)) fgall = false;
|
---|
| 340 |
|
---|
| 341 | cout << " ovharr/ Testing TArray overhead - NLoop = " << N
|
---|
| 342 | << " NRow=" << nrow << " NCol= " << ncol << endl;
|
---|
| 343 | try {
|
---|
| 344 |
|
---|
| 345 | if (fgall) {
|
---|
| 346 | cout << "1) ------ Overhead using TMatrix<T> ------" << endl;
|
---|
| 347 | Timer tm("Overhead:TMatrix<T>");
|
---|
| 348 | for(k=0; k<N; k++) {
|
---|
| 349 | Matrix * m1 = new Matrix(nrow, ncol);
|
---|
| 350 | Matrix * m2 = new Matrix(nrow, ncol);
|
---|
| 351 | Matrix * m3 = new Matrix(nrow, ncol);
|
---|
| 352 | for(i=0; i<nrow; i++)
|
---|
| 353 | for(j=0; j<ncol; j++) {
|
---|
| 354 | (*m1)(i,j) = k*300+10.*i+j;
|
---|
| 355 | (*m2)(i,j) = k*550+20.*i+2.*j;
|
---|
| 356 | }
|
---|
| 357 | *m3 = *m1 + *m2;
|
---|
| 358 | // m4 = m1*m2 est une multiplication de matrice avec les TMatrix<T>
|
---|
| 359 | Matrix * m4 = new Matrix(*m1);
|
---|
| 360 | m4->MulElt(*m2);
|
---|
| 361 |
|
---|
| 362 | delete m1;
|
---|
| 363 | delete m2;
|
---|
| 364 | delete m3;
|
---|
| 365 | delete m4;
|
---|
| 366 | // if (k%(N/10) == 0) {
|
---|
| 367 | // sprintf(buff, "ovharr: Iteration k= %d ",k);
|
---|
| 368 | // PrtTim(buff);
|
---|
| 369 | // }
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | if (fgall) {
|
---|
| 374 | cout << "2) ------ Overhead using TMatrix<T> No new ------" << endl;
|
---|
| 375 | Timer tm("Overhead:TMatrix<T> No new");
|
---|
| 376 | for(k=0; k<N; k++) {
|
---|
| 377 | Matrix m1(nrow, ncol);
|
---|
| 378 | Matrix m2(nrow, ncol);
|
---|
| 379 | for(i=0; i<nrow; i++)
|
---|
| 380 | for(j=0; j<ncol; j++) {
|
---|
| 381 | m1(i,j) = k*300+10.*i+j;
|
---|
| 382 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 383 | }
|
---|
| 384 | Matrix m3 = m1 + m2;
|
---|
| 385 | Matrix m4(m1);
|
---|
| 386 | m4.MulElt(m2);
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | if (fgall) {
|
---|
| 391 | cout << "3) ------ Overhead using SimpleMatrix<r_8> ------" << endl;
|
---|
| 392 | Timer tm("Overhead:SimpleMatrix<r_8>");
|
---|
| 393 | for(k=0; k<N; k++) {
|
---|
| 394 | SimpleMatrix<r_8> * m1 = new SimpleMatrix<r_8>(nrow, ncol);
|
---|
| 395 | SimpleMatrix<r_8> * m2 = new SimpleMatrix<r_8>(nrow, ncol);
|
---|
| 396 | SimpleMatrix<r_8> * m3 = new SimpleMatrix<r_8>(nrow, ncol);
|
---|
| 397 | SimpleMatrix<r_8> * m4 = new SimpleMatrix<r_8>(nrow, ncol);
|
---|
| 398 | for(i=0; i<nrow; i++)
|
---|
| 399 | for(j=0; j<ncol; j++) {
|
---|
| 400 | (*m1)(i,j) = k*300+10.*i+j;
|
---|
| 401 | (*m2)(i,j) = k*550+20.*i+2.*j;
|
---|
| 402 | }
|
---|
| 403 | *m3 = *m1 + *m2;
|
---|
| 404 | *m4 = *m1 * *m2;
|
---|
| 405 | delete m1;
|
---|
| 406 | delete m2;
|
---|
| 407 | delete m3;
|
---|
| 408 | delete m4;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | if (fgall) {
|
---|
| 413 | cout << "4) ---- Overhead using SimpleMatrix<r_8> NO new ----" << endl;
|
---|
| 414 | Timer tm("Overhead:SimpleMatrix<r_8> NO new");
|
---|
| 415 | for(k=0; k<N; k++) {
|
---|
| 416 | SimpleMatrix<r_8> m1(nrow, ncol);
|
---|
| 417 | SimpleMatrix<r_8> m2(nrow, ncol);
|
---|
| 418 | for(i=0; i<nrow; i++)
|
---|
| 419 | for(j=0; j<ncol; j++) {
|
---|
| 420 | m1(i,j) = k*300+10.*i+j;
|
---|
| 421 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 422 | }
|
---|
| 423 | SimpleMatrix<r_8> m3 = m1 + m2;
|
---|
| 424 | SimpleMatrix<r_8> m4 = m1 * m2;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | if (fgall) {
|
---|
| 429 | cout << "5) ---- Overhead using VerySimpleMatrix( , , zero=true) ----" << endl;
|
---|
| 430 | Timer tm("Overhead:VerySimpleMatrix( , , zero=true)");
|
---|
| 431 | for(k=0; k<N; k++) {
|
---|
| 432 | VerySimpleMatrix * m1 = new VerySimpleMatrix(nrow, ncol, true);
|
---|
| 433 | VerySimpleMatrix * m2 = new VerySimpleMatrix(nrow, ncol, true);
|
---|
| 434 | VerySimpleMatrix * m3 = new VerySimpleMatrix(nrow, ncol, true);
|
---|
| 435 | VerySimpleMatrix * m4 = new VerySimpleMatrix(nrow, ncol, true);
|
---|
| 436 | for(i=0; i<nrow; i++)
|
---|
| 437 | for(j=0; j<ncol; j++) {
|
---|
| 438 | (*m1)(i,j) = k*300+10.*i+j;
|
---|
| 439 | (*m2)(i,j) = k*550+20.*i+2.*j;
|
---|
| 440 | }
|
---|
| 441 | *m3 = *m1 + *m2;
|
---|
| 442 | *m4 = *m1 * *m2;
|
---|
| 443 | delete m1;
|
---|
| 444 | delete m2;
|
---|
| 445 | delete m3;
|
---|
| 446 | delete m4;
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | if (fgall) {
|
---|
| 451 | cout << "6) ---- Overhead using VerySimpleMatrix( , , zero=false) ----" << endl;
|
---|
| 452 | Timer tm("Overhead:VerySimpleMatrix( , , zero=true)");
|
---|
| 453 | for(k=0; k<N; k++) {
|
---|
| 454 | VerySimpleMatrix * m1 = new VerySimpleMatrix(nrow, ncol, false);
|
---|
| 455 | VerySimpleMatrix * m2 = new VerySimpleMatrix(nrow, ncol, false);
|
---|
| 456 | VerySimpleMatrix * m3 = new VerySimpleMatrix(nrow, ncol, false);
|
---|
| 457 | VerySimpleMatrix * m4 = new VerySimpleMatrix(nrow, ncol, false);
|
---|
| 458 | for(i=0; i<nrow; i++)
|
---|
| 459 | for(j=0; j<ncol; j++) {
|
---|
| 460 | (*m1)(i,j) = k*300+10.*i+j;
|
---|
| 461 | (*m2)(i,j) = k*550+20.*i+2.*j;
|
---|
| 462 | }
|
---|
| 463 | *m3 = *m1 + *m2;
|
---|
| 464 | *m4 = *m1 * *m2;
|
---|
| 465 | delete m1;
|
---|
| 466 | delete m2;
|
---|
| 467 | delete m3;
|
---|
| 468 | delete m4;
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | if (fgall) {
|
---|
| 473 | cout << "7) ---- Overhead using VerySimpleMatrix( , , zero=false) NO new ----" << endl;
|
---|
| 474 | Timer tm("Overhead:VerySimpleMatrix( , , zero=true) NO new");
|
---|
| 475 | for(k=0; k<N; k++) {
|
---|
| 476 | VerySimpleMatrix m1(nrow, ncol, false);
|
---|
| 477 | VerySimpleMatrix m2(nrow, ncol, false);
|
---|
| 478 | for(i=0; i<nrow; i++)
|
---|
| 479 | for(j=0; j<ncol; j++) {
|
---|
| 480 | m1(i,j) = k*300+10.*i+j;
|
---|
| 481 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 482 | }
|
---|
| 483 | VerySimpleMatrix m3 = m1 + m2;
|
---|
| 484 | VerySimpleMatrix m4 = m1 * m2;
|
---|
| 485 | }
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | if (fgall) {
|
---|
| 489 | cout << "8) ---- Overhead using new double[nrow*ncol] ----" << endl;
|
---|
| 490 | Timer tm("Overhead:new double[nrow*ncol]");
|
---|
| 491 | for(k=0; k<N; k++) {
|
---|
| 492 | int l = nrow*ncol;
|
---|
| 493 | double* m1 = new double[l];
|
---|
| 494 | double* m2 = new double[l];
|
---|
| 495 | double* m3 = new double[l];
|
---|
| 496 | double* m4 = new double[l];
|
---|
| 497 | for(i=0; i<nrow; i++)
|
---|
| 498 | for(j=0; j<ncol; j++) {
|
---|
| 499 | m1[i*ncol+j] = k*300+10.*i+j;
|
---|
| 500 | m2[i*ncol+j] = k*550+20.*i+2.*j;
|
---|
| 501 | }
|
---|
| 502 | for(i=0; i<l; i++) m3[i] = m1[i] + m2[i];
|
---|
| 503 | for(i=0; i<l; i++) m3[i] = m4[i] * m2[i];
|
---|
| 504 | delete[] m1;
|
---|
| 505 | delete[] m2;
|
---|
| 506 | delete[] m3;
|
---|
| 507 | delete[] m4;
|
---|
| 508 | }
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 |
|
---|
| 512 | {
|
---|
| 513 | cout << "9) ---- Overhead using SmallMatrix<r_8,4,5> No new ----" << endl;
|
---|
| 514 | Timer tm("Overhead:SmallMatrix<r_8,4,5> No new ");
|
---|
| 515 | for(k=0; k<N; k++) {
|
---|
| 516 | SmallMatrix<r_8,4,5> m1,m2;
|
---|
| 517 | for(i=0; i<4; i++)
|
---|
| 518 | for(j=0; j<5; j++) {
|
---|
| 519 | m1(i,j) = k*300+10.*i+j;
|
---|
| 520 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 521 | }
|
---|
| 522 | SmallMatrix<r_8,4,5> m3 = m1 + m2;
|
---|
| 523 | SmallMatrix<r_8,4,5> m4 = m1 * m2;
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | {
|
---|
| 528 | cout << "10) ---- Overhead using SmallMatrix<r_8,2,2> No new ----" << endl;
|
---|
| 529 | Timer tm("Overhead:SmallMatrix<r_8,2,2> No new");
|
---|
| 530 | for(k=0; k<N; k++) {
|
---|
| 531 | SmallMatrix<r_8,2,2> m1,m2;
|
---|
| 532 | for(i=0; i<2; i++)
|
---|
| 533 | for(j=0; j<2; j++) {
|
---|
| 534 | m1(i,j) = k*300+10.*i+j;
|
---|
| 535 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 536 | }
|
---|
| 537 | SmallMatrix<r_8,2,2>m3 = m1 + m2;
|
---|
| 538 | SmallMatrix<r_8,2,2>m4 = m1 * m2;
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 |
|
---|
| 543 | {
|
---|
| 544 | cout << "11) ---- Overhead using VerySimpleMatrix2x2 ----" << endl;
|
---|
| 545 | Timer tm("Overhead:VerySimpleMatrix2x2");
|
---|
| 546 | for(k=0; k<N; k++) {
|
---|
| 547 | VerySimpleMatrix2x2 * m1 = new VerySimpleMatrix2x2();
|
---|
| 548 | VerySimpleMatrix2x2 * m2 = new VerySimpleMatrix2x2();
|
---|
| 549 | VerySimpleMatrix2x2 * m3 = new VerySimpleMatrix2x2();
|
---|
| 550 | VerySimpleMatrix2x2 * m4 = new VerySimpleMatrix2x2();
|
---|
| 551 | for(i=0; i<2; i++)
|
---|
| 552 | for(j=0; j<2; j++) {
|
---|
| 553 | (*m1)(i,j) = k*300+10.*i+j;
|
---|
| 554 | (*m2)(i,j) = k*550+20.*i+2.*j;
|
---|
| 555 | }
|
---|
| 556 | *m3 = *m1 + *m2;
|
---|
| 557 | *m4 = *m1 * *m2;
|
---|
| 558 | delete m1;
|
---|
| 559 | delete m2;
|
---|
| 560 | delete m3;
|
---|
| 561 | delete m4;
|
---|
| 562 | }
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | {
|
---|
| 566 | cout << "12) ---- Overhead using VerySimpleMatrix2x2 NO new ----" << endl;
|
---|
| 567 | Timer tm("Overhead:VerySimpleMatrix2x2 NO new");
|
---|
| 568 | for(k=0; k<N; k++) {
|
---|
| 569 | VerySimpleMatrix2x2 m1,m2;
|
---|
| 570 | for(i=0; i<2; i++)
|
---|
| 571 | for(j=0; j<2; j++) {
|
---|
| 572 | m1(i,j) = k*300+10.*i+j;
|
---|
| 573 | m2(i,j) = k*550+20.*i+2.*j;
|
---|
| 574 | }
|
---|
| 575 | VerySimpleMatrix2x2 m3 = m1+m2;
|
---|
| 576 | VerySimpleMatrix2x2 m4 = m1*m2;
|
---|
| 577 | }
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 |
|
---|
| 581 | {
|
---|
| 582 | cout << "13) ---- Overhead using double a[4] + function call ----" << endl;
|
---|
| 583 | Timer tm("Overhead:double a[4]");
|
---|
| 584 | for(k=0; k<N; k++) {
|
---|
| 585 | double m1[4], m2[4], m3[4], m4[4];
|
---|
| 586 | for(i=0; i<2; i++)
|
---|
| 587 | for(j=0; j<2; j++) {
|
---|
| 588 | m1[i*2+j] = k*300+10.*i+j;
|
---|
| 589 | m2[i*2+j] = k*550+20.*i+2.*j;
|
---|
| 590 | }
|
---|
| 591 | add_double_n(4, m1, m2, m3);
|
---|
| 592 | mul_double_n(4, m1, m2, m4);
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | {
|
---|
| 597 | cout << "14) ---- Overhead a3[4] = a1[4]+a2[4] ----" << endl;
|
---|
| 598 | Timer tm("Overhead:double a[4]");
|
---|
| 599 | for(k=0; k<N; k++) {
|
---|
| 600 | double m1[4], m2[4], m3[4], m4[4];
|
---|
| 601 | for(i=0; i<2; i++)
|
---|
| 602 | for(j=0; j<2; j++) {
|
---|
| 603 | m1[i*2+j] = k*300+10.*i+j;
|
---|
| 604 | m2[i*2+j] = k*550+20.*i+2.*j;
|
---|
| 605 | }
|
---|
| 606 | for(i=0; i<4; i++) m3[i] = m1[i] + m2[i];
|
---|
| 607 | for(i=0; i<4; i++) m4[i] = m1[i] * m2[i];
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 |
|
---|
| 612 | }
|
---|
| 613 | catch (PThrowable exc) {
|
---|
| 614 | cerr << " catched Exception " << exc.Msg() << endl;
|
---|
| 615 | }
|
---|
| 616 | catch (...) {
|
---|
| 617 | cerr << " catched unknown (...) exception " << endl;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | cout << "\n --------------------------------------------------------" << endl;
|
---|
| 621 | PrtTim("--- End of ovharr ---");
|
---|
| 622 | cout << " --------------- END of ovharr programme -------------- " << endl;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | /* Fonction pour ajouter deux tableaux */
|
---|
| 626 | void add_double_n(int n, double* x1, double* x2, double* x3)
|
---|
| 627 | {
|
---|
| 628 | if (n < 1)
|
---|
| 629 | throw(SzMismatchError("add_double_n : n<1 !"));
|
---|
| 630 | for(int i=0; i<n; i++) x3[i] = x1[i] + x2[i];
|
---|
| 631 | return;
|
---|
| 632 | }
|
---|
| 633 | /* Fonction pour multiplier deux tableaux */
|
---|
| 634 | void mul_double_n(int n, double* x1, double* x2, double* x3)
|
---|
| 635 | {
|
---|
| 636 | if (n < 1)
|
---|
| 637 | throw(SzMismatchError("mul_double_n : n<1 !"));
|
---|
| 638 | for(int i=0; i<n; i++) x3[i] = x1[i] * x2[i];
|
---|
| 639 | return;
|
---|
| 640 | }
|
---|