NeoN
A framework for CFD software
Loading...
Searching...
No Matches
containerFreeFunctions.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <type_traits>
8#include <tuple>
9#include <Kokkos_Core.hpp>
10
13#include "NeoN/core/view.hpp"
14
15namespace NeoN
16{
17
18
19namespace detail
20{
21
30template<typename ValueType>
31auto deepCopyVisitor(localIdx ssize, const ValueType* srcPtr, ValueType* dstPtr)
32{
33 size_t size = static_cast<size_t>(ssize);
34 return [size, srcPtr, dstPtr](const auto& srcExec, const auto& dstExec)
35 {
36 Kokkos::deep_copy(
37 dstExec.createKokkosView(dstPtr, size), srcExec.createKokkosView(srcPtr, size)
38 );
39 };
40};
41
42}
43
44
52template<template<typename> class ContType, typename ValueType, typename Inner>
53void map(ContType<ValueType>& cont, const Inner inner, std::pair<localIdx, localIdx> range = {0, 0})
54{
55 auto [start, end] = range;
56 if (end == 0)
57 {
58 end = cont.size();
59 }
60 auto contView = cont.view();
62 cont.exec(),
63 {start, end},
64 KOKKOS_LAMBDA(const localIdx i) { contView[i] = inner(i); },
65 "mapField"
66 );
67}
68
69
77template<template<typename> class ContType, typename ValueType>
78void fill(
79 ContType<ValueType>& cont,
80 const std::type_identity_t<ValueType> value,
81 std::pair<localIdx, localIdx> range = {0, 0}
82)
83{
84 auto [start, end] = range;
85 NF_DEBUG_ASSERT(start <= end, "Range must be ordered in ascending fashion");
86 if (end == 0)
87 {
88 end = cont.size();
89 }
90 auto viewA = cont.view();
92 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = value; }, "fill"
93 );
94}
95
96
104template<template<typename> class ContType, typename ValueType>
106 ContType<ValueType>& cont,
107 const View<const std::type_identity_t<ValueType>> view,
108 std::pair<localIdx, localIdx> range = {0, 0}
109)
110{
111 auto [start, end] = range;
112 if (end == 0)
113 {
114 end = cont.size();
115 }
116 auto contView = cont.view();
118 cont.exec(),
119 {start, end},
120 KOKKOS_LAMBDA(const localIdx i) { contView[i] = view[i]; },
121 "setContainer"
122 );
123}
124
125template<typename... Args>
126auto copyToHosts(Args&... cont)
127{
128 return std::make_tuple(cont.copyToHost()...);
129}
130
131template<template<typename> class ContType, typename ValueType>
132bool equal(ContType<ValueType>& cont, ValueType value)
133{
134 auto hostCont = cont.copyToHost();
135 auto hostView = hostCont.view();
136 for (localIdx i = 0; i < hostView.size(); i++)
137 {
138 if (hostView[i] != value)
139 {
140 return false;
141 }
142 }
143 return true;
144};
145
146template<template<typename> class ContType, typename ValueType>
147bool equal(const ContType<ValueType>& cont1, const ContType<ValueType>& cont2)
148{
149 auto [hostCont1, hostCont2] = copyToHosts(cont1, cont2);
150 auto [hostView1, hostView2] = views(hostCont1, hostCont2);
151
152 if (hostView1.size() != hostView2.size())
153 {
154 return false;
155 }
156
157 for (localIdx i = 0; i < hostView1.size(); i++)
158 {
159 if (hostView1[i] != hostView2[i])
160 {
161 return false;
162 }
163 }
164
165 return true;
166};
167
168template<template<typename> class ContType, typename ValueType>
169bool equal(const ContType<ValueType>& cont, View<ValueType> view2)
170{
171 auto hostView = cont.copyToHost().view();
172
173 if (hostView.size() != view2.size())
174 {
175 return false;
176 }
177
178 for (localIdx i = 0; i < hostView.size(); i++)
179 {
180 if (hostView[i] != view2[i])
181 {
182 return false;
183 }
184 }
185
186 return true;
187}
188
189}
KOKKOS_INLINE_FUNCTION localIdx size() const
Definition view.hpp:80
#define NF_DEBUG_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false (only in debu...
Definition error.hpp:213
auto deepCopyVisitor(localIdx ssize, const ValueType *srcPtr, ValueType *dstPtr)
A helper function to simplify the common pattern of copying between and to executor.
Definition array.hpp:20
int32_t localIdx
Definition label.hpp:32
void map(ContType< ValueType > &cont, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
auto copyToHosts(Args &... cont)
bool equal(ContType< ValueType > &cont, ValueType value)
void setContainer(ContType< ValueType > &cont, const View< const std::type_identity_t< ValueType > > view, std::pair< localIdx, localIdx > range={0, 0})
Set the container with a view of values using a specific executor.
void parallelFor(const ExecutorType &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name)
void fill(ContType< ValueType > &cont, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a vector value using a specific executor.
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:110