Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
DenseMatrix/Serial/Morpheus_Convert_Impl.hpp
1
24#ifndef MORPHEUS_DENSEMATRIX_SERIAL_CONVERT_IMPL_HPP
25#define MORPHEUS_DENSEMATRIX_SERIAL_CONVERT_IMPL_HPP
26
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_SERIAL)
29
30#include <Morpheus_SpaceTraits.hpp>
31#include <Morpheus_FormatTraits.hpp>
32#include <Morpheus_FormatTags.hpp>
33#include <Morpheus_Spaces.hpp>
34
35namespace Morpheus {
36namespace Impl {
37
38template <typename ExecSpace, typename SourceType, typename DestinationType>
39void convert(
40 const SourceType& src, DestinationType& dst,
41 typename std::enable_if<
42 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
43 Morpheus::is_dense_matrix_format_container_v<DestinationType> &&
44 Morpheus::has_custom_backend_v<ExecSpace> &&
45 Morpheus::has_serial_execution_space_v<ExecSpace> &&
46 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
47 nullptr) {
48 using size_type = typename SourceType::size_type;
49
50 MORPHEUS_ASSERT((dst.nrows() >= src.nrows()) && (dst.ncols() >= src.ncols()),
51 "Destination matrix must have equal or larger shape to the "
52 "source matrix");
53
54 for (size_type i = 0; i < src.nrows(); i++) {
55 for (size_type j = 0; j < src.ncols(); j++) {
56 dst(i, j) = src(i, j);
57 }
58 }
59}
60
61template <typename ExecSpace, typename SourceType, typename DestinationType>
62void convert(
63 const SourceType& src, DestinationType& dst,
64 typename std::enable_if<
65 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
66 Morpheus::is_dense_vector_format_container_v<DestinationType> &&
67 Morpheus::has_custom_backend_v<ExecSpace> &&
68 Morpheus::has_serial_execution_space_v<ExecSpace> &&
69 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
70 nullptr) {
71 using size_type = typename SourceType::size_type;
72
73 dst.resize(src.nrows() * src.ncols());
74
75 for (size_type i = 0; i < src.nrows(); i++) {
76 for (size_type j = 0; j < src.ncols(); j++) {
77 size_type idx = i * src.ncols() + j;
78 dst(idx) = src(i, j);
79 }
80 }
81}
82
83template <typename ExecSpace, typename SourceType, typename DestinationType>
84void convert(
85 const SourceType& src, DestinationType& dst,
86 typename std::enable_if<
87 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
88 Morpheus::is_coo_matrix_format_container_v<DestinationType> &&
89 Morpheus::has_custom_backend_v<ExecSpace> &&
90 Morpheus::has_serial_execution_space_v<ExecSpace> &&
91 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
92 nullptr) {
93 using size_type = typename SourceType::size_type;
94 using value_type = typename SourceType::value_type;
95
96 // Count non-zeros
97 size_type nnz = 0;
98 for (size_type i = 0; i < src.nrows(); i++) {
99 for (size_type j = 0; j < src.ncols(); j++) {
100 if (src(i, j) != value_type(0)) nnz = nnz + 1;
101 }
102 }
103
104 dst.resize(src.nrows(), src.ncols(), nnz);
105
106 for (size_type i = 0, n = 0; i < src.nrows(); i++) {
107 for (size_type j = 0; j < src.ncols(); j++) {
108 if (src(i, j) != value_type(0)) {
109 dst.row_indices(n) = i;
110 dst.column_indices(n) = j;
111 dst.values(n) = src(i, j);
112 n = n + 1;
113 }
114 }
115 }
116}
117
118template <typename ExecSpace, typename SourceType, typename DestinationType>
119void convert(
120 const SourceType& src, DestinationType& dst,
121 typename std::enable_if<
122 Morpheus::is_coo_matrix_format_container_v<SourceType> &&
123 Morpheus::is_dense_matrix_format_container_v<DestinationType> &&
124 Morpheus::has_custom_backend_v<ExecSpace> &&
125 Morpheus::has_serial_execution_space_v<ExecSpace> &&
126 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
127 nullptr) {
128 using size_type = typename SourceType::size_type;
129
130 dst.resize(src.nrows(), src.ncols());
131
132 for (size_type n = 0; n < src.nnnz(); n++) {
133 size_type i = src.crow_indices(n);
134 size_type j = src.ccolumn_indices(n);
135 dst(i, j) = src.cvalues(n);
136 }
137}
138
139} // namespace Impl
140} // namespace Morpheus
141
142#endif // MORPHEUS_ENABLE_SERIAL
143#endif // MORPHEUS_DENSEMATRIX_SERIAL_CONVERT_IMPL_HPP
Generic Morpheus interfaces.
Definition: dummy.cpp:24