Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
DenseMatrix/OpenMP/Morpheus_Convert_Impl.hpp
1
24#ifndef MORPHEUS_DENSEMATRIX_OPENMP_CONVERT_IMPL_HPP
25#define MORPHEUS_DENSEMATRIX_OPENMP_CONVERT_IMPL_HPP
26
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_OPENMP)
29
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>
35
36namespace Morpheus {
37namespace Impl {
38
39template <typename ExecSpace, typename SourceType, typename DestinationType>
40void convert(
41 const SourceType& src, DestinationType& dst,
42 typename std::enable_if<
43 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
44 Morpheus::is_dense_matrix_format_container_v<DestinationType> &&
45 Morpheus::has_custom_backend_v<ExecSpace> &&
46 Morpheus::has_openmp_execution_space_v<ExecSpace> &&
47 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
48 nullptr) {
49 using size_type = typename SourceType::size_type;
50
51 MORPHEUS_ASSERT((dst.nrows() >= src.nrows()) && (dst.ncols() >= src.ncols()),
52 "Destination matrix must have equal or larger shape to the "
53 "source matrix");
54
55#pragma omp parallel for collapse(2)
56 for (size_type i = 0; i < src.nrows(); i++) {
57 for (size_type j = 0; j < src.ncols(); j++) {
58 dst(i, j) = src(i, j);
59 }
60 }
61}
62
63template <typename ExecSpace, typename SourceType, typename DestinationType>
64void convert(
65 const SourceType& src, DestinationType& dst,
66 typename std::enable_if<
67 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
68 Morpheus::is_dense_vector_format_container_v<DestinationType> &&
69 Morpheus::has_custom_backend_v<ExecSpace> &&
70 Morpheus::has_openmp_execution_space_v<ExecSpace> &&
71 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
72 nullptr) {
73 using size_type = typename SourceType::size_type;
74
75 dst.resize(src.nrows() * src.ncols());
76
77#pragma omp parallel for collapse(2)
78 for (size_type i = 0; i < src.nrows(); i++) {
79 for (size_type j = 0; j < src.ncols(); j++) {
80 size_type idx = i * src.ncols() + j;
81 dst(idx) = src(i, j);
82 }
83 }
84}
85
86template <typename ExecSpace, typename SourceType, typename DestinationType>
87void convert(
88 const SourceType&, DestinationType&,
89 typename std::enable_if<
90 Morpheus::is_dense_matrix_format_container_v<SourceType> &&
91 Morpheus::is_coo_matrix_format_container_v<DestinationType> &&
92 Morpheus::has_custom_backend_v<ExecSpace> &&
93 Morpheus::has_openmp_execution_space_v<ExecSpace> &&
94 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
95 nullptr) {
96 throw Morpheus::NotImplementedException("convert<Kokkos::OpenMP>");
97}
98
99template <typename ExecSpace, typename SourceType, typename DestinationType>
100void convert(
101 const SourceType& src, DestinationType& dst,
102 typename std::enable_if<
103 Morpheus::is_coo_matrix_format_container_v<SourceType> &&
104 Morpheus::is_dense_matrix_format_container_v<DestinationType> &&
105 Morpheus::has_custom_backend_v<ExecSpace> &&
106 Morpheus::has_openmp_execution_space_v<ExecSpace> &&
107 Morpheus::has_access_v<ExecSpace, SourceType, DestinationType>>::type* =
108 nullptr) {
109 using size_type = typename SourceType::size_type;
110
111 dst.resize(src.nrows(), src.ncols());
112
113#pragma omp parallel for
114 for (size_type n = 0; n < src.nnnz(); n++) {
115 size_type i = src.crow_indices(n);
116 size_type j = src.ccolumn_indices(n);
117 dst(i, j) = src.cvalues(n);
118 }
119}
120
121} // namespace Impl
122} // namespace Morpheus
123
124#endif // MORPHEUS_ENABLE_OPENMP
125#endif // MORPHEUS_DENSEMATRIX_OPENMP_CONVERT_IMPL_HPP
Definition: Morpheus_Exceptions.hpp:43
Generic Morpheus interfaces.
Definition: dummy.cpp:24