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

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

#include <TNL/Matrices/MatrixReader.h>

Public Types

using DeviceType = typename Matrix::RealType
 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 void readMtx (const std::string &fileName, Matrix &matrix, bool verbose=false)
 Method for importing matrix from file with given filename.
 
static void readMtx (std::istream &str, Matrix &matrix, bool verbose=false)
 Method for importing matrix from STL input stream.
 

Protected Types

using HostMatrix = typename Matrix::template Self< RealType, TNL::Devices::Host >
 

Detailed Description

template<typename Matrix, typename Device = typename Matrix::DeviceType>
class TNL::Matrices::MatrixReader< Matrix, Device >

Helper class for importing of matrices from different input formats.

Currently it supports:

  1. Coordinate MTX Format is supported.
Template Parameters
Matrixis a type of matrix into which we want to import the MTX file.
Deviceis used only for the purpose of template specialization.
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: " << std::endl << matrix << std::endl;
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 " << std::endl;
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 " << std::endl;
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 " << std::endl;
}
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 " << std::endl;
std::cout << "Imported matrix is: " << std::endl << sparseMatrix << std::endl;
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 " << std::endl;
std::cout << "Imported matrix is: " << std::endl << denseMatrix << std::endl;
}
int
main( int argc, char* argv[] )
{
std::cout << "Creating matrices on CPU ... " << std::endl;
matrixWriterExample< TNL::Devices::Host >();
matrixReaderExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Creating matrices on CUDA GPU ... " << std::endl;
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:31
static void readMtx(const std::string &fileName, Matrix &matrix, bool verbose=false)
Method for importing matrix from file with given filename.
Definition MatrixReader.hpp:19
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.
Definition MatrixWriter.hpp:51
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.
Definition MatrixWriter.hpp:15
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.
Definition MatrixWriter.hpp:33
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:57
T endl(T... args)
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

◆ readMtx() [1/2]

template<typename Matrix , typename Device >
void TNL::Matrices::MatrixReader< Matrix, Device >::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/2]

template<typename Matrix , typename Device >
void TNL::Matrices::MatrixReader< Matrix, Device >::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 files: