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

Helper class for exporting of matrices to different output formats. More...

#include <TNL/Matrices/MatrixWriter.h>

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 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 writeEps (std::ostream &str, const Matrix &matrix, bool verbose=false)
 Method for exporting matrix to STL output stream 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.
static void writeGnuplot (std::ostream &str, const Matrix &matrix, bool verbose=false)
 Method for exporting matrix to STL output stream using Gnuplot format.
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 writeMtx (std::ostream &str, const Matrix &matrix, bool verbose=false)
 Method for exporting matrix to STL output stream using MTX format.

Protected Types

using HostMatrix = typename Matrix::template Self< RealType, TNL::Devices::Host >
 Host copy of the matrix used for exporting matrices from other devices.

Static Protected Member Functions

static void writeEpsBody (std::ostream &str, const Matrix &matrix, int elementSize, bool verbose)
 Method for writing the EPS body with the matrix pattern.
static void writeEpsHeader (std::ostream &str, const Matrix &matrix, int elementSize)
 Method for writing the EPS header.

Detailed Description

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

Helper class for exporting of matrices to different output formats.

Currently it supports:

  1. Coordinate MTX Format is supported.
  2. Gnuplot format for matrix visualization in Gnuplot.
  3. EPS format for matrix pattern visualization in Encapsulated PostScript

Matrices allocated on TNL::Devices::Host and TNL::Devices::Sequential are written directly, matrices allocated on other devices are first copied to a host matrix.

Template Parameters
Matrixis a type of matrix we want to export.
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

◆ writeEps() [1/2]

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

Method for exporting matrix to file with given filename using EPS format.

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

◆ writeEps() [2/2]

template<typename Matrix>
void TNL::Matrices::MatrixWriter< Matrix >::writeEps ( std::ostream & str,
const Matrix & matrix,
bool verbose = false )
static

Method for exporting matrix to STL output stream using EPS format.

Parameters
stris the output stream.
matrixis the source matrix.
verbosecontrols verbosity of the matrix export.

◆ writeEpsBody()

template<typename Matrix>
void TNL::Matrices::MatrixWriter< Matrix >::writeEpsBody ( std::ostream & str,
const Matrix & matrix,
int elementSize,
bool verbose )
staticprotected

Method for writing the EPS body with the matrix pattern.

Parameters
stris the output stream.
matrixis the source matrix.
elementSizeis the size of a single element in PostScript units.
verbosecontrols verbosity of the matrix export.

◆ writeEpsHeader()

template<typename Matrix>
void TNL::Matrices::MatrixWriter< Matrix >::writeEpsHeader ( std::ostream & str,
const Matrix & matrix,
int elementSize )
staticprotected

Method for writing the EPS header.

Parameters
stris the output stream.
matrixis the source matrix.
elementSizeis the size of a single element in PostScript units.

◆ writeGnuplot() [1/2]

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

Method for exporting matrix to file with given filename using Gnuplot format.

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

◆ writeGnuplot() [2/2]

template<typename Matrix>
void TNL::Matrices::MatrixWriter< Matrix >::writeGnuplot ( std::ostream & str,
const Matrix & matrix,
bool verbose = false )
static

Method for exporting matrix to STL output stream using Gnuplot format.

Parameters
stris the output stream.
matrixis the source matrix.
verbosecontrols verbosity of the matrix export.

◆ writeMtx() [1/2]

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

Method for exporting matrix to file with given filename using MTX format.

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

◆ writeMtx() [2/2]

template<typename Matrix>
void TNL::Matrices::MatrixWriter< Matrix >::writeMtx ( std::ostream & str,
const Matrix & matrix,
bool verbose = false )
static

Method for exporting matrix to STL output stream using MTX format.

Parameters
stris the output stream.
matrixis the source matrix.
verbosecontrols verbosity of the matrix export.

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