/// This class abstracts over the information stored in file headers,
/// as well as providing the buffers and methods for parsing those headers.
-class SparseTensorFile final {
+class SparseTensorReader final {
public:
enum class ValueKind : uint8_t {
// The value before calling `readHeader`.
kUndefined = 5
};
- explicit SparseTensorFile(const char *filename) : filename(filename) {
+ explicit SparseTensorReader(const char *filename) : filename(filename) {
assert(filename && "Received nullptr for filename");
}
// Disallows copying, to avoid duplicating the `file` pointer.
- SparseTensorFile(const SparseTensorFile &) = delete;
- SparseTensorFile &operator=(const SparseTensorFile &) = delete;
+ SparseTensorReader(const SparseTensorReader &) = delete;
+ SparseTensorReader &operator=(const SparseTensorReader &) = delete;
// This dtor tries to avoid leaking the `file`. (Though it's better
// to call `closeFile` explicitly when possible, since there are
// circumstances where dtors are not called reliably.)
- ~SparseTensorFile() { closeFile(); }
+ ~SparseTensorReader() { closeFile(); }
/// Opens the file for reading.
void openFile();
inline SparseTensorCOO<V> *
openSparseTensorCOO(const char *filename, uint64_t rank, const uint64_t *shape,
const uint64_t *perm, PrimaryType valTp) {
- SparseTensorFile stfile(filename);
+ SparseTensorReader stfile(filename);
stfile.openFile();
stfile.readHeader();
// Check tensor element type against the value type in the input file.
using namespace mlir::sparse_tensor;
/// Opens the file for reading.
-void SparseTensorFile::openFile() {
+void SparseTensorReader::openFile() {
if (file)
MLIR_SPARSETENSOR_FATAL("Already opened file %s\n", filename);
file = fopen(filename, "r");
}
/// Closes the file.
-void SparseTensorFile::closeFile() {
+void SparseTensorReader::closeFile() {
if (file) {
fclose(file);
file = nullptr;
// `char const* *restrict`).
//
/// Attempts to read a line from the file.
-char *SparseTensorFile::readLine() {
+char *SparseTensorReader::readLine() {
if (fgets(line, kColWidth, file))
return line;
MLIR_SPARSETENSOR_FATAL("Cannot read next line of %s\n", filename);
}
/// Reads and parses the file's header.
-void SparseTensorFile::readHeader() {
+void SparseTensorReader::readHeader() {
assert(file && "Attempt to readHeader() before openFile()");
if (strstr(filename, ".mtx"))
readMMEHeader();
/// Asserts the shape subsumes the actual dimension sizes. Is only
/// valid after parsing the header.
-void SparseTensorFile::assertMatchesShape(uint64_t rank,
- const uint64_t *shape) const {
+void SparseTensorReader::assertMatchesShape(uint64_t rank,
+ const uint64_t *shape) const {
assert(rank == getRank() && "Rank mismatch");
for (uint64_t r = 0; r < rank; ++r)
assert((shape[r] == 0 || shape[r] == idata[2 + r]) &&
"Dimension size mismatch");
}
-bool SparseTensorFile::canReadAs(PrimaryType valTy) const {
+bool SparseTensorReader::canReadAs(PrimaryType valTy) const {
switch (valueKind_) {
case ValueKind::kInvalid:
assert(false && "Must readHeader() before calling canReadAs()");
}
/// Read the MME header of a general sparse matrix of type real.
-void SparseTensorFile::readMMEHeader() {
+void SparseTensorReader::readMMEHeader() {
char header[64];
char object[64];
char format[64];
/// format, we assume that the file starts with optional comments followed
/// by two lines that define the rank, the number of nonzeros, and the
/// dimensions sizes (one per rank) of the sparse tensor.
-void SparseTensorFile::readExtFROSTTHeader() {
+void SparseTensorReader::readExtFROSTTHeader() {
// Skip comments.
while (true) {
readLine();