-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DAPHNE-#822] Added MatrixMarket write support #832
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
#ifndef WRITE_MM_H | ||
#define WRITE_MM_H | ||
|
||
#include <runtime/local/datastructures/CSRMatrix.h> | ||
#include <runtime/local/datastructures/DenseMatrix.h> | ||
#include <runtime/local/datastructures/Frame.h> | ||
#include <runtime/local/io/MMFile.h> | ||
|
||
#include <cstdio> | ||
#include <cstring> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
template <class DTArg> struct WriteMM { | ||
static void apply(const DTArg *arg, const char *filename) = delete; | ||
}; | ||
|
||
// Convenience function | ||
template <class DTArg> void writeMM(const DTArg *arg, const char *filename) { WriteMM<DTArg>::apply(arg, filename); } | ||
|
||
// ---------------------------------------------------------------------------- | ||
// DenseMatrix | ||
// ---------------------------------------------------------------------------- | ||
|
||
template <typename VT> struct WriteMM<DenseMatrix<VT>> { | ||
static void apply(const DenseMatrix<VT> *arg, const char *filename) { | ||
const char *format = MM_DENSE_STR; | ||
std::ofstream f(filename); | ||
if (!f.is_open()) { | ||
throw std::runtime_error("WriteMM::apply: Cannot open file"); | ||
} | ||
|
||
const char *field; | ||
if (std::is_integral<VT>::value) { | ||
field = MM_INT_STR; | ||
} else if (std::is_floating_point<VT>::value) { | ||
field = MM_REAL_STR; | ||
} else { | ||
throw std::runtime_error("WriteMM::apply: Unsupported data type"); | ||
} | ||
|
||
const char *symmetry = MM_GENERAL_STR; | ||
if (isSymmetric(arg)) { | ||
symmetry = MM_SYMM_STR; | ||
} else if (isSkewSymmetric(arg)) { | ||
symmetry = MM_SKEW_STR; | ||
} | ||
|
||
f << MatrixMarketBanner << " matrix " << format << " " << field << " " << symmetry << std::endl; | ||
|
||
size_t rows = arg->getNumRows(); | ||
size_t cols = arg->getNumCols(); | ||
f << rows << " " << cols << std::endl; | ||
|
||
const VT *values = arg->getValues(); | ||
if (!values) { | ||
throw std::runtime_error("WriteMM::apply: Null pointer for 'values' in DenseMatrix"); | ||
} | ||
|
||
if (strcmp(symmetry, MM_GENERAL_STR) == 0) { | ||
for (size_t i = 0; i < cols; ++i) { | ||
for (size_t j = 0; j < rows; ++j) { | ||
size_t idx = j * cols + i; | ||
if (strcmp(field, MM_REAL_STR) == 0) | ||
f << std::scientific << std::setprecision(13) << values[idx] << std::endl; | ||
else | ||
f << values[idx] << std::endl; | ||
} | ||
} | ||
} else if (strcmp(symmetry, MM_SYMM_STR) == 0) { | ||
for (size_t i = 0; i < cols; ++i) { | ||
for (size_t j = i; j < rows; ++j) { | ||
size_t idx = j * cols + i; | ||
if (strcmp(field, MM_REAL_STR) == 0) | ||
f << std::scientific << std::setprecision(13) << values[idx] << std::endl; | ||
else | ||
f << values[idx] << std::endl; | ||
} | ||
} | ||
} else if (strcmp(symmetry, MM_SKEW_STR) == 0) { | ||
for (size_t i = 0; i < cols; ++i) { | ||
for (size_t j = i + 1; j < rows; ++j) { | ||
size_t idx = j * cols + i; | ||
if (strcmp(field, MM_REAL_STR) == 0) | ||
f << std::scientific << std::setprecision(13) << values[idx] << std::endl; | ||
else | ||
f << values[idx] << std::endl; | ||
} | ||
} | ||
} else { | ||
throw std::runtime_error("WriteMM::apply: Unsupported symmetry type"); | ||
} | ||
f.close(); | ||
} | ||
|
||
private: | ||
static bool isSymmetric(const DenseMatrix<VT> *arg) { | ||
size_t rows = arg->getNumRows(); | ||
size_t cols = arg->getNumCols(); | ||
if (rows != cols) | ||
return false; | ||
const VT *values = arg->getValues(); | ||
for (size_t i = 0; i < cols; ++i) { | ||
for (size_t j = i + 1; j < rows; ++j) { | ||
size_t idx1 = j + i * rows; | ||
size_t idx2 = i + j * rows; | ||
if (values[idx1] != values[idx2]) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
static bool isSkewSymmetric(const DenseMatrix<VT> *arg) { | ||
size_t rows = arg->getNumRows(); | ||
size_t cols = arg->getNumCols(); | ||
if (rows != cols) | ||
return false; | ||
const VT *values = arg->getValues(); | ||
for (size_t i = 0; i < rows; ++i) { | ||
size_t idx_diag = i + i * rows; | ||
if (values[idx_diag] != 0) | ||
return false; | ||
} | ||
for (size_t i = 0; i < cols; ++i) { | ||
for (size_t j = i + 1; j < rows; ++j) { | ||
size_t idx1 = j + i * rows; | ||
size_t idx2 = i + j * rows; | ||
if (values[idx1] != -values[idx2]) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// CSRMatrix | ||
// ---------------------------------------------------------------------------- | ||
|
||
template <typename VT> struct WriteMM<CSRMatrix<VT>> { | ||
static void apply(const CSRMatrix<VT> *arg, const char *filename) { | ||
const char *format = MM_SPARSE_STR; | ||
std::ofstream f(filename); | ||
if (!f.is_open()) { | ||
throw std::runtime_error("WriteMM::apply: Cannot open file"); | ||
} | ||
|
||
const char *field; | ||
if (std::is_integral<VT>::value) { | ||
field = MM_INT_STR; | ||
} else if (std::is_floating_point<VT>::value) { | ||
field = MM_REAL_STR; | ||
} else { | ||
throw std::runtime_error("WriteMM::apply: Unsupported data type"); | ||
} | ||
|
||
const char *symmetry = MM_GENERAL_STR; | ||
|
||
f << MatrixMarketBanner << " matrix " << format << " " << field << " " << symmetry << std::endl; | ||
|
||
size_t rows = arg->getNumRows(); | ||
size_t cols = arg->getNumCols(); | ||
size_t nnz = countNNZ(arg, symmetry); | ||
|
||
f << rows << " " << cols << " " << nnz << std::endl; | ||
|
||
const size_t *rowOffsets = arg->getRowOffsets(); | ||
const size_t *colIdxs = arg->getColIdxs(); | ||
const VT *values = arg->getValues(); | ||
|
||
std::vector<std::vector<std::pair<size_t, VT>>> colEntries(cols); | ||
|
||
for (size_t i = 0; i < rows; ++i) { | ||
for (size_t idx = rowOffsets[i]; idx < rowOffsets[i + 1]; ++idx) { | ||
size_t j = colIdxs[idx]; | ||
VT val = values[idx]; | ||
colEntries[j].emplace_back(i, val); | ||
} | ||
} | ||
|
||
if (strcmp(symmetry, MM_GENERAL_STR) == 0) { | ||
for (size_t j = 0; j < cols; ++j) { | ||
for (const auto &entry : colEntries[j]) { | ||
size_t i = entry.first; | ||
VT val = entry.second; | ||
if (strcmp(field, MM_REAL_STR) == 0) { | ||
if (val >= 0) { | ||
f << i + 1 << " " << j + 1 << " " << std::scientific << std::setprecision(13) << val | ||
<< std::endl; | ||
} else { | ||
f << i + 1 << " " << j + 1 << " " << std::scientific << std::setprecision(13) << val | ||
<< std::endl; | ||
} | ||
} else { | ||
f << i + 1 << " " << j + 1 << " " << val << std::endl; | ||
} | ||
} | ||
} | ||
} else { | ||
throw std::runtime_error("WriteMM::apply: Unsupported symmetry type"); | ||
} | ||
|
||
f.close(); | ||
} | ||
|
||
private: | ||
static size_t countNNZ(const CSRMatrix<VT> *arg, const char *symmetry) { | ||
size_t nnz = 0; | ||
size_t rows = arg->getNumRows(); | ||
size_t cols = arg->getNumCols(); | ||
|
||
std::vector<std::vector<std::pair<size_t, VT>>> colEntries(cols); | ||
|
||
const size_t *rowOffsets = arg->getRowOffsets(); | ||
const size_t *colIdxs = arg->getColIdxs(); | ||
const VT *values = arg->getValues(); | ||
|
||
for (size_t i = 0; i < rows; ++i) { | ||
for (size_t idx = rowOffsets[i]; idx < rowOffsets[i + 1]; ++idx) { | ||
size_t j = colIdxs[idx]; | ||
colEntries[j].emplace_back(i, values[idx]); | ||
} | ||
} | ||
|
||
if (strcmp(symmetry, MM_GENERAL_STR) == 0) { | ||
nnz = arg->getNumNonZeros(); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I don't fully understand why the computation above is needed if you return getNumNonZeros or fail. |
||
throw std::runtime_error("WriteMM::apply: Unsupported symmetry type"); | ||
} | ||
|
||
return nnz; | ||
} | ||
}; | ||
|
||
#endif // WRITE_MM_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,49 @@ | |
#include <catch.hpp> | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
|
||
const std::string dirPath = "test/api/cli/io/"; | ||
const std::string dirPath2 = "test/runtime/local/io/"; | ||
|
||
bool compareFiles(const std::string &filePath1, const std::string &filePath2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this one could go into Utils.h? Ideally with a documenting comment (doxygen format) above the function signature ;-) |
||
|
||
std::ifstream file1(filePath1, std::ios::binary); | ||
std::ifstream file2(filePath2, std::ios::binary); | ||
|
||
if (!file1.is_open() || !file2.is_open()) { | ||
std::cerr << "Cannot open one or both files." << std::endl; | ||
return false; | ||
} | ||
|
||
std::string line1, line2; | ||
bool filesAreEqual = true; | ||
|
||
while (std::getline(file1, line1)) { | ||
if (!std::getline(file2, line2)) { | ||
filesAreEqual = false; | ||
break; | ||
} | ||
|
||
if (line1 != line2) { | ||
filesAreEqual = false; | ||
break; | ||
} | ||
} | ||
|
||
if (filesAreEqual && std::getline(file2, line2)) { | ||
if (!line2.empty()) { | ||
filesAreEqual = false; | ||
} | ||
} | ||
|
||
file1.close(); | ||
file2.close(); | ||
|
||
return filesAreEqual; | ||
} | ||
|
||
TEST_CASE("writeMatrixCSV_Full", TAG_IO) { | ||
std::string csvPath = dirPath + "matrix_full.csv"; | ||
|
@@ -41,4 +81,31 @@ TEST_CASE("writeMatrixCSV_View", TAG_IO) { | |
std::string("outPath=\"" + csvPath + "\"").c_str()); | ||
compareDaphneToRef(dirPath + "matrix_view_ref.csv", dirPath + "readMatrix.daphne", "--args", | ||
std::string("inPath=\"" + csvPath + "\"").c_str()); | ||
} | ||
|
||
TEST_CASE("writeMatrixMtxaig", TAG_IO) { | ||
std::string expectedPath = dirPath2 + "aig.mtx"; | ||
std::string resultPath = "out.mtx"; | ||
checkDaphneStatusCode(StatusCode::SUCCESS, dirPath + "readAndWriteMtx.daphne", "--args", | ||
std::string("inPath=\"" + expectedPath + "\"").c_str()); | ||
CHECK(compareFiles(expectedPath, resultPath)); | ||
std::filesystem::remove(resultPath); // remove old file if it still exists | ||
} | ||
|
||
TEST_CASE("writeMatrixMtxaik", TAG_IO) { | ||
std::string expectedPath = dirPath2 + "aik.mtx"; | ||
std::string resultPath = "out.mtx"; | ||
checkDaphneStatusCode(StatusCode::SUCCESS, dirPath + "readAndWriteMtx.daphne", "--args", | ||
std::string("inPath=\"" + expectedPath + "\"").c_str()); | ||
CHECK(compareFiles(expectedPath, resultPath)); | ||
std::filesystem::remove(resultPath); // remove old file if it still exists | ||
} | ||
|
||
TEST_CASE("writeMatrixMtxais", TAG_IO) { | ||
std::string expectedPath = dirPath2 + "ais.mtx"; | ||
std::string resultPath = "out.mtx"; | ||
checkDaphneStatusCode(StatusCode::SUCCESS, dirPath + "readAndWriteMtx.daphne", "--args", | ||
std::string("inPath=\"" + expectedPath + "\"").c_str()); | ||
CHECK(compareFiles(expectedPath, resultPath)); | ||
std::filesystem::remove(resultPath); // remove old file if it still exists | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
X = readMatrix($inPath); | ||
print(X); | ||
write(X, "out.mtx"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this value be a candidate for an item in our user configuration?