NeoN
A framework for CFD software
Loading...
Searching...
No Matches
containerFreeFunctions.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 <type_traits>
6#include <tuple>
7#include <Kokkos_Core.hpp>
8
11#include "NeoN/core/view.hpp"
12
13namespace NeoN
14{
15
16
17namespace detail
18{
19
28template<typename ValueType>
29auto deepCopyVisitor(localIdx ssize, const ValueType* srcPtr, ValueType* dstPtr)
30{
31 size_t size = static_cast<size_t>(ssize);
32 return [size, srcPtr, dstPtr](const auto& srcExec, const auto& dstExec)
33 {
34 Kokkos::deep_copy(
35 dstExec.createKokkosView(dstPtr, size), srcExec.createKokkosView(srcPtr, size)
36 );
37 };
38};
39
40}
41
42
50template<template<typename> class ContType, typename ValueType, typename Inner>
51void map(ContType<ValueType>& cont, const Inner inner, std::pair<localIdx, localIdx> range = {0, 0})
52{
53 auto [start, end] = range;
54 if (end == 0)
55 {
56 end = cont.size();
57 }
58 auto contView = cont.view();
60 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { contView[i] = inner(i); }
61 );
62}
63
64
72template<template<typename> class ContType, typename ValueType>
73void fill(
74 ContType<ValueType>& cont,
75 const std::type_identity_t<ValueType> value,
76 std::pair<localIdx, localIdx> range = {0, 0}
77)
78{
79 auto [start, end] = range;
80 NF_DEBUG_ASSERT(start <= end, "Range must be ordered in ascending fashion");
81 if (end == 0)
82 {
83 end = cont.size();
84 }
85 auto viewA = cont.view();
87 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { viewA[i] = value; }
88 );
89}
90
91
99template<template<typename> class ContType, typename ValueType>
101 ContType<ValueType>& cont,
102 const View<const std::type_identity_t<ValueType>> view,
103 std::pair<localIdx, localIdx> range = {0, 0}
104)
105{
106 auto [start, end] = range;
107 if (end == 0)
108 {
109 end = cont.size();
110 }
111 auto contView = cont.view();
113 cont.exec(), {start, end}, KOKKOS_LAMBDA(const localIdx i) { contView[i] = view[i]; }
114 );
115}
116
117template<typename... Args>
118auto copyToHosts(Args&... cont)
119{
120 return std::make_tuple(cont.copyToHost()...);
121}
122
123template<template<typename> class ContType, typename ValueType>
124bool equal(ContType<ValueType>& cont, ValueType value)
125{
126 auto hostCont = cont.copyToHost();
127 auto hostView = hostCont.view();
128 for (localIdx i = 0; i < hostView.size(); i++)
129 {
130 if (hostView[i] != value)
131 {
132 return false;
133 }
134 }
135 return true;
136};
137
138template<template<typename> class ContType, typename ValueType>
139bool equal(const ContType<ValueType>& cont1, const ContType<ValueType>& cont2)
140{
141 auto [hostCont1, hostCont2] = copyToHosts(cont1, cont2);
142 auto [hostView1, hostView2] = views(hostCont1, hostCont2);
143
144 if (hostView1.size() != hostView2.size())
145 {
146 return false;
147 }
148
149 for (localIdx i = 0; i < hostView1.size(); i++)
150 {
151 if (hostView1[i] != hostView2[i])
152 {
153 return false;
154 }
155 }
156
157 return true;
158};
159
160template<template<typename> class ContType, typename ValueType>
161bool equal(const ContType<ValueType>& cont, View<ValueType> view2)
162{
163 auto hostView = cont.copyToHost().view();
164
165 if (hostView.size() != view2.size())
166 {
167 return false;
168 }
169
170 for (localIdx i = 0; i < hostView.size(); i++)
171 {
172 if (hostView[i] != view2[i])
173 {
174 return false;
175 }
176 }
177
178 return true;
179}
180
181}
KOKKOS_INLINE_FUNCTION localIdx size() const
Definition view.hpp:78
#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:211
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:19
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
int32_t localIdx
Definition label.hpp:30
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:108