Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.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 
12 #include <ginkgo/core/base/matrix_data.hpp>
13 
14 
15 namespace gko {
16 
17 
32 template <typename ValueType = default_precision, typename IndexType = int32>
33 matrix_data<ValueType, IndexType> read_raw(std::istream& is);
34 
35 
67 template <typename ValueType = default_precision, typename IndexType = int32>
68 matrix_data<ValueType, IndexType> read_binary_raw(std::istream& is);
69 
70 
86 template <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 
119 template <typename ValueType, typename IndexType>
120 void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
122 
123 
140 template <typename ValueType, typename IndexType>
141 void write_binary_raw(std::ostream& os,
142  const matrix_data<ValueType, IndexType>& data);
143 
144 
159 template <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 
183 template <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 
209 template <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 
220 namespace matrix {
221 
222 
223 template <typename ValueType>
224 class Dense;
225 
226 
227 class Fft;
228 
229 
230 class Fft2;
231 
232 
233 class Fft3;
234 
235 
236 } // namespace matrix
237 
238 
239 namespace detail {
240 
241 
251 template <typename MatrixType>
252 struct mtx_io_traits {
253  static constexpr auto default_layout = layout_type::coordinate;
254 };
255 
256 
257 template <typename ValueType>
258 struct mtx_io_traits<gko::matrix::Dense<ValueType>> {
259  static constexpr auto default_layout = layout_type::array;
260 };
261 
262 
263 template <>
264 struct mtx_io_traits<gko::matrix::Fft> {
265  static constexpr auto default_layout = layout_type::array;
266 };
267 
268 
269 template <>
270 struct mtx_io_traits<gko::matrix::Fft2> {
271  static constexpr auto default_layout = layout_type::array;
272 };
273 
274 
275 template <>
276 struct mtx_io_traits<gko::matrix::Fft3> {
277  static constexpr auto default_layout = layout_type::array;
278 };
279 
280 
281 } // namespace detail
282 
283 
295 template <typename MatrixPtrType, typename StreamType>
296 inline 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 
323 template <typename MatrixPtrType, typename StreamType>
324 inline 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_
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::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::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:127
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:296
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:184
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:324
gko::layout_type
layout_type
Specifies the layout type when writing data in matrix market format.
Definition: mtx_io.hpp:93