Template Numerical Library version\ main:8246197
Loading...
Searching...
No Matches
TNL::Matrices::MatrixReader< Matrix > Class Template Reference

Helper class for importing of matrices from different input formats. More...

#include <TNL/Matrices/MatrixReader.h>

Classes

struct  MtxHeader

Public Types

using DeviceType = typename Matrix::DeviceType
 Device where the matrix is allocated.
using IndexType = typename Matrix::IndexType
 Type used for indexing of matrix elements.
using RealType = typename Matrix::RealType
 Type of matrix elements values.

Static Public Member Functions

static bool isSymmetric (const std::string &fileName)
 Check if the MTX file with given filename declares a symmetric matrix.
static bool isSymmetric (std::istream &file)
 Check if the MTX file in STL input stream declares a symmetric matrix.
static void readMtx (const std::string &fileName, Matrix &matrix, bool verbose=false)
 Method for importing matrix from file with given filename.
static void readMtx (const std::string &fileName, Matrix &matrix, const MtxReaderOptions &options)
 Method for importing matrix from file with given filename.
static void readMtx (std::istream &file, Matrix &matrix, const MtxReaderOptions &options)
 Method for importing matrix from STL input stream.
static void readMtx (std::istream &str, Matrix &matrix, bool verbose=false)
 Method for importing matrix from STL input stream.

Static Protected Member Functions

static bool readMatrixElement (std::istream &file, const MtxHeader &header, std::string &line, long &lineNumber, IndexType &row, IndexType &column, RealType &value)
static void readMatrixElements (std::istream &file, const MtxHeader &header, long &lineNumber, Matrix &matrix, const MtxReaderOptions &options)
static MtxHeader readMtxHeader (std::istream &file, long &lineNumber)
static void throwDuplicateElementError (std::istream &file, IndexType row, IndexType column)

Detailed Description

template<typename Matrix>
class TNL::Matrices::MatrixReader< Matrix >

Helper class for importing of matrices from different input formats.

Currently it supports:

  1. Coordinate MTX Format is supported.

The matrix is filled by its method setElements taking std::vector of the matrix elements, so the matrix may be allocated on any device.

Template Parameters
Matrixis a type of matrix into which we want to import the MTX file.
Example
#include <iostream>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Matrices/MatrixReader.h>
#include <TNL/Matrices/MatrixWriter.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void
matrixWriterExample()
{
Matrix matrix(
5, // number of matrix rows
5, // number of matrix columns
{
// matrix elements definition
// clang-format off
{ 0, 0, 2.0 },
{ 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 },
{ 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 },
{ 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 },
{ 4, 4, 2.0 },
// clang-format on
} );
std::cout << "Matrix:\n" << matrix << '\n';
std::cout << "Writing matrix in Gnuplot format into the file matrix-writer-example.gplt ...";
TNL::Matrices::MatrixWriter< Matrix >::writeGnuplot( "matrix-writer-example.gplt", matrix );
std::cout << " OK\n";
std::cout << "Writing matrix pattern in EPS format into the file matrix-writer-example.eps ...";
TNL::Matrices::MatrixWriter< Matrix >::writeEps( "matrix-writer-example.eps", matrix );
std::cout << " OK\n";
std::cout << "Writing matrix in MTX format into the file matrix-writer-example.mtx ...";
TNL::Matrices::MatrixWriter< Matrix >::writeMtx( "matrix-writer-example.mtx", matrix );
std::cout << " OK\n";
}
template< typename Device >
void
matrixReaderExample()
{
SparseMatrix sparseMatrix;
std::cout << "Reading sparse matrix from MTX file matrix-writer-example.mtx ... ";
TNL::Matrices::MatrixReader< SparseMatrix >::readMtx( "matrix-writer-example.mtx", sparseMatrix );
std::cout << " OK\n";
std::cout << "Imported matrix is:\n" << sparseMatrix << '\n';
DenseMatrix denseMatrix;
std::cout << "Reading dense matrix from MTX file matrix-writer-example.mtx ... ";
TNL::Matrices::MatrixReader< DenseMatrix >::readMtx( "matrix-writer-example.mtx", denseMatrix );
std::cout << " OK\n";
std::cout << "Imported matrix is:\n" << denseMatrix << '\n';
}
int
main( int argc, char* argv[] )
{
std::cout << "Creating matrices on CPU ...\n";
matrixWriterExample< TNL::Devices::Host >();
matrixReaderExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "\n\n";
std::cout << "Creating matrices on CUDA GPU ...\n";
matrixWriterExample< TNL::Devices::Cuda >();
matrixReaderExample< TNL::Devices::Cuda >();
#endif
}
Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros.
Definition DenseMatrix.h:33
static void readMtx(const std::string &fileName, Matrix &matrix, bool verbose=false)
Method for importing matrix from file with given filename.
static void writeMtx(const std::string &fileName, const Matrix &matrix, bool verbose=false)
Method for exporting matrix to file with given filename using MTX format.
static void writeEps(const std::string &fileName, const Matrix &matrix, bool verbose=false)
Method for exporting matrix to file with given filename using EPS format.
static void writeGnuplot(const std::string &fileName, const Matrix &matrix, bool verbose=false)
Method for exporting matrix to file with given filename using Gnuplot format.
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:59
Output
Creating matrices on CPU ...
Matrix:
Row: 0 -> 0:2
Row: 1 -> 0:-1 1:2 2:-1
Row: 2 -> 1:-1 2:2 3:-1
Row: 3 -> 2:-1 3:2 4:-1
Row: 4 -> 4:2
Writing matrix in Gnuplot format into the file matrix-writer-example.gplt ... OK
Writing matrix pattern in EPS format into the file matrix-writer-example.eps ... OK
Writing matrix in MTX format into the file matrix-writer-example.mtx ... OK
Reading sparse matrix from MTX file matrix-writer-example.mtx ... OK
Imported matrix is:
Row: 0 -> 0:2
Row: 1 -> 0:-1 1:2 2:-1
Row: 2 -> 1:-1 2:2 3:-1
Row: 3 -> 2:-1 3:2 4:-1
Row: 4 -> 4:2
Reading dense matrix from MTX file matrix-writer-example.mtx ... OK
Imported matrix is:
Row: 0 -> 0:2 1:0 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:2 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:2 4:-1
Row: 4 -> 0:0 1:0 2:0 3:0 4:2
Creating matrices on CUDA GPU ...
Matrix:
Row: 0 -> 0:2
Row: 1 -> 0:-1 1:2 2:-1
Row: 2 -> 1:-1 2:2 3:-1
Row: 3 -> 2:-1 3:2 4:-1
Row: 4 -> 4:2
Writing matrix in Gnuplot format into the file matrix-writer-example.gplt ... OK
Writing matrix pattern in EPS format into the file matrix-writer-example.eps ... OK
Writing matrix in MTX format into the file matrix-writer-example.mtx ... OK
Reading sparse matrix from MTX file matrix-writer-example.mtx ... OK
Imported matrix is:
Row: 0 -> 0:2
Row: 1 -> 0:-1 1:2 2:-1
Row: 2 -> 1:-1 2:2 3:-1
Row: 3 -> 2:-1 3:2 4:-1
Row: 4 -> 4:2
Reading dense matrix from MTX file matrix-writer-example.mtx ... OK
Imported matrix is:
Row: 0 -> 0:2 1:0 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:2 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:2 4:-1
Row: 4 -> 0:0 1:0 2:0 3:0 4:2

Member Function Documentation

◆ isSymmetric() [1/2]

template<typename Matrix>
bool TNL::Matrices::MatrixReader< Matrix >::isSymmetric ( const std::string & fileName)
staticnodiscard

Check if the MTX file with given filename declares a symmetric matrix.

Only the header of the file is read, i.e. the symmetry is the one declared by the header line %MatrixMarket matrix coordinate <field> symmetric, the matrix elements are not checked.

Parameters
fileNameis the name of the source file.
Returns
true if the header declares a symmetric matrix and false if it declares a general or skew-symmetric one.

◆ isSymmetric() [2/2]

template<typename Matrix>
bool TNL::Matrices::MatrixReader< Matrix >::isSymmetric ( std::istream & file)
staticnodiscard

Check if the MTX file in STL input stream declares a symmetric matrix.

The header is read from the beginning of the stream.

Parameters
fileis the input stream.
Returns
true if the header declares a symmetric matrix and false if it declares a general or skew-symmetric one.

◆ readMtx() [1/4]

template<typename Matrix>
void TNL::Matrices::MatrixReader< Matrix >::readMtx ( const std::string & fileName,
Matrix & matrix,
bool verbose = false )
static

Method for importing matrix from file with given filename.

Parameters
fileNameis the name of the source file.
matrixis the target matrix.
verbosecontrols verbosity of the matrix import.

◆ readMtx() [2/4]

template<typename Matrix>
void TNL::Matrices::MatrixReader< Matrix >::readMtx ( const std::string & fileName,
Matrix & matrix,
const MtxReaderOptions & options )
static

Method for importing matrix from file with given filename.

Parameters
fileNameis the name of the source file.
matrixis the target matrix.
optionsare the options of the import, see MtxReaderOptions.

◆ readMtx() [3/4]

template<typename Matrix>
void TNL::Matrices::MatrixReader< Matrix >::readMtx ( std::istream & file,
Matrix & matrix,
const MtxReaderOptions & options )
static

Method for importing matrix from STL input stream.

Parameters
fileis the input stream.
matrixis the target matrix.
optionsare the options of the import, see MtxReaderOptions.

◆ readMtx() [4/4]

template<typename Matrix>
void TNL::Matrices::MatrixReader< Matrix >::readMtx ( std::istream & str,
Matrix & matrix,
bool verbose = false )
static

Method for importing matrix from STL input stream.

Parameters
stris the input stream.
matrixis the target matrix.
verbosecontrols verbosity of the matrix import.

The documentation for this class was generated from the following file:
  • src/TNL/Matrices/MatrixReader.h