MLIR 22.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
24using namespace mlir::sparse_tensor;
25
26extern "C" {
27
28//===----------------------------------------------------------------------===//
29//
30// Public functions which operate on MLIR buffers (memrefs) to interact
31// with sparse tensors (which are only visible as opaque pointers externally).
32// Because these functions deal with memrefs, they should only be used
33// by MLIR compiler-generated code (or code that is in sync with MLIR).
34//
35//===----------------------------------------------------------------------===//
36
37/// This is the "swiss army knife" method for materializing sparse
38/// tensors into the computation. The types of the `ptr` argument and
39/// the result depend on the action, as explained in the following table,
40/// where "STS" means a sparse-tensor-storage object.
41///
42/// Action: `ptr`: Returns:
43/// ---------------------------------------------------------------------------
44/// kEmpty - STS, empty
45/// kFromReader reader STS, input from reader
46/// kPack buffers STS, from level buffers
47/// kSortCOOInPlace STS STS, sorted in place
54 OverheadType crdTp, PrimaryType valTp, Action action, void *ptr);
55
56/// Tensor-storage method to obtain direct access to the values array.
57#define DECL_SPARSEVALUES(VNAME, V) \
58 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseValues##VNAME( \
59 StridedMemRefType<V, 1> *out, void *tensor);
61#undef DECL_SPARSEVALUES
62
63/// Tensor-storage method to obtain direct access to the positions array
64/// for the given level.
65#define DECL_SPARSEPOSITIONS(PNAME, P) \
66 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparsePositions##PNAME( \
67 StridedMemRefType<P, 1> *out, void *tensor, index_type lvl);
69#undef DECL_SPARSEPOSITIONS
70
71/// Tensor-storage method to obtain direct access to the coordinates array
72/// for the given level.
73#define DECL_SPARSECOORDINATES(CNAME, C) \
74 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseCoordinates##CNAME( \
75 StridedMemRefType<C, 1> *out, void *tensor, index_type lvl);
77#undef DECL_SPARSECOORDINATES
78
79/// Tensor-storage method to obtain direct access to the coordinates array
80/// buffer for the given level (provides an AoS view into the library).
81#define DECL_SPARSECOORDINATES(CNAME, C) \
82 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_sparseCoordinatesBuffer##CNAME( \
83 StridedMemRefType<C, 1> *out, void *tensor, index_type lvl);
85#undef DECL_SPARSECOORDINATES
86
87/// Tensor-storage method to insert elements in lexicographical
88/// level-coordinate order.
89#define DECL_LEXINSERT(VNAME, V) \
90 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_lexInsert##VNAME( \
91 void *tensor, StridedMemRefType<index_type, 1> *lvlCoordsRef, \
92 StridedMemRefType<V, 0> *vref);
94#undef DECL_LEXINSERT
95
96/// Tensor-storage method to insert using expansion.
97#define DECL_EXPINSERT(VNAME, V) \
98 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_expInsert##VNAME( \
99 void *tensor, StridedMemRefType<index_type, 1> *lvlCoordsRef, \
100 StridedMemRefType<V, 1> *vref, StridedMemRefType<bool, 1> *fref, \
101 StridedMemRefType<index_type, 1> *aref, index_type count);
103#undef DECL_EXPINSERT
104
105/// Constructs a new SparseTensorReader object, opens the file, reads the
106/// header, and validates that the actual contents of the file match
107/// the expected `dimShapeRef` and `valTp`.
109 char *filename, StridedMemRefType<index_type, 1> *dimShapeRef,
110 PrimaryType valTp);
111
112/// SparseTensorReader method to obtain direct access to the
113/// dimension-sizes array.
116
117/// Reads the sparse tensor, stores the coordinates and values to the given
118/// memrefs of a COO in AoS format. Returns a boolean to indicate whether
119/// the COO elements are sorted.
120#define DECL_READTOBUFFERS(VNAME, V, CNAME, C) \
121 MLIR_CRUNNERUTILS_EXPORT bool \
122 _mlir_ciface_getSparseTensorReaderReadToBuffers##CNAME##VNAME( \
123 void *p, StridedMemRefType<index_type, 1> *dim2lvlRef, \
124 StridedMemRefType<index_type, 1> *lvl2dimRef, \
125 StridedMemRefType<C, 1> *cref, StridedMemRefType<V, 1> *vref) \
126 MLIR_SPARSETENSOR_FOREVERY_V_O(DECL_READTOBUFFERS)
127#undef DECL_READTOBUFFERS
128
129/// Outputs the sparse tensor dim-rank, nse, and dim-shape.
131 void *p, index_type dimRank, index_type nse,
133
134/// Outputs an element for the sparse tensor.
135#define DECL_OUTNEXT(VNAME, V) \
136 MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_outSparseTensorWriterNext##VNAME( \
137 void *p, index_type dimRank, \
138 StridedMemRefType<index_type, 1> *dimCoordsRef, \
139 StridedMemRefType<V, 0> *vref);
141#undef DECL_OUTNEXT
142
143//===----------------------------------------------------------------------===//
144//
145// Public functions which accept only C-style data structures to interact
146// with sparse tensors (which are only visible as opaque pointers externally).
147// These functions can be used both by MLIR compiler-generated code
148// as well as by any external runtime that wants to interact with MLIR
149// compiler-generated code.
150//
151//===----------------------------------------------------------------------===//
152
153/// Tensor-storage method to get the size of the given level.
155
156/// Tensor-storage method to get the size of the given dimension.
158
159/// Tensor-storage method to finalize lexicographic insertions.
161
162/// Releases the memory for the tensor-storage object.
164
165/// Helper function to read a sparse tensor filename from the environment,
166/// defined with the naming convention ${TENSOR0}, ${TENSOR1}, etc.
168
169/// Returns the number of stored elements for the sparse tensor being read.
171
172/// Releases the SparseTensorReader and closes the associated file.
174
175/// Creates a SparseTensorWriter for outputting a sparse tensor to a file
176/// with the given file name. When the file name is empty, std::cout is used.
177/// Only the extended FROSTT format is supported currently.
179
180/// Finalizes the outputing of a sparse tensor to a file and releases the
181/// SparseTensorWriter.
183
184} // extern "C"
185
186#endif // MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H
#define MLIR_CRUNNERUTILS_EXPORT
#define MLIR_SPARSETENSOR_FOREVERY_O(DO)
Definition Enums.h:71
#define MLIR_SPARSETENSOR_FOREVERY_V(DO)
Definition Enums.h:96
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 ...
MLIR_CRUNNERUTILS_EXPORT void * createSparseTensorWriter(char *filename)
Creates a SparseTensorWriter for outputting a sparse tensor to a file with the given file name.
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.
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.
#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.
#define DECL_LEXINSERT(VNAME, V)
Element-wise insertion in lexicographic coordinate order.
Definition Storage.h:159
#define DECL_EXPINSERT(VNAME, V)
Expanded insertion.
Definition Storage.h:166
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.