13 #ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_COO_H
14 #define MLIR_EXECUTIONENGINE_SPARSETENSOR_COO_H
23 namespace sparse_tensor {
51 for (uint64_t d = 0; d <
rank; ++d) {
71 uint64_t capacity = 0)
78 uint64_t capacity = 0)
79 : dimSizes(dimSizes, dimSizes + dimRank), isSorted(true) {
80 assert(dimRank > 0 &&
"Trivial shape is not supported");
81 for (uint64_t d = 0; d < dimRank; ++d)
82 assert(dimSizes[d] > 0 &&
"Dimension size zero has trivial storage");
84 elements.reserve(capacity);
85 coordinates.reserve(capacity * dimRank);
90 uint64_t
getRank()
const {
return dimSizes.size(); }
93 const std::vector<uint64_t> &
getDimSizes()
const {
return dimSizes; }
96 const std::vector<Element<V>> &
getElements()
const {
return elements; }
102 void add(
const std::vector<uint64_t> &dimCoords, V val) {
103 const uint64_t *base = coordinates.data();
104 const uint64_t size = coordinates.size();
105 const uint64_t dimRank =
getRank();
106 assert(dimCoords.size() == dimRank &&
"Element rank mismatch");
107 for (uint64_t d = 0; d < dimRank; ++d) {
108 assert(dimCoords[d] < dimSizes[d] &&
109 "Coordinate is too large for the dimension");
110 coordinates.push_back(dimCoords[d]);
117 const uint64_t *
const newBase = coordinates.data();
118 if (newBase != base) {
119 for (uint64_t i = 0, n = elements.size(); i < n; ++i)
120 elements[i].coords = newBase + (elements[i].coords - base);
125 if (!elements.empty() && isSorted)
127 elements.push_back(addedElem);
136 std::sort(elements.begin(), elements.end(),
getElementLT());
141 const std::vector<uint64_t> dimSizes;
142 std::vector<Element<V>> elements;
143 std::vector<uint64_t> coordinates;
A memory-resident sparse tensor in coordinate-scheme representation (a collection of Elements).
ElementLT< V > getElementLT() const
Returns the operator< closure object for the COO's element type.
SparseTensorCOO(const std::vector< uint64_t > &dimSizes, uint64_t capacity=0)
Constructs a new coordinate-scheme sparse tensor with the given sizes and an optional initial storage...
const std::vector< Element< V > > & getElements() const
Gets the elements array.
void sort()
Sorts elements lexicographically by coordinates.
uint64_t getRank() const
Gets the dimension-rank of the tensor.
void add(const std::vector< uint64_t > &dimCoords, V val)
Adds an element to the tensor.
const std::vector< uint64_t > & getDimSizes() const
Gets the dimension-sizes array.
SparseTensorCOO(uint64_t dimRank, const uint64_t *dimSizes, uint64_t capacity=0)
Constructs a new coordinate-scheme sparse tensor with the given sizes and an optional initial storage...
Include the generated interface declarations.
Closure object for operator< on Element with a given rank.
bool operator()(const Element< V > &e1, const Element< V > &e2) const
An element of a sparse tensor in coordinate-scheme representation (i.e., a pair of coordinates and va...
Element(const uint64_t *coords, V val)