diff --git a/RandBLAS/base.hh b/RandBLAS/base.hh index 9eb2fdac..e6529898 100644 --- a/RandBLAS/base.hh +++ b/RandBLAS/base.hh @@ -376,6 +376,7 @@ concept SketchingOperator = requires(SKOP S) { { S.dist } -> SketchingDistribution; { S.seed_state } -> std::same_as; { S.next_state } -> std::same_as; + { S.own_memory } -> std::same_as; }; #else #define SketchingOperator typename diff --git a/RandBLAS/sparse_data/coo_matrix.hh b/RandBLAS/sparse_data/coo_matrix.hh index c02445a8..0a29e610 100644 --- a/RandBLAS/sparse_data/coo_matrix.hh +++ b/RandBLAS/sparse_data/coo_matrix.hh @@ -220,9 +220,9 @@ struct COOMatrix { ~COOMatrix() { if (this->own_memory) { - delete [] this->vals; - delete [] this->rows; - delete [] this->cols; + if (vals != nullptr) delete [] vals; + if (rows != nullptr) delete [] rows; + if (cols != nullptr) delete [] cols; } }; @@ -235,13 +235,16 @@ struct COOMatrix { void reserve(int64_t nnz) { randblas_require(this->_can_reserve); randblas_require(this->own_memory); + randblas_require(vals == nullptr); + randblas_require(rows == nullptr); + randblas_require(cols == nullptr); this->nnz = nnz; if (this->nnz > 0) { - this->vals = new T[nnz]; - this->rows = new sint_t[nnz]; - this->cols = new sint_t[nnz]; + vals = new T[nnz]; + rows = new sint_t[nnz]; + cols = new sint_t[nnz]; } - this->_can_reserve = false; + _can_reserve = false; } ///////////////////////////////////////////////////////////////////// diff --git a/RandBLAS/sparse_data/csc_matrix.hh b/RandBLAS/sparse_data/csc_matrix.hh index d9c3119c..15933dbd 100644 --- a/RandBLAS/sparse_data/csc_matrix.hh +++ b/RandBLAS/sparse_data/csc_matrix.hh @@ -136,9 +136,9 @@ struct CSCMatrix { ~CSCMatrix() { if (this->own_memory) { - delete [] this->rowidxs; - delete [] this->colptr; - delete [] this->vals; + if (rowidxs != nullptr) delete [] rowidxs; + if (colptr != nullptr) delete [] colptr; + if (vals != nullptr) delete [] vals; } }; @@ -151,15 +151,18 @@ struct CSCMatrix { // // @endverbatim void reserve(int64_t nnz) { - randblas_require(this->_can_reserve); - randblas_require(this->own_memory); - this->colptr = new sint_t[this->n_cols + 1]{0}; + randblas_require(_can_reserve); + randblas_require(own_memory); + if (colptr == nullptr) + colptr = new sint_t[n_cols + 1]{0}; this->nnz = nnz; - if (this->nnz > 0) { - this->rowidxs = new sint_t[nnz]{0}; - this->vals = new T[nnz]{0.0}; + if (nnz > 0) { + randblas_require( rowidxs == nullptr ); + randblas_require( vals == nullptr ); + rowidxs = new sint_t[nnz]{0}; + vals = new T[nnz]{0.0}; } - this->_can_reserve = false; + _can_reserve = false; }; CSCMatrix(CSCMatrix &&other) diff --git a/RandBLAS/sparse_data/csr_matrix.hh b/RandBLAS/sparse_data/csr_matrix.hh index e4f54339..2c3e328c 100644 --- a/RandBLAS/sparse_data/csr_matrix.hh +++ b/RandBLAS/sparse_data/csr_matrix.hh @@ -139,10 +139,10 @@ struct CSRMatrix { ) : CSRMatrix(n_rows, n_cols, nnz, vals, rowptr, colidxs, IndexBase::Zero) {}; ~CSRMatrix() { - if (this->own_memory) { - delete [] this->rowptr; - delete [] this->colidxs; - delete [] this->vals; + if (own_memory) { + if (rowptr != nullptr) delete [] rowptr; + if (colidxs != nullptr) delete [] colidxs; + if (vals != nullptr) delete [] vals; } }; @@ -154,15 +154,18 @@ struct CSRMatrix { // // @endverbatim void reserve(int64_t nnz) { - randblas_require(this->_can_reserve); - randblas_require(this->own_memory); - this->rowptr = new sint_t[this->n_rows + 1]{0}; + randblas_require(_can_reserve); + randblas_require(own_memory); + if (rowptr == nullptr) + rowptr = new sint_t[n_rows + 1]{0}; this->nnz = nnz; - if (this->nnz > 0) { - this->colidxs = new sint_t[nnz]{0}; - this->vals = new T[nnz]{0.0}; + if (nnz > 0) { + randblas_require(colidxs == nullptr); + randblas_require(vals == nullptr); + colidxs = new sint_t[nnz]{0}; + vals = new T[nnz]{0.0}; } - this->_can_reserve = false; + _can_reserve = false; }; ///////////////////////////////////////////////////////////////////// diff --git a/RandBLAS/sparse_skops.hh b/RandBLAS/sparse_skops.hh index 29bd538d..87ce97e7 100644 --- a/RandBLAS/sparse_skops.hh +++ b/RandBLAS/sparse_skops.hh @@ -360,13 +360,7 @@ struct SparseSkOp { seed_state(seed_state), next_state(compute_next_state(dist, seed_state)), n_rows(dist.n_rows), - n_cols(dist.n_cols) - { // actual work - // int64_t nnz = dist.full_nnz; - // this->rows = new sint_t[nnz]{-1}; - // this->cols = new sint_t[nnz]{-1}; - // this->vals = new T[nnz]{0.0}; - } + n_cols(dist.n_cols) { } /// -------------------------------------------------------------------------------- /// **View constructor**. The arguments provided to this @@ -399,7 +393,7 @@ struct SparseSkOp { SparseSkOp(SparseSkOp &&S ) : dist(S.dist), seed_state(S.seed_state), next_state(S.next_state), n_rows(dist.n_rows), n_cols(dist.n_cols), own_memory(S.own_memory), - rows(S.rows), cols(S.cols), vals(S.vals) + nnz(S.nnz), rows(S.rows), cols(S.cols), vals(S.vals) { S.rows = nullptr; S.cols = nullptr; @@ -408,13 +402,10 @@ struct SparseSkOp { // Destructor ~SparseSkOp() { - if (this->own_memory) { - if (this->rows != nullptr) - delete [] this->rows; - if (this->cols != nullptr) - delete [] this->cols; - if (this->vals != nullptr) - delete [] this->vals; + if (own_memory) { + if (rows != nullptr) delete [] rows; + if (cols != nullptr) delete [] cols; + if (vals != nullptr) delete [] vals; } } };