TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TRUSTTab_tools.cpp
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#include <TRUSTTab.h>
17#include <TRUSTTab_tools.tpp>
18#include <MD_Vector_seq.h>
19#include <limits>
20
21namespace
22{
23template <typename ExecSpace, typename _TYPE_>
24void local_carre_norme_tab_kernel(const TRUSTTab<_TYPE_>& tableau, TRUSTArray<_TYPE_>& norme_colonne)
25{
26 const TRUSTVect<_TYPE_,int>& vect = tableau;
27 const int lsize = vect.line_size(), vect_size_tot = vect.size_totale();
28 assert(lsize == norme_colonne.size_array());
29
30 int nblocs_left;
31 Block_Iter<int> bloc_itr = ::determine_blocks(VECT_SEQUENTIAL_ITEMS, tableau.get_md_vector(), vect_size_tot, lsize, nblocs_left);
32
33 for (int j = 0; j < lsize; j++) norme_colonne[j] = 0;
34
35 auto tableau_view = tableau.template view_ro<2, ExecSpace>();
36 auto norme_colonne_view = norme_colonne.template view_rw<1, ExecSpace>();
37#ifdef TRUST_USE_GPU
38 if (nblocs_left>3) ToDo_Kokkos("nblocs_left too high, optimize by rewriting as local_operations_vect_bis_generic_kernel");
39#endif
40 for (; nblocs_left; nblocs_left--)
41 {
42 const int begin_bloc = (*(bloc_itr++)), end_bloc = (*(bloc_itr++));
43 if (begin_bloc<end_bloc) // very important: empty bloc at the end would erase norme_colonne
44 {
45 Kokkos::RangePolicy<ExecSpace> policy(begin_bloc, end_bloc);
46
47 for (int j=0; j<lsize; j++) //Outer loop
48 {
49 if (statistics().get_use_gpu()) start_gpu_timer(__KERNEL_NAME__);
50 Kokkos::parallel_reduce(policy,
51 KOKKOS_LAMBDA(const int i, _TYPE_& local_sum)
52 {
53 const _TYPE_ x = tableau_view(i,j);
54 local_sum += x*x;
55 },
56 //Reduce in a subview, enabled by specifying execspace in the reducer !
57 Kokkos::Sum<_TYPE_, ExecSpace>(Kokkos::subview(norme_colonne_view,j)));
58
59 bool kernelOnDevice = is_default_exec_space<ExecSpace>;
60 if (statistics().get_use_gpu()) end_gpu_timer(__KERNEL_NAME__, kernelOnDevice);
61 }
62 }
63 }
64}
65
66}
67
68template <typename _TYPE_>
69void local_carre_norme_tab(const TRUSTTab<_TYPE_>& tableau, TRUSTArray<_TYPE_>& norme_colonne)
70{
71 norme_colonne = 0.;
72 bool kernelOnDevice = tableau.checkDataOnDevice();
73
74 if (kernelOnDevice)
75 local_carre_norme_tab_kernel<Kokkos::DefaultExecutionSpace, _TYPE_>(tableau, norme_colonne);
76 else
77 local_carre_norme_tab_kernel<Kokkos::DefaultHostExecutionSpace, _TYPE_>(tableau, norme_colonne);
78}
79
80namespace
81{
82template <typename ExecSpace, typename _TYPE_>
83void local_max_abs_tab_kernel(const TRUSTTab<_TYPE_>& tableau, TRUSTArray<_TYPE_>& max_colonne)
84{
85 const TRUSTVect<_TYPE_,int>& vect = tableau;
86 const int lsize = vect.line_size(), vect_size_tot = vect.size_totale();
87 assert(lsize == max_colonne.size_array());
88
89 int nblocs_left;
90 Block_Iter<int> bloc_itr = ::determine_blocks(VECT_REAL_ITEMS, tableau.get_md_vector(), vect_size_tot, lsize, nblocs_left);
91
92 for (int j = 0; j < lsize; j++) max_colonne[j] = 0;
93 assert(lsize == max_colonne.size_array());
94
95 auto tableau_view= tableau.template view_ro<2, ExecSpace>();
96 auto max_colonne_view= max_colonne.template view_rw<1, ExecSpace>();
97#ifdef TRUST_USE_GPU
98 if (nblocs_left>3) ToDo_Kokkos("nblocs_left too high, optimize by rewriting as local_operations_vect_bis_generic_kernel");
99#endif
100 for (; nblocs_left; nblocs_left--)
101 {
102 const int begin_bloc = (*(bloc_itr++)), end_bloc = (*(bloc_itr++));
103 if (begin_bloc<end_bloc) // very important: empty bloc at the end would erase max_colonne
104 {
105 Kokkos::RangePolicy<ExecSpace> policy(begin_bloc, end_bloc);
106
107 for (int j=0; j<lsize; j++) //Outer loop
108 {
109 if (statistics().get_use_gpu()) start_gpu_timer(__KERNEL_NAME__);
110 Kokkos::parallel_reduce(policy,
111 KOKKOS_LAMBDA(const int i, _TYPE_& local_max)
112 {
113 const _TYPE_ x = Kokkos::fabs(tableau_view(i,j));
114 local_max = Kokkos::fmax(local_max, x);
115 },
116 //Reduce in a subview, enabled by specifying execspace in the reducer !
117 Kokkos::Max<_TYPE_, ExecSpace>(Kokkos::subview(max_colonne_view,j)));
118
119 bool kernelOnDevice = is_default_exec_space<ExecSpace>;
120 if (statistics().get_use_gpu()) end_gpu_timer(__KERNEL_NAME__, kernelOnDevice);
121 }
122 }
123 }
124}
125
126}
127
128template <typename _TYPE_>
129void local_max_abs_tab(const TRUSTTab<_TYPE_>& tableau, TRUSTArray<_TYPE_>& max_colonne)
130{
131 max_colonne = std::numeric_limits<_TYPE_>::min();
132 bool kernelOnDevice = tableau.checkDataOnDevice();
133
134 if (kernelOnDevice)
135 local_max_abs_tab_kernel<Kokkos::DefaultExecutionSpace, _TYPE_>(tableau, max_colonne);
136 else
137 local_max_abs_tab_kernel<Kokkos::DefaultHostExecutionSpace, _TYPE_>(tableau, max_colonne);
138}
139
140template void local_carre_norme_tab<double>(const TRUSTTab<double,int>& tableau, TRUSTArray<double,int>& norme_colonne);
141template void local_carre_norme_tab<float>(const TRUSTTab<float,int>& tableau, TRUSTArray<float,int>& norme_colonne);
142template void local_max_abs_tab<double>(const TRUSTTab<double,int>& tableau, TRUSTArray<double,int>& max_colonne);
143template void local_max_abs_tab<float>(const TRUSTTab<float,int>& tableau, TRUSTArray<float,int>& max_colonne);
Represents a an array of int/int64/double/... values.
Definition TRUSTArray.h:81
_SIZE_ size_array() const
: Tableau a n entrees pour n<= 4.
Definition TRUSTTab.h:31
_SIZE_ size_totale() const
Definition TRUSTVect.tpp:61
int line_size() const
Definition TRUSTVect.tpp:67
virtual const MD_Vector & get_md_vector() const
Definition TRUSTVect.h:123