www.cemf.ir
KokkosUtilities.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __KokkosUtilities_hpp__
22 #define __KokkosUtilities_hpp__
23 
24 #include "KokkosTypes.hpp"
25 #include "dataIO.hpp"
26 #include "iOstream.hpp"
27 #include "pFlowMacros.hpp"
28 #include "span.hpp"
29 #include "types.hpp"
30 
31 namespace pFlow
32 {
33 
34 template<typename ExecutionSpace>
36 {
37  return Kokkos::SpaceAccessibility<ExecutionSpace, HostSpace>::accessible;
38 }
39 
40 template<typename ExecutionSpace>
42 {
43  return Kokkos::SpaceAccessibility<
44  ExecutionSpace,
45  DefaultExecutionSpace::memory_space>::accessible;
46 }
47 
49 template<typename ExecutionSpace, typename MemoerySpace>
51 {
52  return Kokkos::SpaceAccessibility<ExecutionSpace, MemoerySpace>::accessible;
53 }
54 
55 template<typename Type, typename... Properties>
58 {
59  Kokkos::realloc(Kokkos::WithoutInitializing, view, len);
60 }
61 
62 template<typename Type, typename... Properties>
65 {
66  Kokkos::realloc(Kokkos::WithoutInitializing, view, len);
67 }
68 
69 template<typename Type, typename... Properties>
72 {
73  reallocNoInit(view, len);
74  Kokkos::deep_copy(view, val);
75 }
76 
77 template<typename Type, typename... Properties>
80 {
81  Kokkos::realloc(view, len1, len2);
82 }
83 
84 template<typename Type, typename... Properties>
87 {
88  Kokkos::realloc(Kokkos::WithoutInitializing, view, len1, len2);
89 }
90 
91 template<typename Type, typename... Properties>
95  uint32 len1,
96  uint32 len2,
97  Type val
98 )
99 {
100  reallocNoInit(view, len1, len2);
101  Kokkos::deep_copy(view, val);
102 }
103 
104 template<typename Type, typename... Properties>
108  uint32 len1,
109  uint32 len2,
110  uint32 len3
111 )
112 {
113  Kokkos::realloc(view, len1, len2, len3);
114 }
115 
116 template<typename Type, typename... Properties>
120  uint32 len1,
121  uint32 len2,
122  uint32 len3
123 )
124 {
125  Kokkos::realloc(Kokkos::WithoutInitializing, view, len1, len2, len3);
126 }
127 
128 template<typename Type, typename... Properties>
132  uint32 len1,
133  uint32 len2,
134  uint32 len3,
135  Type val
136 )
137 {
138  reallocNoInit(view, len1, len2, len3);
139  Kokkos::deep_copy(view, val);
140 }
141 
142 template<typename Type, typename... Properties>
145 {
146  Kokkos::resize(view, newLen);
147 }
148 
149 template<typename Type, typename... Properties>
152 {
153  Kokkos::resize(Kokkos::WithoutInitializing, view, newLen);
154 }
155 
156 template<typename ViewType>
158 swapViews(ViewType& v1, ViewType& v2)
159 {
160  static_assert(
161  std::is_move_assignable_v<ViewType> &&
162  std::is_move_constructible_v<ViewType>,
163  "swapViews arguments must be move assignable and move constructible"
164  );
165 
166  ViewType tmp = std::move(v1);
167  v1 = std::move(v2);
168  v2 = std::move(tmp);
169 }
170 
171 template<typename T1, typename T2>
172 INLINE_FUNCTION_H iOstream&
174 {
175  os << '(' << p.first << " " << p.second << ')';
176  return os;
177 }
178 
179 template<typename T, typename... properties>
180 INLINE_FUNCTION_H span<T>
182 {
183  return span<T>(v.data(), v.size());
184 }
185 
186 template<typename T, typename... properties>
187 INLINE_FUNCTION_H span<T>
189 {
190  return span<T>(v.data(), size);
191 }
192 
193 template<typename T, typename... properties>
194 INLINE_FUNCTION_H span<T>
196 {
197  return span<T>(const_cast<T*>(v.data()), v.size());
198 }
199 
200 template<typename T, typename... properties>
201 INLINE_FUNCTION_H span<T>
203 {
204  return span<T>(const_cast<T*>(v.data()), size);
205 }
206 
207 template<typename T, typename... properties>
208 INLINE_FUNCTION_H iOstream&
210 {
211  using ExSpace = typename ViewType1D<T, properties...>::execution_space;
212 
213  static_assert(
214  isHostAccessible<ExSpace>(), "View memory is not accessible from Host"
215  );
216 
217  span<T> spn(v.data(), v.size());
218  os << spn;
219 
220  return os;
221 }
222 
223 } // pFlow
224 
225 #endif //__KokkosUtilities_hpp__
pFlow::span
Definition: span.hpp:31
pFlow::isDeviceAccessible
INLINE_FUNCTION_H constexpr bool isDeviceAccessible()
Definition: KokkosUtilities.hpp:41
pFlow::reallocFill
INLINE_FUNCTION_H void reallocFill(ViewType1D< Type, Properties... > &view, uint32 len, Type val)
Definition: KokkosUtilities.hpp:71
pFlow::resizeNoInit
INLINE_FUNCTION_H void resizeNoInit(ViewType1D< Type, Properties... > &view, uint32 newLen)
Definition: KokkosUtilities.hpp:151
pFlow::reallocInit
INLINE_FUNCTION_H void reallocInit(ViewType1D< Type, Properties... > &view, uint32 len)
Definition: KokkosUtilities.hpp:57
types.hpp
pFlow::Pair
Kokkos::pair< T1, T2 > Pair
Pair of two variables.
Definition: KokkosTypes.hpp:85
pFlow::ViewType2D
Kokkos::View< T **, properties... > ViewType2D
2D view as an array
Definition: KokkosTypes.hpp:97
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::reallocNoInit
INLINE_FUNCTION_H void reallocNoInit(ViewType1D< Type, Properties... > &view, uint32 len)
Definition: KokkosUtilities.hpp:64
KokkosTypes.hpp
pFlow
Definition: demGeometry.hpp:27
pFlowMacros.hpp
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::resizeInit
INLINE_FUNCTION_H void resizeInit(ViewType1D< Type, Properties... > &view, uint32 newLen)
Definition: KokkosUtilities.hpp:144
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
dataIO.hpp
pFlow::ViewType3D
Kokkos::View< T ***, properties... > ViewType3D
3D view as an array
Definition: KokkosTypes.hpp:101
pFlow::areAccessible
INLINE_FUNCTION_H constexpr bool areAccessible()
Is MemoerySpace accessible from ExecutionSpace.
Definition: KokkosUtilities.hpp:50
iOstream.hpp
pFlow::isHostAccessible
INLINE_FUNCTION_H constexpr bool isHostAccessible()
Definition: KokkosUtilities.hpp:35
pFlow::makeSpan
span< T > makeSpan(std::vector< T > &container)
Definition: stdVectorHelper.hpp:53
span.hpp
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::swapViews
INLINE_FUNCTION_H void swapViews(ViewType &v1, ViewType &v2)
Definition: KokkosUtilities.hpp:158