Skip to content

Commit

Permalink
implement new memory management policy
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyjmurray committed Aug 30, 2024
1 parent 16535cd commit 531f938
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
1 change: 1 addition & 0 deletions RandBLAS/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ concept SketchingOperator = requires(SKOP S) {
{ S.dist } -> SketchingDistribution;
{ S.seed_state } -> std::same_as<const typename SKOP::state_t&>;
{ S.next_state } -> std::same_as<const typename SKOP::state_t&>;
{ S.own_memory } -> std::same_as<bool&>;
};
#else
#define SketchingOperator typename
Expand Down
17 changes: 10 additions & 7 deletions RandBLAS/sparse_data/coo_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand All @@ -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;
}

/////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 13 additions & 10 deletions RandBLAS/sparse_data/csc_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand All @@ -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<T, sint_t> &&other)
Expand Down
25 changes: 14 additions & 11 deletions RandBLAS/sparse_data/csr_matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand All @@ -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;
};

/////////////////////////////////////////////////////////////////////
Expand Down
21 changes: 6 additions & 15 deletions RandBLAS/sparse_skops.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -399,7 +393,7 @@ struct SparseSkOp {
SparseSkOp(SparseSkOp<T,RNG,sint_t> &&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;
Expand All @@ -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;
}
}
};
Expand Down

0 comments on commit 531f938

Please sign in to comment.