Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
mtx_io.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
34#define GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
35
36
37#include <istream>
38
39
40#include <ginkgo/core/base/matrix_data.hpp>
41
42
43namespace gko {
44
45
60template <typename ValueType = default_precision, typename IndexType = int32>
62
63
95template <typename ValueType = default_precision, typename IndexType = int32>
97
98
114template <typename ValueType = default_precision, typename IndexType = int32>
116
117
121enum class layout_type {
125 array,
130};
131
132
147template <typename ValueType, typename IndexType>
148void write_raw(std::ostream& os, const matrix_data<ValueType, IndexType>& data,
150
151
168template <typename ValueType, typename IndexType>
169void write_binary_raw(std::ostream& os,
171
172
187template <typename MatrixType, typename StreamType, typename... MatrixArgs>
188inline std::unique_ptr<MatrixType> read(StreamType&& is, MatrixArgs&&... args)
189{
190 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
191 mtx->read(read_raw<typename MatrixType::value_type,
192 typename MatrixType::index_type>(is));
193 return mtx;
194}
195
196
211template <typename MatrixType, typename StreamType, typename... MatrixArgs>
212inline std::unique_ptr<MatrixType> read_binary(StreamType&& is,
213 MatrixArgs&&... args)
214{
215 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
216 mtx->read(read_binary_raw<typename MatrixType::value_type,
217 typename MatrixType::index_type>(is));
218 return mtx;
219}
220
221
237template <typename MatrixType, typename StreamType, typename... MatrixArgs>
238inline std::unique_ptr<MatrixType> read_generic(StreamType&& is,
239 MatrixArgs&&... args)
240{
241 auto mtx = MatrixType::create(std::forward<MatrixArgs>(args)...);
242 mtx->read(read_generic_raw<typename MatrixType::value_type,
243 typename MatrixType::index_type>(is));
244 return mtx;
245}
246
247
248namespace matrix {
249
250
251template <typename ValueType>
252class Dense;
253
254
255class Fft;
256
257
258class Fft2;
259
260
261class Fft3;
262
263
264} // namespace matrix
265
266
267namespace detail {
268
269
279template <typename MatrixType>
280struct mtx_io_traits {
281 static constexpr auto default_layout = layout_type::coordinate;
282};
283
284
285template <typename ValueType>
286struct mtx_io_traits<gko::matrix::Dense<ValueType>> {
287 static constexpr auto default_layout = layout_type::array;
288};
289
290
291template <>
292struct mtx_io_traits<gko::matrix::Fft> {
293 static constexpr auto default_layout = layout_type::array;
294};
295
296
297template <>
298struct mtx_io_traits<gko::matrix::Fft2> {
299 static constexpr auto default_layout = layout_type::array;
300};
301
302
303template <>
304struct mtx_io_traits<gko::matrix::Fft3> {
305 static constexpr auto default_layout = layout_type::array;
306};
307
308
309} // namespace detail
310
311
323template <typename MatrixPtrType, typename StreamType>
324inline void write(
325 StreamType&& os, MatrixPtrType&& matrix,
326 layout_type layout = detail::mtx_io_traits<
327 std::remove_cv_t<detail::pointee<MatrixPtrType>>>::default_layout)
328{
329 using MatrixType = detail::pointee<MatrixPtrType>;
330 matrix_data<typename MatrixType::value_type,
331 typename MatrixType::index_type>
332 data{};
333 matrix->write(data);
334 write_raw(os, data, layout);
335}
336
337
351template <typename MatrixPtrType, typename StreamType>
352inline void write_binary(StreamType&& os, MatrixPtrType&& matrix)
353{
354 using MatrixType = detail::pointee<MatrixPtrType>;
355 matrix_data<typename MatrixType::value_type,
356 typename MatrixType::index_type>
357 data{};
358 matrix->write(data);
359 write_binary_raw(os, data);
360}
361
362
363} // namespace gko
364
365
366#endif // GKO_PUBLIC_CORE_BASE_MTX_IO_HPP_
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
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:212
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:238
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:324
void write_raw(std::ostream &os, const matrix_data< ValueType, IndexType > &data, layout_type layout=layout_type::array)
Writes a matrix_data structure to a stream in matrix market format.
void write_binary(StreamType &&os, MatrixPtrType &&matrix)
Writes a matrix into an output stream in binary format.
Definition mtx_io.hpp:352
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:188
layout_type
Specifies the layout type when writing data in matrix market format.
Definition mtx_io.hpp:121
@ 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:155