Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
mtx_io.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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
12#include <ginkgo/core/base/matrix_data.hpp>
13
14
15namespace gko {
16
17
32template <typename ValueType = default_precision, typename IndexType = int32>
34
35
67template <typename ValueType = default_precision, typename IndexType = int32>
69
70
86template <typename ValueType = default_precision, typename IndexType = int32>
88
89
93enum class layout_type {
97 array,
102};
103
104
119template <typename ValueType, typename IndexType>
120void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
122
123
140template <typename ValueType, typename IndexType>
141void write_binary_raw(std::ostream& os,
143
144
159template <typename MatrixType, typename StreamType, typename... MatrixArgs>
160inline 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>
184inline 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>
210inline 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 <typename MatrixPtrType, typename StreamType>
296inline void write(
297 StreamType&& os, MatrixPtrType&& matrix,
298 layout_type layout = detail::mtx_io_traits<
299 std::remove_cv_t<detail::pointee<MatrixPtrType>>>::default_layout)
300{
301 using MatrixType = detail::pointee<MatrixPtrType>;
302 matrix_data<typename MatrixType::value_type,
303 typename MatrixType::index_type>
304 data{};
305 matrix->write(data);
306 write_raw(os, data, layout);
307}
308
309
323template <typename MatrixPtrType, typename StreamType>
324inline void write_binary(StreamType&& os, MatrixPtrType&& matrix)
325{
326 using MatrixType = detail::pointee<MatrixPtrType>;
327 matrix_data<typename MatrixType::value_type,
328 typename MatrixType::index_type>
329 data{};
330 matrix->write(data);
331 write_binary_raw(os, data);
332}
333
334
335} // namespace gko
336
337
338#endif // GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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
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.
void write_binary_raw(std::ostream &os, const matrix_data< ValueType, IndexType > &data)
Writes a matrix_data structure to a stream in binary format.
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
matrix_data< ValueType, IndexType > read_binary_raw(std::istream &is)
Reads a matrix stored in Ginkgo's binary matrix format from an input stream.
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.
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:296
void write_binary(StreamType &&os, MatrixPtrType &&matrix)
Writes a matrix into an output stream in binary format.
Definition mtx_io.hpp:324
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
layout_type
Specifies the layout type when writing data in matrix market format.
Definition mtx_io.hpp:93
@ array
The matrix should be written as dense matrix in column-major order.
@ coordinate
The matrix should be written as a sparse matrix in coordinate format.
matrix_data< ValueType, IndexType > read_raw(std::istream &is)
Reads a matrix stored in matrix market format from an input stream.
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:127