io matrix to csv

This commit is contained in:
kai
2025-05-31 15:04:04 +02:00
parent 12b5849d8b
commit 657ad92ae9
2 changed files with 13 additions and 13 deletions

View File

@@ -156,7 +156,7 @@ int main(int argc, char *argv[]) {
// Result is saved to file.
// Use this to graphically verify the correctness of the parallel
// implementation.
// runSerial(n, eps, maxNumIter);
runSerial(n, eps, maxNumIter);
runParallel(n, eps, maxNumIter);

View File

@@ -1,16 +1,16 @@
#include "matrix_io.h"
#include "matrix.h"
#include <exception>
#include <fstream>
#include <mpi.h>
#include <exception>
#include "matrix.h"
#include "matrix_io.h"
MatrixIO::MatrixIO() {
MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
MPI_Comm_size(MPI_COMM_WORLD, &numProc_);
}
void MatrixIO::saveDistributed(const Matrix& distributedMatrix,
const std::string& filename) {
void MatrixIO::saveDistributed(const Matrix &distributedMatrix,
const std::string &filename) {
Matrix mat = gatherMatrixOnRoot(distributedMatrix);
if (rank_ == 0) {
@@ -18,20 +18,20 @@ void MatrixIO::saveDistributed(const Matrix& distributedMatrix,
}
}
void MatrixIO::saveSerial(const Matrix& m, const std::string& filename) {
void MatrixIO::saveSerial(const Matrix &m, const std::string &filename) {
std::ofstream fout(filename);
const int numRows = m.rows();
const int numCols = m.cols();
fout << "# " << numRows << "\t" << numCols << "\n";
// fout << numRows << "," << numCols << "\n";
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
fout << m(i, j) << "\t";
fout << m(i, j) << ",";
}
fout << "\n";
}
}
Matrix MatrixIO::loadDistributed(const std::string& filename) {
Matrix MatrixIO::loadDistributed(const std::string &filename) {
Matrix matrixOnRoot = Matrix::zeros(0, 0);
if (rank_ == 0) {
matrixOnRoot = loadSerial(filename);
@@ -41,7 +41,7 @@ Matrix MatrixIO::loadDistributed(const std::string& filename) {
return distributedMatrix;
}
Matrix MatrixIO::loadSerial(const std::string& filename) {
Matrix MatrixIO::loadSerial(const std::string &filename) {
std::ifstream fin(filename);
// Check first character (has to be #)
@@ -66,7 +66,7 @@ Matrix MatrixIO::loadSerial(const std::string& filename) {
return mat;
}
Matrix MatrixIO::gatherMatrixOnRoot(const Matrix& distributedMatrix) {
Matrix MatrixIO::gatherMatrixOnRoot(const Matrix &distributedMatrix) {
const int numRowsLocal = distributedMatrix.rows();
const int numCols = distributedMatrix.cols();
const int numRowsTotal = numRowsLocal * numProc_;
@@ -84,7 +84,7 @@ Matrix MatrixIO::gatherMatrixOnRoot(const Matrix& distributedMatrix) {
return matrixOnRoot;
}
Matrix MatrixIO::scatterMatrixFromRoot(const Matrix& matrixOnRoot) {
Matrix MatrixIO::scatterMatrixFromRoot(const Matrix &matrixOnRoot) {
int numRowsTotal = matrixOnRoot.rows();
int numCols = matrixOnRoot.cols();