Skip to content

Commit a3582f1

Browse files
authored
Move empty storage construction to inline helper method (#857)
* Move empty storage construction to inline helper method
1 parent 592c593 commit a3582f1

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

include/matx/core/make_sparse_tensor.h

+19-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
namespace matx {
3838
namespace experimental {
3939

40+
// Helper method to create empty storage.
41+
template <typename T>
42+
__MATX_INLINE__ static auto makeDefaultNonOwningStorage() {
43+
raw_pointer_buffer<T, matx_allocator<T>> buf{nullptr, 0, /*owning=*/false};
44+
return basic_storage<decltype(buf)>{std::move(buf)};
45+
}
46+
4047
//
4148
// MatX implements a universal sparse tensor type that uses a tensor format
4249
// DSL (Domain Specific Language) to describe a vast space of storage formats.
@@ -51,7 +58,7 @@ namespace experimental {
5158
// column. Duplicate entries should not occur. Explicit zeros may be stored.
5259
template <typename ValTensor, typename CrdTensor>
5360
auto make_tensor_coo(ValTensor &val, CrdTensor &row, CrdTensor &col,
54-
const index_t (&shape)[2], bool owning = false) {
61+
const index_t (&shape)[2]) {
5562
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1);
5663
using VAL = typename ValTensor::value_type;
5764
using CRD = typename CrdTensor::value_type;
@@ -66,50 +73,45 @@ auto make_tensor_coo(ValTensor &val, CrdTensor &row, CrdTensor &col,
6673
ptr[0] = 0;
6774
ptr[1] = val.Size(0);
6875
raw_pointer_buffer<POS, matx_allocator<POS>> topp{ptr, 2 * sizeof(POS),
69-
owning};
76+
/*owning=*/false};
7077
basic_storage<decltype(topp)> tp{std::move(topp)};
71-
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning};
72-
basic_storage<decltype(emptyp)> ep{std::move(emptyp)};
7378
return sparse_tensor_t<VAL, CRD, POS, COO>(
74-
shape, val.GetStorage(), {row.GetStorage(), col.GetStorage()}, {tp, ep});
79+
shape, val.GetStorage(), {row.GetStorage(), col.GetStorage()},
80+
{tp, makeDefaultNonOwningStorage<POS>()});
7581
}
7682

7783
// Constructs a sparse matrix in CSR format directly from the values, the row
7884
// positions, and column coordinates vectors. The entries should be sorted by
7985
// row, then column. Explicit zeros may be stored.
8086
template <typename ValTensor, typename PosTensor, typename CrdTensor>
8187
auto make_tensor_csr(ValTensor &val, PosTensor &rowp, CrdTensor &col,
82-
const index_t (&shape)[2], bool owning = false) {
88+
const index_t (&shape)[2]) {
8389
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1 &&
8490
PosTensor::Rank() == 1);
8591
using VAL = typename ValTensor::value_type;
8692
using CRD = typename CrdTensor::value_type;
8793
using POS = typename PosTensor::value_type;
88-
raw_pointer_buffer<CRD, matx_allocator<CRD>> emptyc{nullptr, 0, owning};
89-
basic_storage<decltype(emptyc)> ec{std::move(emptyc)};
90-
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning};
91-
basic_storage<decltype(emptyp)> ep{std::move(emptyp)};
9294
return sparse_tensor_t<VAL, CRD, POS, CSR>(
93-
shape, val.GetStorage(), {ec, col.GetStorage()}, {ep, rowp.GetStorage()});
95+
shape, val.GetStorage(),
96+
{makeDefaultNonOwningStorage<CRD>(), col.GetStorage()},
97+
{makeDefaultNonOwningStorage<POS>(), rowp.GetStorage()});
9498
}
9599

96100
// Constructs a sparse matrix in CSC format directly from the values,
97101
// the row coordinates, and column position vectors. The entries should
98102
// be sorted by column, then row. Explicit zeros may be stored.
99103
template <typename ValTensor, typename PosTensor, typename CrdTensor>
100104
auto make_tensor_csc(ValTensor &val, CrdTensor &row, PosTensor &colp,
101-
const index_t (&shape)[2], bool owning = false) {
105+
const index_t (&shape)[2]) {
102106
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1 &&
103107
PosTensor::Rank() == 1);
104108
using VAL = typename ValTensor::value_type;
105109
using CRD = typename CrdTensor::value_type;
106110
using POS = typename PosTensor::value_type;
107-
raw_pointer_buffer<CRD, matx_allocator<CRD>> emptyc{nullptr, 0, owning};
108-
basic_storage<decltype(emptyc)> ec{std::move(emptyc)};
109-
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning};
110-
basic_storage<decltype(emptyp)> ep{std::move(emptyp)};
111111
return sparse_tensor_t<VAL, CRD, POS, CSC>(
112-
shape, val.GetStorage(), {ec, row.GetStorage()}, {ep, colp.GetStorage()});
112+
shape, val.GetStorage(),
113+
{makeDefaultNonOwningStorage<CRD>(), row.GetStorage()},
114+
{makeDefaultNonOwningStorage<POS>(), colp.GetStorage()});
113115
}
114116

115117
} // namespace experimental

0 commit comments

Comments
 (0)