TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
View_Types.h
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef View_Types_included
17#define View_Types_included
18
19#include <kokkos++.h>
20#include <arch.h>
21#ifdef KOKKOS
22
23template<typename T, int _SHAPE_> struct InnerType { using TYPE = void; };
24template<typename T> struct InnerType<T,1> { using TYPE = T*; };
25template<typename T> struct InnerType<T,2> { using TYPE = T**; };
26template<typename T> struct InnerType<T,3> { using TYPE = T***; };
27template<typename T> struct InnerType<T,4> { using TYPE = T****; };
28
29//I had to do this otherwise ConstViews becomes view of pointers to consts T...
30template<typename T, int _SHAPE_> struct ConstInnerType { using TYPE = void; };
31template<typename T> struct ConstInnerType<T,1> { using TYPE = const T*; };
32template<typename T> struct ConstInnerType<T,2> { using TYPE = const T**; };
33template<typename T> struct ConstInnerType<T,3> { using TYPE = const T***; };
34template<typename T> struct ConstInnerType<T,4> { using TYPE = const T****; };
35
36using unmanaged_memory = Kokkos::MemoryTraits<Kokkos::Unmanaged>;
37
38template<typename T, int _SHAPE_>
39using DeviceView = Kokkos::View<typename InnerType<T, _SHAPE_>::TYPE, Kokkos::LayoutRight, unmanaged_memory>;
40
41// Whatever the compilation type, the host memory space:
42using host_mirror_space = Kokkos::HostSpace;
43
44// The execution space (=where code is run): on the device if compiled for GPU, else CPU.
45using execution_space = DeviceView<double, 1>::execution_space;
46
47// Typedefs for range policies in kernels
48using range_1D = Kokkos::RangePolicy<execution_space>;
49using range_2D = Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<2>>;
50using range_3D = Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<3>>;
51
52// The memory space (=where data is stored): on the device if compiled for GPU, or on CPU otherwise:
53typedef std::conditional< \
54std::is_same<execution_space, Kokkos::DefaultExecutionSpace>::value , \
55DeviceView<double,1>::memory_space, host_mirror_space>::type \
56memory_space;
57
58//for host views:
59//"You do not need to explicitly specify host_execution_space because host_mirror_space already implies that you are using the host execution space."
60//memory space implies execution space
61
62// The actual view type that will be manipulated everywhere in the kernels (a *device* view)
63template<typename T, int _SHAPE_>
64using View = Kokkos::View<typename InnerType<T, _SHAPE_>::TYPE, typename DeviceView<T,_SHAPE_>::array_layout, memory_space, unmanaged_memory>;
65
66// Views on the host that allow conditional execution of loop that are not fully ported to device. They are unmanaged to avoid new allocation
67template<typename T, int _SHAPE_>
68using HostView = Kokkos::View<typename InnerType<T, _SHAPE_>::TYPE, typename DeviceView<T,_SHAPE_>::array_layout, host_mirror_space, unmanaged_memory>;
69template<typename T, int _SHAPE_>
70using ConstView = Kokkos::View<typename ConstInnerType<T, _SHAPE_>::TYPE, typename DeviceView<T,_SHAPE_>::array_layout, memory_space, unmanaged_memory>;
71template<typename T, int _SHAPE_>
72using ConstHostView = Kokkos::View<typename ConstInnerType<T, _SHAPE_>::TYPE, typename DeviceView<T,_SHAPE_>::array_layout, host_mirror_space, unmanaged_memory>;
73
74// Add a const View on GPU with RandomAccess (seems beneficial on Nvidia with nvcc (not nvc++ -cuda!) and very AMD)
75template<typename T, int _SHAPE_>
76using RandomAccessView = Kokkos::View<typename ConstInnerType<T, _SHAPE_>::TYPE, typename DeviceView<T,_SHAPE_>::array_layout, memory_space, Kokkos::MemoryRandomAccess>;
77
78// Handy aliases
79using IntArrView = View<int, 1>;
80using DoubleArrView = View<double, 1>;
81using TIDArrView = View<trustIdType, 1>;
82
83using CIntArrView = ConstView<int, 1>;
84using CDoubleArrView = ConstView<double, 1>;
85using CTIDArrView = ConstView<trustIdType, 1>;
86
87using IntTabView = View<int, 2>;
88using IntTabView3 = View<int, 3>;
89using IntTabView4 = View<int, 4>;
90using CTIDTabView = ConstView<trustIdType, 2>;
91using DoubleTabView = View<double, 2>;
92using DoubleTabView3 = View<double, 3>;
93using DoubleTabView4 = View<double, 4>;
94
95using CIntTabView = ConstView<int, 2>;
96using CIntTabView3 = ConstView<int, 3>;
97using CIntTabView4 = ConstView<int, 4>; // Changed from double to int
98using CDoubleTabView = ConstView<double, 2>;
99using CDoubleTabView3 = ConstView<double, 3>;
100using CDoubleTabView4 = ConstView<double, 4>;
101
102// Host views
103using IntArrHostView = HostView<int, 1>;
104using DoubleArrHostView = HostView<double, 1>;
105
106using CIntArrHostView = ConstHostView<int, 1>;
107using CDoubleArrHostView = ConstHostView<double, 1>;
108
109using IntTabHostView = HostView<int, 2>;
110using IntTabHostView3 = HostView<int, 3>;
111using IntTabHostView4 = HostView<int, 4>;
112using DoubleTabHostView = HostView<double, 2>;
113using DoubleTabHostView3 = HostView<double, 3>;
114using DoubleTabHostView4 = HostView<double, 4>;
115
116using CIntTabHostView = ConstHostView<int, 2>;
117using CIntTabHostView3 = ConstHostView<int, 3>;
118using CIntTabHostView4 = ConstHostView<int, 4>; // Changed from double to int
119using CDoubleTabHostView = ConstHostView<double, 2>;
120using CDoubleTabHostView3 = ConstHostView<double, 3>;
121using CDoubleTabHostView4 = ConstHostView<double, 4>;
122
123// Wrapper functions to build a unmanaged view from a pointer and a set of dimensions
124// Primary template - not defined
125template <typename ViewType, typename _TYPE_, int _SHAPE_, typename _SIZE_>
127
128template <typename ViewType, typename _TYPE_, typename _SIZE_>
129struct ViewCreator<ViewType, _TYPE_, 1, _SIZE_>
130{
131 static ViewType create(_TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
132 {
133 return ViewType(ptr, dims[0]);
134 }
135};
136template <typename ViewType, typename _TYPE_, typename _SIZE_>
137struct ViewCreator<ViewType, _TYPE_, 2, _SIZE_>
138{
139 static ViewType create(_TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
140 {
141 return ViewType(ptr, dims[0], dims[1]);
142 }
143};
144template <typename ViewType, typename _TYPE_, typename _SIZE_>
145struct ViewCreator<ViewType, _TYPE_, 3, _SIZE_>
146{
147 static ViewType create(_TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
148 {
149 return ViewType(ptr, dims[0], dims[1], dims[2]);
150 }
151};
152template <typename ViewType, typename _TYPE_, typename _SIZE_>
153struct ViewCreator<ViewType, _TYPE_, 4, _SIZE_>
154{
155 static ViewType create(_TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
156 {
157 return ViewType(ptr, dims[0], dims[1], dims[2], dims[3]);
158 }
159};
160template <typename ViewType, typename _TYPE_, int _SHAPE_, typename _SIZE_>
161inline ViewType createView(_TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
162{
163 static_assert(_SHAPE_ >= 1 && _SHAPE_ <= 4, "Invalid _SHAPE_!");
165}
166template <typename ViewType, typename _TYPE_, int _SHAPE_, typename _SIZE_>
167inline ViewType createView(const _TYPE_* ptr, const std::array<_SIZE_, 4>& dims)
168{
169 static_assert(_SHAPE_ >= 1 && _SHAPE_ <= 4, "Invalid _SHAPE_!");
170 return ViewCreator<ViewType, _TYPE_, _SHAPE_, _SIZE_>::create(const_cast<_TYPE_*>(ptr), dims);
171}
172extern void kokkos_self_test();
173
174#else // Kokkos not defined
175
176template<int _SHAPE_> using IntView = IntTab;
177template<int _SHAPE_> using DoubleView = DoubleTab;
178
179#define KOKKOS_INLINE_FUNCTION inline
180#endif
181
182#endif
void TYPE
Definition View_Types.h:23
static ViewType create(_TYPE_ *ptr, const std::array< _SIZE_, 4 > &dims)
Definition View_Types.h:131
static ViewType create(_TYPE_ *ptr, const std::array< _SIZE_, 4 > &dims)
Definition View_Types.h:139
static ViewType create(_TYPE_ *ptr, const std::array< _SIZE_, 4 > &dims)
Definition View_Types.h:147
static ViewType create(_TYPE_ *ptr, const std::array< _SIZE_, 4 > &dims)
Definition View_Types.h:155