MLIR  19.0.0git
SparseTensorRuntime.h
Go to the documentation of this file.
1 //===- SparseTensorRuntime.h - SparseTensor runtime support lib -*- C++ -*-===//
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 header file provides the functions which comprise the public API of the
10 // sparse tensor runtime support library for the SparseTensor dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H
15 #define MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H
16 
20 
21 #include <cinttypes>
22 #include <complex>
23 #include <vector>
24 
25 using namespace mlir::sparse_tensor;
26 
27 extern "C" {
28 
29 //===----------------------------------------------------------------------===//
30 //
31 // Public functions which operate on MLIR buffers (memrefs) to interact
32 // with sparse tensors (which are only visible as opaque pointers externally).
33 // Because these functions deal with memrefs, they should only be used
34 // by MLIR compiler-generated code (or code that is in sync with MLIR).
35 //
36 //===----------------------------------------------------------------------===//
37 
38 /// This is the "swiss army knife" method for materializing sparse
39 /// tensors into the computation. The types of the `ptr` argument and
40 /// the result depend on the action, as explained in the following table,
41 /// where "STS" means a sparse-tensor-storage object.
42 ///
43 /// Action: `ptr`: Returns:
44 /// ---------------------------------------------------------------------------
45 /// kEmpty - STS, empty
46 /// kFromReader reader STS, input from reader
47 /// kPack buffers STS, from level buffers
48 /// kSortCOOInPlace STS STS, sorted in place
55  OverheadType crdTp, PrimaryType valTp, Action action, void *ptr);
56 
57 /// Tensor-storage method to obtain direct access to the values array.
58 #define DECL_SPARSEVALUES(VNAME, V) \
59  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseValues##VNAME( \
60  StridedMemRefType<V, 1> *out, void *tensor);
62 #undef DECL_SPARSEVALUES
63 
64 /// Tensor-storage method to obtain direct access to the positions array
65 /// for the given level.
66 #define DECL_SPARSEPOSITIONS(PNAME, P) \
67  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparsePositions##PNAME( \
68  StridedMemRefType<P, 1> *out, void *tensor, index_type lvl);
70 #undef DECL_SPARSEPOSITIONS
71 
72 /// Tensor-storage method to obtain direct access to the coordinates array
73 /// for the given level.
74 #define DECL_SPARSECOORDINATES(CNAME, C) \
75  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseCoordinates##CNAME( \
76  StridedMemRefType<C, 1> *out, void *tensor, index_type lvl);
78 #undef DECL_SPARSECOORDINATES
79 
80 /// Tensor-storage method to obtain direct access to the coordinates array
81 /// buffer for the given level (provides an AoS view into the library).
82 #define DECL_SPARSECOORDINATES(CNAME, C) \
83  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseCoordinatesBuffer##CNAME( \
84  StridedMemRefType<C, 1> *out, void *tensor, index_type lvl);
86 #undef DECL_SPARSECOORDINATES
87 
88 /// Tensor-storage method to insert elements in lexicographical
89 /// level-coordinate order.
90 #define DECL_LEXINSERT(VNAME, V) \
91  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_lexInsert##VNAME( \
92  void *tensor, StridedMemRefType<index_type, 1> *lvlCoordsRef, \
93  StridedMemRefType<V, 0> *vref);
95 #undef DECL_LEXINSERT
96 
97 /// Tensor-storage method to insert using expansion.
98 #define DECL_EXPINSERT(VNAME, V) \
99  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_expInsert##VNAME( \
100  void *tensor, StridedMemRefType<index_type, 1> *lvlCoordsRef, \
101  StridedMemRefType<V, 1> *vref, StridedMemRefType<bool, 1> *fref, \
102  StridedMemRefType<index_type, 1> *aref, index_type count);
104 #undef DECL_EXPINSERT
105 
106 /// Constructs a new SparseTensorReader object, opens the file, reads the
107 /// header, and validates that the actual contents of the file match
108 /// the expected `dimShapeRef` and `valTp`.
110  char *filename, StridedMemRefType<index_type, 1> *dimShapeRef,
111  PrimaryType valTp);
112 
113 /// SparseTensorReader method to obtain direct access to the
114 /// dimension-sizes array.
116  StridedMemRefType<index_type, 1> *out, void *p);
117 
118 /// Reads the sparse tensor, stores the coordinates and values to the given
119 /// memrefs of a COO in AoS format. Returns a boolean to indicate whether
120 /// the COO elements are sorted.
121 #define DECL_READTOBUFFERS(VNAME, V, CNAME, C) \
122  MLIR_CRUNNERUTILS_EXPORT bool \
123  _mlir_ciface_getSparseTensorReaderReadToBuffers##CNAME##VNAME( \
124  void *p, StridedMemRefType<index_type, 1> *dim2lvlRef, \
125  StridedMemRefType<index_type, 1> *lvl2dimRef, \
126  StridedMemRefType<C, 1> *cref, StridedMemRefType<V, 1> *vref) \
127  MLIR_SPARSETENSOR_FOREVERY_V_O(DECL_READTOBUFFERS)
128 #undef DECL_READTOBUFFERS
129 
130 /// Outputs the sparse tensor dim-rank, nse, and dim-shape.
132  void *p, index_type dimRank, index_type nse,
133  StridedMemRefType<index_type, 1> *dimSizesRef);
134 
135 /// Outputs an element for the sparse tensor.
136 #define DECL_OUTNEXT(VNAME, V) \
137  MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_outSparseTensorWriterNext##VNAME( \
138  void *p, index_type dimRank, \
139  StridedMemRefType<index_type, 1> *dimCoordsRef, \
140  StridedMemRefType<V, 0> *vref);
142 #undef DECL_OUTNEXT
143 
144 //===----------------------------------------------------------------------===//
145 //
146 // Public functions which accept only C-style data structures to interact
147 // with sparse tensors (which are only visible as opaque pointers externally).
148 // These functions can be used both by MLIR compiler-generated code
149 // as well as by any external runtime that wants to interact with MLIR
150 // compiler-generated code.
151 //
152 //===----------------------------------------------------------------------===//
153 
154 /// Tensor-storage method to get the size of the given level.
156 
157 /// Tensor-storage method to get the size of the given dimension.
159 
160 /// Tensor-storage method to finalize lexicographic insertions.
162 
163 /// Releases the memory for the tensor-storage object.
165 
166 /// Helper function to read a sparse tensor filename from the environment,
167 /// defined with the naming convention ${TENSOR0}, ${TENSOR1}, etc.
169 
170 /// Returns the number of stored elements for the sparse tensor being read.
172 
173 /// Releases the SparseTensorReader and closes the associated file.
175 
176 /// Creates a SparseTensorWriter for outputting a sparse tensor to a file
177 /// with the given file name. When the file name is empty, std::cout is used.
178 /// Only the extended FROSTT format is supported currently.
180 
181 /// Finalizes the outputing of a sparse tensor to a file and releases the
182 /// SparseTensorWriter.
184 
185 } // extern "C"
186 
187 #endif // MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H
#define MLIR_CRUNNERUTILS_EXPORT
Definition: CRunnerUtils.h:31
#define MLIR_SPARSETENSOR_FOREVERY_O(DO)
Definition: Enums.h:71
#define MLIR_SPARSETENSOR_FOREVERY_V(DO)
Definition: Enums.h:96
MLIR_CRUNNERUTILS_EXPORT index_type sparseDimSize(void *tensor, index_type d)
Tensor-storage method to get the size of the given dimension.
MLIR_CRUNNERUTILS_EXPORT index_type sparseLvlSize(void *tensor, index_type l)
Tensor-storage method to get the size of the given level.
MLIR_CRUNNERUTILS_EXPORT void delSparseTensor(void *tensor)
Releases the memory for the tensor-storage object.
#define DECL_LEXINSERT(VNAME, V)
Tensor-storage method to insert elements in lexicographical level-coordinate order.
MLIR_CRUNNERUTILS_EXPORT index_type getSparseTensorReaderNSE(void *p)
Returns the number of stored elements for the sparse tensor being read.
#define DECL_OUTNEXT(VNAME, V)
Outputs an element for the sparse tensor.
MLIR_CRUNNERUTILS_EXPORT void * _mlir_ciface_createCheckedSparseTensorReader(char *filename, StridedMemRefType< index_type, 1 > *dimShapeRef, PrimaryType valTp)
Constructs a new SparseTensorReader object, opens the file, reads the header, and validates that the ...
#define DECL_EXPINSERT(VNAME, V)
Tensor-storage method to insert using expansion.
#define DECL_SPARSEVALUES(VNAME, V)
Tensor-storage method to obtain direct access to the values array.
MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_outSparseTensorWriterMetaData(void *p, index_type dimRank, index_type nse, StridedMemRefType< index_type, 1 > *dimSizesRef)
Outputs the sparse tensor dim-rank, nse, and dim-shape.
MLIR_CRUNNERUTILS_EXPORT void delSparseTensorReader(void *p)
Releases the SparseTensorReader and closes the associated file.
MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_getSparseTensorReaderDimSizes(StridedMemRefType< index_type, 1 > *out, void *p)
SparseTensorReader method to obtain direct access to the dimension-sizes array.
MLIR_CRUNNERUTILS_EXPORT void * _mlir_ciface_newSparseTensor(StridedMemRefType< index_type, 1 > *dimSizesRef, StridedMemRefType< index_type, 1 > *lvlSizesRef, StridedMemRefType< LevelType, 1 > *lvlTypesRef, StridedMemRefType< index_type, 1 > *dim2lvlRef, StridedMemRefType< index_type, 1 > *lvl2dimRef, OverheadType posTp, OverheadType crdTp, PrimaryType valTp, Action action, void *ptr)
This is the "swiss army knife" method for materializing sparse tensors into the computation.
#define DECL_SPARSECOORDINATES(CNAME, C)
Tensor-storage method to obtain direct access to the coordinates array for the given level.
MLIR_CRUNNERUTILS_EXPORT char * getTensorFilename(index_type id)
Helper function to read a sparse tensor filename from the environment, defined with the naming conven...
#define DECL_SPARSEPOSITIONS(PNAME, P)
Tensor-storage method to obtain direct access to the positions array for the given level.
MLIR_CRUNNERUTILS_EXPORT void endLexInsert(void *tensor)
Tensor-storage method to finalize lexicographic insertions.
MLIR_CRUNNERUTILS_EXPORT void delSparseTensorWriter(void *p)
Finalizes the outputing of a sparse tensor to a file and releases the SparseTensorWriter.
MLIR_CRUNNERUTILS_EXPORT void * createSparseTensorWriter(char *filename)
Creates a SparseTensorWriter for outputting a sparse tensor to a file with the given file name.
OverheadType
Encoding of overhead types (both position overhead and coordinate overhead), for "overloading" @newSp...
Definition: Enums.h:51
Action
The actions performed by @newSparseTensor.
Definition: Enums.h:146
PrimaryType
Encoding of the elemental type, for "overloading" @newSparseTensor.
Definition: Enums.h:82
uint64_t index_type
This type is used in the public API at all places where MLIR expects values with the built-in type "i...
Definition: Enums.h:47
StridedMemRef descriptor type with static rank.
Definition: CRunnerUtils.h:131