Skip to content

Commit

Permalink
[mlir][sparse] Rename SparseTensorFile to SparseTensorReader.
Browse files Browse the repository at this point in the history
This is to prepare for adding SparseTensorWriter.

Reviewed By: wrengr

Differential Revision: https://reviews.llvm.org/D135477
  • Loading branch information
bixia1 committed Oct 10, 2022
1 parent 80e49f4 commit 461c461
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions mlir/include/mlir/ExecutionEngine/SparseTensor/File.h
Expand Up @@ -42,7 +42,7 @@ namespace sparse_tensor {

/// 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`.
Expand All @@ -56,18 +56,18 @@ class SparseTensorFile final {
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();
Expand Down Expand Up @@ -194,7 +194,7 @@ template <typename V>
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.
Expand Down
18 changes: 9 additions & 9 deletions mlir/lib/ExecutionEngine/SparseTensor/File.cpp
Expand Up @@ -31,7 +31,7 @@
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");
Expand All @@ -40,7 +40,7 @@ void SparseTensorFile::openFile() {
}

/// Closes the file.
void SparseTensorFile::closeFile() {
void SparseTensorReader::closeFile() {
if (file) {
fclose(file);
file = nullptr;
Expand All @@ -55,14 +55,14 @@ void SparseTensorFile::closeFile() {
// `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();
Expand All @@ -75,15 +75,15 @@ void SparseTensorFile::readHeader() {

/// 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()");
Expand Down Expand Up @@ -129,7 +129,7 @@ static inline bool strne(const char *lhs, const char *rhs) {
}

/// 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];
Expand Down Expand Up @@ -181,7 +181,7 @@ void SparseTensorFile::readMMEHeader() {
/// 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();
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp
Expand Up @@ -100,7 +100,7 @@ MLIR_SPARSETENSOR_FOREVERY_V(IMPL_EXPINSERT)

#undef FATAL_PIV

// TODO: try to unify this with `SparseTensorFile::assertMatchesShape`
// TODO: try to unify this with `SparseTensorReader::assertMatchesShape`
// (which is used by `openSparseTensorCOO`). It's easy enough to resolve
// the `std::vector` vs pointer mismatch for `dimSizes`; but it's trickier
// to resolve the presence/absence of `perm` (without introducing extra
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/ExecutionEngine/SparseTensorUtils.cpp
Expand Up @@ -501,7 +501,7 @@ char *getTensorFilename(index_type id) {

void readSparseTensorShape(char *filename, std::vector<uint64_t> *out) {
assert(out && "Received nullptr for out-parameter");
SparseTensorFile stfile(filename);
SparseTensorReader stfile(filename);
stfile.openFile();
stfile.readHeader();
stfile.closeFile();
Expand Down

0 comments on commit 461c461

Please sign in to comment.