24#ifndef MORPHEUS_DIA_SERIAL_CONVERT_IMPL_HPP
25#define MORPHEUS_DIA_SERIAL_CONVERT_IMPL_HPP
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_SERIAL)
30#include <Morpheus_Exceptions.hpp>
31#include <Morpheus_SpaceTraits.hpp>
32#include <Morpheus_FormatTraits.hpp>
33#include <Morpheus_FormatTags.hpp>
34#include <Morpheus_Spaces.hpp>
36#include <impl/Coo/Serial/Morpheus_Sort_Impl.hpp>
44template <
typename ExecSpace,
typename SourceType,
typename DestinationType>
46 const SourceType& src, DestinationType& dst,
47 typename std::enable_if<
48 Morpheus::is_dia_matrix_format_container_v<SourceType> &&
49 Morpheus::is_dia_matrix_format_container_v<DestinationType> &&
50 Morpheus::has_custom_backend_v<ExecSpace> &&
51 Morpheus::has_serial_execution_space_v<ExecSpace> &&
52 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
54 using size_type =
typename SourceType::size_type;
56 dst.resize(src.nrows(), src.ncols(), src.nnnz(),
57 src.cdiagonal_offsets().size());
60 for (size_type n = 0; n < src.cdiagonal_offsets().size(); n++) {
61 dst.diagonal_offsets(n) = src.cdiagonal_offsets(n);
64 for (size_type j = 0; j < src.cvalues().ncols(); j++) {
65 for (size_type i = 0; i < src.cvalues().nrows(); i++) {
66 dst.values(i, j) = src.cvalues(i, j);
71template <
typename ExecSpace,
typename SourceType,
typename DestinationType>
73 const SourceType& src, DestinationType& dst,
74 typename std::enable_if<
75 Morpheus::is_dia_matrix_format_container_v<SourceType> &&
76 Morpheus::is_coo_matrix_format_container_v<DestinationType> &&
77 Morpheus::has_custom_backend_v<ExecSpace> &&
78 Morpheus::has_serial_execution_space_v<ExecSpace> &&
79 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
81 using index_type =
typename SourceType::index_type;
82 using size_type =
typename SourceType::size_type;
83 using value_type =
typename SourceType::value_type;
85 dst.resize(src.nrows(), src.ncols(), src.nnnz());
87 const size_type ndiag = src.cvalues().ncols();
89 for (size_type i = 0, nnzid = 0; i < ndiag; i++) {
90 const index_type k = src.cdiagonal_offsets(i);
92 const size_type i_start = std::max<index_type>(0, -k);
93 const size_type j_start = std::max<index_type>(0, k);
96 const size_type N = std::min(src.nrows() - i_start, src.ncols() - j_start);
98 for (size_type n = 0; n < N; n++) {
99 const value_type temp = src.cvalues(i_start + n, i);
101 if (temp != value_type(0)) {
102 dst.row_indices(nnzid) = i_start + n;
103 dst.column_indices(nnzid) = j_start + n;
104 dst.values(nnzid) = temp;
110 if (!Impl::is_sorted<ExecSpace>(dst)) {
111 Impl::sort_by_row_and_column<ExecSpace>(dst);
116template <
typename ExecSpace,
typename SourceType,
typename DestinationType>
118 const SourceType& src, DestinationType& dst,
119 typename std::enable_if<
120 Morpheus::is_coo_matrix_format_container_v<SourceType> &&
121 Morpheus::is_dia_matrix_format_container_v<DestinationType> &&
122 Morpheus::has_custom_backend_v<ExecSpace> &&
123 Morpheus::has_serial_execution_space_v<ExecSpace> &&
124 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
126 using index_type =
typename SourceType::index_type;
127 using size_type =
typename SourceType::size_type;
129 if (src.nnnz() == 0) {
130 dst.resize(src.nrows(), src.ncols(), src.nnnz(), 0);
134 std::vector<index_type> diag_map(src.nnnz(), 0);
137 for (size_type n = 0; n < src.nnnz(); n++) {
138 diag_map[n] = src.ccolumn_indices(n) - src.crow_indices(n);
142 std::set<index_type> diag_set(diag_map.begin(), diag_map.end());
143 size_type ndiags = diag_set.size();
145 if (Impl::exceeds_tolerance(src.nrows(), src.nnnz(), ndiags)) {
147 "DiaMatrix fill-in would exceed maximum tolerance");
150 dst.resize(src.nrows(), src.ncols(), src.nnnz(), ndiags);
152 for (
auto it = diag_set.cbegin(); it != diag_set.cend(); ++it) {
153 auto i = std::distance(diag_set.cbegin(), it);
154 dst.diagonal_offsets(i) = *it;
157 for (size_type n = 0; n < src.nnnz(); n++) {
158 for (size_type i = 0; i < ndiags; i++) {
159 if (diag_map[n] == dst.diagonal_offsets(i)) {
160 dst.values(src.crow_indices(n), i) = src.cvalues(n);
Generic Morpheus interfaces.
Definition: dummy.cpp:24