TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
ParserView.h
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 ParserView_included
17#define ParserView_included
18
19#include <Parser_U.h>
20#include <TRUST_Deriv.h>
21#include <TRUST_List.h>
22#include <Constante.h>
23#include <algorithm>
24#include <Stack.h>
25#include <math.h>
26#include <string>
27#include <kokkos++.h>
28#include <Noms.h>
29#include <TRUSTArray.h>
30#include <kokkos++.h>
31#define KOKKOS_IMPL_PUBLIC_INCLUDE
32#include <Kokkos_UniqueToken.hpp>
33
34#ifdef __NVCC__
35// PL: flemme de corriger un warning assez obscur...
36#pragma nv_diag_suppress 20011
37#endif
38class ParserView : public Parser
39{
40public:
41 /**
42 * Constructors (generally by copy of a ParserU)
43 * See an example here: Champ_Generique_Transformation::get_champ()
44 */
46 ParserView(Parser_U& p) : Parser(p.getParser()) {}
47 ParserView(std::string& expr, int nvar) : Parser(expr,nvar) {}
48 void set(Parser_U& p) { Parser::set(p.getParser()); }
49 /**
50 * Parse string
51 */
52 void parseString() override
53 {
55 les_var_view = Kokkos::View<double**>("les_vars", getNbVar(), token.size());
56 PNodes_view = Kokkos::View<PNodePod*>("PNodes device", PNodes.size());
57 // Copy data from std::vector to the host mirror of the Kokkos View
58 auto host_PNodes_view = Kokkos::create_mirror_view(PNodes_view);
59 for (size_t i = 0; i < PNodes.size(); i++)
60 host_PNodes_view(i) = PNodes[i];
61 // Copy host mirror to device
62 Kokkos::deep_copy(PNodes_view, host_PNodes_view);
63 }
64 KOKKOS_INLINE_FUNCTION void setVar(int i, double val, int threadId) const
65 {
66 assert(i>=0 && i<ivar);
67 les_var_view(i, threadId) = val;
68 }
69 KOKKOS_INLINE_FUNCTION double eval(int threadId) const
70 {
71 return eval(PNodes_view[0], threadId);
72 }
73 /**
74 * Token: get a unic threadId to fill safely les_var_view
75 */
76#pragma clang optimize off
77 KOKKOS_INLINE_FUNCTION int acquire() const { return token.acquire(); }
78#pragma clang optimize on
79 KOKKOS_INLINE_FUNCTION void release(int threadId) const { token.release(threadId); }
80private:
81 Kokkos::Experimental::UniqueToken<Kokkos::DefaultExecutionSpace> token;
82 Kokkos::View<double**> les_var_view;
83 Kokkos::View<PNodePod*> PNodes_view;
84
85 struct StackEntry
86 {
87 int node_idx; // Index in PNodes_view array
88 double result;
89 int state; // 0: new, 1: need right, 2: done
90 bool is_root; // To identify if this is the root node passed by reference
91 };
92 KOKKOS_INLINE_FUNCTION double eval(const PNodePod& node, int threadId) const
93 {
94 // Direct evaluation for simple cases of the input node
95 if (node.type == 2) return node.nvalue; // VALUE
96 if (node.type == 4) return les_var_view(node.value, threadId); // VAR
97
98 // Define a small stack structure for the device
99 const int MAX_STACK = 64; // Adjust size as needed
100 StackEntry stack[MAX_STACK];
101
102 // Initialize stack with the input node state
103 int stack_ptr = 0;
104 stack[stack_ptr] = {0, 0.0, 0, true}; // Mark as root node
105
106 while (stack_ptr >= 0)
107 {
108 StackEntry& current = stack[stack_ptr];
109 const PNodePod& current_node = current.is_root ? node : PNodes_view[current.node_idx];
110
111 switch (current_node.type)
112 {
113 case 2: // VALUE
114 current.result = current_node.nvalue;
115 stack_ptr--;
116 break;
117
118 case 4: // VAR
119 current.result = les_var_view(current_node.value, threadId);
120 stack_ptr--;
121 break;
122
123 case 1: // OP
124 switch (current.state)
125 {
126 case 0: // Need to evaluate left
127 if (current_node.left != -1)
128 {
129 current.state = 1;
130 stack_ptr++;
131 stack[stack_ptr] = {current_node.left, 0.0, 0, false};
132 }
133 else
134 {
135 current.result = 0.0;
136 current.state = 1;
137 }
138 break;
139
140 case 1: // Need to evaluate right
141 {
142 double left_result = (stack_ptr < MAX_STACK-1) ? stack[stack_ptr + 1].result : 0.0;
143 if (current_node.right != -1)
144 {
145 current.state = 2;
146 stack[stack_ptr].result = left_result; // Save left result
147 stack_ptr++;
148 stack[stack_ptr] = {current_node.right, 0.0, 0, false};
149 }
150 else
151 {
152 current.result = const_cast<ParserView*>(this)->evalOp(current_node, left_result, 0.0);
153 stack_ptr--;
154 }
155 }
156 break;
157
158 case 2: // Both sides evaluated
159 {
160 double right_result = stack[stack_ptr + 1].result;
161 current.result = const_cast<ParserView*>(this)->evalOp(current_node, current.result, right_result);
162 stack_ptr--;
163 }
164 break;
165 }
166 break;
167
168 case 3: // FUNCTION
169 if (current.state == 0)
170 {
171 if (current_node.value <= 0 && current_node.left != -1)
172 {
173 current.state = 1;
174 stack_ptr++;
175 stack[stack_ptr] = {current_node.left, 0.0, 0, false};
176 }
177 else
178 {
179 current.result = const_cast<ParserView*>(this)->evalFunc(current_node, 0.0);
180 stack_ptr--;
181 }
182 }
183 else
184 {
185 double arg = stack[stack_ptr + 1].result;
186 current.result = const_cast<ParserView*>(this)->evalFunc(current_node, arg);
187 stack_ptr--;
188 }
189 break;
190
191 default:
192 Process::Kokkos_exit("method eval : Unknown type for this node !!!");
193 return 0;
194 }
195 }
196
197 return stack[0].result;
198 }
199};
200#ifdef __NVCC__
201#pragma diag_warning 20011
202#endif
203#endif
int right
Definition PNode.h:50
int value
Definition PNode.h:52
double nvalue
Definition PNode.h:53
int left
Definition PNode.h:49
int type
Definition PNode.h:51
void set(Parser_U &p)
Definition ParserView.h:48
KOKKOS_INLINE_FUNCTION int acquire() const
Definition ParserView.h:77
KOKKOS_INLINE_FUNCTION void setVar(int i, double val, int threadId) const
Definition ParserView.h:64
ParserView(Parser_U &p)
Definition ParserView.h:46
void parseString() override
Definition ParserView.h:52
KOKKOS_INLINE_FUNCTION void release(int threadId) const
Definition ParserView.h:79
KOKKOS_INLINE_FUNCTION double eval(int threadId) const
Definition ParserView.h:69
ParserView(std::string &expr, int nvar)
Definition ParserView.h:47
classe Parser_U Version de la classe Parser, derivant de Objet_U.
Definition Parser_U.h:32
Parser & getParser()
Definition Parser_U.h:101
Parser()
Definition Parser.cpp:25
int ivar
Definition Parser.h:167
std::vector< PNodePod > PNodes
Definition Parser.h:161
KOKKOS_INLINE_FUNCTION double evalFunc(const PNodePod &node, double x)
Definition Parser.h:177
void set(const Parser &)
Definition Parser.cpp:53
double eval()
Definition Parser.h:68
int state
Definition Parser.h:153
int getNbVar()
Definition Parser.h:100
virtual void parseString()
Definition Parser.cpp:124
KOKKOS_INLINE_FUNCTION double evalOp(const PNodePod &node, double x, double y)
Definition Parser.h:257
static KOKKOS_INLINE_FUNCTION void Kokkos_exit(const char *)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.h:173