Debian Codegraph @

packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.C

raw
1 // Copyright(C) 1999-2021, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include <catalyst/Iocatalyst_CatalystLogging.h>
8 #include <catalyst/Iocatalyst_CatalystManager.h>
9 #include <fstream>
10
11 namespace Iocatalyst {
12
13 CatalystManager::CatalystManager() { reset(); }
14
15 CatalystManager::~CatalystManager() {}
16
17 void CatalystManager::writeToCatalystLogFile(const Ioss::ParallelUtils &putils,
18 const Ioss::PropertyManager &props)
19 {
20 if (putils.parallel_rank() == 0) {
21 CatalystLogging catLog = CatalystLogging();
22 catLog.setProperties(&props);
23 if (catLog.isCatalystLoggingON()) {
24 catLog.writeToLogFile();
25 }
26 }
27 putils.barrier();
28 }
29
30 std::string CatalystManager::getCatDataPath(const Ioss::PropertyManager &props)
31 {
32 auto inputName = CATALYST_INPUT_DEFAULT;
33 if (props.exists(CATALYST_INPUT_NAME)) {
34 inputName = props.get(CATALYST_INPUT_NAME).get_string();
35 }
36 return CATALYST + FS + CHANNELS + FS + inputName + FS + DATA;
37 }
38
39 CatalystManager::CatalystPipelineID
40 CatalystManager::initialize(const Ioss::PropertyManager &props, const Ioss::ParallelUtils &putils)
41 {
42 if (getManagerState() != mInit) {
43 std::ostringstream errmsg;
44 errmsg << "Catalyst Manager not in mInit state";
45 IOSS_ERROR(errmsg);
46 }
47
48 CatalystManager::CatalystProps catalystProps;
49 catalystProps.catalystPipelineID = catalystOutputIDNumber;
50 incrementOutputCounts();
51
52 if (props.exists(CATALYST_BLOCK_PARSE_JSON_STRING)) {
53 catalystProps.catalystBlockJSON = props.get(CATALYST_BLOCK_PARSE_JSON_STRING).get_string();
54 }
55 else if (props.exists(PHACTORI_JSON_SCRIPT)) {
56 bool readOkay = false;
57 std::string phactoriJSONFilePath = props.get(PHACTORI_JSON_SCRIPT).get_string();
58 if (putils.parallel_rank() == 0) {
59 std::ifstream f(phactoriJSONFilePath);
60 if (f) {
61 std::ostringstream ss;
62 ss << f.rdbuf();
63 catalystProps.catalystBlockJSON = ss.str();
64 readOkay = true;
65 }
66 }
67 this->broadCastStatusCode(readOkay, putils);
68 if (!readOkay) {
69 std::ostringstream errmsg;
70 errmsg << "Unable to read input file: " << phactoriJSONFilePath << "\n";
71 IOSS_ERROR(errmsg);
72 }
73 else {
74 this->broadCastString(catalystProps.catalystBlockJSON, putils);
75 }
76 }
77
78 if (props.exists(CATALYST_SCRIPT)) {
79 catalystProps.catalystPythonFilename = props.get(CATALYST_SCRIPT).get_string();
80 }
81 else {
82 catalystProps.catalystPythonFilename = this->getCatalystPythonDriverPath();
83 }
84
85 if (props.exists(CATALYST_SCRIPT_EXTRA_FILE)) {
86 catalystProps.catalystScriptExtraFile = props.get(CATALYST_SCRIPT_EXTRA_FILE).get_string();
87 }
88
89 if (props.exists(CATALYST_BLOCK_PARSE_INPUT_DECK_NAME)) {
90 catalystProps.catalystInputDeckName =
91 props.get(CATALYST_BLOCK_PARSE_INPUT_DECK_NAME).get_string();
92 }
93
94 if (props.exists(CATALYST_ENABLE_LOGGING)) {
95 catalystProps.enableLogging = props.get(CATALYST_ENABLE_LOGGING).get_int();
96 }
97
98 if (props.exists(CATALYST_DEBUG_LEVEL)) {
99 catalystProps.debugLevel = props.get(CATALYST_DEBUG_LEVEL).get_int();
100 }
101
102 if (props.exists(CATALYST_OUTPUT_DIRECTORY)) {
103 catalystProps.catalystOutputDirectory = props.get(CATALYST_OUTPUT_DIRECTORY).get_string();
104 }
105
106 if (props.exists(CATALYST_INPUT_NAME)) {
107 catalystProps.catalystInputName = props.get(CATALYST_INPUT_NAME).get_string();
108 }
109
110 if (props.exists(CATALYST_MULTI_INPUT_PIPELINE_NAME)) {
111 catalystProps.enableCatalystMultiInputPipeline = true;
112 catalystProps.catalystMultiInputPipelineName =
113 props.get(CATALYST_MULTI_INPUT_PIPELINE_NAME).get_string();
114 }
115
116 catPipes[catalystProps.catalystPipelineID] = catalystProps;
117 return catalystProps.catalystPipelineID;
118 }
119
120 CatalystManager::CatalystProps &CatalystManager::getCatalystProps(CatalystPipelineID id)
121 {
122 if (catPipes.find(id) == catPipes.end()) {
123 std::ostringstream errmsg;
124 errmsg << "Catalyst Pipeline ID does not exist: " << id << "\n";
125 IOSS_ERROR(errmsg);
126 }
127 return catPipes[id];
128 }
129
130 void CatalystManager::execute(CatalystManager::CatalystPipelineID id, int state, double time,
131 conduit_cpp::Node &data)
132 {
133 if (getManagerState() == mFinalize) {
134 std::ostringstream errmsg;
135 errmsg << "Catalyst Manager in mFinalize state, cannot execute()";
136 IOSS_ERROR(errmsg);
137 }
138 auto p = getCatalystProps(id);
139 if (p.pipelineState != pExecute) {
140 std::ostringstream errmsg;
141 errmsg << "Database not in pExecute state, cannot execute()";
142 IOSS_ERROR(errmsg);
143 }
144 if (getManagerState() == mInit) {
145 auto n = getInitializeConduit();
146 catalyst_initialize(conduit_cpp::c_node(&n));
147 managerState = mExecute;
148 }
149
150 conduit_cpp::Node n;
151 addExecuteProps(n, p, state, time);
152
153 if (p.enableCatalystMultiInputPipeline) {
154 setMultiInputWaitState(p.catalystPipelineID, state, time, data);
155 if (canExecuteMultiInputScript(p.catalystPipelineID)) {
156 for (auto cp : catPipes) {
157 addExecuteData(n, cp.second.catalystInputName, cp.second.state, cp.second.time,
158 cp.second.data);
159 }
160 }
161 else {
162 return;
163 }
164 }
165 else {
166 addExecuteData(n, p.catalystInputName, state, time, data);
167 }
168
169 catalyst_execute(conduit_cpp::c_node(&n));
170
171 if (p.enableCatalystMultiInputPipeline) {
172 if (canExecuteMultiInputScript(p.catalystPipelineID)) {
173 clearAllMultiInputWaitStates(p.catalystPipelineID);
174 }
175 }
176 }
177
178 void CatalystManager::finalize(CatalystPipelineID id)
179 {
180 getCatalystProps(id).pipelineState = pFinalize;
181 managerState = mExecute;
182 bool canFinalizeManager = true;
183 for (auto p : catPipes) {
184 canFinalizeManager &= p.second.pipelineState == pFinalize;
185 }
186 if (canFinalizeManager) {
187 managerState = mFinalize;
188 conduit_cpp::Node node;
189 catalyst_finalize(conduit_cpp::c_node(&node));
190 }
191 }
192
193 CatalystManager::pState CatalystManager::getPipelineState(CatalystPipelineID id)
194 {
195 return getCatalystProps(id).pipelineState;
196 }
197
198 conduit_cpp::Node CatalystManager::getInitializeConduit()
199 {
200 conduit_cpp::Node n;
201 for (auto p : catPipes) {
202 addScriptProps(n, p.second);
203 }
204 return n;
205 }
206
207 void CatalystManager::addScriptProps(conduit_cpp::Node &n, const CatalystProps &p)
208 {
209 n[getCatScriptFnamePath(p)] = p.catalystPythonFilename;
210 n[getCatScriptArgsPath(p)].append().set(p.catalystInputName);
211 n[getCatScriptArgsPath(p)].append().set(p.catalystBlockJSON);
212 n[getCatScriptArgsPath(p)].append().set(p.catalystScriptExtraFile);
213 n[getCatScriptArgsPath(p)].append().set(p.catalystInputDeckName);
214 n[getCatScriptArgsPath(p)].append().set(p.catalystOutputDirectory);
215 n[getCatScriptArgsPath(p)].append().set(std::to_string(p.enableLogging));
216 n[getCatScriptArgsPath(p)].append().set(std::to_string(p.debugLevel));
217 }
218
219 void CatalystManager::addExecuteProps(conduit_cpp::Node &n, const CatalystProps &p, int state,
220 double time)
221 {
222 n[getCatStatePath() + TIMESTEP].set(state - 1);
223 n[getCatStatePath() + CYCLE].set(state - 1);
224 n[getCatStatePath() + TIME].set(time);
225 auto scriptName = std::to_string(p.catalystPipelineID);
226 n[getCatStatePath() + PIPELINES + FS + scriptName] = scriptName;
227 }
228
229 void CatalystManager::addExecuteData(conduit_cpp::Node &n, const std::string &channelName,
230 int state, double time, conduit_cpp::Node &data)
231 {
232 auto ipath = getCatChannelsPath() + channelName + FS;
233 n[ipath + TYPE].set(std::string(IOSS));
234 auto dpath = ipath + FS + DATA;
235 n[dpath].set_external(data);
236 n[dpath + FS + TIMESTEP].set(state - 1);
237 n[dpath + FS + CYCLE].set(state - 1);
238 n[dpath + FS + TIME].set(time);
239 }
240
241 void CatalystManager::setMultiInputWaitState(CatalystPipelineID id, int state, double time,
242 conduit_cpp::Node &data)
243 {
244 auto &p = CatalystManager::getInstance().getCatalystProps(id);
245 if (!p.enableCatalystMultiInputPipeline) {
246 std::ostringstream errmsg;
247 errmsg << "Catalyst pipeline is not a multi-input pipeline";
248 IOSS_ERROR(errmsg);
249 }
250 p.pipelineState = pWaitExecute;
251 p.data.set_external(data);
252 p.state = state;
253 p.time = time;
254 }
255
256 bool CatalystManager::canExecuteMultiInputScript(CatalystPipelineID id)
257 {
258 bool canExecute = true;
259 auto &p = CatalystManager::getInstance().getCatalystProps(id);
260 if (!p.enableCatalystMultiInputPipeline) {
261 std::ostringstream errmsg;
262 errmsg << "Catalyst pipeline is not a multi-input pipeline";
263 IOSS_ERROR(errmsg);
264 }
265 const auto name = p.catalystMultiInputPipelineName;
266 for (auto cp : catPipes) {
267 if (cp.second.enableCatalystMultiInputPipeline &&
268 cp.second.catalystMultiInputPipelineName == name) {
269 canExecute &= cp.second.pipelineState == pWaitExecute;
270 }
271 }
272 return canExecute;
273 }
274
275 void CatalystManager::clearAllMultiInputWaitStates(CatalystPipelineID id)
276 {
277 auto &p = CatalystManager::getInstance().getCatalystProps(id);
278 if (!p.enableCatalystMultiInputPipeline) {
279 std::ostringstream errmsg;
280 errmsg << "Catalyst pipeline is not a multi-input pipeline";
281 IOSS_ERROR(errmsg);
282 }
283 const auto name = p.catalystMultiInputPipelineName;
284 for (auto &cp : catPipes) {
285 if (cp.second.enableCatalystMultiInputPipeline &&
286 cp.second.catalystMultiInputPipelineName == name) {
287 cp.second.pipelineState = pExecute;
288 cp.second.data.set(conduit_cpp::Node());
289 }
290 }
291 }
292
293 void CatalystManager::incrementOutputCounts() { catalystOutputIDNumber++; }
294
295 void CatalystManager::broadCastString(IOSS_MAYBE_UNUSED std::string &s,
296 IOSS_MAYBE_UNUSED const Ioss::ParallelUtils &putils)
297 {
298 IOSS_PAR_UNUSED(s);
299 IOSS_PAR_UNUSED(putils);
300 #ifdef SEACAS_HAVE_MPI
301 int size = s.size();
302 putils.broadcast(size);
303 if (putils.parallel_rank() != 0) {
304 s.resize(size);
305 }
306 putils.broadcast(s);
307 #endif
308 }
309
310 void CatalystManager::broadCastStatusCode(IOSS_MAYBE_UNUSED bool &statusCode,
311 IOSS_MAYBE_UNUSED const Ioss::ParallelUtils &putils)
312 {
313 IOSS_PAR_UNUSED(statusCode);
314 IOSS_PAR_UNUSED(putils);
315 #ifdef SEACAS_HAVE_MPI
316
317 int code = statusCode;
318 putils.broadcast(code);
319 statusCode = code;
320 #endif
321 }
322
323 } // namespace Iocatalyst