TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TRUSTArray_kokkos.tpp
1/****************************************************************************
2* Copyright (c) 2025, 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 TRUSTArr_kokkos_TPP_included
17#define TRUSTArr_kokkos_TPP_included
18
19#include <TRUSTArray.h>
20
21#ifdef KOKKOS
22
23//Deduce dimensions from shape and internal values
24//To be later used in View_Types.h's createView.
25template<typename _TYPE_, typename _SIZE_>
26template <int _SHAPE_>
27std::array<_SIZE_, 4> TRUSTArray<_TYPE_, _SIZE_>::getDims() const
28{
29 // The accessors should never be called with the wrong _SHAPE_
30 bool flattened = check_flattened<_SHAPE_>();
31
32 // If flattened, use size_array() for the first dimension,
33 // otherwise use dimension_tot(0)
34 _SIZE_ dimension_tot_0 = flattened ? this->size_array() : this->dimension_tot(0);
35
36//Useful when casting a 1D Tab into a multi-D View !
37 return { dimension_tot_0,
38 nb_dim_ > 1 ? this->dimension_tot(1) : 0,
39 nb_dim_ > 2 ? this->dimension_tot(2) : 0,
40 nb_dim_ > 3 ? this->dimension_tot(3) : 0
41 };
42
43}
44
45// Create internal DeviceView member
46template<typename _TYPE_, typename _SIZE_>
47template<int _SHAPE_>
49{
50
51 const auto& device_view = get_device_view<_SHAPE_>();
52
53 auto dims = this->getDims<_SHAPE_>();
54
55 // change of alloc or resize triggers re-init (for now - resize could be done better)
56 if(device_view.data() == addrOnDevice(*this) &&
57 (long) device_view.extent(0) == dims[0] &&
58 (_SHAPE_ >= 2 && (long) device_view.extent(1) == dims[1]) &&
59 (_SHAPE_ >= 3 && (long) device_view.extent(2) == dims[2]) &&
60 (_SHAPE_ >= 4 && (long) device_view.extent(3) == dims[3]) )
61 return;
62
63 mapToDevice(*this); // Device memory is allocated
64 auto& mutable_device_view = const_cast<DeviceView<_TYPE_, _SHAPE_>&>(device_view);
65 mutable_device_view = createView<DeviceView<_TYPE_, _SHAPE_>, _TYPE_, _SHAPE_, _SIZE_>(const_cast<_TYPE_ *>(addrOnDevice(*this)), dims);
66}
67
68//Check if the internal value of nb_dim_ (that can be >1 if the Array is a Tab) is compatible with the _SHAPE_
69//argument of the accessors. Morevover, it returns true if you are trying to flatten a Tab into an array, or false otherwise
70template<typename _TYPE_, typename _SIZE_>
71template<int _SHAPE_>
73{
74 //Trying to represent a TRUSTArray with a multi-D View
75#ifndef NDEBUG
76 bool is_array = std::string(typeid(*this).name()).find("TRUSTArray") != std::string::npos;;
77 assert(not(is_array && _SHAPE_>1));
78#endif
79
80 //Mismatch in multi-D Tab dimension and accessor _SHAPE_ value !
81 assert((not(this->nb_dim_>1 && _SHAPE_>1 && _SHAPE_ != this->nb_dim_)));
82
83 // The Tab accessor can sometime be called with _SHAPE_ == 1.
84 // For instance, this can happen when a vect operation is called on a Tab
85 // In this case, we want the View to be flattened with the first dimension as the total size of the tab
86 // Otherwise, this would give a 1D View of dimension equal to the first dimension of the Tab (typically <)
87 // When true is returned, we do a 1D view with dimension size_array(). This is always what we want with _SHAPE_=1
88 return (_SHAPE_==1);
89}
90
91///////////// Read-Only ////////////////////////////
92// Device version (GPU / CPU compiled)
93template<typename _TYPE_, typename _SIZE_> // this one first!!
94template<int _SHAPE_, typename EXEC_SPACE>
95inline std::enable_if_t<is_default_exec_space<EXEC_SPACE>, ConstView<_TYPE_,_SHAPE_> >
97{
98 this->template init_device_view<_SHAPE_>();
99 mapToDevice(*this);
100 return get_device_view<_SHAPE_>();
101}
102
103// GPU compiled, host view version
104template<typename _TYPE_, typename _SIZE_> // this one first!!
105template<int _SHAPE_, typename EXEC_SPACE>
106inline std::enable_if_t<gpu_enabled_is_host_exec_space<EXEC_SPACE>, ConstHostView<_TYPE_,_SHAPE_> >
108{
109 auto dims = this->getDims<_SHAPE_>();
110 return createView<ConstHostView<_TYPE_, _SHAPE_>, _TYPE_, _SHAPE_, _SIZE_>(this->addr(), dims);
111}
112
113
114//////////// Write-only ////////////////////////////
115// Device version (GPU / CPU compiled)
116template<typename _TYPE_, typename _SIZE_> // this one first!!
117template<int _SHAPE_, typename EXEC_SPACE>
118inline std::enable_if_t<is_default_exec_space<EXEC_SPACE>, View<_TYPE_,_SHAPE_> >
120{
121 this->template init_device_view<_SHAPE_>();
122 computeOnTheDevice(*this);
123 return get_device_view<_SHAPE_>();
124}
125
126// GPU compiled, host view version
127template<typename _TYPE_, typename _SIZE_> // this one first!!
128template<int _SHAPE_, typename EXEC_SPACE>
129inline std::enable_if_t<gpu_enabled_is_host_exec_space<EXEC_SPACE>, HostView<_TYPE_,_SHAPE_> >
131{
132 auto dims = this->getDims<_SHAPE_>();
133 return createView<HostView<_TYPE_, _SHAPE_>, _TYPE_, _SHAPE_, _SIZE_>(this->addr(), dims);
134}
135
136//////////// Read-Write ////////////////////////////
137// Device version (GPU / CPU compiled)
138template<typename _TYPE_, typename _SIZE_> // this one first!!
139template<int _SHAPE_, typename EXEC_SPACE>
140inline std::enable_if_t<is_default_exec_space<EXEC_SPACE>, View<_TYPE_,_SHAPE_> >
142{
143 this->template init_device_view<_SHAPE_>();
144 computeOnTheDevice(*this);
145 return get_device_view<_SHAPE_>();
146}
147// GPU compiled, host view version
148template<typename _TYPE_, typename _SIZE_> // this one first!!
149template<int _SHAPE_, typename EXEC_SPACE>
150inline std::enable_if_t<gpu_enabled_is_host_exec_space<EXEC_SPACE>, HostView<_TYPE_,_SHAPE_> >
152{
153 auto dims = this->getDims<_SHAPE_>();
154 return createView<HostView<_TYPE_, _SHAPE_>, _TYPE_, _SHAPE_, _SIZE_>(this->addr(), dims);
155}
156
157#endif
158#endif
159
Represents a an array of int/int64/double/... values.
Definition TRUSTArray.h:81