diff --git a/mlir/lib/ExecutionEngine/SparseUtils.cpp b/mlir/lib/ExecutionEngine/SparseUtils.cpp --- a/mlir/lib/ExecutionEngine/SparseUtils.cpp +++ b/mlir/lib/ExecutionEngine/SparseUtils.cpp @@ -185,7 +185,7 @@ } } // Then setup the tensor. - traverse(tensor, sparsity, 0, nnz, 0); + fromCOO(tensor, sparsity, 0, nnz, 0); } virtual ~SparseTensorStorage() {} @@ -203,6 +203,15 @@ } void getValues(std::vector **out) override { *out = &values; } + /// Returns this sparse tensor storage scheme as a new memory-resident + /// sparse tensor in coordinate scheme. + SparseTensor *asCOO() { + SparseTensor *tensor = new SparseTensor(sizes, values.size()); + std::vector idx; // per-rank dimension sizes + toCOO(tensor, idx, 0, 0); + return tensor; + } + /// Factory method. static SparseTensorStorage *newSparseTensor(SparseTensor *t, uint8_t *s) { @@ -216,8 +225,8 @@ /// Initializes sparse tensor storage scheme from a memory-resident sparse /// tensor in coordinate scheme. This method prepares the pointers and indices /// arrays under the given per-rank dimension dense/sparse annotations. - void traverse(SparseTensor *tensor, uint8_t *sparsity, uint64_t lo, - uint64_t hi, uint64_t d) { + void fromCOO(SparseTensor *tensor, uint8_t *sparsity, uint64_t lo, + uint64_t hi, uint64_t d) { const std::vector> &elements = tensor->getElements(); // Once dimensions are exhausted, insert the numerical values. if (d == getRank()) { @@ -240,10 +249,10 @@ indices[d].push_back(idx); } else { for (; full < idx; full++) - traverse(tensor, sparsity, 0, 0, d + 1); // pass empty + fromCOO(tensor, sparsity, 0, 0, d + 1); // pass empty full++; } - traverse(tensor, sparsity, lo, seg, d + 1); + fromCOO(tensor, sparsity, lo, seg, d + 1); // And move on to next segment in interval. lo = seg; } @@ -252,7 +261,30 @@ pointers[d].push_back(indices[d].size()); } else { for (uint64_t sz = tensor->getSizes()[d]; full < sz; full++) - traverse(tensor, sparsity, 0, 0, d + 1); // pass empty + fromCOO(tensor, sparsity, 0, 0, d + 1); // pass empty + } + } + + /// Stores the sparse tensor storage scheme into a memory-resident sparse + /// tensor in coordinate scheme. + void toCOO(SparseTensor *tensor, std::vector &idx, uint64_t pos, + uint64_t d) { + if (d == getRank()) { + tensor->add(idx, values[pos]); + } else if (pointers[d].empty()) { + // Dense dimension. + for (uint64_t i = 0; i < sizes[d]; i++) { + idx.push_back(i); + toCOO(tensor, idx, pos * sizes[d] + i, d + 1); + idx.pop_back(); + } + } else { + // Sparse dimension. + for (uint64_t ii = pointers[d][pos]; ii < pointers[d][pos + 1]; ii++) { + idx.push_back(indices[d][ii]); + toCOO(tensor, idx, ii, d + 1); + idx.pop_back(); + } } } @@ -437,8 +469,10 @@ tensor = openTensor(static_cast(ptr), asize, sizes, perm); \ else if (action == 1) \ tensor = static_cast *>(ptr); \ - else \ + else if (action == 2) \ return SparseTensor::newSparseTensor(asize, sizes, perm); \ + else \ + return static_cast *>(ptr)->asCOO(); \ return SparseTensorStorage::newSparseTensor(tensor, sparsity); \ } @@ -498,9 +532,10 @@ /// Constructs a new sparse tensor. This is the "swiss army knife" /// method for materializing sparse tensors into the computation. /// action -/// 0 : ptr contains filename to read into storage -/// 1 : ptr contains coordinate scheme to assign to storage -/// 2 : returns coordinate scheme to fill (call back later with 1) +/// 0 : ptr contains filename to read into storage +/// 1 : ptr contains coordinate scheme to assign to new storage +/// 2 : returns empty coordinate scheme to fill (call back 1 to setup) +/// 3 : returns coordinate scheme from storage in ptr (call back 1 to convert) void *newSparseTensor(uint8_t *abase, uint8_t *adata, uint64_t aoff, uint64_t asize, uint64_t astride, uint64_t *sbase, uint64_t *sdata, uint64_t soff, uint64_t ssize,