improved efficiency of plain data matrix transposed get() and put()

This commit is contained in:
jiri committed 2026-11-25 19:33:56 +01:00
1 parent d3a106019f
commit f028a2e0fc
3 files changed
+50 -2

No files matched your search

+2 -1
View File
@@ -287,7 +287,8 @@ void get(int fd, bool dimensions=1, bool transp=0, bool orca_format=false);
\item \code{NRVec}: header $n$ and a padding \code{int} 1 (8-byte alignment), then the $n$ elements; \code{transp} is ignored. \item \code{NRVec}: header $n$ and a padding \code{int} 1 (8-byte alignment), then the $n$ elements; \code{transp} is ignored.
\item \code{NRMat}: header \code{nrows}, \code{ncols}, then the elements by rows. With \code{transp} the transposed matrix is \item \code{NRMat}: header \code{nrows}, \code{ncols}, then the elements by rows. With \code{transp} the transposed matrix is
written (dimensions swapped, elements column by column); \code{get} with \code{transp} reads such data back into the written (dimensions swapped, elements column by column); \code{get} with \code{transp} reads such data back into the
original orientation, i.e.\ it reads a file stored by columns. original orientation, i.e.\ it reads a file stored by columns. For elements of plain types this is done by one
write or read of a temporary transposed copy of the matrix (elements of class type are processed one by one).
\item \code{NRSMat}: header $n$, $n$, then the packed lower triangle; \code{transp} is ignored. \item \code{NRSMat}: header $n$, $n$, then the packed lower triangle; \code{transp} is ignored.
\item \code{SparseMat}: header \code{nrows}, \code{ncols} (\code{SPMatindex}) and the \code{int} flags \code{symmetric} and \item \code{SparseMat}: header \code{nrows}, \code{ncols} (\code{SPMatindex}) and the \code{int} flags \code{symmetric} and
\code{nonzero}, then triples (row, column, element) and a terminating pair of indices $-1$; \code{SparseSMat}: header \code{nonzero}, then triples (row, column, element) and a terminating pair of indices $-1$; \code{SparseSMat}: header
+23 -1
View File
@@ -167,7 +167,18 @@ void NRMat<T>::put(int fd, bool dim, bool transp, bool orcaformat) const {
} }
} }
if(transp){ //not particularly efficient if(transp){
if(LA_traits<T>::is_plaindata()){ //transpose in memory and write at once
NRMat<T> tmp(mm,nn);
for(int i=0; i<nn; ++i) for(int j=0; j<mm; ++j) tmp[j][i] = (*this)[i][j];
LA_traits<T>::multiput((size_t)nn*(size_t)mm,fd,
#ifdef MATPTR
tmp.v[0]
#else
tmp.v
#endif
,dim);
}else //element by element, not particularly efficient
for(int j=0; j<mm; ++j){ for(int j=0; j<mm; ++j){
for(int i=0; i<nn; ++i){ for(int i=0; i<nn; ++i){
LA_traits<T>::put(fd, LA_traits<T>::put(fd,
@@ -226,6 +237,17 @@ void NRMat<T>::get(int fd, bool dim, bool transp, bool orcaformat){
} }
if(transp){ if(transp){
if(LA_traits<T>::is_plaindata()){ //read at once and transpose in memory
NRMat<T> tmp(mm,nn);
LA_traits<T>::multiget((size_t)nn*(size_t)mm,fd,
#ifdef MATPTR
tmp.v[0]
#else
tmp.v
#endif
,dim);
for(int i=0; i<nn; ++i) for(int j=0; j<mm; ++j) (*this)[i][j] = tmp[j][i];
}else //element by element
for(register int j=0; j<mm; ++j){ for(register int j=0; j<mm; ++j){
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
LA_traits<T>::get(fd, LA_traits<T>::get(fd,
+25
View File
@@ -4867,6 +4867,31 @@ cout <<"Error of the inverse block = "<<(b*r.submatrix(rows,cols)-one).norm()<<e
if(n>k&&m>k) cout <<"Error of the Schur complement = "<<(r.submatrix(orows,ocols)-a.submatrix(orows,ocols)+a.submatrix(orows,cols)*calcinverse(b)*a.submatrix(rows,ocols)).norm()<<endl; if(n>k&&m>k) cout <<"Error of the Schur complement = "<<(r.submatrix(orows,ocols)-a.submatrix(orows,ocols)+a.submatrix(orows,cols)*calcinverse(b)*a.submatrix(rows,ocols)).norm()<<endl;
} }
if(0) //transposed binary put/get of a matrix: the file is the plain storage of the transposed matrix
{
int n,m;
cin>>n>>m;
NRMat<double> a(n,m),b,c;
a.randomize(1.);
NRMat<int> ia(n,m),ib,ic;
for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) ia(i,j)=1000*i+j;
int f=open("transposed_put_get.tmp",O_RDWR|O_CREAT|O_TRUNC,0600);
a.put(f,true,true);
ia.put(f,true,true);
lseek(f,0,SEEK_SET);
b.get(f,true,true);
ib.get(f,true,true);
lseek(f,0,SEEK_SET);
c.get(f,true,false);
ic.get(f,true,false);
close(f);
unlink("transposed_put_get.tmp");
cout <<"Error of get after put, both transposed = "<<(b-a).norm()<<" "<<(ib!=ia)<<endl;
int err=0;
for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) if(c(j,i)!=a(i,j) || ic(j,i)!=ia(i,j)) ++err;
cout <<"Dimensions of the plain read "<<c.nrows()<<" "<<c.ncols()<<", number of elements differing from the transposed matrix = "<<err<<endl;
}
if(0) //testbed of the general methods if(0) //testbed of the general methods
{ {
int seed; int seed;