Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
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 
14 namespace gko {
15 
16 
31 template <typename ValueType = default_precision, typename IndexType = int32>
32 matrix_data<ValueType, IndexType> read_raw(std::istream& is);
33 
34 
66 template <typename ValueType = default_precision, typename IndexType = int32>
67 matrix_data<ValueType, IndexType> read_binary_raw(std::istream& is);
68 
69 
85 template <typename ValueType = default_precision, typename IndexType = int32>
86 matrix_data<ValueType, IndexType> read_generic_raw(std::istream& is);
87 
88 
92 enum class layout_type {
96  array,
100  coordinate
101 };
102 
103 
118 template <typename ValueType, typename IndexType>
119 void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
121 
122 
139 template <typename ValueType, typename IndexType>
140 void write_binary_raw(std::ostream& os,
141  const matrix_data<ValueType, IndexType>& data);
142 
143 
158 template <typename MatrixType, typename StreamType, typename... MatrixArgs>
159 inline 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 
182 template <typename MatrixType, typename StreamType, typename... MatrixArgs>
183 inline 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 
208 template <typename MatrixType, typename StreamType, typename... MatrixArgs>
209 inline 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 
219 namespace matrix {
220 
221 
222 template <typename ValueType>
223 class Dense;
224 
225 
226 class Fft;
227 
228 
229 class Fft2;
230 
231 
232 class Fft3;
233 
234 
235 } // namespace matrix
236 
237 
238 namespace detail {
239 
240 
250 template <typename MatrixType>
251 struct mtx_io_traits {
252  static constexpr auto default_layout = layout_type::coordinate;
253 };
254 
255 
256 template <typename ValueType>
257 struct mtx_io_traits<gko::matrix::Dense<ValueType>> {
258  static constexpr auto default_layout = layout_type::array;
259 };
260 
261 
262 template <>
263 struct mtx_io_traits<gko::matrix::Fft> {
264  static constexpr auto default_layout = layout_type::array;
265 };
266 
267 
268 template <>
269 struct mtx_io_traits<gko::matrix::Fft2> {
270  static constexpr auto default_layout = layout_type::array;
271 };
272 
273 
274 template <>
275 struct mtx_io_traits<gko::matrix::Fft3> {
276  static constexpr auto default_layout = layout_type::array;
277 };
278 
279 
280 } // namespace detail
281 
282 
294 template <typename MatrixPtrType, typename StreamType>
295 inline 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 
322 template <typename MatrixPtrType, typename StreamType>
323 inline 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_
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:159
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:209
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::layout_type::array
The matrix should be written as dense matrix in column-major order.
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::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
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
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:295
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_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:183
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::layout_type::coordinate
The matrix should be written as a sparse matrix in coordinate format.
gko::write_binary
void write_binary(StreamType &&os, MatrixPtrType &&matrix)
Writes a matrix into an output stream in binary format.
Definition: mtx_io.hpp:323
gko::layout_type
layout_type
Specifies the layout type when writing data in matrix market format.
Definition: mtx_io.hpp:92