Ginkgo Generated from branch based on main. Ginkgo version 1.10.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#include <ginkgo/core/base/matrix_data.hpp>
12
13
14namespace gko {
15
16
31template <typename ValueType = default_precision, typename IndexType = int32>
33
34
66template <typename ValueType = default_precision, typename IndexType = int32>
68
69
85template <typename ValueType = default_precision, typename IndexType = int32>
87
88
102
103
118template <typename ValueType, typename IndexType>
119void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
121
122
139template <typename ValueType, typename IndexType>
140void write_binary_raw(std::ostream& os,
142
143
158template <typename MatrixType, typename StreamType, typename... MatrixArgs>
159inline std::unique_ptr<MatrixType> read(StreamType&& is, MatrixArgs&&... args)
160{
161 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
162 mtx->read(read_raw<typename MatrixType::value_type,
163 typename MatrixType::index_type>(is));
164 return mtx;
165}
166
167
182template <typename MatrixType, typename StreamType, typename... MatrixArgs>
183inline std::unique_ptr<MatrixType> read_binary(StreamType&& is,
184 MatrixArgs&&... args)
185{
186 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
187 mtx->read(read_binary_raw<typename MatrixType::value_type,
188 typename MatrixType::index_type>(is));
189 return mtx;
190}
191
192
208template <typename MatrixType, typename StreamType, typename... MatrixArgs>
209inline std::unique_ptr<MatrixType> read_generic(StreamType&& is,
210 MatrixArgs&&... args)
211{
212 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
213 mtx->read(read_generic_raw<typename MatrixType::value_type,
214 typename MatrixType::index_type>(is));
215 return mtx;
216}
217
218
219namespace matrix {
220
221
222template <typename ValueType>
223class Dense;
224
225
226class Fft;
227
228
229class Fft2;
230
231
232class Fft3;
233
234
235} // namespace matrix
236
237
238namespace detail {
239
240
250template <typename MatrixType>
251struct mtx_io_traits {
252 static constexpr auto default_layout = layout_type::coordinate;
253};
254
255
256template <typename ValueType>
257struct mtx_io_traits<gko::matrix::Dense<ValueType>> {
258 static constexpr auto default_layout = layout_type::array;
259};
260
261
262template <>
263struct mtx_io_traits<gko::matrix::Fft> {
264 static constexpr auto default_layout = layout_type::array;
265};
266
267
268template <>
269struct mtx_io_traits<gko::matrix::Fft2> {
270 static constexpr auto default_layout = layout_type::array;
271};
272
273
274template <>
275struct mtx_io_traits<gko::matrix::Fft3> {
276 static constexpr auto default_layout = layout_type::array;
277};
278
279
280} // namespace detail
281
282
294template <typename MatrixPtrType, typename StreamType>
295inline void write(
296 StreamType&& os, MatrixPtrType&& matrix,
297 layout_type layout = detail::mtx_io_traits<
298 std::remove_cv_t<detail::pointee<MatrixPtrType>>>::default_layout)
299{
300 using MatrixType = detail::pointee<MatrixPtrType>;
301 matrix_data<typename MatrixType::value_type,
302 typename MatrixType::index_type>
303 data{};
304 matrix->write(data);
305 write_raw(os, data, layout);
306}
307
308
322template <typename MatrixPtrType, typename StreamType>
323inline void write_binary(StreamType&& os, MatrixPtrType&& matrix)
324{
325 using MatrixType = detail::pointee<MatrixPtrType>;
326 matrix_data<typename MatrixType::value_type,
327 typename MatrixType::index_type>
328 data{};
329 matrix->write(data);
330 write_binary_raw(os, data);
331}
332
333
334} // namespace gko
335
336
337#endif // GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
The matrix namespace.
Definition dense_cache.hpp:24
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:183
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:209
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:295
void write_binary(StreamType &&os, MatrixPtrType &&matrix)
Writes a matrix into an output stream in binary format.
Definition mtx_io.hpp:323
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:159
layout_type
Specifies the layout type when writing data in matrix market format.
Definition mtx_io.hpp:92
@ array
The matrix should be written as dense matrix in column-major order.
Definition mtx_io.hpp:96
@ coordinate
The matrix should be written as a sparse matrix in coordinate format.
Definition mtx_io.hpp:100
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:126