Changeset 8433f39 in tol


Ignore:
Timestamp:
Sep 4, 2009, 5:32:09 PM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
7ec5738
Parents:
d134076
Message:

MatReadRows and MatReadCell can take a path or a file handler created with FOpen to be more efficient.
MatReadRows and MatReadCell have new optional arguments to specify matrix dimensions instead of reading them from file header.

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1331 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/btol/matrix_type/matgra.cpp

    rd134076 r8433f39  
    4444#include <tol/tol_ois.h>
    4545#include <tol/tol_bsvdsep.h>
     46#include <tol/tol_filehandle.h>
    4647
    4748#include <tol/tol_blas.h>
    … …  
    355356
    356357//--------------------------------------------------------------------
    357 void BBM_BinReadRows(const BText& fileName, BMat& M,
     358void BBM_BinReadRows(FILE* fil, BMat& M,
     359                     int m, int n,
    358360                     int i, int r, int t=1)
    359361//--------------------------------------------------------------------
    360362{
    361 #ifdef BBM_CHECK_SIZE
    362   BInt fs = GetFileSize(fileName);
    363 #endif
    364363  int k, tr = t*r;
    365364//Std(BText("\nBBM_BinRead ")+fileName+" bytes "+fs);
    366   FILE* fil = fopen(fileName.String(),"rb");
    367   if(!fil)
    368   {
    369     Error(I2("Cannot open for read BBM file ",
    370              "No se pudo abrir para lectura el fichero BBM ")+
    371           fileName);
    372     return;
    373   }
    374   BInt m, n, mn;
    375   fread(&m,sizeof(BInt),1,fil);
    376   fread(&n,sizeof(BInt),1,fil);
     365  BInt mn;
     366  if((m<=0)||(n<=0))
     367  {
     368    fseek(fil,0,SEEK_SET);
     369    fread(&m,sizeof(BInt),1,fil);
     370    fread(&n,sizeof(BInt),1,fil);
     371  }
    377372  mn= m*n;
    378373  if(!m || !n)
    379374  {
    380     fclose(fil);
    381375    return;
    382376  }
    … …  
    388382     (0>=tr)||(i+tr-1>=m) )
    389383  {
    390     fclose(fil);
    391384    return;
    392385  }
    393 #ifdef BBM_CHECK_SIZE
    394   int s = fs -2*sizeof(BInt);
    395   //In TOL versions after 2007-01-15 structure page size = 4 => sizeof(BDat)=12
    396   if(s==mn*(int)sizeof(BDat))
    397 #endif
    398386  {
    399387    M.Alloc(r,n);
    … …  
    420408    }
    421409  }
    422   if(fclose(fil))
    423   {
    424     Error(I2("Cannot close after reading BBM file ",
    425              "No se pudo cerrar despues de leer el fichero BBM ")+
    426           fileName);
    427   };
    428   BBM_CheckSize("BBM_BinReadRows",fileName,m,n);
    429 }
    430 
    431 //--------------------------------------------------------------------
    432 void BBM_BinReadCell(const BText& fileName, BDat& cell, int i, int j)
    433 //--------------------------------------------------------------------
    434 {
    435   FILE* fil = fopen(fileName.String(),"rb");
    436   if(!fil)
    437   {
    438     Error(I2("Cannot open for read BBM file ",
    439              "No se pudo abrir para lectura el fichero BBM ")+
    440           fileName);
    441     return;
    442   }
    443   BInt m, n;
    444   fread(&m,sizeof(BInt),1,fil);
    445   fread(&n,sizeof(BInt),1,fil);
     410}
     411
     412//--------------------------------------------------------------------
     413void BBM_BinReadCell(FILE* fil, BDat& cell,
     414                     int m, int n,
     415                     int i, int j)
     416//--------------------------------------------------------------------
     417{
     418  if((m<=0)||(n<=0))
     419  {
     420    fseek(fil,0,SEEK_SET);
     421    fread(&m,sizeof(BInt),1,fil);
     422    fread(&n,sizeof(BInt),1,fil);
     423  }
    446424  if((0>i)||(i>=m)||
    447425     (0>j)||(j>=n) )
    … …  
    453431  fseek(fil,pos,SEEK_SET);
    454432  fread(&cell, 1, sizeof(BDat), fil);
    455   if(fclose(fil))
    456   {
    457     Error(I2("Cannot close after reading BBM file ",
    458              "No se pudo cerrar despues de leer el fichero BBM ")+
    459           fileName);
    460   };
    461   BBM_CheckSize("BBM_BinReadCell",fileName,m,n);
    462433}
    463434
    … …  
    602573  contens_.AddElement(c_);
    603574}
     575
    604576//--------------------------------------------------------------------
    605577DeclareContensClass(BMat, BMatTemporary, BMatReadRows);
    606 DefExtOpr(1, BMatReadRows, "MatReadRows", 3, 4, "Text Real Real Real",
    607   "(Text filename, Real firstRow, Real numRows [, Real thinning=1])",
    608   I2("Reads a range of rows of a matrix from a file in binary format",
    609      "Lee un rango de filas de una matriz de un fichero en formato binario"),
     578DefExtOpr(1, BMatReadRows, "MatReadRows", 3, 6,
     579  "{Text|Real} Real Real Real Real Real",
     580  "({Text|Real} file, Real firstRow, Real numRows "
     581  "[, Real thinning=1, Real numRows=-1, Real numColumns=-1])",
     582  I2("Reads a range of rows of a matrix from a file in binary format\n"
     583     "If argument <file> is a Text then the file in these path will be "
     584     "open before to read and it will be closed after.\n"
     585     "If argument <file> is Real created with FOpen the file is already "
     586     "open and the user should close with FClose after. This is a faster "
     587     "method for multiple reading over the same file.\n"
     588     "Afterwords, if arguments <numRows> and <numColumns> are greater than "
     589     "zero, then it will not have to read them from the file, which can "
     590     "greatly accelerate the process.",
     591     "Lee un rango de filas de una matriz de un fichero en formato binario\n"
     592     "Si el argumento <file> es un Text entonces el fichero en ese "
     593     "camino será abierto antes de leer y cerrado después.\n"
     594     "Si el argumento <file> es un Real creado con FOpen entonces el "
     595     "fichero ya está abierto y será el usuario el que lo cierre cuando "
     596     "se requiera. Este es el método más rápido cuando hay que hacer "
     597     "múltiples lecturas sobre un mismo fichero.\n"
     598     "Si además los argumentos <numRows> y <numColumns> son mayores que "
     599     "cero entonces no será necesario leerlos del propio fichero lo cual "
     600     "puede acelerarse mucho el proceso."),
    610601    BOperClassify::MatrixAlgebra_);
    611602//--------------------------------------------------------------------
    … …  
    613604//--------------------------------------------------------------------
    614605{
    615   BText& f =      Text(Arg(1));
    616   int    i = (int)Real(Arg(2))-1;
    617   int    r = (int)Real(Arg(3));
    618   int    t = 1;
     606  FILE* file = NULL;
     607  int i = (int)Real(Arg(2))-1;
     608  int r = (int)Real(Arg(3));
     609  int t = 1;
     610  int m = -1;
     611  int n = -1;
    619612  if(Arg(4)) { t = (int)Real(Arg(4)); }
    620   BBM_BinReadRows(f,contens_,i,r,t);
     613  if(Arg(5)) { m = (int)Real(Arg(5)); }
     614  if(Arg(6)) { n = (int)Real(Arg(6)); }
     615  if(Arg(1)->Grammar()==GraReal())
     616  {
     617    int handle = (int)Real(Arg(1));
     618    FILE* file = BFileDesc::CheckFileHandle(handle,true,
     619      I2("Fail in ","Fallo en ")+"BMatReadRows"); 
     620    if(file)
     621    {
     622      BBM_BinReadRows(file,contens_,m,n,i,r,t);
     623    }
     624  }
     625  else
     626  {
     627    BText& path = Text(Arg(1));
     628    file = fopen(path.String(),"rb");
     629    if(!file)
     630    {
     631      Error(I2("Cannot open for MatReadRows BBM file ",
     632               "No se pudo abrir para MatReadRows el fichero BBM ")+
     633            path);
     634      return;
     635    }
     636    BBM_BinReadRows(file,contens_,m,n,i,r,t);
     637    fclose(file);
     638  }
    621639}
    622640//--------------------------------------------------------------------
    623641DeclareContensClass(BDat, BDatTemporary, BMatReadCell);
    624 DefExtOpr(1, BMatReadCell, "MatReadCell", 3, 3, "Text Real Real",
    625   I2("(Text filename, Real row, Real column)",
    626      "(Text nombreFichero, Real file, Real columna)"),
    627   I2("Reads a cell matrix from a file in binary format",
    628      "Lee una celda de una matriz de un fichero en formato binario"),
     642DefExtOpr(1, BMatReadCell, "MatReadCell", 3, 5,
     643  "{Text|Real} Real Real Real Real",
     644  "({Text|Real} file, Real row, Real column)"
     645  "[, Real numRows=-1, Real numColumns=-1])",
     646  I2("Reads a cell matrix from a file in binary format.\n"
     647     "If argument <file> is a Text then the file in these path will be "
     648     "open before to read and it will be closed after.\n"
     649     "If argument <file> is Real created with FOpen the file is already "
     650     "open and the user should close with FClose after. This is a faster "
     651     "method for multiple reading over the same file.\n"
     652     "Afterwords, if arguments <numRows> and <numColumns> are greater than "
     653     "zero, then it will not have to read them from the file, which can "
     654     "greatly accelerate the process.",
     655     "Lee una celda de una matriz de un fichero en formato binario\n"
     656     "Si el argumento <file> es un Text entonces el fichero en ese "
     657     "camino será abierto antes de leer y cerrado después.\n"
     658     "Si el argumento <file> es un Real creado con FOpen entonces el "
     659     "fichero ya está abierto y será el usuario el que lo cierre cuando "
     660     "se requiera. Este es el método más rápido cuando hay que hacer "
     661     "múltiples lecturas sobre un mismo fichero.\n"
     662     "Si además los argumentos <numRows> y <numColumns> son mayores que "
     663     "cero entonces no será necesario leerlos del propio fichero lo cual "
     664     "puede acelerarse mucho el proceso."),
    629665    BOperClassify::MatrixAlgebra_);
    630666//--------------------------------------------------------------------
    … …  
    632668//--------------------------------------------------------------------
    633669{
    634   BText& f =      Text(Arg(1));
    635   int    i = (int)Real(Arg(2))-1;
    636   int    j = (int)Real(Arg(3))-1;
    637   BBM_BinReadCell(f,contens_,i,j);
     670  FILE* file = NULL;
     671  int i = (int)Real(Arg(2))-1;
     672  int j = (int)Real(Arg(3))-1;
     673  int m = -1;
     674  int n = -1;
     675  if(Arg(4)) { m = (int)Real(Arg(4)); }
     676  if(Arg(5)) { n = (int)Real(Arg(5)); }
     677  if(Arg(1)->Grammar()==GraReal())
     678  {
     679    int handle = (int)Real(Arg(1));
     680    FILE* file = BFileDesc::CheckFileHandle(handle,true,
     681      I2("Fail in ","Fallo en ")+"MatReadCell"); 
     682    if(file)
     683    {
     684      BBM_BinReadCell(file,contens_,m,n,i,j);
     685    }
     686  }
     687  else
     688  {
     689    BText& path = Text(Arg(1));
     690    file = fopen(path.String(),"rb");
     691    if(!file)
     692    {
     693      Error(I2("Cannot open for MatReadCell BBM file ",
     694               "No se pudo abrir para MatReadCell el fichero BBM ")+
     695            path);
     696      return;
     697    }
     698    BBM_BinReadCell(file,contens_,m,n,i,j);
     699    fclose(file);
     700  }
    638701}
    639702
    … …  
    641704// Algebraic temporary class declarations.
    642705//--------------------------------------------------------------------
    643 
    644 
    645706
    646707//--------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.