1 //===- SparseTensorUtils.cpp - Sparse Tensor Utils for MLIR execution -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a light-weight runtime support library that is useful
10 // for sparse tensor manipulations. The functionality provided in this library
11 // is meant to simplify benchmarking, testing, and debugging MLIR code that
12 // operates on sparse tensors. The provided functionality is **not** part
13 // of core MLIR, however.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "mlir/ExecutionEngine/SparseTensorUtils.h"
18 #include "mlir/ExecutionEngine/CRunnerUtils.h"
19 
20 #ifdef MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
21 
22 #include <algorithm>
23 #include <cassert>
24 #include <cctype>
25 #include <cinttypes>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <fstream>
30 #include <iostream>
31 #include <limits>
32 #include <numeric>
33 #include <vector>
34 
35 //===----------------------------------------------------------------------===//
36 //
37 // Internal support for storing and reading sparse tensors.
38 //
39 // The following memory-resident sparse storage schemes are supported:
40 //
41 // (a) A coordinate scheme for temporarily storing and lexicographically
42 //     sorting a sparse tensor by index (SparseTensorCOO).
43 //
44 // (b) A "one-size-fits-all" sparse tensor storage scheme defined by
45 //     per-dimension sparse/dense annnotations together with a dimension
46 //     ordering used by MLIR compiler-generated code (SparseTensorStorage).
47 //
48 // The following external formats are supported:
49 //
50 // (1) Matrix Market Exchange (MME): *.mtx
51 //     https://math.nist.gov/MatrixMarket/formats.html
52 //
53 // (2) Formidable Repository of Open Sparse Tensors and Tools (FROSTT): *.tns
54 //     http://frostt.io/tensors/file-formats.html
55 //
56 // Two public APIs are supported:
57 //
58 // (I) Methods operating on MLIR buffers (memrefs) to interact with sparse
59 //     tensors. These methods should be used exclusively by MLIR
60 //     compiler-generated code.
61 //
62 // (II) Methods that accept C-style data structures to interact with sparse
63 //      tensors. These methods can be used by any external runtime that wants
64 //      to interact with MLIR compiler-generated code.
65 //
66 // In both cases (I) and (II), the SparseTensorStorage format is externally
67 // only visible as an opaque pointer.
68 //
69 //===----------------------------------------------------------------------===//
70 
71 namespace {
72 
73 static constexpr int kColWidth = 1025;
74 
75 /// A sparse tensor element in coordinate scheme (value and indices).
76 /// For example, a rank-1 vector element would look like
77 ///   ({i}, a[i])
78 /// and a rank-5 tensor element like
79 ///   ({i,j,k,l,m}, a[i,j,k,l,m])
80 template <typename V>
81 struct Element {
82   Element(const std::vector<uint64_t> &ind, V val) : indices(ind), value(val){};
83   std::vector<uint64_t> indices;
84   V value;
85 };
86 
87 /// A memory-resident sparse tensor in coordinate scheme (collection of
88 /// elements). This data structure is used to read a sparse tensor from
89 /// any external format into memory and sort the elements lexicographically
90 /// by indices before passing it back to the client (most packed storage
91 /// formats require the elements to appear in lexicographic index order).
92 template <typename V>
93 struct SparseTensorCOO {
94 public:
95   SparseTensorCOO(const std::vector<uint64_t> &szs, uint64_t capacity)
96       : sizes(szs), iteratorLocked(false), iteratorPos(0) {
97     if (capacity)
98       elements.reserve(capacity);
99   }
100   /// Adds element as indices and value.
101   void add(const std::vector<uint64_t> &ind, V val) {
102     assert(!iteratorLocked && "Attempt to add() after startIterator()");
103     uint64_t rank = getRank();
104     assert(rank == ind.size());
105     for (uint64_t r = 0; r < rank; r++)
106       assert(ind[r] < sizes[r]); // within bounds
107     elements.emplace_back(ind, val);
108   }
109   /// Sorts elements lexicographically by index.
110   void sort() {
111     assert(!iteratorLocked && "Attempt to sort() after startIterator()");
112     // TODO: we may want to cache an `isSorted` bit, to avoid
113     // unnecessary/redundant sorting.
114     std::sort(elements.begin(), elements.end(), lexOrder);
115   }
116   /// Returns rank.
117   uint64_t getRank() const { return sizes.size(); }
118   /// Getter for sizes array.
119   const std::vector<uint64_t> &getSizes() const { return sizes; }
120   /// Getter for elements array.
121   const std::vector<Element<V>> &getElements() const { return elements; }
122 
123   /// Switch into iterator mode.
124   void startIterator() {
125     iteratorLocked = true;
126     iteratorPos = 0;
127   }
128   /// Get the next element.
129   const Element<V> *getNext() {
130     assert(iteratorLocked && "Attempt to getNext() before startIterator()");
131     if (iteratorPos < elements.size())
132       return &(elements[iteratorPos++]);
133     iteratorLocked = false;
134     return nullptr;
135   }
136 
137   /// Factory method. Permutes the original dimensions according to
138   /// the given ordering and expects subsequent add() calls to honor
139   /// that same ordering for the given indices. The result is a
140   /// fully permuted coordinate scheme.
141   static SparseTensorCOO<V> *newSparseTensorCOO(uint64_t rank,
142                                                 const uint64_t *sizes,
143                                                 const uint64_t *perm,
144                                                 uint64_t capacity = 0) {
145     std::vector<uint64_t> permsz(rank);
146     for (uint64_t r = 0; r < rank; r++)
147       permsz[perm[r]] = sizes[r];
148     return new SparseTensorCOO<V>(permsz, capacity);
149   }
150 
151 private:
152   /// Returns true if indices of e1 < indices of e2.
153   static bool lexOrder(const Element<V> &e1, const Element<V> &e2) {
154     uint64_t rank = e1.indices.size();
155     assert(rank == e2.indices.size());
156     for (uint64_t r = 0; r < rank; r++) {
157       if (e1.indices[r] == e2.indices[r])
158         continue;
159       return e1.indices[r] < e2.indices[r];
160     }
161     return false;
162   }
163   const std::vector<uint64_t> sizes; // per-dimension sizes
164   std::vector<Element<V>> elements;
165   bool iteratorLocked;
166   unsigned iteratorPos;
167 };
168 
169 /// Abstract base class of sparse tensor storage. Note that we use
170 /// function overloading to implement "partial" method specialization.
171 class SparseTensorStorageBase {
172 public:
173   /// Dimension size query.
174   virtual uint64_t getDimSize(uint64_t) = 0;
175 
176   /// Overhead storage.
177   virtual void getPointers(std::vector<uint64_t> **, uint64_t) { fatal("p64"); }
178   virtual void getPointers(std::vector<uint32_t> **, uint64_t) { fatal("p32"); }
179   virtual void getPointers(std::vector<uint16_t> **, uint64_t) { fatal("p16"); }
180   virtual void getPointers(std::vector<uint8_t> **, uint64_t) { fatal("p8"); }
181   virtual void getIndices(std::vector<uint64_t> **, uint64_t) { fatal("i64"); }
182   virtual void getIndices(std::vector<uint32_t> **, uint64_t) { fatal("i32"); }
183   virtual void getIndices(std::vector<uint16_t> **, uint64_t) { fatal("i16"); }
184   virtual void getIndices(std::vector<uint8_t> **, uint64_t) { fatal("i8"); }
185 
186   /// Primary storage.
187   virtual void getValues(std::vector<double> **) { fatal("valf64"); }
188   virtual void getValues(std::vector<float> **) { fatal("valf32"); }
189   virtual void getValues(std::vector<int64_t> **) { fatal("vali64"); }
190   virtual void getValues(std::vector<int32_t> **) { fatal("vali32"); }
191   virtual void getValues(std::vector<int16_t> **) { fatal("vali16"); }
192   virtual void getValues(std::vector<int8_t> **) { fatal("vali8"); }
193 
194   /// Element-wise insertion in lexicographic index order.
195   virtual void lexInsert(const uint64_t *, double) { fatal("insf64"); }
196   virtual void lexInsert(const uint64_t *, float) { fatal("insf32"); }
197   virtual void lexInsert(const uint64_t *, int64_t) { fatal("insi64"); }
198   virtual void lexInsert(const uint64_t *, int32_t) { fatal("insi32"); }
199   virtual void lexInsert(const uint64_t *, int16_t) { fatal("ins16"); }
200   virtual void lexInsert(const uint64_t *, int8_t) { fatal("insi8"); }
201 
202   /// Expanded insertion.
203   virtual void expInsert(uint64_t *, double *, bool *, uint64_t *, uint64_t) {
204     fatal("expf64");
205   }
206   virtual void expInsert(uint64_t *, float *, bool *, uint64_t *, uint64_t) {
207     fatal("expf32");
208   }
209   virtual void expInsert(uint64_t *, int64_t *, bool *, uint64_t *, uint64_t) {
210     fatal("expi64");
211   }
212   virtual void expInsert(uint64_t *, int32_t *, bool *, uint64_t *, uint64_t) {
213     fatal("expi32");
214   }
215   virtual void expInsert(uint64_t *, int16_t *, bool *, uint64_t *, uint64_t) {
216     fatal("expi16");
217   }
218   virtual void expInsert(uint64_t *, int8_t *, bool *, uint64_t *, uint64_t) {
219     fatal("expi8");
220   }
221 
222   /// Finishes insertion.
223   virtual void endInsert() = 0;
224 
225   virtual ~SparseTensorStorageBase() = default;
226 
227 private:
228   void fatal(const char *tp) {
229     fprintf(stderr, "unsupported %s\n", tp);
230     exit(1);
231   }
232 };
233 
234 /// A memory-resident sparse tensor using a storage scheme based on
235 /// per-dimension sparse/dense annotations. This data structure provides a
236 /// bufferized form of a sparse tensor type. In contrast to generating setup
237 /// methods for each differently annotated sparse tensor, this method provides
238 /// a convenient "one-size-fits-all" solution that simply takes an input tensor
239 /// and annotations to implement all required setup in a general manner.
240 template <typename P, typename I, typename V>
241 class SparseTensorStorage : public SparseTensorStorageBase {
242 public:
243   /// Constructs a sparse tensor storage scheme with the given dimensions,
244   /// permutation, and per-dimension dense/sparse annotations, using
245   /// the coordinate scheme tensor for the initial contents if provided.
246   SparseTensorStorage(const std::vector<uint64_t> &szs, const uint64_t *perm,
247                       const DimLevelType *sparsity,
248                       SparseTensorCOO<V> *tensor = nullptr)
249       : sizes(szs), rev(getRank()), idx(getRank()), pointers(getRank()),
250         indices(getRank()) {
251     uint64_t rank = getRank();
252     // Store "reverse" permutation.
253     for (uint64_t r = 0; r < rank; r++)
254       rev[perm[r]] = r;
255     // Provide hints on capacity of pointers and indices.
256     // TODO: needs fine-tuning based on sparsity
257     bool allDense = true;
258     uint64_t sz = 1;
259     for (uint64_t r = 0; r < rank; r++) {
260       assert(sizes[r] > 0 && "Dimension size zero has trivial storage");
261       sz *= sizes[r];
262       if (sparsity[r] == DimLevelType::kCompressed) {
263         pointers[r].reserve(sz + 1);
264         indices[r].reserve(sz);
265         sz = 1;
266         allDense = false;
267         // Prepare the pointer structure.  We cannot use `appendPointer`
268         // here, because `isCompressedDim` won't work until after this
269         // preparation has been done.
270         pointers[r].push_back(0);
271       } else {
272         assert(sparsity[r] == DimLevelType::kDense &&
273                "singleton not yet supported");
274       }
275     }
276     // Then assign contents from coordinate scheme tensor if provided.
277     if (tensor) {
278       // Ensure both preconditions of `fromCOO`.
279       assert(tensor->getSizes() == sizes && "Tensor size mismatch");
280       tensor->sort();
281       // Now actually insert the `elements`.
282       const std::vector<Element<V>> &elements = tensor->getElements();
283       uint64_t nnz = elements.size();
284       values.reserve(nnz);
285       fromCOO(elements, 0, nnz, 0);
286     } else if (allDense) {
287       values.resize(sz, 0);
288     }
289   }
290 
291   ~SparseTensorStorage() override = default;
292 
293   /// Get the rank of the tensor.
294   uint64_t getRank() const { return sizes.size(); }
295 
296   /// Get the size in the given dimension of the tensor.
297   uint64_t getDimSize(uint64_t d) override {
298     assert(d < getRank());
299     return sizes[d];
300   }
301 
302   /// Partially specialize these getter methods based on template types.
303   void getPointers(std::vector<P> **out, uint64_t d) override {
304     assert(d < getRank());
305     *out = &pointers[d];
306   }
307   void getIndices(std::vector<I> **out, uint64_t d) override {
308     assert(d < getRank());
309     *out = &indices[d];
310   }
311   void getValues(std::vector<V> **out) override { *out = &values; }
312 
313   /// Partially specialize lexicographical insertions based on template types.
314   void lexInsert(const uint64_t *cursor, V val) override {
315     // First, wrap up pending insertion path.
316     uint64_t diff = 0;
317     uint64_t top = 0;
318     if (!values.empty()) {
319       diff = lexDiff(cursor);
320       endPath(diff + 1);
321       top = idx[diff] + 1;
322     }
323     // Then continue with insertion path.
324     insPath(cursor, diff, top, val);
325   }
326 
327   /// Partially specialize expanded insertions based on template types.
328   /// Note that this method resets the values/filled-switch array back
329   /// to all-zero/false while only iterating over the nonzero elements.
330   void expInsert(uint64_t *cursor, V *values, bool *filled, uint64_t *added,
331                  uint64_t count) override {
332     if (count == 0)
333       return;
334     // Sort.
335     std::sort(added, added + count);
336     // Restore insertion path for first insert.
337     uint64_t rank = getRank();
338     uint64_t index = added[0];
339     cursor[rank - 1] = index;
340     lexInsert(cursor, values[index]);
341     assert(filled[index]);
342     values[index] = 0;
343     filled[index] = false;
344     // Subsequent insertions are quick.
345     for (uint64_t i = 1; i < count; i++) {
346       assert(index < added[i] && "non-lexicographic insertion");
347       index = added[i];
348       cursor[rank - 1] = index;
349       insPath(cursor, rank - 1, added[i - 1] + 1, values[index]);
350       assert(filled[index]);
351       values[index] = 0.0;
352       filled[index] = false;
353     }
354   }
355 
356   /// Finalizes lexicographic insertions.
357   void endInsert() override {
358     if (values.empty())
359       endDim(0);
360     else
361       endPath(0);
362   }
363 
364   /// Returns this sparse tensor storage scheme as a new memory-resident
365   /// sparse tensor in coordinate scheme with the given dimension order.
366   SparseTensorCOO<V> *toCOO(const uint64_t *perm) {
367     // Restore original order of the dimension sizes and allocate coordinate
368     // scheme with desired new ordering specified in perm.
369     uint64_t rank = getRank();
370     std::vector<uint64_t> orgsz(rank);
371     for (uint64_t r = 0; r < rank; r++)
372       orgsz[rev[r]] = sizes[r];
373     SparseTensorCOO<V> *tensor = SparseTensorCOO<V>::newSparseTensorCOO(
374         rank, orgsz.data(), perm, values.size());
375     // Populate coordinate scheme restored from old ordering and changed with
376     // new ordering. Rather than applying both reorderings during the recursion,
377     // we compute the combine permutation in advance.
378     std::vector<uint64_t> reord(rank);
379     for (uint64_t r = 0; r < rank; r++)
380       reord[r] = perm[rev[r]];
381     toCOO(*tensor, reord, 0, 0);
382     assert(tensor->getElements().size() == values.size());
383     return tensor;
384   }
385 
386   /// Factory method. Constructs a sparse tensor storage scheme with the given
387   /// dimensions, permutation, and per-dimension dense/sparse annotations,
388   /// using the coordinate scheme tensor for the initial contents if provided.
389   /// In the latter case, the coordinate scheme must respect the same
390   /// permutation as is desired for the new sparse tensor storage.
391   static SparseTensorStorage<P, I, V> *
392   newSparseTensor(uint64_t rank, const uint64_t *sizes, const uint64_t *perm,
393                   const DimLevelType *sparsity, SparseTensorCOO<V> *tensor) {
394     SparseTensorStorage<P, I, V> *n = nullptr;
395     if (tensor) {
396       assert(tensor->getRank() == rank);
397       for (uint64_t r = 0; r < rank; r++)
398         assert(sizes[r] == 0 || tensor->getSizes()[perm[r]] == sizes[r]);
399       n = new SparseTensorStorage<P, I, V>(tensor->getSizes(), perm, sparsity,
400                                            tensor);
401       delete tensor;
402     } else {
403       std::vector<uint64_t> permsz(rank);
404       for (uint64_t r = 0; r < rank; r++)
405         permsz[perm[r]] = sizes[r];
406       n = new SparseTensorStorage<P, I, V>(permsz, perm, sparsity);
407     }
408     return n;
409   }
410 
411 private:
412   /// Appends the next free position of `indices[d]` to `pointers[d]`.
413   /// Thus, when called after inserting the last element of a segment,
414   /// it will append the position where the next segment begins.
415   inline void appendPointer(uint64_t d) {
416     assert(isCompressedDim(d)); // Entails `d < getRank()`.
417     uint64_t p = indices[d].size();
418     assert(p <= std::numeric_limits<P>::max() &&
419            "Pointer value is too large for the P-type");
420     pointers[d].push_back(p); // Here is where we convert to `P`.
421   }
422 
423   /// Appends the given index to `indices[d]`.
424   inline void appendIndex(uint64_t d, uint64_t i) {
425     assert(isCompressedDim(d)); // Entails `d < getRank()`.
426     assert(i <= std::numeric_limits<I>::max() &&
427            "Index value is too large for the I-type");
428     indices[d].push_back(i); // Here is where we convert to `I`.
429   }
430 
431   /// Initializes sparse tensor storage scheme from a memory-resident sparse
432   /// tensor in coordinate scheme. This method prepares the pointers and
433   /// indices arrays under the given per-dimension dense/sparse annotations.
434   ///
435   /// Preconditions:
436   /// (1) the `elements` must be lexicographically sorted.
437   /// (2) the indices of every element are valid for `sizes` (equal rank
438   ///     and pointwise less-than).
439   void fromCOO(const std::vector<Element<V>> &elements, uint64_t lo,
440                uint64_t hi, uint64_t d) {
441     // Once dimensions are exhausted, insert the numerical values.
442     assert(d <= getRank() && hi <= elements.size());
443     if (d == getRank()) {
444       assert(lo < hi);
445       values.push_back(elements[lo].value);
446       return;
447     }
448     // Visit all elements in this interval.
449     uint64_t full = 0;
450     while (lo < hi) { // If `hi` is unchanged, then `lo < elements.size()`.
451       // Find segment in interval with same index elements in this dimension.
452       uint64_t i = elements[lo].indices[d];
453       uint64_t seg = lo + 1;
454       while (seg < hi && elements[seg].indices[d] == i)
455         seg++;
456       // Handle segment in interval for sparse or dense dimension.
457       if (isCompressedDim(d)) {
458         appendIndex(d, i);
459       } else {
460         // For dense storage we must fill in all the zero values between
461         // the previous element (when last we ran this for-loop) and the
462         // current element.
463         for (; full < i; full++)
464           endDim(d + 1);
465         full++;
466       }
467       fromCOO(elements, lo, seg, d + 1);
468       // And move on to next segment in interval.
469       lo = seg;
470     }
471     // Finalize the sparse pointer structure at this dimension.
472     if (isCompressedDim(d)) {
473       appendPointer(d);
474     } else {
475       // For dense storage we must fill in all the zero values after
476       // the last element.
477       for (uint64_t sz = sizes[d]; full < sz; full++)
478         endDim(d + 1);
479     }
480   }
481 
482   /// Stores the sparse tensor storage scheme into a memory-resident sparse
483   /// tensor in coordinate scheme.
484   void toCOO(SparseTensorCOO<V> &tensor, std::vector<uint64_t> &reord,
485              uint64_t pos, uint64_t d) {
486     assert(d <= getRank());
487     if (d == getRank()) {
488       assert(pos < values.size());
489       tensor.add(idx, values[pos]);
490     } else if (isCompressedDim(d)) {
491       // Sparse dimension.
492       for (uint64_t ii = pointers[d][pos]; ii < pointers[d][pos + 1]; ii++) {
493         idx[reord[d]] = indices[d][ii];
494         toCOO(tensor, reord, ii, d + 1);
495       }
496     } else {
497       // Dense dimension.
498       for (uint64_t i = 0, sz = sizes[d], off = pos * sz; i < sz; i++) {
499         idx[reord[d]] = i;
500         toCOO(tensor, reord, off + i, d + 1);
501       }
502     }
503   }
504 
505   /// Ends a deeper, never seen before dimension.
506   void endDim(uint64_t d) {
507     assert(d <= getRank());
508     if (d == getRank()) {
509       values.push_back(0);
510     } else if (isCompressedDim(d)) {
511       appendPointer(d);
512     } else {
513       for (uint64_t full = 0, sz = sizes[d]; full < sz; full++)
514         endDim(d + 1);
515     }
516   }
517 
518   /// Wraps up a single insertion path, inner to outer.
519   void endPath(uint64_t diff) {
520     uint64_t rank = getRank();
521     assert(diff <= rank);
522     for (uint64_t i = 0; i < rank - diff; i++) {
523       uint64_t d = rank - i - 1;
524       if (isCompressedDim(d)) {
525         appendPointer(d);
526       } else {
527         for (uint64_t full = idx[d] + 1, sz = sizes[d]; full < sz; full++)
528           endDim(d + 1);
529       }
530     }
531   }
532 
533   /// Continues a single insertion path, outer to inner.
534   void insPath(const uint64_t *cursor, uint64_t diff, uint64_t top, V val) {
535     uint64_t rank = getRank();
536     assert(diff < rank);
537     for (uint64_t d = diff; d < rank; d++) {
538       uint64_t i = cursor[d];
539       if (isCompressedDim(d)) {
540         appendIndex(d, i);
541       } else {
542         for (uint64_t full = top; full < i; full++)
543           endDim(d + 1);
544       }
545       top = 0;
546       idx[d] = i;
547     }
548     values.push_back(val);
549   }
550 
551   /// Finds the lexicographic differing dimension.
552   uint64_t lexDiff(const uint64_t *cursor) {
553     for (uint64_t r = 0, rank = getRank(); r < rank; r++)
554       if (cursor[r] > idx[r])
555         return r;
556       else
557         assert(cursor[r] == idx[r] && "non-lexicographic insertion");
558     assert(0 && "duplication insertion");
559     return -1u;
560   }
561 
562   /// Returns true if dimension is compressed.
563   inline bool isCompressedDim(uint64_t d) const {
564     assert(d < getRank());
565     return (!pointers[d].empty());
566   }
567 
568 private:
569   std::vector<uint64_t> sizes; // per-dimension sizes
570   std::vector<uint64_t> rev;   // "reverse" permutation
571   std::vector<uint64_t> idx;   // index cursor
572   std::vector<std::vector<P>> pointers;
573   std::vector<std::vector<I>> indices;
574   std::vector<V> values;
575 };
576 
577 /// Helper to convert string to lower case.
578 static char *toLower(char *token) {
579   for (char *c = token; *c; c++)
580     *c = tolower(*c);
581   return token;
582 }
583 
584 /// Read the MME header of a general sparse matrix of type real.
585 static void readMMEHeader(FILE *file, char *filename, char *line,
586                           uint64_t *idata, bool *isSymmetric) {
587   char header[64];
588   char object[64];
589   char format[64];
590   char field[64];
591   char symmetry[64];
592   // Read header line.
593   if (fscanf(file, "%63s %63s %63s %63s %63s\n", header, object, format, field,
594              symmetry) != 5) {
595     fprintf(stderr, "Corrupt header in %s\n", filename);
596     exit(1);
597   }
598   *isSymmetric = (strcmp(toLower(symmetry), "symmetric") == 0);
599   // Make sure this is a general sparse matrix.
600   if (strcmp(toLower(header), "%%matrixmarket") ||
601       strcmp(toLower(object), "matrix") ||
602       strcmp(toLower(format), "coordinate") || strcmp(toLower(field), "real") ||
603       (strcmp(toLower(symmetry), "general") && !(*isSymmetric))) {
604     fprintf(stderr,
605             "Cannot find a general sparse matrix with type real in %s\n",
606             filename);
607     exit(1);
608   }
609   // Skip comments.
610   while (true) {
611     if (!fgets(line, kColWidth, file)) {
612       fprintf(stderr, "Cannot find data in %s\n", filename);
613       exit(1);
614     }
615     if (line[0] != '%')
616       break;
617   }
618   // Next line contains M N NNZ.
619   idata[0] = 2; // rank
620   if (sscanf(line, "%" PRIu64 "%" PRIu64 "%" PRIu64 "\n", idata + 2, idata + 3,
621              idata + 1) != 3) {
622     fprintf(stderr, "Cannot find size in %s\n", filename);
623     exit(1);
624   }
625 }
626 
627 /// Read the "extended" FROSTT header. Although not part of the documented
628 /// format, we assume that the file starts with optional comments followed
629 /// by two lines that define the rank, the number of nonzeros, and the
630 /// dimensions sizes (one per rank) of the sparse tensor.
631 static void readExtFROSTTHeader(FILE *file, char *filename, char *line,
632                                 uint64_t *idata) {
633   // Skip comments.
634   while (true) {
635     if (!fgets(line, kColWidth, file)) {
636       fprintf(stderr, "Cannot find data in %s\n", filename);
637       exit(1);
638     }
639     if (line[0] != '#')
640       break;
641   }
642   // Next line contains RANK and NNZ.
643   if (sscanf(line, "%" PRIu64 "%" PRIu64 "\n", idata, idata + 1) != 2) {
644     fprintf(stderr, "Cannot find metadata in %s\n", filename);
645     exit(1);
646   }
647   // Followed by a line with the dimension sizes (one per rank).
648   for (uint64_t r = 0; r < idata[0]; r++) {
649     if (fscanf(file, "%" PRIu64, idata + 2 + r) != 1) {
650       fprintf(stderr, "Cannot find dimension size %s\n", filename);
651       exit(1);
652     }
653   }
654   fgets(line, kColWidth, file); // end of line
655 }
656 
657 /// Reads a sparse tensor with the given filename into a memory-resident
658 /// sparse tensor in coordinate scheme.
659 template <typename V>
660 static SparseTensorCOO<V> *openSparseTensorCOO(char *filename, uint64_t rank,
661                                                const uint64_t *sizes,
662                                                const uint64_t *perm) {
663   // Open the file.
664   FILE *file = fopen(filename, "r");
665   if (!file) {
666     fprintf(stderr, "Cannot find %s\n", filename);
667     exit(1);
668   }
669   // Perform some file format dependent set up.
670   char line[kColWidth];
671   uint64_t idata[512];
672   bool isSymmetric = false;
673   if (strstr(filename, ".mtx")) {
674     readMMEHeader(file, filename, line, idata, &isSymmetric);
675   } else if (strstr(filename, ".tns")) {
676     readExtFROSTTHeader(file, filename, line, idata);
677   } else {
678     fprintf(stderr, "Unknown format %s\n", filename);
679     exit(1);
680   }
681   // Prepare sparse tensor object with per-dimension sizes
682   // and the number of nonzeros as initial capacity.
683   assert(rank == idata[0] && "rank mismatch");
684   uint64_t nnz = idata[1];
685   for (uint64_t r = 0; r < rank; r++)
686     assert((sizes[r] == 0 || sizes[r] == idata[2 + r]) &&
687            "dimension size mismatch");
688   SparseTensorCOO<V> *tensor =
689       SparseTensorCOO<V>::newSparseTensorCOO(rank, idata + 2, perm, nnz);
690   //  Read all nonzero elements.
691   std::vector<uint64_t> indices(rank);
692   for (uint64_t k = 0; k < nnz; k++) {
693     if (!fgets(line, kColWidth, file)) {
694       fprintf(stderr, "Cannot find next line of data in %s\n", filename);
695       exit(1);
696     }
697     char *linePtr = line;
698     for (uint64_t r = 0; r < rank; r++) {
699       uint64_t idx = strtoul(linePtr, &linePtr, 10);
700       // Add 0-based index.
701       indices[perm[r]] = idx - 1;
702     }
703     // The external formats always store the numerical values with the type
704     // double, but we cast these values to the sparse tensor object type.
705     double value = strtod(linePtr, &linePtr);
706     tensor->add(indices, value);
707     // We currently chose to deal with symmetric matrices by fully constructing
708     // them. In the future, we may want to make symmetry implicit for storage
709     // reasons.
710     if (isSymmetric && indices[0] != indices[1])
711       tensor->add({indices[1], indices[0]}, value);
712   }
713   // Close the file and return tensor.
714   fclose(file);
715   return tensor;
716 }
717 
718 /// Writes the sparse tensor to extended FROSTT format.
719 template <typename V>
720 void outSparseTensor(void *tensor, void *dest, bool sort) {
721   assert(tensor && dest);
722   auto coo = static_cast<SparseTensorCOO<V> *>(tensor);
723   if (sort)
724     coo->sort();
725   char *filename = static_cast<char *>(dest);
726   auto &sizes = coo->getSizes();
727   auto &elements = coo->getElements();
728   uint64_t rank = coo->getRank();
729   uint64_t nnz = elements.size();
730   std::fstream file;
731   file.open(filename, std::ios_base::out | std::ios_base::trunc);
732   assert(file.is_open());
733   file << "; extended FROSTT format\n" << rank << " " << nnz << std::endl;
734   for (uint64_t r = 0; r < rank - 1; r++)
735     file << sizes[r] << " ";
736   file << sizes[rank - 1] << std::endl;
737   for (uint64_t i = 0; i < nnz; i++) {
738     auto &idx = elements[i].indices;
739     for (uint64_t r = 0; r < rank; r++)
740       file << (idx[r] + 1) << " ";
741     file << elements[i].value << std::endl;
742   }
743   file.flush();
744   file.close();
745   assert(file.good());
746   delete coo;
747 }
748 
749 /// Initializes sparse tensor from an external COO-flavored format.
750 template <typename V>
751 SparseTensorStorage<uint64_t, uint64_t, V> *
752 toMLIRSparseTensor(uint64_t rank, uint64_t nse, uint64_t *shape, V *values,
753                    uint64_t *indices, uint64_t *perm, uint8_t *sparse) {
754   const DimLevelType *sparsity = (DimLevelType *)(sparse);
755 #ifndef NDEBUG
756   // Verify that perm is a permutation of 0..(rank-1).
757   std::vector<uint64_t> order(perm, perm + rank);
758   std::sort(order.begin(), order.end());
759   for (uint64_t i = 0; i < rank; ++i) {
760     if (i != order[i]) {
761       fprintf(stderr, "Not a permutation of 0..%" PRIu64 "\n", rank);
762       exit(1);
763     }
764   }
765 
766   // Verify that the sparsity values are supported.
767   for (uint64_t i = 0; i < rank; ++i) {
768     if (sparsity[i] != DimLevelType::kDense &&
769         sparsity[i] != DimLevelType::kCompressed) {
770       fprintf(stderr, "Unsupported sparsity value %d\n",
771               static_cast<int>(sparsity[i]));
772       exit(1);
773     }
774   }
775 #endif
776 
777   // Convert external format to internal COO.
778   auto *tensor = SparseTensorCOO<V>::newSparseTensorCOO(rank, shape, perm, nse);
779   std::vector<uint64_t> idx(rank);
780   for (uint64_t i = 0, base = 0; i < nse; i++) {
781     for (uint64_t r = 0; r < rank; r++)
782       idx[perm[r]] = indices[base + r];
783     tensor->add(idx, values[i]);
784     base += rank;
785   }
786   // Return sparse tensor storage format as opaque pointer.
787   return SparseTensorStorage<uint64_t, uint64_t, V>::newSparseTensor(
788       rank, shape, perm, sparsity, tensor);
789 }
790 
791 /// Converts a sparse tensor to an external COO-flavored format.
792 template <typename V>
793 void fromMLIRSparseTensor(void *tensor, uint64_t *pRank, uint64_t *pNse,
794                           uint64_t **pShape, V **pValues, uint64_t **pIndices) {
795   auto sparseTensor =
796       static_cast<SparseTensorStorage<uint64_t, uint64_t, V> *>(tensor);
797   uint64_t rank = sparseTensor->getRank();
798   std::vector<uint64_t> perm(rank);
799   std::iota(perm.begin(), perm.end(), 0);
800   SparseTensorCOO<V> *coo = sparseTensor->toCOO(perm.data());
801 
802   const std::vector<Element<V>> &elements = coo->getElements();
803   uint64_t nse = elements.size();
804 
805   uint64_t *shape = new uint64_t[rank];
806   for (uint64_t i = 0; i < rank; i++)
807     shape[i] = coo->getSizes()[i];
808 
809   V *values = new V[nse];
810   uint64_t *indices = new uint64_t[rank * nse];
811 
812   for (uint64_t i = 0, base = 0; i < nse; i++) {
813     values[i] = elements[i].value;
814     for (uint64_t j = 0; j < rank; j++)
815       indices[base + j] = elements[i].indices[j];
816     base += rank;
817   }
818 
819   delete coo;
820   *pRank = rank;
821   *pNse = nse;
822   *pShape = shape;
823   *pValues = values;
824   *pIndices = indices;
825 }
826 
827 } // namespace
828 
829 extern "C" {
830 
831 //===----------------------------------------------------------------------===//
832 //
833 // Public API with methods that operate on MLIR buffers (memrefs) to interact
834 // with sparse tensors, which are only visible as opaque pointers externally.
835 // These methods should be used exclusively by MLIR compiler-generated code.
836 //
837 // Some macro magic is used to generate implementations for all required type
838 // combinations that can be called from MLIR compiler-generated code.
839 //
840 //===----------------------------------------------------------------------===//
841 
842 #define CASE(p, i, v, P, I, V)                                                 \
843   if (ptrTp == (p) && indTp == (i) && valTp == (v)) {                          \
844     SparseTensorCOO<V> *tensor = nullptr;                                      \
845     if (action <= Action::kFromCOO) {                                          \
846       if (action == Action::kFromFile) {                                       \
847         char *filename = static_cast<char *>(ptr);                             \
848         tensor = openSparseTensorCOO<V>(filename, rank, sizes, perm);          \
849       } else if (action == Action::kFromCOO) {                                 \
850         tensor = static_cast<SparseTensorCOO<V> *>(ptr);                       \
851       } else {                                                                 \
852         assert(action == Action::kEmpty);                                      \
853       }                                                                        \
854       return SparseTensorStorage<P, I, V>::newSparseTensor(rank, sizes, perm,  \
855                                                            sparsity, tensor);  \
856     }                                                                          \
857     if (action == Action::kEmptyCOO)                                           \
858       return SparseTensorCOO<V>::newSparseTensorCOO(rank, sizes, perm);        \
859     tensor = static_cast<SparseTensorStorage<P, I, V> *>(ptr)->toCOO(perm);    \
860     if (action == Action::kToIterator) {                                       \
861       tensor->startIterator();                                                 \
862     } else {                                                                   \
863       assert(action == Action::kToCOO);                                        \
864     }                                                                          \
865     return tensor;                                                             \
866   }
867 
868 #define CASE_SECSAME(p, v, P, V) CASE(p, p, v, P, P, V)
869 
870 #define IMPL_SPARSEVALUES(NAME, TYPE, LIB)                                     \
871   void _mlir_ciface_##NAME(StridedMemRefType<TYPE, 1> *ref, void *tensor) {    \
872     assert(ref &&tensor);                                                      \
873     std::vector<TYPE> *v;                                                      \
874     static_cast<SparseTensorStorageBase *>(tensor)->LIB(&v);                   \
875     ref->basePtr = ref->data = v->data();                                      \
876     ref->offset = 0;                                                           \
877     ref->sizes[0] = v->size();                                                 \
878     ref->strides[0] = 1;                                                       \
879   }
880 
881 #define IMPL_GETOVERHEAD(NAME, TYPE, LIB)                                      \
882   void _mlir_ciface_##NAME(StridedMemRefType<TYPE, 1> *ref, void *tensor,      \
883                            index_type d) {                                     \
884     assert(ref &&tensor);                                                      \
885     std::vector<TYPE> *v;                                                      \
886     static_cast<SparseTensorStorageBase *>(tensor)->LIB(&v, d);                \
887     ref->basePtr = ref->data = v->data();                                      \
888     ref->offset = 0;                                                           \
889     ref->sizes[0] = v->size();                                                 \
890     ref->strides[0] = 1;                                                       \
891   }
892 
893 #define IMPL_ADDELT(NAME, TYPE)                                                \
894   void *_mlir_ciface_##NAME(void *tensor, TYPE value,                          \
895                             StridedMemRefType<index_type, 1> *iref,            \
896                             StridedMemRefType<index_type, 1> *pref) {          \
897     assert(tensor &&iref &&pref);                                              \
898     assert(iref->strides[0] == 1 && pref->strides[0] == 1);                    \
899     assert(iref->sizes[0] == pref->sizes[0]);                                  \
900     const index_type *indx = iref->data + iref->offset;                        \
901     const index_type *perm = pref->data + pref->offset;                        \
902     uint64_t isize = iref->sizes[0];                                           \
903     std::vector<index_type> indices(isize);                                    \
904     for (uint64_t r = 0; r < isize; r++)                                       \
905       indices[perm[r]] = indx[r];                                              \
906     static_cast<SparseTensorCOO<TYPE> *>(tensor)->add(indices, value);         \
907     return tensor;                                                             \
908   }
909 
910 #define IMPL_GETNEXT(NAME, V)                                                  \
911   bool _mlir_ciface_##NAME(void *tensor,                                       \
912                            StridedMemRefType<index_type, 1> *iref,             \
913                            StridedMemRefType<V, 0> *vref) {                    \
914     assert(tensor &&iref &&vref);                                              \
915     assert(iref->strides[0] == 1);                                             \
916     index_type *indx = iref->data + iref->offset;                              \
917     V *value = vref->data + vref->offset;                                      \
918     const uint64_t isize = iref->sizes[0];                                     \
919     auto iter = static_cast<SparseTensorCOO<V> *>(tensor);                     \
920     const Element<V> *elem = iter->getNext();                                  \
921     if (elem == nullptr) {                                                     \
922       delete iter;                                                             \
923       return false;                                                            \
924     }                                                                          \
925     for (uint64_t r = 0; r < isize; r++)                                       \
926       indx[r] = elem->indices[r];                                              \
927     *value = elem->value;                                                      \
928     return true;                                                               \
929   }
930 
931 #define IMPL_LEXINSERT(NAME, V)                                                \
932   void _mlir_ciface_##NAME(void *tensor,                                       \
933                            StridedMemRefType<index_type, 1> *cref, V val) {    \
934     assert(tensor &&cref);                                                     \
935     assert(cref->strides[0] == 1);                                             \
936     index_type *cursor = cref->data + cref->offset;                            \
937     assert(cursor);                                                            \
938     static_cast<SparseTensorStorageBase *>(tensor)->lexInsert(cursor, val);    \
939   }
940 
941 #define IMPL_EXPINSERT(NAME, V)                                                \
942   void _mlir_ciface_##NAME(                                                    \
943       void *tensor, StridedMemRefType<index_type, 1> *cref,                    \
944       StridedMemRefType<V, 1> *vref, StridedMemRefType<bool, 1> *fref,         \
945       StridedMemRefType<index_type, 1> *aref, index_type count) {              \
946     assert(tensor &&cref &&vref &&fref &&aref);                                \
947     assert(cref->strides[0] == 1);                                             \
948     assert(vref->strides[0] == 1);                                             \
949     assert(fref->strides[0] == 1);                                             \
950     assert(aref->strides[0] == 1);                                             \
951     assert(vref->sizes[0] == fref->sizes[0]);                                  \
952     index_type *cursor = cref->data + cref->offset;                            \
953     V *values = vref->data + vref->offset;                                     \
954     bool *filled = fref->data + fref->offset;                                  \
955     index_type *added = aref->data + aref->offset;                             \
956     static_cast<SparseTensorStorageBase *>(tensor)->expInsert(                 \
957         cursor, values, filled, added, count);                                 \
958   }
959 
960 // Assume index_type is in fact uint64_t, so that _mlir_ciface_newSparseTensor
961 // can safely rewrite kIndex to kU64.  We make this assertion to guarantee
962 // that this file cannot get out of sync with its header.
963 static_assert(std::is_same<index_type, uint64_t>::value,
964               "Expected index_type == uint64_t");
965 
966 /// Constructs a new sparse tensor. This is the "swiss army knife"
967 /// method for materializing sparse tensors into the computation.
968 ///
969 /// Action:
970 /// kEmpty = returns empty storage to fill later
971 /// kFromFile = returns storage, where ptr contains filename to read
972 /// kFromCOO = returns storage, where ptr contains coordinate scheme to assign
973 /// kEmptyCOO = returns empty coordinate scheme to fill and use with kFromCOO
974 /// kToCOO = returns coordinate scheme from storage in ptr to use with kFromCOO
975 /// kToIterator = returns iterator from storage in ptr (call getNext() to use)
976 void *
977 _mlir_ciface_newSparseTensor(StridedMemRefType<DimLevelType, 1> *aref, // NOLINT
978                              StridedMemRefType<index_type, 1> *sref,
979                              StridedMemRefType<index_type, 1> *pref,
980                              OverheadType ptrTp, OverheadType indTp,
981                              PrimaryType valTp, Action action, void *ptr) {
982   assert(aref && sref && pref);
983   assert(aref->strides[0] == 1 && sref->strides[0] == 1 &&
984          pref->strides[0] == 1);
985   assert(aref->sizes[0] == sref->sizes[0] && sref->sizes[0] == pref->sizes[0]);
986   const DimLevelType *sparsity = aref->data + aref->offset;
987   const index_type *sizes = sref->data + sref->offset;
988   const index_type *perm = pref->data + pref->offset;
989   uint64_t rank = aref->sizes[0];
990 
991   // Rewrite kIndex to kU64, to avoid introducing a bunch of new cases.
992   // This is safe because of the static_assert above.
993   if (ptrTp == OverheadType::kIndex)
994     ptrTp = OverheadType::kU64;
995   if (indTp == OverheadType::kIndex)
996     indTp = OverheadType::kU64;
997 
998   // Double matrices with all combinations of overhead storage.
999   CASE(OverheadType::kU64, OverheadType::kU64, PrimaryType::kF64, uint64_t,
1000        uint64_t, double);
1001   CASE(OverheadType::kU64, OverheadType::kU32, PrimaryType::kF64, uint64_t,
1002        uint32_t, double);
1003   CASE(OverheadType::kU64, OverheadType::kU16, PrimaryType::kF64, uint64_t,
1004        uint16_t, double);
1005   CASE(OverheadType::kU64, OverheadType::kU8, PrimaryType::kF64, uint64_t,
1006        uint8_t, double);
1007   CASE(OverheadType::kU32, OverheadType::kU64, PrimaryType::kF64, uint32_t,
1008        uint64_t, double);
1009   CASE(OverheadType::kU32, OverheadType::kU32, PrimaryType::kF64, uint32_t,
1010        uint32_t, double);
1011   CASE(OverheadType::kU32, OverheadType::kU16, PrimaryType::kF64, uint32_t,
1012        uint16_t, double);
1013   CASE(OverheadType::kU32, OverheadType::kU8, PrimaryType::kF64, uint32_t,
1014        uint8_t, double);
1015   CASE(OverheadType::kU16, OverheadType::kU64, PrimaryType::kF64, uint16_t,
1016        uint64_t, double);
1017   CASE(OverheadType::kU16, OverheadType::kU32, PrimaryType::kF64, uint16_t,
1018        uint32_t, double);
1019   CASE(OverheadType::kU16, OverheadType::kU16, PrimaryType::kF64, uint16_t,
1020        uint16_t, double);
1021   CASE(OverheadType::kU16, OverheadType::kU8, PrimaryType::kF64, uint16_t,
1022        uint8_t, double);
1023   CASE(OverheadType::kU8, OverheadType::kU64, PrimaryType::kF64, uint8_t,
1024        uint64_t, double);
1025   CASE(OverheadType::kU8, OverheadType::kU32, PrimaryType::kF64, uint8_t,
1026        uint32_t, double);
1027   CASE(OverheadType::kU8, OverheadType::kU16, PrimaryType::kF64, uint8_t,
1028        uint16_t, double);
1029   CASE(OverheadType::kU8, OverheadType::kU8, PrimaryType::kF64, uint8_t,
1030        uint8_t, double);
1031 
1032   // Float matrices with all combinations of overhead storage.
1033   CASE(OverheadType::kU64, OverheadType::kU64, PrimaryType::kF32, uint64_t,
1034        uint64_t, float);
1035   CASE(OverheadType::kU64, OverheadType::kU32, PrimaryType::kF32, uint64_t,
1036        uint32_t, float);
1037   CASE(OverheadType::kU64, OverheadType::kU16, PrimaryType::kF32, uint64_t,
1038        uint16_t, float);
1039   CASE(OverheadType::kU64, OverheadType::kU8, PrimaryType::kF32, uint64_t,
1040        uint8_t, float);
1041   CASE(OverheadType::kU32, OverheadType::kU64, PrimaryType::kF32, uint32_t,
1042        uint64_t, float);
1043   CASE(OverheadType::kU32, OverheadType::kU32, PrimaryType::kF32, uint32_t,
1044        uint32_t, float);
1045   CASE(OverheadType::kU32, OverheadType::kU16, PrimaryType::kF32, uint32_t,
1046        uint16_t, float);
1047   CASE(OverheadType::kU32, OverheadType::kU8, PrimaryType::kF32, uint32_t,
1048        uint8_t, float);
1049   CASE(OverheadType::kU16, OverheadType::kU64, PrimaryType::kF32, uint16_t,
1050        uint64_t, float);
1051   CASE(OverheadType::kU16, OverheadType::kU32, PrimaryType::kF32, uint16_t,
1052        uint32_t, float);
1053   CASE(OverheadType::kU16, OverheadType::kU16, PrimaryType::kF32, uint16_t,
1054        uint16_t, float);
1055   CASE(OverheadType::kU16, OverheadType::kU8, PrimaryType::kF32, uint16_t,
1056        uint8_t, float);
1057   CASE(OverheadType::kU8, OverheadType::kU64, PrimaryType::kF32, uint8_t,
1058        uint64_t, float);
1059   CASE(OverheadType::kU8, OverheadType::kU32, PrimaryType::kF32, uint8_t,
1060        uint32_t, float);
1061   CASE(OverheadType::kU8, OverheadType::kU16, PrimaryType::kF32, uint8_t,
1062        uint16_t, float);
1063   CASE(OverheadType::kU8, OverheadType::kU8, PrimaryType::kF32, uint8_t,
1064        uint8_t, float);
1065 
1066   // Integral matrices with both overheads of the same type.
1067   CASE_SECSAME(OverheadType::kU64, PrimaryType::kI64, uint64_t, int64_t);
1068   CASE_SECSAME(OverheadType::kU64, PrimaryType::kI32, uint64_t, int32_t);
1069   CASE_SECSAME(OverheadType::kU64, PrimaryType::kI16, uint64_t, int16_t);
1070   CASE_SECSAME(OverheadType::kU64, PrimaryType::kI8, uint64_t, int8_t);
1071   CASE_SECSAME(OverheadType::kU32, PrimaryType::kI32, uint32_t, int32_t);
1072   CASE_SECSAME(OverheadType::kU32, PrimaryType::kI16, uint32_t, int16_t);
1073   CASE_SECSAME(OverheadType::kU32, PrimaryType::kI8, uint32_t, int8_t);
1074   CASE_SECSAME(OverheadType::kU16, PrimaryType::kI32, uint16_t, int32_t);
1075   CASE_SECSAME(OverheadType::kU16, PrimaryType::kI16, uint16_t, int16_t);
1076   CASE_SECSAME(OverheadType::kU16, PrimaryType::kI8, uint16_t, int8_t);
1077   CASE_SECSAME(OverheadType::kU8, PrimaryType::kI32, uint8_t, int32_t);
1078   CASE_SECSAME(OverheadType::kU8, PrimaryType::kI16, uint8_t, int16_t);
1079   CASE_SECSAME(OverheadType::kU8, PrimaryType::kI8, uint8_t, int8_t);
1080 
1081   // Unsupported case (add above if needed).
1082   fputs("unsupported combination of types\n", stderr);
1083   exit(1);
1084 }
1085 
1086 /// Methods that provide direct access to pointers.
1087 IMPL_GETOVERHEAD(sparsePointers, index_type, getPointers)
1088 IMPL_GETOVERHEAD(sparsePointers64, uint64_t, getPointers)
1089 IMPL_GETOVERHEAD(sparsePointers32, uint32_t, getPointers)
1090 IMPL_GETOVERHEAD(sparsePointers16, uint16_t, getPointers)
1091 IMPL_GETOVERHEAD(sparsePointers8, uint8_t, getPointers)
1092 
1093 /// Methods that provide direct access to indices.
1094 IMPL_GETOVERHEAD(sparseIndices, index_type, getIndices)
1095 IMPL_GETOVERHEAD(sparseIndices64, uint64_t, getIndices)
1096 IMPL_GETOVERHEAD(sparseIndices32, uint32_t, getIndices)
1097 IMPL_GETOVERHEAD(sparseIndices16, uint16_t, getIndices)
1098 IMPL_GETOVERHEAD(sparseIndices8, uint8_t, getIndices)
1099 
1100 /// Methods that provide direct access to values.
1101 IMPL_SPARSEVALUES(sparseValuesF64, double, getValues)
1102 IMPL_SPARSEVALUES(sparseValuesF32, float, getValues)
1103 IMPL_SPARSEVALUES(sparseValuesI64, int64_t, getValues)
1104 IMPL_SPARSEVALUES(sparseValuesI32, int32_t, getValues)
1105 IMPL_SPARSEVALUES(sparseValuesI16, int16_t, getValues)
1106 IMPL_SPARSEVALUES(sparseValuesI8, int8_t, getValues)
1107 
1108 /// Helper to add value to coordinate scheme, one per value type.
1109 IMPL_ADDELT(addEltF64, double)
1110 IMPL_ADDELT(addEltF32, float)
1111 IMPL_ADDELT(addEltI64, int64_t)
1112 IMPL_ADDELT(addEltI32, int32_t)
1113 IMPL_ADDELT(addEltI16, int16_t)
1114 IMPL_ADDELT(addEltI8, int8_t)
1115 
1116 /// Helper to enumerate elements of coordinate scheme, one per value type.
1117 IMPL_GETNEXT(getNextF64, double)
1118 IMPL_GETNEXT(getNextF32, float)
1119 IMPL_GETNEXT(getNextI64, int64_t)
1120 IMPL_GETNEXT(getNextI32, int32_t)
1121 IMPL_GETNEXT(getNextI16, int16_t)
1122 IMPL_GETNEXT(getNextI8, int8_t)
1123 
1124 /// Insert elements in lexicographical index order, one per value type.
1125 IMPL_LEXINSERT(lexInsertF64, double)
1126 IMPL_LEXINSERT(lexInsertF32, float)
1127 IMPL_LEXINSERT(lexInsertI64, int64_t)
1128 IMPL_LEXINSERT(lexInsertI32, int32_t)
1129 IMPL_LEXINSERT(lexInsertI16, int16_t)
1130 IMPL_LEXINSERT(lexInsertI8, int8_t)
1131 
1132 /// Insert using expansion, one per value type.
1133 IMPL_EXPINSERT(expInsertF64, double)
1134 IMPL_EXPINSERT(expInsertF32, float)
1135 IMPL_EXPINSERT(expInsertI64, int64_t)
1136 IMPL_EXPINSERT(expInsertI32, int32_t)
1137 IMPL_EXPINSERT(expInsertI16, int16_t)
1138 IMPL_EXPINSERT(expInsertI8, int8_t)
1139 
1140 #undef CASE
1141 #undef IMPL_SPARSEVALUES
1142 #undef IMPL_GETOVERHEAD
1143 #undef IMPL_ADDELT
1144 #undef IMPL_GETNEXT
1145 #undef IMPL_LEXINSERT
1146 #undef IMPL_EXPINSERT
1147 
1148 /// Output a sparse tensor, one per value type.
1149 void outSparseTensorF64(void *tensor, void *dest, bool sort) {
1150   return outSparseTensor<double>(tensor, dest, sort);
1151 }
1152 void outSparseTensorF32(void *tensor, void *dest, bool sort) {
1153   return outSparseTensor<float>(tensor, dest, sort);
1154 }
1155 void outSparseTensorI64(void *tensor, void *dest, bool sort) {
1156   return outSparseTensor<int64_t>(tensor, dest, sort);
1157 }
1158 void outSparseTensorI32(void *tensor, void *dest, bool sort) {
1159   return outSparseTensor<int32_t>(tensor, dest, sort);
1160 }
1161 void outSparseTensorI16(void *tensor, void *dest, bool sort) {
1162   return outSparseTensor<int16_t>(tensor, dest, sort);
1163 }
1164 void outSparseTensorI8(void *tensor, void *dest, bool sort) {
1165   return outSparseTensor<int8_t>(tensor, dest, sort);
1166 }
1167 
1168 //===----------------------------------------------------------------------===//
1169 //
1170 // Public API with methods that accept C-style data structures to interact
1171 // with sparse tensors, which are only visible as opaque pointers externally.
1172 // These methods can be used both by MLIR compiler-generated code as well as by
1173 // an external runtime that wants to interact with MLIR compiler-generated code.
1174 //
1175 //===----------------------------------------------------------------------===//
1176 
1177 /// Helper method to read a sparse tensor filename from the environment,
1178 /// defined with the naming convention ${TENSOR0}, ${TENSOR1}, etc.
1179 char *getTensorFilename(index_type id) {
1180   char var[80];
1181   sprintf(var, "TENSOR%" PRIu64, id);
1182   char *env = getenv(var);
1183   return env;
1184 }
1185 
1186 /// Returns size of sparse tensor in given dimension.
1187 index_type sparseDimSize(void *tensor, index_type d) {
1188   return static_cast<SparseTensorStorageBase *>(tensor)->getDimSize(d);
1189 }
1190 
1191 /// Finalizes lexicographic insertions.
1192 void endInsert(void *tensor) {
1193   return static_cast<SparseTensorStorageBase *>(tensor)->endInsert();
1194 }
1195 
1196 /// Releases sparse tensor storage.
1197 void delSparseTensor(void *tensor) {
1198   delete static_cast<SparseTensorStorageBase *>(tensor);
1199 }
1200 
1201 /// Initializes sparse tensor from a COO-flavored format expressed using C-style
1202 /// data structures. The expected parameters are:
1203 ///
1204 ///   rank:    rank of tensor
1205 ///   nse:     number of specified elements (usually the nonzeros)
1206 ///   shape:   array with dimension size for each rank
1207 ///   values:  a "nse" array with values for all specified elements
1208 ///   indices: a flat "nse x rank" array with indices for all specified elements
1209 ///   perm:    the permutation of the dimensions in the storage
1210 ///   sparse:  the sparsity for the dimensions
1211 ///
1212 /// For example, the sparse matrix
1213 ///     | 1.0 0.0 0.0 |
1214 ///     | 0.0 5.0 3.0 |
1215 /// can be passed as
1216 ///      rank    = 2
1217 ///      nse     = 3
1218 ///      shape   = [2, 3]
1219 ///      values  = [1.0, 5.0, 3.0]
1220 ///      indices = [ 0, 0,  1, 1,  1, 2]
1221 //
1222 // TODO: generalize beyond 64-bit indices.
1223 //
1224 void *convertToMLIRSparseTensorF64(uint64_t rank, uint64_t nse, uint64_t *shape,
1225                                    double *values, uint64_t *indices,
1226                                    uint64_t *perm, uint8_t *sparse) {
1227   return toMLIRSparseTensor<double>(rank, nse, shape, values, indices, perm,
1228                                     sparse);
1229 }
1230 void *convertToMLIRSparseTensorF32(uint64_t rank, uint64_t nse, uint64_t *shape,
1231                                    float *values, uint64_t *indices,
1232                                    uint64_t *perm, uint8_t *sparse) {
1233   return toMLIRSparseTensor<float>(rank, nse, shape, values, indices, perm,
1234                                    sparse);
1235 }
1236 
1237 /// Converts a sparse tensor to COO-flavored format expressed using C-style
1238 /// data structures. The expected output parameters are pointers for these
1239 /// values:
1240 ///
1241 ///   rank:    rank of tensor
1242 ///   nse:     number of specified elements (usually the nonzeros)
1243 ///   shape:   array with dimension size for each rank
1244 ///   values:  a "nse" array with values for all specified elements
1245 ///   indices: a flat "nse x rank" array with indices for all specified elements
1246 ///
1247 /// The input is a pointer to SparseTensorStorage<P, I, V>, typically returned
1248 /// from convertToMLIRSparseTensor.
1249 ///
1250 //  TODO: Currently, values are copied from SparseTensorStorage to
1251 //  SparseTensorCOO, then to the output. We may want to reduce the number of
1252 //  copies.
1253 //
1254 // TODO: generalize beyond 64-bit indices, no dim ordering, all dimensions
1255 // compressed
1256 //
1257 void convertFromMLIRSparseTensorF64(void *tensor, uint64_t *pRank,
1258                                     uint64_t *pNse, uint64_t **pShape,
1259                                     double **pValues, uint64_t **pIndices) {
1260   fromMLIRSparseTensor<double>(tensor, pRank, pNse, pShape, pValues, pIndices);
1261 }
1262 void convertFromMLIRSparseTensorF32(void *tensor, uint64_t *pRank,
1263                                     uint64_t *pNse, uint64_t **pShape,
1264                                     float **pValues, uint64_t **pIndices) {
1265   fromMLIRSparseTensor<float>(tensor, pRank, pNse, pShape, pValues, pIndices);
1266 }
1267 
1268 } // extern "C"
1269 
1270 #endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
1271