23#include <alien/handlers/scalar/BaseDirectMatrixBuilder.h>
24#include <alien/handlers/scalar/BaseVectorWriter.h>
30#ifdef ALIEN_USE_LIBARCHIVE
40 std::fstream* m_file_stream =
nullptr;
44 FStreamReader() =
delete;
45 FStreamReader(FStreamReader
const&) =
delete;
46 FStreamReader& operator=(FStreamReader
const&) =
delete;
48 explicit FStreamReader(std::fstream* fdes)
54 std::getline(*m_file_stream, m_line);
56 return m_line.c_str();
59 const char* currentLine()
const
61 return m_line.c_str();
65#ifdef ALIEN_USE_LIBARCHIVE
69 const std::size_t m_buffer_size = 1024;
71 archive* m_archive =
nullptr;
73 std::vector<char> m_buffer;
74 std::size_t m_pos = 0;
77 LibArchiveReader() =
delete;
78 LibArchiveReader(
const LibArchiveReader&) =
delete;
79 LibArchiveReader& operator=(
const LibArchiveReader&) =
delete;
81 LibArchiveReader(archive* archive)
83 , m_buffer(m_buffer_size, 0)
85 m_line.reserve(m_buffer_size);
95 r = archive_read_data(m_archive, m_buffer.data(), m_buffer.size());
99 for (
auto i = m_pos; i < m_buffer_size; ++i) {
100 m_line.push_back(m_buffer[i]);
101 if (m_buffer[i] ==
'\n') {
104 return m_line.c_str();
108 r = archive_read_data(m_archive, m_buffer.data(), m_buffer.size());
113 return m_line.c_str();
116 const char* currentLine()
const
118 return m_line.c_str();
123template <
typename ReaderT>
124bool readMMHeaderFromReader(
const std::string& mm_type, ReaderT& reader)
133 if (sscanf(reader.line(),
"%%%%MatrixMarket %31s %31s %31s %31s", param1, param2, param3, param4) != 4) {
134 throw FatalErrorException(A_FUNCINFO,
"Matrix market wrong header");
136 if (std::string(param1) != std::string(
"matrix")) {
137 throw FatalErrorException(A_FUNCINFO,
"Matrix market wrong header 1");
139 if (std::string(param2) != mm_type) {
140 throw FatalErrorException(A_FUNCINFO,
"Matrix market wrong header 2");
142 if (std::string(param3) != std::string(
"real")) {
143 throw FatalErrorException(A_FUNCINFO,
"Matrix market wrong header 3");
147 while (reader.line()[0] ==
'%')
150 return std::string(param4) == std::string(
"symmetric");
153template <
typename MatrixT,
typename ReaderT>
154void loadMMMatrixFromReader(MatrixT& A, ReaderT& reader)
156 bool is_symmetric = readMMHeaderFromReader(
"coordinate", reader);
160 if (sscanf(reader.currentLine(),
"%d %d %d", &n, &m, &nnz) != 3) {
161 perror(
"read mtx size line");
162 throw FatalErrorException(A_FUNCINFO,
"IOError");
165 Alien::MatrixDistribution dist(n, m, n,
nullptr);
169 DirectMatrixOptions::SymmetricFlag sym_flag = is_symmetric ? DirectMatrixOptions::eSymmetric : DirectMatrixOptions::eUnSymmetric;
172 matrix_builder.allocate();
174 for (
int i = 0; i < nnz; ++i) {
179 if (sscanf(reader.line(),
"%d %d %lg\n", &li, &ci, &val) != 3) {
180 perror(
"read mtx line");
181 throw FatalErrorException(A_FUNCINFO,
"IOError");
186 matrix_builder.addData(li, ci, val);
190template <
typename VectorT,
typename ReaderT>
191void loadMMRhsFromReader(VectorT& rhs, ReaderT& reader)
193 readMMHeaderFromReader(
"array", reader);
197 if (sscanf(reader.currentLine(),
"%d %d", &n, &m) != 2) {
198 perror(
"read mtx size line");
199 throw FatalErrorException(A_FUNCINFO,
"IOError");
204 throw FatalErrorException(A_FUNCINFO,
"More than one vector not allowed");
207 Alien::VectorDistribution dist(n, n,
nullptr);
213 for (
int i = 0; i < n; ++i) {
215 if (sscanf(reader.line(),
"%lg\n", &val) != 1) {
216 perror(
"read mtx line");
217 throw FatalErrorException(A_FUNCINFO,
"IOError");
220 vector_writer[i] = val;
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --