Ginkgo: ginkgo/core/base/mtx_io.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
mtx_io.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
6#define GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
7
8
9#include <istream>
10
11#include <ginkgo/core/base/matrix_data.hpp>
12#include <ginkgo/core/base/utils_helper.hpp>
13
14
15namespace gko {
16
17
32template <typename ValueType = default_precision, typename IndexType = int32>
33 matrix_data<ValueType, IndexType> read_raw(std::istream& is);
34
35
67template <typename ValueType = default_precision, typename IndexType = int32>
68 matrix_data<ValueType, IndexType> read_binary_raw(std::istream& is);
69
70
86template <typename ValueType = default_precision, typename IndexType = int32>
87 matrix_data<ValueType, IndexType> read_generic_raw(std::istream& is);
88
89
93 enum class layout_type {
97 array,
101 coordinate
102};
103
104
119template <typename ValueType, typename IndexType>
120 void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
121 layout_type layout = layout_type::coordinate);
122
123
140template <typename ValueType, typename IndexType>
141 void write_binary_raw(std::ostream& os,
142 const matrix_data<ValueType, IndexType>& data);
143
144
159template <typename MatrixType, typename StreamType, typename... MatrixArgs>
160 inline std::unique_ptr<MatrixType> read(StreamType&& is, MatrixArgs&&... args)
161{
162 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
163 mtx->read(read_raw<typename MatrixType::value_type,
164 typename MatrixType::index_type>(is));
165 return mtx;
166}
167
168
183template <typename MatrixType, typename StreamType, typename... MatrixArgs>
184 inline std::unique_ptr<MatrixType> read_binary(StreamType&& is,
185 MatrixArgs&&... args)
186{
187 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
188 mtx->read(read_binary_raw<typename MatrixType::value_type,
189 typename MatrixType::index_type>(is));
190 return mtx;
191}
192
193
209template <typename MatrixType, typename StreamType, typename... MatrixArgs>
210 inline std::unique_ptr<MatrixType> read_generic(StreamType&& is,
211 MatrixArgs&&... args)
212{
213 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
214 mtx->read(read_generic_raw<typename MatrixType::value_type,
215 typename MatrixType::index_type>(is));
216 return mtx;
217}
218
219
220namespace matrix {
221
222
223template <typename ValueType>
224class Dense;
225
226
227class Fft;
228
229
230class Fft2;
231
232
233class Fft3;
234
235
236} // namespace matrix
237
238
239namespace detail {
240
241
251template <typename MatrixType>
252struct mtx_io_traits {
253 static constexpr auto default_layout = layout_type::coordinate;
254};
255
256
257template <typename ValueType>
258struct mtx_io_traits<gko::matrix::Dense<ValueType>> {
259 static constexpr auto default_layout = layout_type::array;
260};
261
262
263template <>
264struct mtx_io_traits<gko::matrix::Fft> {
265 static constexpr auto default_layout = layout_type::array;
266};
267
268
269template <>
270struct mtx_io_traits<gko::matrix::Fft2> {
271 static constexpr auto default_layout = layout_type::array;
272};
273
274
275template <>
276struct mtx_io_traits<gko::matrix::Fft3> {
277 static constexpr auto default_layout = layout_type::array;
278};
279
280
281} // namespace detail
282
283
295template <
296 typename MatrixPtrType, typename StreamType,
297 std::enable_if_t<!std::is_same_v<
298 std::remove_cv_t<detail::pointee<MatrixPtrType>>, LinOp>>* = nullptr>
299 inline void write(
300 StreamType&& os, MatrixPtrType&& matrix,
301 layout_type layout = detail::mtx_io_traits<
302 std::remove_cv_t<detail::pointee<MatrixPtrType>>>::default_layout)
303{
304 using MatrixType = detail::pointee<MatrixPtrType>;
305 matrix_data<typename MatrixType::value_type,
306 typename MatrixType::index_type>
307 data{};
308 matrix->write(data);
309 write_raw(os, data, layout);
310}
311
320 void write(std::ostream& os, ptr_param<const LinOp> matrix);
321
322
336template <typename MatrixPtrType, typename StreamType>
337 inline void write_binary(StreamType&& os, MatrixPtrType&& matrix)
338{
339 using MatrixType = detail::pointee<MatrixPtrType>;
340 matrix_data<typename MatrixType::value_type,
341 typename MatrixType::index_type>
342 data{};
343 matrix->write(data);
344 write_binary_raw(os, data);
345}
346
347
348} // namespace gko
349
350
351#endif // GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
gko::LinOp
Definition lin_op.hpp:117
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
gko::matrix
The matrix namespace.
Definition dense_cache.hpp:24
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::read_binary
std::unique_ptr< MatrixType > read_binary(StreamType &&is, MatrixArgs &&... args)
Reads a matrix stored in binary format from an input stream.
Definition mtx_io.hpp:184
gko::write_raw
void write_raw(std::ostream &os, const matrix_data< ValueType, IndexType > &data, layout_type layout=layout_type::coordinate)
Writes a matrix_data structure to a stream in matrix market format.
gko::write_binary_raw
void write_binary_raw(std::ostream &os, const matrix_data< ValueType, IndexType > &data)
Writes a matrix_data structure to a stream in binary format.
gko::read_generic
std::unique_ptr< MatrixType > read_generic(StreamType &&is, MatrixArgs &&... args)
Reads a matrix stored either in binary or matrix market format from an input stream.
Definition mtx_io.hpp:210
gko::read_binary_raw
matrix_data< ValueType, IndexType > read_binary_raw(std::istream &is)
Reads a matrix stored in Ginkgo's binary matrix format from an input stream.
gko::read_generic_raw
matrix_data< ValueType, IndexType > read_generic_raw(std::istream &is)
Reads a matrix stored in either binary or matrix market format from an input stream.
gko::write_binary
void write_binary(StreamType &&os, MatrixPtrType &&matrix)
Writes a matrix into an output stream in binary format.
Definition mtx_io.hpp:337
gko::read
std::unique_ptr< MatrixType > read(StreamType &&is, MatrixArgs &&... args)
Reads a matrix stored in matrix market format from an input stream.
Definition mtx_io.hpp:160
gko::layout_type
layout_type
Specifies the layout type when writing data in matrix market format.
Definition mtx_io.hpp:93
gko::layout_type::array
@ array
The matrix should be written as dense matrix in column-major order.
Definition mtx_io.hpp:97
gko::layout_type::coordinate
@ coordinate
The matrix should be written as a sparse matrix in coordinate format.
Definition mtx_io.hpp:101
gko::write
void write(StreamType &&os, MatrixPtrType &&matrix, layout_type layout=detail::mtx_io_traits< std::remove_cv_t< detail::pointee< MatrixPtrType > > >::default_layout)
Writes a matrix into an output stream in matrix market format.
Definition mtx_io.hpp:299
gko::read_raw
matrix_data< ValueType, IndexType > read_raw(std::istream &is)
Reads a matrix stored in matrix market format from an input stream.
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126