TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Sortie_Fichier_base.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 <Sortie_Fichier_base.h>
17#include <fstream>
18#include <Process.h>
19#include <Nom.h>
20#include <map>
21#include <string>
22#include <stdlib.h>
23#include <sys/stat.h>
24
25Implemente_base_sans_constructeur_ni_destructeur(Sortie_Fichier_base,"Sortie_Fichier_base",Objet_U);
26
28Sortie& Sortie_Fichier_base::printOn(Sortie& s) const { throw; }
29
31{
32 set_toFlush();
33}
34
35Sortie_Fichier_base::Sortie_Fichier_base(const char* name, IOS_OPEN_MODE mode) : Sortie()
36{
37 ouvrir(name,mode);
38}
39
40Sortie_Fichier_base::~Sortie_Fichier_base()
41{
42 close();
43}
44
45void Sortie_Fichier_base::set_toFlush()
46{
47 // Les acces disques ont ete optimises depuis
48 // donc par defaut on flushe a nouveau car
49 // sinon les sondes pas ecrites...
50 // Si lent, mettre export TRIOU_FLUSHFILES=0 dans son sub_file
51 toFlush_ = 1;
52 char* theValue = getenv("TRUST_FLUSHFILES");
53 if (theValue != nullptr)
54 {
55 toFlush_=atoi(theValue);
56 }
57}
58
59void Sortie_Fichier_base::set_buffer()
60{
61 if (!toFlush_)
62 {
63 char* theValue = getenv("TRUST_BUFFSIZE");
64 // On fixe une valeur a 3000000 par defaut pour buffSize
65 // Elle peut etre changee par la variable d'environnement
66 int buffSize = 3000000;
67 if (theValue != nullptr)
68 {
69 buffSize = atoi(theValue);
70 }
71 if (buffSize>=0)
72 {
73 size_t internalBuffSize = buffSize;
74 internalBuff_ = new char[internalBuffSize];
75 ofstream_->rdbuf()->pubsetbuf(internalBuff_,internalBuffSize);
76 }
77 }
78}
79
81{
82 return *ofstream_;
83}
84
86{
87 if(ofstream_)
88 {
89 ofstream_->flush();
90 ofstream_->close();
91 }
92 if (internalBuff_)
93 {
94 delete[] internalBuff_;
95 internalBuff_ = nullptr;
96 }
97}
98
100{
101 if(ofstream_)
102 ofstream_->precision(pre);
103}
104
106{
107 return (int)ofstream_->precision();
108}
109
110void Sortie_Fichier_base::setf(IOS_FORMAT code)
111{
112 if(ofstream_)
113 ofstream_->setf(code);
114}
115
116std::string Sortie_Fichier_base::root = "";
117int Sortie_Fichier_base::ouvrir(const char* name,IOS_OPEN_MODE mode)
118{
119 struct stat sb;
120 // Create the root directory if it doesn't exit by the master process:
121 if (!root.empty() && stat(root.c_str(), &sb) && Process::je_suis_maitre())
122 {
123 std::string cmd="mkdir -p ";
124 cmd+=root;
125 int err = system(cmd.c_str());
126 if (err)
127 {
128 Cerr << "Sortie_Fichier_base::ouvrir: Error while creating " << root << " folder!" << finl;
130 }
131 }
132 std::string pathname = root;
133 if (!pathname.empty()) pathname+="/";
134 pathname += name;
135 Nom p(pathname);
136 if (++counter_%100==0 && !p.finit_par(".lata") && !p.finit_par(".lml") && !p.finit_par(".lata_single") && !p.finit_par(".med.index"))
137 {
138 Cerr << "Warning, file " << pathname << " has been opened/closed " << counter_ << " times..." << finl;
139 }
140 IOS_OPEN_MODE ios_mod=mode;
141 int new_bin=0;
142 if (bin_)
143 {
144
145 if (ios_mod==ios::out)
146 new_bin=1;
147 else
148 assert((ios_mod==ios::app)||(ios_mod==(ios::app|ios::out)));
149 ios_mod=ios_mod|ios::binary;
150 }
151 ostream_ = std::make_unique<ofstream>(pathname,ios_mod); // will delete any existing ostream_ member
152 ofstream_ = static_cast<ofstream *>(ostream_.get()); // the typed version of the pointer for this class.
153 set_toFlush();
154 set_buffer();
155 if (!ofstream_->good())
156 {
157 Cerr << "Error when opening the file " << pathname << ". File was opened " << counter_ << " time(s) ..." << finl;
158 Cerr << "Either:\n you don't have write rights,\n or your filesystem is very slow and multiple file open/close." << finl;
159 Cerr << "Contact TRUST support team." << finl;
162 }
163
164 if (new_bin && is_64b_)
165 {
166 // Put the 64b marker when requested by is_64b_ flag
167 // In the new 64b mode the save/restart files can stay in 32b (because they correspond to information saved for each proc, where only
168 // 32b is used).
169 // Lata files on the other hand definitely need this.
170 Nom marq("INT64");
171 (*this)<<marq;
172 }
173 return 1;
174}
175
176/*! @brief Force l'ecriture sur disque des donnees dans le tampon Utilise l'implementation de la classe ofstream
177 *
178 * @return (Sortie&) *this
179 */
181{
182 if (toFlush()) ofstream_->flush();
183 return *this;
184}
185
187{
188 return (ofstream_&&get_ofstream().is_open()) ;
189}
190
191void Sortie_Fichier_base::set_root(const std::string dirname)
192{
193 root=dirname;
194 Cerr << "[IO] Setting output directory to: " << root << finl;
195}
bool bin_
Is this a binary flux?
Definition AbstractIO.h:50
bool is_64b_
Will we be reading/writing in 64b? (Init in ctor to avoid including arch.h probably).
Definition AbstractIO.h:51
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
virtual int finit_par(const char *const n) const
Definition Nom.cpp:324
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
friend class Sortie
Definition Objet_U.h:75
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
static int exception_sur_exit
Definition Process.h:163
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
static int je_suis_maitre()
renvoie 1 si on est sur le processeur maitre du groupe courant (c'est a dire me() == 0),...
Definition Process.cpp:86
virtual int ouvrir(const char *name, IOS_OPEN_MODE mode=ios::out)
Sortie & flush() override
Force l'ecriture sur disque des donnees dans le tampon Utilise l'implementation de la classe ofstream...
void precision(int pre) override
void setf(IOS_FORMAT code) override
static void set_root(const std::string dirname)
static std::string root
Classe de base des flux de sortie.
Definition Sortie.h:52
std::unique_ptr< ostream > ostream_
Definition Sortie.h:116