bugfixes in vec mat smat - mainly missing copyonwrite()s

This commit is contained in:
jiri committed 2026-10-26 15:11:45 +01:00
1 parent eccb3a68f8
commit f7ef134fc5
5 files changed
+31 -10

No files matched your search

+18 -7
View File
@@ -127,6 +127,7 @@ void NRMat<T>::rowset(const NRVec<T> &r, const int i, int l, int offset) {
if(i < 0 || i >= nn) laerror("illegal index"); if(i < 0 || i >= nn) laerror("illegal index");
if(offset<0||l+offset>mm) laerror("illegal len/offset"); if(offset<0||l+offset>mm) laerror("illegal len/offset");
#endif #endif
copyonwrite();
LA_traits<T>::copy( LA_traits<T>::copy(
#ifdef MATPTR #ifdef MATPTR
v[i]+offset v[i]+offset
@@ -893,6 +894,7 @@ void NRMat<T>::storesubmatrix(const int fromrow, const int fromcol, const NRMat
#endif #endif
SAME_LOC(*this, rhs); SAME_LOC(*this, rhs);
if(!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data"); if(!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data");
copyonwrite();
const int m = tocol - fromcol + 1; const int m = tocol - fromcol + 1;
for(register int i = fromrow; i <= torow; ++i){ for(register int i = fromrow; i <= torow; ++i){
@@ -921,7 +923,8 @@ void NRMat<T>::storesubmatrix(const NRVec<int> &rows, const NRVec<int> &cols, co
const int n = rows.size(); const int n = rows.size();
const int m = cols.size(); const int m = cols.size();
if(rhs.nrows()!=n || rhs.ncols()!=m) laerror("incompatible dimensions in storesubmatrix"); if(rhs.nrows()!=n || rhs.ncols()!=m) laerror("incompatible dimensions in storesubmatrix");
copyonwrite();
for(int i=0; i<n; ++i) for(int i=0; i<n; ++i)
{ {
int ii=rows[i]; int ii=rows[i];
@@ -942,7 +945,7 @@ void NRMat<T>::storesubmatrix(const NRVec<int> &rows, const NRVec<int> &cols, co
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
NRMat<T>& NRMat<T>::transposeme(const int _n) { NRMat<T>& NRMat<T>::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 #ifdef DEBUG
if (n==nn && nn != mm || n>mm || n>nn ) laerror("NRMat<T>::transposeme() - invalid parameter n. Non-square matrix?"); if (n==nn && nn != mm || n>mm || n>nn ) laerror("NRMat<T>::transposeme() - invalid parameter n. Non-square matrix?");
#endif #endif
@@ -1184,23 +1187,27 @@ void NRMat<T>::fscanf(FILE *f, const char *format) {
int n(0), m(0); int n(0), m(0);
if (::fscanf(f, "%d %d", &n, &m) != 2) laerror("cannot read matrix dimensions"); if (::fscanf(f, "%d %d", &n, &m) != 2) laerror("cannot read matrix dimensions");
resize(n, m);
#ifdef CUDALA #ifdef CUDALA
if(location == cpu){ if(location == cpu){
p = *this; #endif
p = *this; //resize detached any shared data
#ifdef CUDALA
}else{ }else{
tmp = new NRMat<T>(n, m, this->location); tmp = new NRMat<T>(n, m, cpu); //read on cpu, then copied to the device
p = *tmp; p = *tmp;
} }
#endif #endif
resize(n, m);
for(int i=0; i<n; i++){ for(int i=0; i<n; i++){
for(int j=0; j<n; j++){ for(int j=0; j<m; j++){
if(::fscanf(f,format, p++) != 1) laerror("cannot read matrix element"); if(::fscanf(f,format, p++) != 1) laerror("cannot read matrix element");
} }
} }
#ifdef CUDALA #ifdef CUDALA
if(location != cpu){ if(location != cpu){
tmp->moveto(this->location);
*this |= *tmp;
delete tmp; delete tmp;
} }
#endif #endif
@@ -1402,6 +1409,7 @@ void NRMat<double>::randomize(const double &x) {
#ifdef CUDALA #ifdef CUDALA
if(location == cpu){ if(location == cpu){
#endif #endif
copyonwrite();
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
for(register int j=0; j<mm; ++j){ for(register int j=0; j<mm; ++j){
(*this)(i,j) = x*RANDDOUBLESIGNED(); (*this)(i,j) = x*RANDDOUBLESIGNED();
@@ -1431,6 +1439,7 @@ void NRMat<std::complex<double> >::randomize(const double &x) {
#ifdef CUDALA #ifdef CUDALA
if(location == cpu){ if(location == cpu){
#endif #endif
copyonwrite();
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
for(register int j=0; j<mm; ++j){ for(register int j=0; j<mm; ++j){
const double re = x*RANDDOUBLESIGNED(); const double re = x*RANDDOUBLESIGNED();
@@ -2641,7 +2650,8 @@ int nnmin= nn<=mm?nn:mm;
template<> template<>
void NRMat<double>::orthonormalize(const bool rowcol, const NRSMat<double> *metric) { void NRMat<double>::orthonormalize(const bool rowcol, const NRSMat<double> *metric) {
SAME_LOC(*this, *metric); if(metric) SAME_LOC(*this, *metric);
copyonwrite();
if(metric){ if(metric){
if(rowcol){ //vectors are stored in rows if(rowcol){ //vectors are stored in rows
if((*metric).nrows() != mm) laerror("incompatible metric in NRMat<double>::orthonormalize(rowcol = true)"); if((*metric).nrows() != mm) laerror("incompatible metric in NRMat<double>::orthonormalize(rowcol = true)");
@@ -3271,6 +3281,7 @@ template<typename T>
void NRMat<T>::axpy(const T alpha, const NRPerm<int> &p, const bool direction) void NRMat<T>::axpy(const T alpha, const NRPerm<int> &p, const bool direction)
{ {
int n=p.size(); int n=p.size();
copyonwrite();
for(int i=0; i<n; ++i) for(int i=0; i<n; ++i)
{ {
if(direction) (*this)(i,p[i+1]-1) +=alpha; if(direction) (*this)(i,p[i+1]-1) +=alpha;
+1
View File
@@ -291,6 +291,7 @@ public:
void columnset(const NRVec<T> &r, const int j, int l = -1) { void columnset(const NRVec<T> &r, const int j, int l = -1) {
NOT_GPU(*this); NOT_GPU(*this);
if(l < 0) l = nn; if(l < 0) l = nn;
copyonwrite();
for(register int i=0; i<l; ++i) (*this)(i,j) = r[i]; for(register int i=0; i<l; ++i) (*this)(i,j) = r[i];
}; };
+7 -2
View File
@@ -258,6 +258,7 @@ const T NRSMat<T>::trace() const {
template<> template<>
void NRSMat<double>::randomize(const double &x) { void NRSMat<double>::randomize(const double &x) {
NOT_GPU(*this); NOT_GPU(*this);
copyonwrite();
for(size_t i=0; i<NN2; ++i){ for(size_t i=0; i<NN2; ++i){
v[i] = x*RANDDOUBLESIGNED(); v[i] = x*RANDDOUBLESIGNED();
@@ -270,6 +271,7 @@ void NRSMat<double>::randomize(const double &x) {
******************************************************************************/ ******************************************************************************/
template<> template<>
void NRSMat<std::complex<double> >::randomize(const double &x) { void NRSMat<std::complex<double> >::randomize(const double &x) {
copyonwrite();
for(register size_t i=0; i<NN2; ++i) v[i].real(x*RANDDOUBLESIGNED()); for(register size_t i=0; i<NN2; ++i) v[i].real(x*RANDDOUBLESIGNED());
for(register size_t i=0; i<NN2; ++i) v[i].imag(x*RANDDOUBLESIGNED()); for(register size_t i=0; i<NN2; ++i) v[i].imag(x*RANDDOUBLESIGNED());
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
@@ -308,7 +310,8 @@ void NRSMat<T>::fscanf(FILE *f, const char *format) {
laerror("cannot read matrix dimensions in NRSMat<T>::fscanf(FILE *, const char *)"); laerror("cannot read matrix dimensions in NRSMat<T>::fscanf(FILE *, const char *)");
if (n != m) laerror("different dimensions in NRSMat<T>::fscanf(FILE *, const char *)"); if (n != m) laerror("different dimensions in NRSMat<T>::fscanf(FILE *, const char *)");
resize(n); resize(n);
for (int i=0; i<n; i++) //the whole square matrix is read on purpose, the data are assumed to be symmetric (elements (i,j) and (j,i) share the storage)
for (int i=0; i<n; i++)
for (int j=0; j<n; j++) for (int j=0; j<n; j++)
if (::fscanf(f,format,&((*this)(i,j))) != 1) if (::fscanf(f,format,&((*this)(i,j))) != 1)
laerror("NRSMat<T>::fscanf(FILE *, const char *) - unable to read matrix element"); laerror("NRSMat<T>::fscanf(FILE *, const char *) - unable to read matrix element");
@@ -873,6 +876,7 @@ void NRSMat<T>::storesubmatrix(const int from, const NRSMat &rhs)
#endif #endif
NOT_GPU(*this); NOT_GPU(*this);
copyonwrite();
const int n = rhs.nrows(); const int n = rhs.nrows();
@@ -894,7 +898,8 @@ void NRSMat<T>::storesubmatrix(const NRVec<int> &selection, const NRSMat &rhs)
{ {
NOT_GPU(*this); NOT_GPU(*this);
const int n = selection.size(); 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<n; ++i) for(int i=0; i<n; ++i)
{ {
+2
View File
@@ -192,6 +192,7 @@ const bool NRVec<T>::operator<(const NRVec &rhs) const {
template<> template<>
void NRVec<double>::randomize(const double &x){ void NRVec<double>::randomize(const double &x){
NOT_GPU(*this); NOT_GPU(*this);
copyonwrite();
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
v[i] = x*RANDDOUBLESIGNED(); v[i] = x*RANDDOUBLESIGNED();
@@ -209,6 +210,7 @@ void NRVec<double>::randomize(const double &x){
template<> template<>
void NRVec<std::complex<double> >::randomize(const double &x) { void NRVec<std::complex<double> >::randomize(const double &x) {
NOT_GPU(*this); NOT_GPU(*this);
copyonwrite();
for(register int i=0; i<nn; ++i){ for(register int i=0; i<nn; ++i){
v[i] = std::complex<double>(x*RANDDOUBLESIGNED(), x*RANDDOUBLESIGNED()); v[i] = std::complex<double>(x*RANDDOUBLESIGNED(), x*RANDDOUBLESIGNED());
+3 -1
View File
@@ -202,7 +202,7 @@ public:
const NRVec subvector(const int from, const int to) const; const NRVec subvector(const int from, const int to) const;
const NRVec subvector(const NRVec<int> &selection) const; const NRVec subvector(const NRVec<int> &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 int from, const NRVec &rhs);
void storesubvector(const NRVec<int> &selection, const NRVec &rhs); void storesubvector(const NRVec<int> &selection, const NRVec &rhs);
@@ -2294,6 +2294,7 @@ void NRVec<T>::storesubvector(const int from, const NRVec &rhs)
#endif #endif
SAME_LOC(*this, rhs); SAME_LOC(*this, rhs);
if(!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data"); if(!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data");
copyonwrite();
#ifdef CUDALA #ifdef CUDALA
if(location == cpu){ if(location == cpu){
@@ -2315,6 +2316,7 @@ void NRVec<T>::storesubvector(const NRVec<int> &selection, const NRVec &rhs)
NOT_GPU(*this); NOT_GPU(*this);
const int n = selection.size(); const int n = selection.size();
if(n!=rhs.size()) laerror("size mismatch in storesubvector"); if(n!=rhs.size()) laerror("size mismatch in storesubvector");
copyonwrite();
for(int i=0; i<n; ++i) for(int i=0; i<n; ++i)
{ {