copyonwrite() on nested LA types will recursively call element copyonwrites even if the top type had count=1

This commit is contained in:
Jiri Pittner 2024-04-03 22:12:08 +02:00
parent 80f915946b
commit 8bbbaa5bae
5 changed files with 43 additions and 8 deletions

12
mat.h
View File

@ -126,7 +126,7 @@ public:
inline int getcount() const {return count?*count:0;} inline int getcount() const {return count?*count:0;}
//! ensure that the data of this matrix are referenced exactly once //! ensure that the data of this matrix are referenced exactly once
void copyonwrite(bool detachonly=false); void copyonwrite(bool detachonly=false, bool deep=true);
//! permute matrix elements //! permute matrix elements
const NRMat permuted_rows(const NRPerm<int> &p, const bool inverse=false) const; const NRMat permuted_rows(const NRPerm<int> &p, const bool inverse=false) const;
@ -1107,7 +1107,7 @@ NRMat<T> & NRMat<T>::operator|=(const NRMat<T> &rhs) {
* @see NRMat<T>::count, NRMat<T>::operator|=() * @see NRMat<T>::count, NRMat<T>::operator|=()
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRMat<T>::copyonwrite(bool detachonly) { void NRMat<T>::copyonwrite(bool detachonly, bool deep) {
if(!count) if(!count)
{ {
if(nn||mm) laerror("nonempty matrix without reference count encountered"); if(nn||mm) laerror("nonempty matrix without reference count encountered");
@ -1115,6 +1115,14 @@ void NRMat<T>::copyonwrite(bool detachonly) {
return; return;
} }
if(*count == 1 && !LA_traits<T>::is_plaindata() && !detachonly &&deep) //type-nested copyonwrite
{
#ifdef CUDALA
if(location != cpu) laerror("nested types not supported on gpu memory");
#endif
for(int i=0; i<nn*mm; ++i) LA_traits<T>::copyonwrite(v[i]);
}
if(*count > 1){ if(*count > 1){
(*count)--; (*count)--;
count = new int; count = new int;

12
smat.h
View File

@ -168,7 +168,7 @@ public:
void get(int fd, bool dimensions = 1, bool transp = 0); void get(int fd, bool dimensions = 1, bool transp = 0);
void put(int fd, bool dimensions = 1, bool transp = 0) const; void put(int fd, bool dimensions = 1, bool transp = 0) const;
void copyonwrite(bool detachonly=false); void copyonwrite(bool detachonly=false, bool deep=true);
void clear() {copyonwrite(LA_traits<T>::is_plaindata()); LA_traits<T>::clear(v,NN2);}; //zero out void clear() {copyonwrite(LA_traits<T>::is_plaindata()); LA_traits<T>::clear(v,NN2);}; //zero out
void resize(const int n); void resize(const int n);
@ -984,13 +984,21 @@ NRSMat<T> & NRSMat<T>::operator=(const NRSMat<T> & rhs) {
* @see NRSMat<T>::operator|=, NRSMat<T>::copyonwrite() * @see NRSMat<T>::operator|=, NRSMat<T>::copyonwrite()
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRSMat<T>::copyonwrite(bool detachonly) { void NRSMat<T>::copyonwrite(bool detachonly, bool deep) {
if(!count) if(!count)
{ {
if(nn) laerror("nonempty smat without reference count encountered"); if(nn) laerror("nonempty smat without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty smat\n"; if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty smat\n";
return; return;
} }
if(*count == 1 && !LA_traits<T>::is_plaindata() && !detachonly && deep) //type-nested copyonwrite
{
#ifdef CUDALA
if(location != cpu) laerror("nested types not supported on gpu memory");
#endif
for(int i=0; i<NN2; ++i) LA_traits<T>::copyonwrite(v[i]);
}
if(*count > 1){ if(*count > 1){
(*count)--; (*count)--;
count = new int; count = new int;

View File

@ -143,7 +143,7 @@ while(l)
} }
template <class T> template <class T>
void SparseMat<T>::copyonwrite(bool detachonly) void SparseMat<T>::copyonwrite(bool detachonly, bool deep)
{ {
if(!count) if(!count)
{ {
@ -152,6 +152,18 @@ void SparseMat<T>::copyonwrite(bool detachonly)
return; return;
} }
if(*count == 1 && !LA_traits<T>::is_plaindata() && !detachonly && deep) //type-nested copyonwrite
{
matel<T> *l =list;
while(l)
{
LA_traits<T>::copyonwrite(l->elem);
l=l->next;
}
}
if(*count > 1) if(*count > 1)
{ {
(*count)--; (*count)--;

View File

@ -139,7 +139,7 @@ public:
void setunsymmetric();//unwind the matrix assuming it was indeed symmetric void setunsymmetric();//unwind the matrix assuming it was indeed symmetric
inline bool issymmetric() const {return symmetric;} inline bool issymmetric() const {return symmetric;}
unsigned int length() const; unsigned int length() const;
void copyonwrite(bool detachonly=false); void copyonwrite(bool detachonly=false, bool deep=true);
void clear() {copyonwrite(LA_traits<T>::is_plaindata()); if(count) {delete count; count=NULL;}} void clear() {copyonwrite(LA_traits<T>::is_plaindata()); if(count) {delete count; count=NULL;}}
void simplify(const double sparseepsilon=SPARSEEPSILON); void simplify(const double sparseepsilon=SPARSEEPSILON);
const T trace() const; const T trace() const;

11
vec.h
View File

@ -177,7 +177,7 @@ public:
#endif #endif
//! create separate copy of the data corresponding to this vector //! create separate copy of the data corresponding to this vector
void copyonwrite(bool detachonly=false); void copyonwrite(bool detachonly=false, bool deep=true);
//! purge this vector //! purge this vector
void clear() { copyonwrite(LA_traits<T>::is_plaindata()); LA_traits<T>::clear(v, nn); }; void clear() { copyonwrite(LA_traits<T>::is_plaindata()); LA_traits<T>::clear(v, nn); };
@ -1127,13 +1127,20 @@ NRVec<T>::~NRVec() {
* make own copy of the underlying data connected with this vector * make own copy of the underlying data connected with this vector
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
void NRVec<T>::copyonwrite(bool detachonly) { void NRVec<T>::copyonwrite(bool detachonly,bool deep) {
if(!count) if(!count)
{ {
if(nn) laerror("nonempty vector without reference count encountered"); if(nn) laerror("nonempty vector without reference count encountered");
if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty vector\n"; if(_LA_warn_empty_copyonwrite) std::cout <<"Warning: copyonwrite of empty vector\n";
return; return;
} }
if(*count == 1 && !LA_traits<T>::is_plaindata() && !detachonly && deep) //type-nested copyonwrite
{
#ifdef CUDALA
if(location != cpu) laerror("nested types not supported on gpu memory");
#endif
for(int i=0; i<nn; ++i) LA_traits<T>::copyonwrite(v[i]);
}
if(*count > 1) { if(*count > 1) {
(*count)--; (*count)--;
count = new int; count = new int;