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

This commit is contained in:
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;}
//! 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
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|=()
******************************************************************************/
template <typename T>
void NRMat<T>::copyonwrite(bool detachonly) {
void NRMat<T>::copyonwrite(bool detachonly, bool deep) {
if(!count)
{
if(nn||mm) laerror("nonempty matrix without reference count encountered");
@@ -1115,6 +1115,14 @@ void NRMat<T>::copyonwrite(bool detachonly) {
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){
(*count)--;
count = new int;