Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
SimpleTableWriterHelper.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* SimpleTableWriterHelper.cc (C) 2000-2023 */
9/* */
10/* Class allowing writing a SimpleTableInternal to a file. */
11/* Simplifies the use of the writer by managing multiprocess and the names */
12/* of files/directories. */
13/*---------------------------------------------------------------------------*/
14/*---------------------------------------------------------------------------*/
15
16#include "arcane/std/SimpleTableWriterHelper.h"
17
18#include "arcane/utils/StringBuilder.h"
19
20#include "arcane/core/Directory.h"
21#include "arcane/core/IParallelMng.h"
22
23#include <optional>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
35init(const Directory& root_directory, const String& table_name, const String& directory_name)
36{
37 setTableName(table_name);
38 _computeTableName();
39
40 setOutputDirectory(directory_name);
41 _computeOutputDirectory();
42
43 m_root = Directory(root_directory, m_simple_table_reader_writer->fileType());
44 return true;
45}
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
51print(Integer rank)
52{
53 if (rank != -1 && m_simple_table_internal->m_parallel_mng->commRank() != rank)
54 return;
55 m_simple_table_reader_writer->print();
56}
57
62writeFile(const Directory& root_directory, Integer rank)
63{
64 // Finalizing the name and directory of the csv (if not already done).
65 _computeTableName();
66 _computeOutputDirectory();
67
68 // Creating the directory.
69 bool result = SimpleTableReaderWriterUtils::createDirectoryOnlyProcess0(m_simple_table_internal->m_parallel_mng, root_directory);
70 if (!result) {
71 return false;
72 }
73
74 Directory output_directory(root_directory, m_name_output_directory);
75
76 result = SimpleTableReaderWriterUtils::createDirectoryOnlyProcess0(m_simple_table_internal->m_parallel_mng, output_directory);
77 if (!result) {
78 return false;
79 }
80
81 // If we are not the requested process, we return true.
82 // -1 = everyone writes.
83 if (rank != -1 && m_simple_table_internal->m_parallel_mng->commRank() != rank)
84 return true;
85
86 // If rank == -1 and isOneFileByRanksPermited() == false, then only process 0
87 // must write.
88 if ((rank == -1 && !isOneFileByRanksPermited()) && m_simple_table_internal->m_parallel_mng->commRank() != 0)
89 return true;
90
91 return m_simple_table_reader_writer->writeTable(output_directory, m_simple_table_internal->m_table_name);
92}
93
99{
100 return writeFile(m_root, rank);
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
107precision()
108{
109 return m_simple_table_reader_writer->precision();
110}
111
114{
115 m_simple_table_reader_writer->setPrecision(precision);
116}
117
119isFixed()
120{
121 return m_simple_table_reader_writer->isFixed();
122}
123
125setFixed(bool fixed)
126{
127 m_simple_table_reader_writer->setFixed(fixed);
128}
129
132{
133 return m_simple_table_reader_writer->isForcedToUseScientificNotation();
134}
135
137setForcedToUseScientificNotation(bool use_scientific)
138{
139 m_simple_table_reader_writer->setForcedToUseScientificNotation(use_scientific);
140}
141
142/*---------------------------------------------------------------------------*/
143/*---------------------------------------------------------------------------*/
144
147{
148 _computeOutputDirectory();
149 return m_name_output_directory;
150}
151
154{
155 return m_name_output_directory_without_computation;
156}
157
159setOutputDirectory(const String& directory)
160{
161 m_name_output_directory = directory;
162 m_name_output_directory_without_computation = directory;
163 m_name_output_directory_computed = false;
164}
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
170tableName()
171{
172 _computeTableName();
173 return m_simple_table_internal->m_table_name;
174}
175
178{
179 return m_name_table_without_computation;
180}
181
183setTableName(const String& name)
184{
185 m_simple_table_internal->m_table_name = name;
186 m_name_table_without_computation = name;
187 m_name_table_computed = false;
188}
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
194fileName()
195{
196 _computeTableName();
197 return m_simple_table_internal->m_table_name + "." + m_simple_table_reader_writer->fileType();
198}
199
202{
203 _computeOutputDirectory();
204 return Directory(m_root, m_name_output_directory);
205}
206
208rootPath()
209{
210 return m_root;
211}
212
215{
216 _computeTableName();
217 _computeOutputDirectory();
218
219 return m_name_table_one_file_by_ranks_permited || m_name_output_directory_one_file_by_ranks_permited;
220}
221
223fileType()
224{
225 return m_simple_table_reader_writer->fileType();
226}
227
228/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230
232internal()
233{
234 return m_simple_table_internal;
235}
236
239{
240 return m_simple_table_reader_writer;
241}
242
244setReaderWriter(const Ref<ISimpleTableReaderWriter>& simple_table_reader_writer)
245{
246 if (simple_table_reader_writer.isNull())
247 ARCANE_FATAL("The reference passed as parameter is Null.");
248 m_simple_table_reader_writer = simple_table_reader_writer;
249 m_simple_table_internal = m_simple_table_reader_writer->internal();
250}
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
254
255void SimpleTableWriterHelper::
256_computeTableName()
257{
258 if (!m_name_table_computed) {
259 m_simple_table_internal->m_table_name = _computeName(m_name_table_without_computation, m_name_table_one_file_by_ranks_permited);
260 m_name_table_computed = true;
261 }
262}
263
264void SimpleTableWriterHelper::
265_computeOutputDirectory()
266{
267 if (!m_name_output_directory_computed) {
268 m_name_output_directory = _computeName(m_name_output_directory_without_computation, m_name_output_directory_one_file_by_ranks_permited);
269 m_name_output_directory_computed = true;
270 }
271}
272
283_computeName(String name, bool& one_file_by_ranks_permited)
284{
285 one_file_by_ranks_permited = false;
286
287 // Allows bypassing the bug with String::split() if the name starts with '@'.
288 if (name.startsWith("@")) {
289 name = "@" + name;
290 }
291
292 StringUniqueArray string_splited;
293 // We split the string where the @ symbols are located.
294 name.split(string_splited, '@');
295
296 // We process the words between the "@" symbols.
297 if (string_splited.size() > 1) {
298 // We search for "proc_id" in the array (i.e., @proc_id@ in the name).
299 std::optional<Integer> proc_id = string_splited.span().findFirst("proc_id");
300 // We replace "@proc_id@" with the process ID.
301 if (proc_id) {
302 string_splited[proc_id.value()] = String::fromNumber(m_simple_table_internal->m_parallel_mng->commRank());
303 one_file_by_ranks_permited = true;
304 }
305 // Only one process writes.
306 else {
307 one_file_by_ranks_permited = false;
308 }
309
310 // We search for "num_procs" in the array (i.e., @num_procs@ in the name).
311 std::optional<Integer> num_procs = string_splited.span().findFirst("num_procs");
312 // We replace "@num_procs@" with the process ID.
313 if (num_procs) {
314 string_splited[num_procs.value()] = String::fromNumber(m_simple_table_internal->m_parallel_mng->commSize());
315 }
316 }
317
318 // We recombine the chain.
319 StringBuilder combined = "";
320 for (String str : string_splited) {
321 // Allows bypassing the bug with String::split() if there is '@@@' in the name or if the
322 // name starts with '@' (in addition to the first lines of the method).
323 if (str == "@")
324 continue;
325 combined.append(str);
326 }
327
328 return combined.toString();
329}
330
331/*---------------------------------------------------------------------------*/
332/*---------------------------------------------------------------------------*/
333
334} // End namespace Arcane
335
336/*---------------------------------------------------------------------------*/
337/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer size() const
Number of elements in the vector.
Span< const T > span() const
Immutable view of this array.
Class managing a directory.
Definition Directory.h:36
bool isNull() const
Indicates if the counter references a non-null instance.
Reference to an instance.
static bool createDirectoryOnlyProcess0(IParallelMng *parallel_mng, const Directory &directory)
Static method allowing the creation of a directory with multiple processes.
bool init(const Directory &root_directory, const String &table_name, const String &directory_name) override
Method to initialize the object. Specifically, the table name and the directory name that will contai...
String fileType() override
Method to know the file type that will be used.
void setPrecision(Integer precision) override
Method to modify the print precision.
String outputDirectory() override
Method to retrieve the directory name where the tables will be placed.
void setTableName(const String &name) override
Method to set the table name.
void setFixed(bool fixed) override
Method to set or unset the 'std::fixed' flag.
void print(Integer rank) override
Method to display the table.
void setForcedToUseScientificNotation(bool use_scientific) override
Method to set or unset the 'std::scientific' flag.
Ref< SimpleTableInternal > internal() override
Method to retrieve a reference to the SimpleTableInternal object used.
bool isForcedToUseScientificNotation() override
Method to check if the 'std::scientific' flag is active for writing values.
String fileName() override
Method to retrieve the file name.
String outputDirectoryWithoutComputation() override
Method to retrieve the directory name as it was previously provided.
Ref< ISimpleTableReaderWriter > readerWriter() override
Method to retrieve a reference to the ISimpleTableReaderWriter object used.
void setOutputDirectory(const String &directory) override
Method to set the directory where the tables should be saved.
bool writeFile(Integer rank) override
String tableName() override
Method to retrieve the table name.
String tableNameWithoutComputation() override
Method to retrieve the table name as it was previously provided.
void setReaderWriter(const Ref< ISimpleTableReaderWriter > &simple_table_reader_writer) override
Method to set a reference to an ISimpleTableReaderWriter.
Integer precision() override
Method to retrieve the precision currently used for writing values.
Directory rootPath() override
Method to retrieve the path where the implementation saves these tables.
bool isFixed() override
Method to check if the 'std::fixed' flag is active for writing values.
Directory outputPath() override
Method to retrieve the path where the tables will be saved.
String _computeName(String name, bool &one_file_by_ranks_permited)
Method allowing replacement of name symbols by their value.
bool isOneFileByRanksPermited() override
Method to check if the parameters currently held by the implementation allow it to write a file per p...
Unicode character string constructor.
StringBuilder & append(const String &str)
Appends str.
String toString() const
Returns the constructed character string.
bool startsWith(const String &s) const
Indicates if the string starts with the characters of s.
Definition String.cc:1111
void split(StringContainer &str_array, char c) const
Splits the string based on the character c.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
UniqueArray< String > StringUniqueArray
Dynamic 1D array of strings.
Definition UtilsTypes.h:359