16#include <Ecrire_YAML.h>
17#include <Equation_base.h>
18#include <Postraitement.h>
19#include <Probleme_base.h>
28 write_checkpoint_restart_file(1 , yaml_fname);
40 write_checkpoint_restart_file(0 , yaml_fname);
50 write_checkpoint_restart_file(0 , yaml_fname);
67void Ecrire_YAML::write_checkpoint_restart_file(
bool save,
const std::string& yaml_fname)
69 std::string text =
"pdi:";
71 declare_metadata(save, text);
72 declare_data(save, text);
75 begin_bloc(
"plugins:", text);
77 add_line(
"trace:", text);
80 add_line(
"mpi:", text);
81 add_line(
"decl_hdf5:", text);
83 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
88 write_file_initialization(i_pb, text);
89 write_config_file_initialization(i_pb, text);
91 write_data_for_checkpoint(i_pb, 1 , text);
92 write_data_for_checkpoint(i_pb, 0 , text);
94 write_time_scheme(1, pbs_[i_pb].filename, text);
96 write_fields_types_for_checkpoint(i_pb, text);
98 write_format_for_checkpoint(pbs_[i_pb].filename, text);
102 write_data_for_restart(i_pb, text);
104 write_config_file(save, i_pb, text);
107 SFichier fic(yaml_fname.c_str());
113void Ecrire_YAML::write_config_file_initialization(
int pb_i, std::string& text)
115 std::string para =
"- file: " + pbs_[pb_i].configFilename_;
116 begin_bloc(para, text);
117 std::string
event =
"on_event: InitConfig";
118 add_line(event, text);
119 add_line(
"collision_policy: replace_and_warn # print a warning if file or any of dataset already exist", text);
121 add_line(
"communicator: $MPI_COMM_SELF", text);
124 begin_bloc(
"write:", text);
125 declare_scalar(
"nb_proc",
"int", text);
132void Ecrire_YAML::write_config_file(
bool save,
int pb_i, std::string& text)
135 std::string para =
"- file: " + pbs_[pb_i].configFilename_;
136 begin_bloc(para, text);
137 std::string
event = save ?
"on_event: WriteConfig" :
"on_event: ReadConfig";
138 add_line(event, text);
140 add_line(
"communicator: $MPI_COMM_SELF", text);
141 std::string ioAction = save ?
"write:" :
"read:";
142 begin_bloc(ioAction, text);
145 declare_scalar(
"nb_proc",
"int", text);
146 declare_scalar(
"nb_nodes",
"int", text);
151 write_partition(save, pb_i, text);
156void Ecrire_YAML::write_partition(
bool save,
int pb_i, std::string& text)
160 add_line(
"# For every processor in the world, save the rank of its group in a unique file", text);
161 add_line(
"# (useful if we later chose to resume the computation with a different number of nodes)", text);
165 add_line(
"# For every processor in the world, read the rank of the group it previously belonged to", text);
166 add_line(
"# (necessary if we are resuming the computation with a different number of nodes)", text);
168 std::string para =
"- file: " + pbs_[pb_i].configFilename_;
170 begin_bloc(para, text);
171 std::string
event = save ?
"on_event: WriteNodeRanks" :
"on_event: ReadNodeRanks";
172 add_line(event, text);
175 add_line(
"communicator: $MPI_COMM_SELF", text);
179 begin_bloc(
"datasets:", text);
180 declare_array(
"nodeRanks",
"int",
"'$nb_proc'", text);
183 std::string ioAction = save ?
"write:" :
"read:";
184 begin_bloc(ioAction, text);
185 write_impl_dataset(
"nodeRanks",
"nodeRanks", text);
192void Ecrire_YAML::write_format_for_checkpoint(
const std::string& fname, std::string& text)
194 std::string format_sauvegarde =
"- file: " + fname;
195 begin_bloc(format_sauvegarde, text);
197 add_line(
"communicator: $master", text);
198 begin_bloc(
"write:", text);
199 write_impl_dataset(
"format_sauvegarde/version",
"version", text);
206void Ecrire_YAML::write_time_scheme(
bool save,
const std::string& fname, std::string& text)
208 std::string time_scheme =
"- file: " + fname;
209 begin_bloc(time_scheme, text);
211 add_line(
"on_event: time_scheme", text);
217 std::string communicator = save ?
"$master" :
"$node";
218 add_line(
"communicator: " + communicator, text);
224 begin_bloc(
"datasets:", text);
225 std::string size =
"\'$nb_iter_max\'";
226 declare_array(
"time/t",
"double", size, text);
230 std::string IO = save ?
"write:" :
"read:";
231 begin_bloc(IO, text);
233 write_TRUST_dataset(
"time/t",
"temps", 0,
"" ,
false, std::vector<std::string>() , text);
235 write_impl_dataset(
"time/t",
"temps_sauvegardes", text);
237 write_impl_dataset(
"time/last_iteration",
"iter", text);
244void Ecrire_YAML::write_file_initialization(
int pb_i, std::string& text)
246 std::string para =
"- file: " + pbs_[pb_i].filename;
247 begin_bloc(para, text);
248 std::string
event =
"on_event: init_" + pbs_[pb_i].pb->le_nom().getString();
249 add_line(event, text);
250 add_line(
"collision_policy: replace_and_warn # print a warning if file or any of dataset already exist", text);
252 add_line(
"communicator: $master", text);
255 begin_bloc(
"write:", text);
256 write_impl_dataset(
"format_sauvegarde/simple_sauvegarde",
"simple_sauvegarde", text);
263void Ecrire_YAML::write_data_for_checkpoint(
int pb_i,
bool local_data, std::string& text)
265 std::string checkpoint =
"- file: " + pbs_[pb_i].filename;
266 begin_bloc(checkpoint, text);
267 std::string event_type = local_data ?
"local_backup_" :
"global_backup_";
268 std::string
event =
"on_event: " + event_type + pbs_[pb_i].pb->le_nom().getString();
269 add_line(event, text);
273 add_line(
"communicator: $node", text);
275 add_line(
"communicator: $master", text);
278 auto add_dataset = [&](
const YAML_data& d,
bool declare)
285 const std::string& name = d.
get_name();
287 const std::string& type = d.
get_type();
291 declare_TRUST_dataset(dname, name, type, nb_dim, is_parallel, text);
296 write_TRUST_dataset(dname, name, nb_dim, cond, is_parallel, attr, text);
300 begin_bloc(
"datasets:", text);
301 for(
const auto& d: pbs_[pb_i].data)
302 add_dataset(d,
true);
306 begin_bloc(
"write:", text);
307 for(
const auto& d: pbs_[pb_i].data)
308 add_dataset(d,
false);
316void Ecrire_YAML::write_fields_types_for_checkpoint(
int pb_i, std::string& text)
318 const std::string& fname = pbs_[pb_i].filename;
319 std::string checkpoint =
"- file: " + fname;
320 begin_bloc(checkpoint, text);
321 const std::string& pb_name = pbs_[pb_i].pb->le_nom().getString();
322 std::string
event =
"on_event: " + pb_name +
"_get_types";
323 add_line(event, text);
325 add_line(
"communicator: $master", text);
328 std::vector<std::string> types;
329 for(
const auto& d: pbs_[pb_i].data)
334 const std::string& name = d.
get_name();
335 std::string type =
"TYPE_" + name;
336 types.push_back(type);
338 std::string sz =
"size_" + type;
342 begin_bloc(
"write:", text);
344 write_impl_dataset(pb_name +
"_TYPES",
"TYPES", types, text);
353void Ecrire_YAML::write_fields_types_for_restart(
int pb_i, std::string& text)
355 const std::string& fname = pbs_[pb_i].filename;
356 const std::string& pb_name = pbs_[pb_i].pb->le_nom().getString();
358 auto add_block = [&](
const std::string& name)
360 std::string restart =
"- file: " + fname;
361 begin_bloc(restart, text);
362 std::string
event =
"on_event: get_" + name;
363 add_line(event, text);
365 add_line(
"communicator: $node", text);
366 begin_bloc(
"read:", text);
367 write_impl_dataset(pb_name +
"_TYPES",
"TYPES", std::vector<std::string> { name }, text);
372 for(
const auto& d: pbs_[pb_i].data)
377 const std::string& name = d.
get_name();
378 std::string type =
"TYPE_" + name;
379 std::string sz =
"size_" + type;
390void Ecrire_YAML::write_data_for_restart(
int pb_i, std::string& text)
392 const std::string& fname = pbs_[pb_i].filename;
394 for(
const auto& d: pbs_[pb_i].data)
399 const std::string& name = d.
get_name();
400 std::string restart =
"- file: " + fname;
401 begin_bloc(restart, text);
403 add_line(
"communicator: $node", text);
405 begin_bloc(
"read:", text);
411 write_TRUST_dataset(dname, name, nb_dim, cond, is_parallel, attr, text);
417 write_time_scheme(0, fname, text);
420 write_fields_types_for_restart(pb_i, text);
425void Ecrire_YAML::declare_metadata(
bool save, std::string& text)
427 begin_bloc(
"metadata: # small values for which PDI keeps a copy", text);
429 add_line(
"# scheme information", text);
432 add_line(
"iter: int # Number of checkpoints performed until now (WARNING: does not correspond to the current iteration in my time loop)", text);
433 add_line(
"nb_iter_max: int # Maximum number of checkpoints (WARNING: if this number is too small, we overwrite the first checkpoints)", text);
434 add_line(
"temps : double # current physical time", text);
438 add_line(
"iter : int # last saved iteration", text);
439 add_line(
"temps : double # physical time from which we want to restart computation", text);
442 add_line(
"# information on format", text);
443 add_line(
"version : int", text);
444 add_line(
"simple_sauvegarde: int", text);
447 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
449 for(
const auto& d: pbs_[i_pb].data)
451 const std::string& name = d.
get_name();
455 std::string size =
"size_TYPE_" + name;
456 declare_scalar(size,
"int", text);
457 std::string tname =
"TYPE_" + name;
458 std::string tsize =
"$size_" + tname;
459 declare_array(tname,
"char", tsize, text);
466 std::string asize = std::to_string(nb_dim);
467 std::string aname =
"dim_" + name;
468 declare_array(aname,
"int", asize, text);
474 std::string glob_dim =
"glob_dim_" + name;
475 declare_scalar(glob_dim,
"int", text);
480 add_line(
"# metadata regarding parallelism", text);
481 add_line(
"# total number of processors for this simulation", text);
482 add_line(
"nb_proc : int", text);
483 add_line(
"# total number of nodes for this simulation", text);
484 add_line(
"nb_nodes : int", text);
487 add_line(
"# MPI communicator for my group", text);
488 add_line(
"node : MPI_Comm", text);
489 add_line(
"# number of processors inside my group", text);
490 add_line(
"nodeSize : int", text);
491 add_line(
"# my rank inside my group", text);
492 add_line(
"nodeRk : int", text);
493 add_line(
"# rank of my group among all the other groups", text);
494 add_line(
"nodeId : int", text);
495 add_line(
"# MPI communicator for masters of each group", text);
496 add_line(
"master : MPI_Comm", text);
497 add_line(
"# List of the node ids of every processor in the world", text);
498 declare_array(
"nodeRanks",
"int",
"[ \'$nb_proc' ]",text);
505void Ecrire_YAML::declare_data(
bool save, std::string& text)
507 begin_bloc(
"data: # data we want to save/restore (essentially fields of unknown)", text);
509 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
511 for(
const auto& d: pbs_[i_pb].data)
513 const std::string& name = d.
get_name();
514 const std::string& type = d.
get_type();
517 declare_scalar(name, type, text);
519 declare_dtab(name, name, type, nb_dim, text);
522 declare_scalar(
"TYPES",
"int", text);
525 declare_array(
"temps_sauvegardes",
"double",
"[ \'$iter + 1' ]",text);
532void Ecrire_YAML::set_data()
534 assert(!pbs_.empty());
536 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
538 const Probleme_base& pb = pbs_[i_pb].pb.valeur();
539 std::vector<YAML_data>& data = pbs_[i_pb].data;
544 data.insert(data.end(), eqs.begin(), eqs.end());
548 data.insert(data.end(), dom.begin(), dom.end());
552 std::vector<YAML_data> post = post_base->data_a_sauvegarder();
553 data.insert(data.end(), post.begin(), post.end());
562 assert(!pbs_.empty());
564 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
568 if(pbs_[i_pb].pb->
le_nom() == pb_name)
570 const std::string& name = nom.
getString();
572 pbs_[i_pb].data.push_back(data);
576 Cerr <<
"Ecrire_Yaml::add_field Trying to add field " << nom <<
" to non-existant problem " << pb_name << finl;
577 Cerr <<
"Add " << pb_name <<
" to the Yaml list of problems first " << finl;
585 assert(!pbs_.empty());
586 for(
unsigned i_pb=0; i_pb<pbs_.size(); i_pb++)
590 if(pbs_[i_pb].pb->
le_nom() == pb_name)
592 const std::string& name = nom.
getString();
595 pbs_[i_pb].data.push_back(data);
599 Cerr <<
"Ecrire_Yaml::add_scalar Trying to add scalar " << nom <<
" to non-existant problem " << pb_name << finl;
600 Cerr <<
"Add " << pb_name <<
" to the Yaml list of problems first " << finl;
609void Ecrire_YAML::declare_scalar(
const std::string& name,
const std::string& type, std::string& text)
611 std::string scal = name +
" : " + type;
612 add_line(scal, text);
622void Ecrire_YAML::declare_array(
const std::string& name,
const std::string& type,
const std::string& size, std::string& text)
624 std::string decl = name +
" : { type: array, subtype: " + type +
", size: " + size +
" }";
625 add_line(decl, text);
635void Ecrire_YAML::declare_dtab(
const std::string& dname,
const std::string& name,
const std::string& type,
int nb_dim, std::string& text)
637 std::string data = dname +
" : { type: array, subtype: " + type +
", size: [ \'$dim_" + name +
"[0]\'";
638 for(
int d=1; d<nb_dim; d++)
639 data = data +
"," +
"\'$dim_" + name +
"[" + std::to_string(d) +
"]\'";
640 data = data +
" ] }";
641 add_line(data, text);
656void Ecrire_YAML::declare_TRUST_dataset(
const std::string& dname,
const std::string& name,
const std::string& type,
int nb_dim,
bool is_parallel, std::string& text)
658 std::string dataset_dim = dname +
": { type: array, subtype: " + type +
", size: [";
660 dataset_dim = dataset_dim +
"\'$nodeSize\', ";
661 dataset_dim = dataset_dim +
"\'$nb_iter_max\'";
662 for(
int d=0; d<nb_dim; d++)
664 if(d == 0 && is_parallel)
665 dataset_dim = dataset_dim +
", \'$glob_dim_" + name +
"\'";
667 dataset_dim = dataset_dim +
", \'$dim_" + name +
"[" + std::to_string(d) +
"]\'";
669 dataset_dim = dataset_dim +
" ] }";
670 add_line(dataset_dim, text);
681void Ecrire_YAML::write_TRUST_dataset(
const std::string& dname,
const std::string& name,
int nb_dim,
const std::string& cond,
bool is_parallel,
const std::vector<std::string>& attributes, std::string& text)
683 std::string header = name +
":";
684 begin_bloc(header, text);
687 std::string condition =
"when: \'" + cond +
"\'";
688 add_line(condition, text);
690 std::string dataset_name =
"dataset: " + dname;
691 add_line(dataset_name, text);
692 write_TRUST_dataset_selection(name, nb_dim, is_parallel, text);
693 if(!attributes.empty())
694 write_attributes(attributes, text);
703void Ecrire_YAML::write_attributes(
const std::vector<std::string>& attributes, std::string& text)
705 begin_bloc(
"attributes:", text);
706 for(
const auto& aname: attributes)
708 std::string attr = aname +
": $" + aname;
709 add_line(attr, text);
721void Ecrire_YAML::write_TRUST_dataset_selection(
const std::string& name,
int nb_dim,
bool is_parallel, std::string& text)
723 std::vector<std::string> sizes;
727 sizes.push_back(
"1");
730 sizes.push_back(
"1");
731 for(
int d=0; d<nb_dim; d++)
732 sizes.push_back(
"\'$dim_" + name +
"[" + std::to_string(d) +
"]\'");
734 std::vector<std::string> offsets;
736 offsets.push_back(
"\'$nodeRk\'");
737 offsets.push_back(
"\'$iter\'");
738 for(
int d=0; d<nb_dim; d++)
739 offsets.push_back(
"0");
741 write_dataset_selection(sizes, offsets, text);
749void Ecrire_YAML::write_dataset_selection(
const std::vector<std::string>& sizes,
const std::vector<std::string>& offsets, std::string& text)
751 assert(!sizes.empty() && sizes.size() == offsets.size());
752 begin_bloc(
"dataset_selection:", text);
753 std::string size =
"size: [";
755 for (
auto sz = std::next(sizes.begin()); sz != sizes.end(); ++sz)
758 add_line(size, text);
759 std::string start =
"start: [";
761 for (
auto ofs = std::next(offsets.begin()); ofs != offsets.end(); ++ofs)
764 add_line(start, text);
774void Ecrire_YAML::write_impl_dataset(
const std::string& dname,
const std::string& name, std::string& text)
776 std::string header = name +
":";
777 begin_bloc(header, text);
778 std::string dataset_name =
"dataset: " + dname;
779 add_line(dataset_name, text);
791void Ecrire_YAML::write_impl_dataset(
const std::string& dname,
const std::string& name,
const std::vector<std::string>& attributes, std::string& text)
793 std::string header = name +
":";
794 begin_bloc(header, text);
795 std::string dataset_name =
"dataset: " + dname;
796 add_line(dataset_name, text);
797 if(!attributes.empty())
798 write_attributes(attributes, text);
virtual std::vector< YAML_data > data_a_sauvegarder(const Probleme_base &pb) const
void add_field(Nom pb, Nom nom, int nb_dim)
Adds a field (name+dimension) to the block of the problem pb_name in the YAML file.
void write_champ_fonc_restart_file(const std::string &filename)
Generate the YAML file that will be read with champ_fonc_reprise.
void write_checkpoint_file(const std::string &yaml_fname)
Generate the YAML file that will be read for checkpoint.
void add_scalar(Nom pb, Nom nom, Nom type, bool is_local)
Adds a scalar (name+type) to the block of the problem pb_name in the YAML file.
void write_restart_file(const std::string &yaml_fname)
Generate the YAML file that will be read for restart.
virtual std::vector< YAML_data > data_a_sauvegarder() const
for PDI IO: retrieve name, type and dimensions of the data to save/restore. This has to be overrode f...
class Nom Une chaine de caractere pour nommer les objets de TRUST
const std::string & getString() const
const Nom & le_nom() const override
Donne le nom de l'Objet_U Methode a surcharger : renvoie "neant" dans cette implementation.
const Domaine & domaine() const
Renvoie le domaine associe au probleme.
Postraitements & postraitements()
virtual int nombre_d_equations() const =0
virtual const Equation_base & equation(int) const =0
static bool is_parallel()
static void barrier()
Synchronise tous les processeurs du groupe courant (attend que tous les processeurs soient arrives a ...
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
static int je_suis_maitre()
renvoie 1 si on est sur le processeur maitre du groupe courant (c'est a dire me() == 0),...
classe YAML_data : collection of all needed information for data to save/restore in order to write th...
const std::string & get_conditions() const
const std::string & get_hdf_name() const
bool save_field_type() const
const std::string & get_name() const
const std::vector< std::string > & get_attributes() const
const std::string & get_type() const