Changeset 2583 in Sophya for trunk/SophyaLib/TArray/tmatrix.cc


Ignore:
Timestamp:
Jul 30, 2004, 12:24:12 PM (21 years ago)
Author:
cmv
Message:
  • Intro decision auto d'optimisation produit de matrices
  • Possibilite a l'utilisateur pour choisir l'optimisation
  • cas FxC optimise par copie

cmv 30/07/04

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/TArray/tmatrix.cc

    r2575 r2583  
    1 // $Id: tmatrix.cc,v 1.26 2004-07-29 12:31:16 ansari Exp $
     1// $Id: tmatrix.cc,v 1.27 2004-07-30 10:24:12 cmv Exp $
    22//                         C.Magneville          04/99
    33#include "machdefs.h"
     
    66#include "pexceptions.h"
    77#include "tmatrix.h"
    8 
    9 //#define DO_NOT_OPTIMIZE_PRODUCT
    108
    119/*!
     
    448446template <class T>
    449447TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
     448// Calcul de C= rm = A*B   (A=*this)
     449// Remember: C-like      matrices are column packed
     450//           Fortan-like matrices are line   packed
    450451{
    451452  if (NCols() != b.NRows())
    452453    throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
    453454
    454   // Commentaire: pas de difference de vitesse notable selon les mappings choisis pour la matrice produit "rm"
     455  // Commentaire: pas de difference de vitesse notable selon le mapping de la matrice produit "rm"
    455456  if (mm == SameMemoryMapping) mm = GetMemoryMapping(); 
    456457  TMatrix<T> rm(NRows(), b.NCols(), mm);
    457   rm = (T) 0;  // Rien ne garantit que rm soit mise a zero a la creation !
    458458
    459459  // Les "steps" pour l'adressage des colonnes de A et des lignes de B
     
    461461  sa_size_t stepb = b.Step(b.RowsKA());
    462462
    463   // On decide si on optimise ou non selon les dimensions de A et B
    464   // (il semble que optimiser ou non ne degrade pas notablement la vitesse pour les petites matrices)
    465 #ifdef DO_NOT_OPTIMIZE_PRODUCT
    466   bool no_optim = true;
    467 #else
    468   bool no_optim = false;
    469   //NON juste pour memoire: if((stepa+stepb)*NCols()*sizeof(T)<100000) no_optim=true;
    470 #endif
    471 
    472   // Calcul de C=rm = A*B   (A=*this)
    473   // Remember: C-like      matrices are column packed
    474   //           Fortan-like matrices are line   packed
     463  // Taille totale des matrices A et B
     464  size_t totsiza = this->DataBlock().Size();
     465  size_t totsizb = b.DataBlock().Size();
     466
     467
     468  ///////////////////////////////////////////////////////////////////
     469  // On decide si on optimise ou non selon les dimensions de A et B //
     470  // (il semble que optimiser ou non ne degrade pas                 //
     471  //  beaucoup la vitesse pour les petites matrices)                //
     472  ////////////////////////////////////////////////////////////////////
     473
     474  uint_2 popt = GetMatProdOpt();
     475  bool no_optim = false; // optimization demandee par default
     476  if( (popt&(uint_2)1) == 0 ) { // pas d'optimization explicitement demande
     477    no_optim = true;
     478  } else if( (popt&(uint_2)2) == 0 ) { // pas d'optimization forcee, la methode decide
     479    // On part sur une disponibilite dans le cache processeur de 100 ko
     480    // (A et B peuvent etre stoquees dans le cache)
     481    if((totsiza+totsizb)*sizeof(T)<100000) no_optim = true;
     482  }
     483
    475484  sa_size_t r,c,k;
    476485  T sum;
    477486  const T * pe;
    478487
    479   // Pas d'optimisation demandee
     488
     489  /////////////////////////////////
     490  // Pas d'optimisation demandee //
     491  /////////////////////////////////
     492
    480493  if( no_optim ) {
    481494    //cout<<"no_optim("<<no_optim<<") "<<stepa<<" "<<stepb<<endl;
     
    493506      }
    494507    }
    495   }
    496   // A.col est packed  et  B.row est packed (on a interet a optimiser quand meme)
    497   else if(stepa==1 && stepb==1) {
     508    return rm;
     509  }
     510
     511
     512  //////////////////////////////////////////////////////////////////////////////////
     513  // A.col est packed  et  B.row est packed (on a interet a optimiser quand meme) //
     514  //////////////////////////////////////////////////////////////////////////////////
     515
     516  if(stepa==1 && stepb==1) {
    498517    //cout<<"A.col packed && B.row not packed "<<stepa<<" "<<stepb<<endl;
    499518     T * pea = new T[rm.NCols()];
     
    510529    }
    511530    delete [] pea;
    512  }
    513   // A.col est packed  et  B.row n'est pas packed
    514   else if(stepa==1 && stepb!=1) {
     531    return rm;
     532  }
     533
     534
     535  //////////////////////////////////////////////////
     536  // A.col est packed  et  B.row n'est pas packed //
     537  //////////////////////////////////////////////////
     538
     539  if(stepa==1 && stepb!=1) {
    515540    //cout<<"A.col packed && B.row not packed "<<stepa<<" "<<stepb<<endl;
    516541    const T * pea;
     
    527552    }
    528553    delete [] peb;
    529   }
    530   // A.col n'est pas packed  et  B.row est packed
    531   else if(stepa!=1 && stepb==1) {
     554    return rm;
     555  }
     556
     557
     558  //////////////////////////////////////////////////
     559  // A.col n'est pas packed  et  B.row est packed //
     560  //////////////////////////////////////////////////
     561
     562  if(stepa!=1 && stepb==1) {
    532563    //cout<<"A.col not packed && B.row packed "<<stepa<<" "<<stepb<<endl;
    533564    T * pea = new T[rm.NCols()];
     
    544575    }
    545576    delete [] pea;
    546   }
    547   // A.col n'est pas packed  et  B.row n'est pas packed, stepb>stepa
    548   else if(stepa!=1 && stepb!=1 && stepb>stepa) {
     577    return rm;
     578  }
     579
     580
     581  ////////////////////////////////////////////////////////
     582  // A.col n'est pas packed  et  B.row n'est pas packed //
     583  ////////////////////////////////////////////////////////
     584
     585  //---- On demande l'optimization par copie d'une des matrices
     586
     587  if( (popt&(uint_2)4) ) {
     588    // On copie la plus petite
     589    if(totsiza<totsizb) {   // on copie A
     590      //cout<<"A.col not packed && B.row not packed ==> copy A to optimize "<<stepa<<" "<<stepb<<endl;
     591      // Acopy doit etre C-like pour etre column-packed
     592      TMatrix<T> acopy(NRows(),NCols(),BaseArray::CMemoryMapping);
     593      acopy = *this;
     594      rm = acopy.Multiply(b,mm);
     595    } else {               // on copie B
     596      //cout<<"A.col not packed && B.row not packed ==> copy B to optimize "<<stepa<<" "<<stepb<<endl;
     597      // Bcopy doit etre Fortran-like pour etre column-packed
     598      TMatrix<T> bcopy(b.NRows(),b.NCols(),BaseArray::FortranMemoryMapping);
     599      bcopy = b;
     600      rm = Multiply(bcopy,mm);
     601    }
     602    return rm;
     603  }
     604 
     605  //---- stepb>stepa
     606
     607  if(stepa!=1 && stepb!=1 && stepb>stepa) {
    549608    //cout<<"A.col not packed && B.row not packed ==> optimize on B "<<stepa<<" "<<stepb<<endl;
    550609    const T * pea;
     
    561620    }
    562621    delete [] peb;
    563   }
    564   // A.col n'est pas packed  et  B.row n'est pas packed, stepa>=stepb
    565   else if(stepa!=1 && stepb!=1) {
     622    return rm;
     623  }
     624
     625  //---- stepa>=stepb
     626
     627  if(stepa!=1 && stepb!=1) {
    566628    //cout<<"A.col not packed && B.row not packed ==> optimize on A "<<stepa<<" "<<stepb<<endl;
    567629    T * pea = new T[rm.NCols()];
     
    578640    }
    579641    delete [] pea;
    580   }
    581   else {
    582     cout<<"TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! "<<endl;
    583     throw(SzMismatchError("TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! ") );
    584   }
    585 
     642    return rm;
     643  }
     644
     645
     646  //////////////////////////////////////////////////
     647  // Cas non prevu, on ne doit JAMAIS arriver ici //
     648  //////////////////////////////////////////////////
     649  cout<<"TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! "<<endl;
     650  throw(SzMismatchError("TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! ") );
    586651  return rm;
    587652}
Note: See TracChangeset for help on using the changeset viewer.