Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_MatrixMarket.hpp
1
24#ifndef MORPHEUS_MATRIXMARKET_HPP
25#define MORPHEUS_MATRIXMARKET_HPP
26
27#include <impl/Morpheus_MatrixMarket_Impl.hpp>
28
29namespace Morpheus {
30namespace IO {
62template <typename Container, typename Stream>
63void read_matrix_market_stream(Container& container, Stream& input) {
64 static_assert(
65 Morpheus::is_coo_matrix_format_container_v<Container> ||
66 Morpheus::is_dynamic_matrix_format_container_v<Container> ||
67 Morpheus::is_dense_matrix_format_container_v<Container> ||
68 Morpheus::is_dense_vector_format_container_v<Container>,
69 "Container must be either a CooMatrix, DynamicMatrix, DenseMatrix "
70 "or DenseVector container");
71 static_assert(Morpheus::has_host_memory_space_v<Container>,
72 "Container must reside in HostSpace");
73 Morpheus::IO::Impl::read_matrix_market_stream(container, input);
74}
75
91template <typename Container>
92void read_matrix_market_file(Container& container,
93 const std::string& filename) {
94 static_assert(
95 Morpheus::is_coo_matrix_format_container_v<Container> ||
96 Morpheus::is_dynamic_matrix_format_container_v<Container> ||
97 Morpheus::is_dense_matrix_format_container_v<Container> ||
98 Morpheus::is_dense_vector_format_container_v<Container>,
99 "Container must be either a CooMatrix, DynamicMatrix, DenseMatrix "
100 "or DenseVector container");
101 static_assert(Morpheus::has_host_memory_space_v<Container>,
102 "Container must reside in HostSpace");
103 std::ifstream file(filename.c_str());
104
105 if (!file)
106 throw Morpheus::IOException(std::string("unable to open file \"") +
107 filename + std::string("\" for reading"));
108
109#ifdef __APPLE__
110 // WAR OSX-specific issue using rdbuf
111 std::stringstream file_string(std::stringstream::in | std::stringstream::out);
112 std::vector<char> buffer(
113 file.rdbuf()->pubseekoff(0, std::ios::end, std::ios::in));
114 file.rdbuf()->pubseekpos(0, std::ios::in);
115 file.rdbuf()->sgetn(&buffer[0], buffer.size());
116 file_string.write(&buffer[0], buffer.size());
117
118 Morpheus::IO::read_matrix_market_stream(container, file_string);
119#else
121#endif
122}
150template <typename Container, typename Stream>
151void write_matrix_market_stream(const Container& container, Stream& output) {
152 static_assert(
153 Morpheus::is_coo_matrix_format_container_v<Container> ||
154 Morpheus::is_dynamic_matrix_format_container_v<Container> ||
155 Morpheus::is_dense_matrix_format_container_v<Container> ||
156 Morpheus::is_dense_vector_format_container_v<Container>,
157 "Container must be either a CooMatrix, DynamicMatrix, DenseMatrix "
158 "or DenseVector container");
159 static_assert(Morpheus::has_host_memory_space_v<Container>,
160 "Container must reside in HostSpace");
161 Morpheus::IO::Impl::write_matrix_market_stream(container, output);
162}
163
180template <typename Container>
181void write_matrix_market_file(const Container& container,
182 const std::string& filename) {
183 static_assert(
184 Morpheus::is_coo_matrix_format_container_v<Container> ||
185 Morpheus::is_dynamic_matrix_format_container_v<Container> ||
186 Morpheus::is_dense_matrix_format_container_v<Container> ||
187 Morpheus::is_dense_vector_format_container_v<Container>,
188 "Container must be either a CooMatrix, DynamicMatrix, DenseMatrix "
189 "or DenseVector container");
190 static_assert(Morpheus::has_host_memory_space_v<Container>,
191 "Container must reside in HostSpace");
192 std::ofstream file(filename.c_str());
193
194 if (!file)
195 throw Morpheus::IOException(std::string("unable to open file \"") +
196 filename + std::string("\" for writing"));
197
198#ifdef __APPLE__
199 // WAR OSX-specific issue using rdbuf
200 std::stringstream file_string(std::stringstream::in | std::stringstream::out);
201
202 Morpheus::IO::write_matrix_market_stream(container, file_string);
203
204 file.rdbuf()->sputn(file_string.str().c_str(), file_string.str().size());
205#else
207#endif
208 file.close();
209}
212} // namespace IO
213} // namespace Morpheus
214
215#endif // MORPHEUS_MATRIXMARKET_HPP
Definition: Morpheus_Exceptions.hpp:51
void read_matrix_market_file(Container &container, const std::string &filename)
Reads a MatrixMarket file from file.
Definition: Morpheus_MatrixMarket.hpp:92
void read_matrix_market_stream(Container &container, Stream &input)
Reads a MatrixMarket file from a stream.
Definition: Morpheus_MatrixMarket.hpp:63
void write_matrix_market_file(const Container &container, const std::string &filename)
Writes a container to a file in the MatrixMarket file format.
Definition: Morpheus_MatrixMarket.hpp:181
void write_matrix_market_stream(const Container &container, Stream &output)
Writes a container to a stream in the MatrixMarket file format.
Definition: Morpheus_MatrixMarket.hpp:151
Generic Morpheus interfaces.
Definition: dummy.cpp:24