|
ug4
|
Provides I/O functionality for MatrixMarket exchange file format. More...
#include <matrix_io_mtx.h>
Inheritance diagram for ug::MatrixIOMtx:Public Member Functions | |
| std::string | get_mat_file_name () const |
| Retreive associated exchange file path and name. | |
| size_t | get_num_cols () const |
| Retrieves number of columns as specified in the exchange file. | |
| size_t | get_num_lines () const |
| Retrieves number of data lines as specified in the exchange file. | |
| size_t | get_num_rows () const |
| Retrieves number of rows as specified in the exchange file. | |
| bool | is_sparse () const |
| Tells whether the associated exchange file holds a sparse matrix. | |
| MatrixIOMtx () | |
| Default constructor. | |
| MatrixIOMtx (std::string mFile, int openMode=MatrixIO::EXISTING) | |
| Constructor specifying matrix exchange file path. | |
| template<typename matrix_type > | |
| void | read_into (matrix_type &matrix) |
| Reads data from associated exchange file into ug matrix. | |
| void | set_mat_dims (size_t rows, size_t cols, size_t lines) |
| Sets the size of the matrix and number of data lines. | |
| void | set_mat_file_name (std::string mFile, int openMode=MatrixIO::EXISTING) |
| Sets associated file to another one. | |
| template<typename matrix_type > | |
| void | write_from (matrix_type &matrix, std::string comment="%Generated with ug4.") |
| Writes a ug matrix into the associated MatrixMarket exchange file. | |
| ~MatrixIOMtx () | |
| Destructor. | |
Private Member Functions | |
| void | close_file () |
| template<typename matrix_type > | |
| std::vector< std::vector< size_t > > | determine_matrix_characteristics (const matrix_type &matrix) |
| Determines characteristics of given matrix. | |
| void | open_file (std::ios_base::openmode mode=std::ios_base::in) |
| void | query_matrix_characteristics () |
| Retreives matrix size and number non-zeros from exchange file. | |
| void | query_matrix_type () |
| Parses banner of MatrixMarket file. | |
| void | read_entry (size_t *m, size_t *n, CPUAlgebra::matrix_type::value_type *val) |
| Reads and parses the next data line. | |
| void | write_banner () |
| Writes the banner to the associated MatrixMarket file. | |
| void | write_entry (size_t m, size_t n, CPUAlgebra::matrix_type::value_type val) |
| Appends a data line to the associated file. | |
Private Member Functions inherited from ug::MatrixIO | |
| std::string | get_mat_file_name () const |
| Retreive associated exchange file path and name. | |
| MatrixIO () | |
| Number of rows as specified in the matrix exchange file. | |
| MatrixIO (std::string mFile, int openMode=EXISTING) | |
| Constructor specifying matrix exchange file path. | |
| void | set_mat_file_name (std::string mFile, int openMode=EXISTING) |
| Sets associated file to another one. | |
| ~MatrixIO () | |
| Destructor. | |
Private Attributes | |
| size_t | m_cols |
| size_t | m_firstDataLine |
| Line number of the first data line (0 based) | |
| size_t | m_lines |
| std::fstream | m_matFileStream |
| MatrixFileType | m_matFileType |
| MMTypeCode | m_mmTypeCode |
| Characteristics of MatrixMarket matrix. | |
| std::string * | m_pMatFileName |
| size_t | m_rows |
Additional Inherited Members | |
Private Types inherited from ug::MatrixIO | |
| enum | OpenMode { EXISTING = 1 , NEW = 2 } |
| Specifies how file names pointing to non-existing files should be handeld. More... | |
Provides I/O functionality for MatrixMarket exchange file format.
Given the path to a .mtx file it queries the therein stored matrix type and characteristics. With readInto(matrixType &matrix) an easy way of reading the represented matrix into a CPUAlgebra::matrix_type object is provided.
The specification of the MatrixMarket matrix exchange format can be found here: http://math.nist.gov/MatrixMarket/formats.html#MMformat
| ug::MatrixIOMtx::MatrixIOMtx | ( | ) |
Default constructor.
| ug::MatrixIOMtx::MatrixIOMtx | ( | std::string | mFile, |
| int | openMode = MatrixIO::EXISTING |
||
| ) |
Constructor specifying matrix exchange file path.
After successful call to MatrixIOMtx::set_mat_file_name it calls MatrixIOMtx::query_matrix_type and MatrixIOMtx::query_matrix_characteristics as long as openMode is MatrixIO::OpenMode::EXISTING.
| [in] | mFile | Full path and name of matrix exchange file. |
| [in] | openMode | how to deal with non-existing files (a value of MatrixIO::OpenMode) |
References ug::MatrixIO::EXISTING, PROFILE_FUNC, query_matrix_characteristics(), query_matrix_type(), and set_mat_file_name().
| ug::MatrixIOMtx::~MatrixIOMtx | ( | ) |
Destructor.
The destructor calls MatrixIOMtx::close_file to make sure, that write operations to the associated exchange file are flushed and terminated correctly.
References close_file(), and m_pMatFileName.
|
private |
References m_matFileStream.
Referenced by query_matrix_characteristics(), query_matrix_type(), read_into(), write_banner(), write_from(), and ~MatrixIOMtx().
|
inlineprivate |
Determines characteristics of given matrix.
Symmetries of the given matrix are determined and saved.
| [in] | matrix | Matrix of CPUAlgebra::matrix_type to analyse |
| std::runtime_error | if the matrix was determined to be symmetric and skew-symmetric at the same time |
References ug::MMTypeCode::COORDINATE, ug::MMTypeCode::GENERAL, m_mmTypeCode, PROFILE_FUNC, ug::MMTypeCode::REAL, ug::MMTypeCode::set_algebraic_type(), ug::MMTypeCode::set_class_type(), set_mat_dims(), ug::MMTypeCode::set_numeric_type(), ug::MMTypeCode::SKEW, ug::MMTypeCode::SYMMETRIC, and UG_ASSERT.
Referenced by write_from().
| std::string ug::MatrixIOMtx::get_mat_file_name | ( | ) | const |
Retreive associated exchange file path and name.
References m_pMatFileName.
| size_t ug::MatrixIOMtx::get_num_cols | ( | ) | const |
Retrieves number of columns as specified in the exchange file.
size_t References m_cols.
| size_t ug::MatrixIOMtx::get_num_lines | ( | ) | const |
Retrieves number of data lines as specified in the exchange file.
size_t References m_lines.
| size_t ug::MatrixIOMtx::get_num_rows | ( | ) | const |
Retrieves number of rows as specified in the exchange file.
size_t References m_rows.
| bool ug::MatrixIOMtx::is_sparse | ( | ) | const |
Tells whether the associated exchange file holds a sparse matrix.
true if it is in coordinate format, false otherwise References ug::MMTypeCode::is_sparse(), and m_mmTypeCode.
Referenced by read_entry().
|
private |
References m_matFileStream, m_pMatFileName, and UG_ASSERT.
Referenced by query_matrix_characteristics(), query_matrix_type(), read_into(), write_banner(), and write_from().
|
private |
Retreives matrix size and number non-zeros from exchange file.
The first non-empty line after the comments (lines starting with %) consists of three values for the coordinate format:
nRows nCols nLines
nRowsnColsnLinessymmetric, skew-symmetric and hermitian, this is not equal to the number of non-zero elements. | std::runtime_error | if the file stream ended unexpectetly (e.g. the exchange file does not contain data lines) |
| UGError | if the exchange file does not describe a coordinate/sparse matrix (as only sparse matrices are supported yet) |
References close_file(), ug::MMTypeCode::is_sparse(), m_firstDataLine, m_matFileStream, m_mmTypeCode, open_file(), PROFILE_FUNC, set_mat_dims(), UG_ASSERT, and UG_THROW.
Referenced by MatrixIOMtx().
|
private |
Parses banner of MatrixMarket file.
The banner is the first line of a MatrixMarket file with the syntax:
%MatrixMarket matrix [format] [numeric] [type]
format coordinate or array, representing a sparse or dense matrix respectively numeric real, complex, integer or pattern, representing the numerical type of the values type general, symmetric, skew-symmetric or hermitian, representing the algebraic type of the matrix | std::runtime_error | if the banner starts not with %MatrixMarket |
| std::runtime_error | if the second word of the banner is not matrix |
| UGError | if the format identifier is invalid |
| UGError | if the numeric identifier is invalid |
| UGError | if the type identifier is invalid |
References close_file(), m_matFileStream, m_mmTypeCode, MM_BANNER_STR, MM_MTX_STR, open_file(), PROFILE_FUNC, s, ug::MMTypeCode::set_algebraic_type(), ug::MMTypeCode::set_class_type(), ug::MMTypeCode::set_numeric_type(), and UG_ASSERT.
Referenced by MatrixIOMtx().
|
private |
Reads and parses the next data line.
| [out] | m | pointer to store the row index to (1 based) |
| [out] | n | pointer to store the column index to (1 based) |
| [out] | val | pointer to store the value to |
| std::runtime_error | if the filestream is not open |
| std::runtime_error | if the matrix is sparse but there were less or more than 3 elements in the line |
| ug::UGError | if the exchange file does not describe a coordinate/sparse matrix (as only sparse matrices are supported yet) |
References is_sparse(), m_matFileStream, PROFILE_FUNC, UG_ASSERT, and UG_THROW.
Referenced by read_into().
|
inline |
Reads data from associated exchange file into ug matrix.
The given ug-matrix is first resized. Each data line of the underlying exchange file is read separately and independently. If the matrix is (skew-)symmetric as defined in the exchange file, this is considered during writing the values to the correct positions.
| [in,out] | matrix | Matrix of CPUAlgebra::matrix_type to read into |
| std::runtime_error | if one of m_rows, m_cols or m_lines is not positive. |
| UGError | if exchange file's matrix is not in coordinate format (as only sparse matrices are supported yet) |
References close_file(), ug::MMTypeCode::is_skew_symmetric(), ug::MMTypeCode::is_sparse(), ug::MMTypeCode::is_symmetric(), m_cols, m_firstDataLine, m_lines, m_matFileStream, m_mmTypeCode, m_rows, open_file(), PROFILE_FUNC, read_entry(), UG_ASSERT, and UG_THROW.
| void ug::MatrixIOMtx::set_mat_dims | ( | size_t | rows, |
| size_t | cols, | ||
| size_t | lines | ||
| ) |
Sets the size of the matrix and number of data lines.
In case of reading from a matrix exchange file, this function is called by MatrixIOMtx::query_matrix_characteristics.
| [in] | rows | Number of rows of the matrix |
| [in] | cols | Number of columns of the matrix |
| [in] | lines | Number of data lines in the exchange file |
| std::runtime_error | if rows is not positive |
| std::runtime_error | if cols is not positive |
| std::runtime_error | if lines is not positive |
References m_cols, m_lines, m_rows, PROFILE_FUNC, and UG_ASSERT.
Referenced by determine_matrix_characteristics(), and query_matrix_characteristics().
| void ug::MatrixIOMtx::set_mat_file_name | ( | std::string | mFile, |
| int | openMode = MatrixIO::EXISTING |
||
| ) |
Sets associated file to another one.
| [in] | mFile | Full path and name of matrix exchange file. |
| [in] | openMode | how to deal with non-existing files |
| std::runtime_error | if exchange file is not found and openMode=MatrixIO::OpenMode::Existing. |
| UGError | if openMode is non of MatrixIO::OpenMode |
References ug::MatrixIO::EXISTING, ug::FileExists(), m_pMatFileName, ug::MatrixIO::NEW, PROFILE_FUNC, UG_ASSERT, and UG_THROW.
Referenced by MatrixIOMtx().
|
private |
Writes the banner to the associated MatrixMarket file.
References close_file(), ug::MMTypeCode::is_general(), ug::MMTypeCode::is_skew_symmetric(), ug::MMTypeCode::is_symmetric(), m_matFileStream, m_mmTypeCode, MM_BANNER_STR, MM_COORDINATE_STR, MM_GENERAL_STR, MM_MTX_STR, MM_REAL_STR, MM_SKEW_STR, MM_SYMMETRIC_STR, and open_file().
Referenced by write_from().
|
private |
Appends a data line to the associated file.
The value is formatted to scientific notation with 13 digits of precision.
| [out] | m | row index (1 based) |
| [out] | n | column index (1 based) |
| [out] | val | value at [row,col] |
| std::runtime_error | if the filestream is not open |
| std::runtime_error | if either the specified row or column index is not positive |
References m_matFileStream, PROFILE_FUNC, and UG_ASSERT.
Referenced by write_from().
|
inline |
Writes a ug matrix into the associated MatrixMarket exchange file.
From this analysis the MatrixMarket exchange file banner is constructed and the characteristics and data lines written to that file consecutively.
A comment is added right after the banner. The default comment is Generated with ug4.. To suppress the comment line completely, pass an empty string as the comment parameter to this function.
%.The resulting MatrixMarket exchange file is in line with the specifications.
| [in] | matrix | Matrix of CPUAlgebra::matrix_type to read from |
| [in] | comment | Optional comment to be put atop of the MatrixMarket matrix exchange file. |
References close_file(), determine_matrix_characteristics(), m_cols, m_lines, m_matFileStream, m_rows, open_file(), PROFILE_FUNC, UG_WARNING, write_banner(), and write_entry().
Referenced by ug::SaveMatrixToMTX().
|
private |
Referenced by get_num_cols(), read_into(), set_mat_dims(), and write_from().
|
private |
Line number of the first data line (0 based)
Referenced by query_matrix_characteristics(), and read_into().
|
private |
Referenced by get_num_lines(), read_into(), set_mat_dims(), and write_from().
|
private |
Referenced by close_file(), open_file(), query_matrix_characteristics(), query_matrix_type(), read_entry(), read_into(), write_banner(), write_entry(), and write_from().
|
private |
|
private |
Characteristics of MatrixMarket matrix.
Referenced by determine_matrix_characteristics(), is_sparse(), query_matrix_characteristics(), query_matrix_type(), read_into(), and write_banner().
|
private |
Referenced by get_mat_file_name(), open_file(), set_mat_file_name(), and ~MatrixIOMtx().
|
private |
Referenced by get_num_rows(), read_into(), set_mat_dims(), and write_from().