From f7ef134fc54bc2e5b01407d8c4280e636a4ec5ec Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Mon, 26 Oct 2026 15:11:45 +0100 Subject: [PATCH] bugfixes in vec mat smat - mainly missing copyonwrite()s --- mat.cc | 25 ++++++++++++++++++------- mat.h | 1 + smat.cc | 9 +++++++-- vec.cc | 2 ++ vec.h | 4 +++- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/mat.cc b/mat.cc index 40c1762..ace1db3 100644 --- a/mat.cc +++ b/mat.cc @@ -127,6 +127,7 @@ void NRMat::rowset(const NRVec &r, const int i, int l, int offset) { if(i < 0 || i >= nn) laerror("illegal index"); if(offset<0||l+offset>mm) laerror("illegal len/offset"); #endif + copyonwrite(); LA_traits::copy( #ifdef MATPTR v[i]+offset @@ -893,6 +894,7 @@ void NRMat::storesubmatrix(const int fromrow, const int fromcol, const NRMat #endif SAME_LOC(*this, rhs); if(!LA_traits::is_plaindata()) laerror("only implemented for plain data"); + copyonwrite(); const int m = tocol - fromcol + 1; for(register int i = fromrow; i <= torow; ++i){ @@ -921,7 +923,8 @@ void NRMat::storesubmatrix(const NRVec &rows, const NRVec &cols, co const int n = rows.size(); const int m = cols.size(); if(rhs.nrows()!=n || rhs.ncols()!=m) laerror("incompatible dimensions in storesubmatrix"); - + copyonwrite(); + for(int i=0; i::storesubmatrix(const NRVec &rows, const NRVec &cols, co ******************************************************************************/ template NRMat& NRMat::transposeme(const int _n) { - const int n = (n <= 0)?nn:_n;//!< transpose the entire matrix + const int n = (_n <= 0)?nn:_n;//!< transpose the entire matrix #ifdef DEBUG if (n==nn && nn != mm || n>mm || n>nn ) laerror("NRMat::transposeme() - invalid parameter n. Non-square matrix?"); #endif @@ -1184,23 +1187,27 @@ void NRMat::fscanf(FILE *f, const char *format) { int n(0), m(0); if (::fscanf(f, "%d %d", &n, &m) != 2) laerror("cannot read matrix dimensions"); + resize(n, m); #ifdef CUDALA if(location == cpu){ - p = *this; +#endif + p = *this; //resize detached any shared data +#ifdef CUDALA }else{ - tmp = new NRMat(n, m, this->location); + tmp = new NRMat(n, m, cpu); //read on cpu, then copied to the device p = *tmp; } #endif - resize(n, m); for(int i=0; imoveto(this->location); + *this |= *tmp; delete tmp; } #endif @@ -1402,6 +1409,7 @@ void NRMat::randomize(const double &x) { #ifdef CUDALA if(location == cpu){ #endif + copyonwrite(); for(register int i=0; i >::randomize(const double &x) { #ifdef CUDALA if(location == cpu){ #endif + copyonwrite(); for(register int i=0; i void NRMat::orthonormalize(const bool rowcol, const NRSMat *metric) { - SAME_LOC(*this, *metric); + if(metric) SAME_LOC(*this, *metric); + copyonwrite(); if(metric){ if(rowcol){ //vectors are stored in rows if((*metric).nrows() != mm) laerror("incompatible metric in NRMat::orthonormalize(rowcol = true)"); @@ -3271,6 +3281,7 @@ template void NRMat::axpy(const T alpha, const NRPerm &p, const bool direction) { int n=p.size(); +copyonwrite(); for(int i=0; i &r, const int j, int l = -1) { NOT_GPU(*this); if(l < 0) l = nn; + copyonwrite(); for(register int i=0; i::trace() const { template<> void NRSMat::randomize(const double &x) { NOT_GPU(*this); + copyonwrite(); for(size_t i=0; i::randomize(const double &x) { ******************************************************************************/ template<> void NRSMat >::randomize(const double &x) { + copyonwrite(); for(register size_t i=0; i::fscanf(FILE *f, const char *format) { laerror("cannot read matrix dimensions in NRSMat::fscanf(FILE *, const char *)"); if (n != m) laerror("different dimensions in NRSMat::fscanf(FILE *, const char *)"); resize(n); - for (int i=0; i::fscanf(FILE *, const char *) - unable to read matrix element"); @@ -873,6 +876,7 @@ void NRSMat::storesubmatrix(const int from, const NRSMat &rhs) #endif NOT_GPU(*this); +copyonwrite(); const int n = rhs.nrows(); @@ -894,7 +898,8 @@ void NRSMat::storesubmatrix(const NRVec &selection, const NRSMat &rhs) { NOT_GPU(*this); const int n = selection.size(); - if(rhs.size()!=n) laerror("size mismatch in storesubmatrix"); + if(rhs.nrows()!=n) laerror("size mismatch in storesubmatrix"); + copyonwrite(); for(int i=0; i::operator<(const NRVec &rhs) const { template<> void NRVec::randomize(const double &x){ NOT_GPU(*this); + copyonwrite(); for(register int i=0; i::randomize(const double &x){ template<> void NRVec >::randomize(const double &x) { NOT_GPU(*this); + copyonwrite(); for(register int i=0; i(x*RANDDOUBLESIGNED(), x*RANDDOUBLESIGNED()); diff --git a/vec.h b/vec.h index 245a783..3992a7f 100644 --- a/vec.h +++ b/vec.h @@ -202,7 +202,7 @@ public: const NRVec subvector(const int from, const int to) const; const NRVec subvector(const NRVec &selection) const; - //! store given vector at given position into the current vector + //! store given vector at given position into the current vector (scatter) void storesubvector(const int from, const NRVec &rhs); void storesubvector(const NRVec &selection, const NRVec &rhs); @@ -2294,6 +2294,7 @@ void NRVec::storesubvector(const int from, const NRVec &rhs) #endif SAME_LOC(*this, rhs); if(!LA_traits::is_plaindata()) laerror("only implemented for plain data"); + copyonwrite(); #ifdef CUDALA if(location == cpu){ @@ -2315,6 +2316,7 @@ void NRVec::storesubvector(const NRVec &selection, const NRVec &rhs) NOT_GPU(*this); const int n = selection.size(); if(n!=rhs.size()) laerror("size mismatch in storesubvector"); + copyonwrite(); for(int i=0; i