NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
utilities.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024-2025 NeoN authors
3
4#pragma once
5
6#if NF_WITH_GINKGO
7
8#include <ginkgo/ginkgo.hpp>
9#include <ginkgo/extensions/kokkos.hpp>
10
11#include "NeoN/fields/field.hpp"
15
16
17namespace NeoN::la
18{
19
20std::shared_ptr<gko::Executor> getGkoExecutor(Executor exec);
21
22namespace detail
23{
24
25template<typename T>
26gko::array<T> createGkoArray(std::shared_ptr<const gko::Executor> exec, std::span<T> values)
27{
28 return gko::make_array_view(exec, values.size(), values.data());
29}
30
31// template<typename T>
32// gko::detail::const_array_view<T>
33// createConstGkoArray(std::shared_ptr<const gko::Executor> exec, const std::span<const T> values)
34// {
35// return gko::make_const_array_view(exec, values.size(), values.data());
36// }
37
38template<typename ValueType, typename IndexType>
39std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>> createGkoMtx(
40 std::shared_ptr<const gko::Executor> exec, const LinearSystem<ValueType, IndexType>& sys
41)
42{
43 auto nrows = static_cast<gko::dim<2>::dimension_type>(sys.rhs().size());
44 auto mtx = sys.view().matrix;
45 // NOTE we get a const view of the system but need a non const view to vals and indices
46 // auto vals = createConstGkoArray(exec, mtx.values).copy_to_array();
47 auto vals = gko::array<ValueType>::view(
48 exec,
49 static_cast<gko::size_type>(mtx.values.size()),
50 const_cast<ValueType*>(mtx.values.data())
51 );
52 // auto col = createGkoArray(exec, mtx.colIdxs);
53 auto col = gko::array<IndexType>::view(
54 exec,
55 static_cast<gko::size_type>(mtx.colIdxs.size()),
56 const_cast<IndexType*>(mtx.colIdxs.data())
57 );
58 // auto row = createGkoArray(exec, mtx.rowOffs);
59 auto row = gko::array<IndexType>::view(
60 exec,
61 static_cast<gko::size_type>(mtx.rowOffs.size()),
62 const_cast<IndexType*>(mtx.rowOffs.data())
63 );
64 return gko::share(gko::matrix::Csr<ValueType, IndexType>::create(
65 exec, gko::dim<2> {nrows, nrows}, vals, col, row
66 ));
67}
68
69template<typename ValueType>
70std::shared_ptr<gko::matrix::Dense<ValueType>>
71createGkoDense(std::shared_ptr<const gko::Executor> exec, ValueType* ptr, localIdx s)
72{
73 auto size = static_cast<std::size_t>(s);
74 return gko::share(gko::matrix::Dense<ValueType>::create(
75 exec, gko::dim<2> {size, 1}, createGkoArray(exec, std::span {ptr, size}), 1
76 ));
77}
78
79template<typename ValueType>
80std::shared_ptr<gko::matrix::Dense<ValueType>>
81createGkoDense(std::shared_ptr<const gko::Executor> exec, const ValueType* ptr, localIdx s)
82{
83 auto size = static_cast<std::size_t>(s);
84 auto const_array_view = gko::array<ValueType>::const_view(exec, size, ptr);
85 return gko::share(gko::matrix::Dense<ValueType>::create(
86 exec, gko::dim<2> {size, 1}, const_array_view.copy_to_array(), 1
87 ));
88}
89
90}
91
92}
93
94#endif
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16