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(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { contView[i] = inner(i); }
63 );
64}
65
66
74template<template<typename> class ContType, typename ValueType>
75void fill(
76 ContType<ValueType>& cont,
77 const std::type_identity_t<ValueType> value,
78 std::pair<localIdx, localIdx> range = {0, 0}
79)
80{
81 auto [start, end] = range;
82 NF_DEBUG_ASSERT(start <= end, "Range must be ordered in ascending fashion");
83 if (end == 0)
84 {
85 end = cont.size();
86 }
87 auto viewA = cont.view();
89 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = value; }
90 );
91}
92
93
101template<template<typename> class ContType, typename ValueType>
103 ContType<ValueType>& cont,
104 const View<const std::type_identity_t<ValueType>> view,
105 std::pair<localIdx, localIdx> range = {0, 0}
106)
107{
108 auto [start, end] = range;
109 if (end == 0)
110 {
111 end = cont.size();
112 }
113 auto contView = cont.view();
115 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { contView[i] = view[i]; }
116 );
117}
118
119template<typename... Args>
120auto copyToHosts(Args&... cont)
121{
122 return std::make_tuple(cont.copyToHost()...);
123}
124
125template<template<typename> class ContType, typename ValueType>
126bool equal(ContType<ValueType>& cont, ValueType value)
127{
128 auto hostCont = cont.copyToHost();
129 auto hostView = hostCont.view();
130 for (localIdx i = 0; i < hostView.size(); i++)
131 {
132 if (hostView[i] != value)
133 {
134 return false;
135 }
136 }
137 return true;
138};
139
140template<template<typename> class ContType, typename ValueType>
141bool equal(const ContType<ValueType>& cont1, const ContType<ValueType>& cont2)
142{
143 auto [hostCont1, hostCont2] = copyToHosts(cont1, cont2);
144 auto [hostView1, hostView2] = views(hostCont1, hostCont2);
145
146 if (hostView1.size() != hostView2.size())
147 {
148 return false;
149 }
150
151 for (localIdx i = 0; i < hostView1.size(); i++)
152 {
153 if (hostView1[i] != hostView2[i])
154 {
155 return false;
156 }
157 }
158
159 return true;
160};
161
162template<template<typename> class ContType, typename ValueType>
163bool equal(const ContType<ValueType>& cont, View<ValueType> view2)
164{
165 auto hostView = cont.copyToHost().view();
166
167 if (hostView.size() != view2.size())
168 {
169 return false;
170 }
171
172 for (localIdx i = 0; i < hostView.size(); i++)
173 {
174 if (hostView[i] != view2[i])
175 {
176 return false;
177 }
178 }
179
180 return true;
181}
182
183}
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
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
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 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