TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TRUST_2_PDI.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 TRUST_2_PDI_included
17#define TRUST_2_PDI_included
18
19#include <string>
20#include <vector>
21#include <map>
22#include <assert.h>
23#include <TRUSTTab.h>
24#include <Comm_Group_MPI.h>
25#include <PE_Groups.h>
26#include <pdi++.h>
27#ifdef HAS_PDI
28#include <pdi.h>
29#include <paraconf.h>
30#endif
31
32/*! @brief classe TRUST_2_PDI Encapsulation of PDI methods (library used for IO operations). See the website pdi.dev for more info
33 *
34 * PDI needs to be initialized with a YAML file, containing all the data that we want to exchange with the outside world (see the class Ecrire_YAML for details).
35 * Note that every data that will be handled via PDI first needs to be declared in the YAML file that has been used to initialize it!
36 *
37 * Here is how PDI works:
38 * 1) TRUST makes it known to PDI that some of our data is accessible (ie we start sharing a pointer to the data, no copy here!)
39 * 2) PDI notifies the selected plugin that the data is ready
40 * 3) The plugin works with the data and behaves just as we described it in the YAML file
41 * 4) Once the work is done, TRUST can reclaim the data (which means PDI will no longer be able to reach it) and pursue the simulation
42 *
43 * These steps can be completed at once in a single operation or asynchronously
44 * (ie we share the data, we busy ourselves with some other stuff, then we trigger the IO operations and reclaim the data).
45 *
46 * An example on how to use it:
47 * """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
48 * int var = 5;
49 * TRUST_2_PDI::init(yaml_filename);
50 * TRUST_2_PDI pdi_interface;
51 * pdi_interface.start_sharing("pb_var", &var); // the YAML file must contain a data named pb_var
52 * pdi_interface.trigger("event"); // there has to be an event in the YAML that tells us what to do with the data
53 * pdi_interface.stop_sharing_last_variable(); // data is no more available
54 * // the last 3 instructions can be squeezed in one with the methods TRUST_2_PDI::read()/TRUST_2_PDI::write()
55 * TRUST_2_PDI::finalize();
56 * """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
57 */
59{
60public:
61
62 // This method is used to initialize PDI
63 static void init(std::string IO_config)
64 {
65 if(PDI_initialized_)
66 {
67 Cerr << "TRUST_2_PDI::initialize PDI has already been initialized" << finl;
69 }
70
71#ifdef HAS_PDI
72 // PDI initialization, done thanks to the specification tree contained in the yaml file IO_config
73 PC_tree_t tconf = PC_parse_path(IO_config.c_str());
74 PDI_init(PC_get(tconf, ".pdi"));
75 PC_tree_destroy(&tconf);
76#endif /* HAS_PDI */
77
79 PDI_initialized_ = 1;
80 }
81
82 // Method to call to finalize PDI
83 static void finalize()
84 {
85 assert(PDI_initialized_);
86 assert(shared_data_.size() == 0);
87 PDI_initialized_ = 0;
88 PDI_checkpoint_ = 0;
89 PDI_restart_ = 0;
90
91#ifdef HAS_PDI
92 // finalize PDI
93 PDI_finalize();
94#endif
95 }
96
97 // Method to use for sharing all data related to the node's parallelism
99 {
100#ifdef MPI_
102 const Comm_Group_MPI* nodeComm = dynamic_cast<const Comm_Group_MPI*>(&ngrp);
103 if (nodeComm)
104 {
105 // retrieving the communicators for each master of the node groups
107 const Comm_Group_MPI* nodeMaster = dynamic_cast<const Comm_Group_MPI*>(&nm);
108 assert(nodeMaster);
109 const MPI_Comm& masterComm = nodeMaster->get_mpi_comm();
110
111 int nodeId = nodeComm->get_node_id();
112 share_parallelism_impl(nodeComm, nodeId, &masterComm);
113 }
114#endif
115 }
116
117 // Method to use for sharing all data related to any MPI group parallelism
118 static void share_parallelism(const Comm_Group& grp, int group_rank)
119 {
120#ifdef MPI_
121 const Comm_Group_MPI* comm = dynamic_cast<const Comm_Group_MPI*>(&grp);
122 if (comm)
123 share_parallelism_impl(comm, group_rank);
124#endif
125 }
126
127 static void set_PDI_checkpoint(int c) { PDI_checkpoint_ = c; }
128 static void set_PDI_restart(int r) { PDI_restart_ = r; }
129
130 static int is_PDI_initialized() { return PDI_initialized_; }
131 static int is_PDI_checkpoint() { return PDI_checkpoint_; }
132 static int is_PDI_restart() { return PDI_restart_; }
133
134 // Method to use to read the data named name and retrieve it in data
135 void read(const std::string& name, void *data)
136 {
137#ifdef HAS_PDI
138 PDI_expose(name.c_str(), data, PDI_INOUT);
139#endif
140 }
141
142 // Method to use to write the data named name and located in data
143 void write(const std::string& name, const void *data)
144 {
145#ifdef HAS_PDI
146 PDI_expose(name.c_str(), data, PDI_OUT);
147#endif
148 }
149
150 // Method to use to start sharing with PDI the data named name and located in data
151 // (while the data is shared, TRUST can not access it)
152 // This will not write or read anything unless some event is triggered!
153 void TRUST_start_sharing(const std::string& name, const void *data)
154 {
155#ifdef HAS_PDI
156 PDI_share(name.c_str(), data, PDI_OUT);
157#endif
158 shared_data_.push_back(name);
159 }
160
161 // Method to use to trigger an event (to supervise the timing of the writes/reads)
162 void trigger(const std::string& event)
163 {
164#ifdef HAS_PDI
165 PDI_event(event.c_str());
166#endif
167 }
168
169 // Method to use to stop sharing the last variable that we shared with PDI
170 // (which makes it available once again)
172 {
173#ifdef HAS_PDI
174 PDI_reclaim(shared_data_.back().c_str());
175#endif
176 shared_data_.pop_back();
177 }
178
179 // Method to use to stop sharing all variables that we shared with PDI
181 {
182#ifdef HAS_PDI
183 // stop sharing everything starting from the end
184 for(auto it = shared_data_.rbegin(); it != shared_data_.rend(); it++)
185 PDI_reclaim(it->c_str());
186#endif
187 shared_data_.clear();
188 }
189
190// Higher level methods
191 void share_type(const Nom& name, const Nom& type);
192 void get_type(const Nom& name, Nom& type);
193 void share_TRUSTTab_dimensions(const DoubleTab& tab, const Nom& name, int write);
194 void prepareRestart(OWN_PTR(Comm_Group)& nodeGroup, int& last_iteration, double& tinit, int resume_last_time);
195
196private:
197
198#ifdef MPI_
199 static void share_parallelism_impl(const Comm_Group_MPI* nodeComm, int nodeId, const MPI_Comm* masterComm = nullptr)
200 {
201#ifdef HAS_PDI
202 const MPI_Comm& comm = nodeComm->get_mpi_comm();
203 int nodeSz = nodeComm->nproc();
204 int nodeRk = nodeComm->rank();
205 PDI_multi_expose("Parallelism",
206 "nodeSize", &nodeSz, PDI_OUT,
207 "nodeRk", &nodeRk, PDI_OUT,
208 "nodeId", &nodeId, PDI_OUT,
209 "node", &comm, PDI_OUT,
210 nullptr);
211 if (masterComm)
212 PDI_expose("master", masterComm, PDI_OUT);
213#endif
214 }
215#endif
216
217 // data that are currently shared with PDI
218 static std::vector<std::string> shared_data_;
219
220 static int PDI_checkpoint_;
221 static int PDI_restart_;
222 static int PDI_initialized_;
223};
224
225#endif
: Classe Comm_Group_MPI, derivee de la classe abstraite Comm_Group.
: Cette classe decrit un groupe de processeurs sur lesquels
Definition Comm_Group.h:40
int get_node_id() const
Retrieve ID of my numa node.
Definition Comm_Group.h:199
int nproc() const
Renvoie le nombre de processeurs dans le groupe *this.
Definition Comm_Group.h:190
int rank() const
Renvoie le rang du processeur local dans le groupe *this.
Definition Comm_Group.h:182
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
static const Comm_Group & get_node_master()
Renvoie le groupe contenant le maitre de mon noeud.
static const Comm_Group & get_node_group()
Renvoie une reference au groupe sur les noeuds.
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
classe TRUST_2_PDI Encapsulation of PDI methods (library used for IO operations). See the website pdi...
Definition TRUST_2_PDI.h:59
void share_type(const Nom &name, const Nom &type)
Generic method to share the type of a TRUST object.
void read(const std::string &name, void *data)
static void set_PDI_restart(int r)
void stop_sharing_last_variable()
void write(const std::string &name, const void *data)
void prepareRestart(OWN_PTR(Comm_Group)&nodeGroup, int &last_iteration, double &tinit, int resume_last_time)
Generic method to prepare the restart of a computation.
void get_type(const Nom &name, Nom &type)
Generic method to read the type of a TRUST object in the HDF5 file.
static void init(std::string IO_config)
Definition TRUST_2_PDI.h:63
static void set_PDI_checkpoint(int c)
static int is_PDI_checkpoint()
void stop_sharing()
static void finalize()
Definition TRUST_2_PDI.h:83
static int is_PDI_restart()
static void share_node_parallelism()
Definition TRUST_2_PDI.h:98
void share_TRUSTTab_dimensions(const DoubleTab &tab, const Nom &name, int write)
Generic method to share the dimensions of a TRUST DoubleTab with PDI.
static int is_PDI_initialized()
static void share_parallelism(const Comm_Group &grp, int group_rank)
void TRUST_start_sharing(const std::string &name, const void *data)
void trigger(const std::string &event)