TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Execute_parallel.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 <LecFicDiffuse_JDD.h>
17#include <Execute_parallel.h>
18#include <Interprete_bloc.h>
19#include <TRUST_Error.h>
20#include <TRUSTArray.h>
21#include <PE_Groups.h>
22#include <Journal.h>
23#include <Param.h>
24
25Implemente_instanciable(Execute_parallel,"Execute_parallel",Interprete);
26// XD execute_parallel interprete execute_parallel BRACE This keyword allows to run several computations in parallel on
27// XD_CONT processors allocated to TRUST. The set of processors is split in N subsets and each subset will read and
28// XD_CONT execute a different data file. Error messages usualy written to stderr and stdout are redirected to .log
29// XD_CONT files (journaling must be activated).
30
32{
33 Cerr << "Execute_parallel::readOn() not coded" << finl;
34 exit();
35 return is;
36}
37
39{
40 Cerr << "Execute_parallel::printOn() not coded" << finl;
41 exit();
42 return os;
43}
44
45/*! @brief Cree une partition des nproc processeurs du calcul pour interpreter N jeux de donnees differents.
46 *
47 * La syntaxe du jeu de donnees est la suivante
48 * Execute_parallel {
49 * liste_cas N cas1 cas2 cas3 ...
50 * [ nb_procs N nproc1 nproc2 nproc3 ... ]
51 * }
52 * "cas1" est le nom du cas (on lit le fichier cas1.data sur disque)
53 * nproc1 est le nombre de processeurs a utiliser pour ce cas
54 * Par defaut, on utilise 1 processeur pour chaque cas
55 * Les sorties Cerr et Cout sont redirigees vers le journal du processeur
56 * maitre de chaque cas.
57 *
58 */
60{
61 Cerr << "Execute_parallel::interpreter to run several cases:" << finl;
62
63 Noms liste_cas;
64 ArrOfInt nb_procs;
65
66 bool disable_journal = false;
67
68 Param param(que_suis_je());
69 param.ajouter("liste_cas", &liste_cas, Param::REQUIRED); // XD_ADD_P listchaine
70 // XD_CONT N datafile1 ... datafileN. datafileX the name of a TRUST data file without the .data extension.
71 param.ajouter("nb_procs", &nb_procs); // XD_ADD_P listentier
72 // XD_CONT nb_procs is the number of processors needed to run each data file. If not given, TRUST assumes that
73 // XD_CONT computations are sequential.
74 param.ajouter_flag("disable_journal", &disable_journal);
76 // Si on n'a pas donne nb_procs, on suppose que ca vaut 1
77 const int n_calculs = liste_cas.size();
78 if (nb_procs.size_array() == 0)
79 {
80 Cerr << "Nb_procs not given, we assume that calculations are sequential."
81 << finl;
82 nb_procs.resize_array(n_calculs);
83 nb_procs = 1;
84 }
85 // Verification de la taille des tableaux
86 if (nb_procs.size_array() != n_calculs)
87 {
88 Cerr << "Error : nb_procs array must have " << n_calculs
89 << " values." << finl;
90 barrier();
91 exit();
92 }
93 if (n_calculs == 0)
94 return is;
95
96 // Verification du contenu :
97 if (min_array(nb_procs) < 1)
98 {
99 Cerr << "Error : processor numbers must be >= 1" << finl;
100 barrier();
101 exit();
102 }
103 int count = 0;
104 for (int i = 0; i < n_calculs; i++)
105 count += nb_procs[i];
106 if (count > nproc())
107 {
108 Cerr << "Error : computations require " << count << " processors." << finl;
109 Cerr << "but only " << nproc() << " processors has been asked." << finl;
110 barrier();
111 exit();
112 }
113
114 // Creation des N groupes de processeurs
115 // (les groupes sont detruits quand le VECT est detruit)
116 VECT(OWN_PTR(Comm_Group)) groupes(n_calculs);
117 count = 0;
118 Nom log_courant("");
119 for (int i = 0; i < n_calculs; i++)
120 {
121 const int n = nb_procs[i];
122 Nom log("");
123 Nom log1(Objet_U::nom_du_cas());
124 log1+="_";
125 char s[20];
126 snprintf(s, 20, "%05d", (int)count);
127 log1+=s;
128 log1+=".log";
129 if (n==1)
130 {
131 log+=log1;
132 log+=" file";
133 }
134 else
135 {
136 Nom log2(Objet_U::nom_du_cas());
137 log2+="_";
138 char s2[20];
139 snprintf(s2, 20, "%05d", (int)(count+n-1));
140 log2+=s2;
141 log2+=".log";
142 log="log files from ";
143 log+=log1;
144 log+=" to ";
145 log+=log2;
146 }
147 Cerr << "Error and standard outputs are redirected into " << log << " for case " << liste_cas[i] << finl;
148 // On stocke dans log_courant car reutilise plus loin
149 if (Process::me()>=count && Process::me()<=count+n-1)
150 log_courant=log;
151
152 ArrOfInt tab(n);
153 for (int j = 0; j < n; j++)
154 tab[j] = count++;
155 PE_Groups::create_group(tab, groupes[i]);
156 }
157 // On lancer une exception si un calcul s'arrete donc
158 // on change le comportement de Process par defaut (MPI_Abort)
160 Cerr << n_calculs << " cases are running..." << finl;
161 // Chaque processeur entre dans son groupe et interprete le jeu de donnees
162 Nom ancien_nom_du_cas(nom_du_cas());
163 const int old_journal_level = get_journal_level();
164 for (int i = 0; i < n_calculs; i++)
165 {
166 if (PE_Groups::enter_group(groupes[i].valeur()))
167 {
168 set_Cerr_to_journal(1);
169 if (disable_journal)
170 change_journal_level(0);
171 Nom nom_fichier(liste_cas[i]);
172 get_set_nom_du_cas() = nom_fichier;
173 Journal(1) << "Execute_parallel: Entering subgroup " << i
174 << " to run case " << nom_fichier << finl;
175
176 nom_fichier += ".data";
177 {
178 // Ouverture du fichier (on cree l'objet LecFicDiffuse a l'interieur
179 // de l'accolade pour detruire le fichier avant de sortir du groupe)
180 LecFicDiffuse_JDD data_file(nom_fichier);
181 data_file.set_check_types(1);
182 // On cree un nouvel interprete. A la fin de la lecture du cas
183 // les objets seront detruits.
184 Interprete_bloc interp;
185 // On utilise les exceptions pour des calculs qui s'arretent
186 int ok=1;
187 try
188 {
189 interp.interpreter_bloc(data_file,
190 Interprete_bloc::FIN /* on attend FIN a la fin du fichier */,
191 0 /* verifie_sans_interpreter=0 */);
192 }
193 catch (TRUST_Error& err)
194 {
195 if (err.get_pe()!=Process::me())
196 {
197 Cerr << err.get_pe() << " <> " << Process::me() << " in Execute_parallel::interpreter." << finl;
199 }
200 ok=0;
201 }
202 set_Cerr_to_journal(0);
203 if (ok)
204 Cerr << "Case " << liste_cas[i] << " has finished. See " << log_courant << finl;
205 else
206 Cerr << "!!! Case " << liste_cas[i] << " has failed. See " << log_courant << " !!!" << finl;
207 }
208 Journal(1) << "Execute_parallel: Exiting subgroup " << i << finl;
210 }
211 }
212 // on revient au comportement standard de trio
214 change_journal_level(old_journal_level);
215 // On attend que tous les processeurs aient fini d'executer leur calcul.
216 barrier();
217 get_set_nom_du_cas() = ancien_nom_du_cas;
218 Cerr << finl << "End of Execute_parallel::interpreter" << finl;
219 return is;
220}
: Cette classe decrit un groupe de processeurs sur lesquels
Definition Comm_Group.h:40
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Interprete permettant l'execution simultanee de plusieurs fichiers .
Entree & interpreter(Entree &) override
Cree une partition des nproc processeurs du calcul pour interpreter N jeux de donnees differents.
Interprete un bloc d'instructions dans le jeu de donnees.
Entree & interpreter_bloc(Entree &is, Bloc_Type bloc_type, int verifier_sans_interpreter)
Interpretation d'un bloc d'instructions prises dans l'entree is.
Classe de base des objets "interprete".
Definition Interprete.h:38
Cette classe implemente les operateurs et les methodes virtuelles de la classe EFichier de la facon s...
void set_check_types(bool flag) override
appelle get_entree_master().
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
Un tableau de chaine de caracteres (VECT(Nom)).
Definition Noms.h:26
friend class Entree
Definition Objet_U.h:76
const Nom & que_suis_je() const
renvoie la chaine identifiant la classe.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
static const Nom & nom_du_cas()
Renvoie une reference constante vers le nom du cas.
Definition Objet_U.cpp:146
static Nom & get_set_nom_du_cas()
Renvoie une reference non constante vers le nom du cas (pour pouvoir le modifier).
Definition Objet_U.cpp:156
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
static int enter_group(const Comm_Group &group)
Si le processeur local appartient au groupe, le groupe courant pour ce processeur devient "group" et ...
static void create_group(const ArrOfInt &liste_pe, OWN_PTR(Comm_Group) &group, int force_Comm_Group_NoParallel=0)
Creation d'un nouveau groupe de processeurs (utilisation possible n'importe ou dans le code).
Definition PE_Groups.cpp:53
static void exit_group()
Retourne dans le groupe ou l'on etait avant le dernier enter_group() reussi (dont le resultat a ete 1...
Helper class to factorize the readOn method of Objet_U classes.
Definition Param.h:112
void ajouter_flag(const char *keyword, const bool *value)
Register a boolean flag whose mere presence switches it to true.
Definition Param.cpp:474
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
@ REQUIRED
Definition Param.h:115
int lire_avec_accolades_depuis(Entree &is)
Parse the parameter block { ... } from is.
Definition Param.cpp:32
static Sortie & Journal(int message_level=0)
Renvoie un objet statique de type Sortie qui sert de journal d'evenements.
Definition Process.cpp:588
static int nproc()
renvoie le nombre de processeurs dans le groupe courant Voir Comm_Group::nproc() et PE_Groups::curren...
Definition Process.cpp:104
static void barrier()
Synchronise tous les processeurs du groupe courant (attend que tous les processeurs soient arrives a ...
Definition Process.cpp:136
static int exception_sur_exit
Definition Process.h:163
static int me()
renvoie mon rang dans le groupe de communication courant.
Definition Process.cpp:125
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52
_SIZE_ size_array() const
void resize_array(_SIZE_ new_size, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)
classe TRUST_Error
Definition TRUST_Error.h:25
const int & get_pe() const
Definition TRUST_Error.h:33