Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove some sparse matrix constructors
Browse files Browse the repository at this point in the history
rileyjmurray committed Sep 3, 2024
1 parent 17fa752 commit 42f5abd
Showing 7 changed files with 74 additions and 102 deletions.
6 changes: 4 additions & 2 deletions RandBLAS/sparse_data/conversions.hh
Original file line number Diff line number Diff line change
@@ -128,7 +128,8 @@ CSRMatrix<T, sint_t> transpose_as_csr(CSCMatrix<T, sint_t> &A, bool share_memory
CSRMatrix<T, sint_t> At(A.n_cols, A.n_rows, A.nnz, A.vals, A.colptr, A.rowidxs, A.index_base);
return At;
} else {
CSRMatrix<T, sint_t> At(A.n_cols, A.n_rows, A.index_base);
CSRMatrix<T, sint_t> At(A.n_cols, A.n_rows);
At.index_base = A.index_base;
reserve(A.nnz, At);
for (int64_t i = 0; i < A.nnz; ++i) {
At.colidxs[i] = A.rowidxs[i];
@@ -146,7 +147,8 @@ CSCMatrix<T, sint_t> transpose_as_csc(CSRMatrix<T, sint_t> &A, bool share_memory
CSCMatrix<T, sint_t> At(A.n_cols, A.n_rows, A.nnz, A.vals, A.colidxs, A.rowptr, A.index_base);
return At;
} else {
CSCMatrix<T, sint_t> At(A.n_cols, A.n_rows, A.index_base);
CSCMatrix<T, sint_t> At(A.n_cols, A.n_rows);
At.index_base = A.index_base;
reserve(A.nnz, At);
for (int64_t i = 0; i < A.nnz; ++i) {
At.rowidxs[i] = A.colidxs[i];
75 changes: 35 additions & 40 deletions RandBLAS/sparse_data/coo_matrix.hh
Original file line number Diff line number Diff line change
@@ -126,42 +126,6 @@ struct COOMatrix {
/// CSC-like order, a CSR-like order, or neither order.
NonzeroSort sort = NonzeroSort::None;

COOMatrix(
int64_t n_rows,
int64_t n_cols,
int64_t nnz,
T *vals,
sint_t *rows,
sint_t *cols,
bool compute_sort_type,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rows(rows), cols(cols) {
if (compute_sort_type) {
sort = coo_sort_type(nnz, rows, cols);
} else {
sort = NonzeroSort::None;
}
};

COOMatrix(
int64_t n_rows,
int64_t n_cols,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(index_base),
vals(nullptr), rows(nullptr), cols(nullptr) {};

// Constructs an empty sparse matrix of given dimensions.
// Data can't stored in this object until a subsequent call to reserve(int64_t nnz).
// This constructor initializes \math{\ttt{own_memory(true)},} and so
// all data stored in this object is deleted once its destructor is invoked.
//
COOMatrix(
int64_t n_rows,
int64_t n_cols
) : COOMatrix(n_rows, n_cols, IndexBase::Zero) {};


// ---------------------------------------------------------------------------
/// @verbatim embed:rst:leading-slashes
/// Constructs a sparse matrix based on declared dimensions and the data in three buffers
@@ -203,6 +167,9 @@ struct COOMatrix {
/// assign M.sort = ``<the order you already know>`` once you
/// have a handle on M.
///
/// index_base - [in]
/// * IndexBase::Zero or IndexBase::One
///
/// @endverbatim
COOMatrix(
int64_t n_rows,
@@ -211,8 +178,37 @@ struct COOMatrix {
T *vals,
sint_t *rows,
sint_t *cols,
bool compute_sort_type = true
) : COOMatrix(n_rows, n_cols, nnz, vals, rows, cols, compute_sort_type, IndexBase::Zero) {};
bool compute_sort_type = true,
IndexBase index_base = IndexBase::Zero
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rows(rows), cols(cols) {
if (compute_sort_type) {
sort = coo_sort_type(nnz, rows, cols);
} else {
sort = NonzeroSort::None;
}
};

// Constructs an empty sparse matrix of given dimensions.
// Data can't stored in this object until a subsequent call to reserve(int64_t nnz).
// This constructor initializes \math{\ttt{own_memory(true)},} and so
// all data stored in this object is deleted once its destructor is invoked.
//
COOMatrix(
int64_t n_rows,
int64_t n_cols
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(IndexBase::Zero),
vals(nullptr), rows(nullptr), cols(nullptr) {};

// COOMatrix(
// int64_t n_rows,
// int64_t n_cols,
// int64_t nnz,
// T *vals,
// sint_t *rows,
// sint_t *cols,
// bool compute_sort_type = true
// ) : COOMatrix(n_rows, n_cols, nnz, vals, rows, cols, compute_sort_type, IndexBase::Zero) {};

~COOMatrix() {
if (own_memory) {
@@ -230,9 +226,8 @@ struct COOMatrix {

// move constructor
COOMatrix(COOMatrix<T, sint_t> &&other)
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(0), index_base(other.index_base),
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(other.nnz), index_base(other.index_base),
vals(nullptr), rows(nullptr), cols(nullptr) {
nnz = other.nnz;
std::swap(rows, other.rows);
std::swap(cols, other.cols);
std::swap(vals, other.vals);
51 changes: 19 additions & 32 deletions RandBLAS/sparse_data/csc_matrix.hh
Original file line number Diff line number Diff line change
@@ -64,34 +64,6 @@ struct CSCMatrix {
///
sint_t *colptr;

CSCMatrix(
int64_t n_rows,
int64_t n_cols,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(index_base),
vals(nullptr), rowidxs(nullptr), colptr(nullptr) { };

CSCMatrix(
int64_t n_rows,
int64_t n_cols,
int64_t nnz,
T *vals,
sint_t *rowidxs,
sint_t *colptr,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rowidxs(rowidxs), colptr(colptr) { };

// Constructs an empty sparse matrix of given dimensions.
// Data can't stored in this object until a subsequent call to reserve(int64_t nnz).
// This constructor initializes \math{\ttt{own_memory(true)},} and so
// all data stored in this object is deleted once its destructor is invoked.
//
CSCMatrix(
int64_t n_rows,
int64_t n_cols
) : CSCMatrix(n_rows, n_cols, IndexBase::Zero) { };

// ---------------------------------------------------------------------------
/// @verbatim embed:rst:leading-slashes
/// Constructs a sparse matrix based on declared dimensions and the data in three buffers
@@ -122,15 +94,31 @@ struct CSCMatrix {
/// colptr - [in]
/// * Pointer to array of sint_t, of length at least n_cols + 1.
///
/// index_base - [in]
/// * IndexBase::Zero or IndexBase::One
///
/// @endverbatim
CSCMatrix(
int64_t n_rows,
int64_t n_cols,
int64_t nnz,
T *vals,
sint_t *rowidxs,
sint_t *colptr
) : CSCMatrix(n_rows, n_cols, nnz, vals, rowidxs, colptr, IndexBase::Zero) {};
sint_t *colptr,
IndexBase index_base = IndexBase::Zero
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rowidxs(rowidxs), colptr(colptr) { };

// Constructs an empty sparse matrix of given dimensions.
// Data can't stored in this object until a subsequent call to reserve(int64_t nnz).
// This constructor initializes \math{\ttt{own_memory(true)},} and so
// all data stored in this object is deleted once its destructor is invoked.
//
CSCMatrix(
int64_t n_rows,
int64_t n_cols
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(IndexBase::Zero),
vals(nullptr), rowidxs(nullptr), colptr(nullptr) { };

~CSCMatrix() {
if (own_memory) {
@@ -141,9 +129,8 @@ struct CSCMatrix {
};

CSCMatrix(CSCMatrix<T, sint_t> &&other)
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(0), index_base(other.index_base),
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(other.nnz), index_base(other.index_base),
vals(nullptr), rowidxs(nullptr), colptr(nullptr) {
nnz = other.nnz;
std::swap(rowidxs, other.rowidxs);
std::swap(colptr , other.colptr );
std::swap(vals , other.vals );
35 changes: 11 additions & 24 deletions RandBLAS/sparse_data/csr_matrix.hh
Original file line number Diff line number Diff line change
@@ -66,24 +66,6 @@ struct CSRMatrix {
///
sint_t *colidxs;

CSRMatrix(
int64_t n_rows,
int64_t n_cols,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(index_base),
vals(nullptr), rowptr(nullptr), colidxs(nullptr) { };

CSRMatrix(
int64_t n_rows,
int64_t n_cols,
int64_t nnz,
T *vals,
sint_t *rowptr,
sint_t *colidxs,
IndexBase index_base
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rowptr(rowptr), colidxs(colidxs) { };

// Constructs an empty sparse matrix of given dimensions.
// Data can't stored in this object until a subsequent call to reserve(int64_t nnz).
// This constructor initializes \math{\ttt{own_memory(true)},} and so
@@ -92,7 +74,8 @@ struct CSRMatrix {
CSRMatrix(
int64_t n_rows,
int64_t n_cols
) : CSRMatrix(n_rows, n_cols, IndexBase::Zero) {};
) : n_rows(n_rows), n_cols(n_cols), own_memory(true), nnz(0), index_base(IndexBase::Zero),
vals(nullptr), rowptr(nullptr), colidxs(nullptr) { };

// ---------------------------------------------------------------------------
/// @verbatim embed:rst:leading-slashes
@@ -124,15 +107,20 @@ struct CSRMatrix {
/// colidxs - [in]
/// * Pointer to array of sint_t, of length at least nnz.
///
/// index_base - [in]
/// * IndexBase::Zero or IndexBase::One
///
/// @endverbatim
CSRMatrix(
int64_t n_rows,
int64_t n_cols,
int64_t nnz,
T *vals,
sint_t *rowptr,
sint_t *colidxs
) : CSRMatrix(n_rows, n_cols, nnz, vals, rowptr, colidxs, IndexBase::Zero) {};
sint_t *colidxs,
IndexBase index_base = IndexBase::Zero
) : n_rows(n_rows), n_cols(n_cols), own_memory(false), nnz(nnz), index_base(index_base),
vals(vals), rowptr(rowptr), colidxs(colidxs) { };

~CSRMatrix() {
if (own_memory) {
@@ -144,9 +132,8 @@ struct CSRMatrix {

// move constructor
CSRMatrix(CSRMatrix<T, sint_t> &&other)
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(0), index_base(other.index_base),
vals(nullptr), rowptr(nullptr), colidxs(nullptr) {
nnz = other.nnz;
: n_rows(other.n_rows), n_cols(other.n_cols), own_memory(other.own_memory), nnz(other.nnz), index_base(other.index_base),
vals(nullptr), rowptr(nullptr), colidxs(nullptr) {
std::swap(colidxs, other.colidxs);
std::swap(rowptr , other.rowptr );
std::swap(vals , other.vals );
3 changes: 2 additions & 1 deletion rtd/source/FAQ.rst
Original file line number Diff line number Diff line change
@@ -125,6 +125,7 @@ Things that affect our API:
In the cases of :ref:`SketchingDistribution <concept_rand_b_l_a_s_1_1_sketching_distribution>` and
:ref:`SketchingOperator <concept_rand_b_l_a_s_1_1_sketching_operator>` this is also a way
for us to declare a common interface for future functionality.
* Default values for trailing function arguments.

Things that are purely internal:
* C++17 ``if constexpr`` branching.
@@ -139,10 +140,10 @@ C++ idioms and features we don't use
* Shared pointers.
* Instance methods for structs (with the exceptions of constructors and destructors).


Naming conventions to resolve function overloading
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


We routinely use function overloading, and that reduces portability across languages.
See below for details on where we stand and where we plan to go to resolve this shortcoming.

2 changes: 1 addition & 1 deletion test/test_datastructures/test_spmats/test_csc.cc
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ class TestCSC_Conversions : public ::testing::Test {
iid_sparsify_random_dense(m, n, layout, dn_mat, p, s);

// Step 2. convert the dense representation into a CSC matrix
CSCMatrix<T> spmat(m, n, IndexBase::Zero);
CSCMatrix<T> spmat(m, n);
dense_to_csc(layout, dn_mat, 0.0, spmat);

// Step 3. reconstruct the dense representation of dn_mat from the CSC matrix.
4 changes: 2 additions & 2 deletions test/test_datastructures/test_spmats/test_csr.cc
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ class TestCSR_Conversions : public ::testing::Test

template <typename T = double>
static void test_csr_to_dense_diagonal(int64_t n) {
CSRMatrix<T> A(n, n, IndexBase::Zero);
CSRMatrix<T> A(n, n);
reserve(n, A);
for (int i = 0; i < n; ++i) {
A.vals[i] = 1.0 + (T) i;
@@ -80,7 +80,7 @@ class TestCSR_Conversions : public ::testing::Test
iid_sparsify_random_dense(m, n, layout, dn_mat, p, s);

// Step 2. convert the dense representation into a CSR matrix
CSRMatrix<T> spmat(m, n, IndexBase::Zero);
CSRMatrix<T> spmat(m, n);
dense_to_csr(layout, dn_mat, 0.0, spmat);

// Step 3. reconstruct the dense representation of dn_mat from the CSR matrix.

0 comments on commit 42f5abd

Please sign in to comment.