NeoN
A framework for CFD software
Loading...
Searching...
No Matches
fieldFreeFunctions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3#pragma once
4
5#include <tuple>
6
7#include <Kokkos_Core.hpp>
11#include "NeoN/core/view.hpp"
12
13namespace NeoN
14{
15
16// Forward declaration
17template<typename ValueType>
18class Vector;
19
20
28template<typename T, typename Inner>
29void map(Vector<T>& a, const Inner inner, std::pair<localIdx, localIdx> range = {0, 0})
30{
31 auto [start, end] = range;
32 if (end == 0)
33 {
34 end = a.size();
35 }
36 auto viewA = a.view();
38 a.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = inner(i); }
39 );
40}
41
49template<typename ValueType>
50void fill(
52 const std::type_identity_t<ValueType> value,
53 std::pair<localIdx, localIdx> range = {0, 0}
54)
55{
56 auto [start, end] = range;
57 if (end == 0)
58 {
59 end = a.size();
60 }
61 auto viewA = a.view();
63 a.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = value; }
64 );
65}
66
67
75template<typename ValueType>
78 const View<const std::type_identity_t<ValueType>> b,
79 std::pair<localIdx, localIdx> range = {0, 0}
80)
81{
82 auto [start, end] = range;
83 if (end == 0)
84 {
85 end = a.size();
86 }
87 auto viewA = a.view();
89 a.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = b[i]; }
90 );
91}
92
93template<typename ValueType>
94void scalarMul(Vector<ValueType>& a, const scalar value)
95{
96 auto viewA = a.view();
98 a, KOKKOS_LAMBDA(const localIdx i) { return viewA[i] * value; }
99 );
100}
101
102namespace detail
103{
104template<typename ValueType, typename BinaryOp>
106 Vector<ValueType>& a, const Vector<std::type_identity_t<ValueType>>& b, BinaryOp op
107)
108{
110 auto viewA = a.view();
111 auto viewB = b.view();
113 a, KOKKOS_LAMBDA(const localIdx i) { return op(viewA[i], viewB[i]); }
114 );
115}
116}
117
118template<typename ValueType>
119void add(Vector<ValueType>& a, const Vector<std::type_identity_t<ValueType>>& b)
120{
122 a, b, KOKKOS_LAMBDA(ValueType va, ValueType vb) { return va + vb; }
123 );
124}
125
126
127template<typename ValueType>
128void sub(Vector<ValueType>& a, const Vector<std::type_identity_t<ValueType>>& b)
129{
131 a, b, KOKKOS_LAMBDA(ValueType va, ValueType vb) { return va - vb; }
132 );
133}
134
135template<typename ValueType>
136void mul(Vector<ValueType>& a, const Vector<std::type_identity_t<ValueType>>& b)
137{
139 a, b, KOKKOS_LAMBDA(ValueType va, ValueType vb) { return va * vb; }
140 );
141}
142
143template<typename... Args>
144auto copyToHosts(Args&... fields)
145{
146 return std::make_tuple(fields.copyToHost()...);
147}
148
149template<typename T>
150bool equal(Vector<T>& field, T value)
151{
152 auto hostVector = field.copyToHost();
153 auto hostView = hostVector.view();
154 for (localIdx i = 0; i < hostView.size(); i++)
155 {
156 if (hostView[i] != value)
157 {
158 return false;
159 }
160 }
161 return true;
162};
163
164template<typename T>
165bool equal(const Vector<T>& field, const Vector<T>& field2)
166{
167 auto [hostVector, hostVector2] = copyToHosts(field, field2);
168 auto [hostView, hostView2] = views(hostVector, hostVector2);
169
170 if (hostView.size() != hostView2.size())
171 {
172 return false;
173 }
174
175 for (localIdx i = 0; i < hostView.size(); i++)
176 {
177 if (hostView[i] != hostView2[i])
178 {
179 return false;
180 }
181 }
182
183 return true;
184};
185
186template<typename T>
187bool equal(const Vector<T>& field, View<T> view2)
188{
189 auto hostView = field.copyToHost().view();
190
191 if (hostView.size() != view2.size())
192 {
193 return false;
194 }
195
196 for (localIdx i = 0; i < hostView.size(); i++)
197 {
198 if (hostView[i] != view2[i])
199 {
200 return false;
201 }
202 }
203
204 return true;
205}
206
207} // namespace NeoN
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
localIdx size() const
Gets the size of the field.
Definition vector.hpp:364
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:358
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Definition vector.hpp:192
KOKKOS_INLINE_FUNCTION localIdx size() const
Definition view.hpp:75
#define NeoN_ASSERT_EQUAL_LENGTH(_op1, _op2)
void fieldBinaryOp(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b, BinaryOp op)
bool equal(Vector< T > &field, T value)
void map(Vector< T > &a, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
int32_t localIdx
Definition label.hpp:30
void mul(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void add(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void fill(Vector< ValueType > &a, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a scalar value using a specific executor.
float scalar
Definition scalar.hpp:14
void setVector(Vector< ValueType > &a, const View< const std::type_identity_t< ValueType > > b, std::pair< localIdx, localIdx > range={0, 0})
Set the field with a view of values using a specific executor.
auto copyToHosts(Args &... fields)
void sub(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void scalarMul(Vector< ValueType > &a, const scalar value)
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:105