Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
DefaultBackwardMng.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/* DefaultBackwardMng.cc (C) 2000-2016 */
9/* */
10/* Default implementation of a backward strategy. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/DefaultBackwardMng.h"
15
16#include "arcane/core/IVariableFilter.h"
17#include "arcane/core/IVariable.h"
18#include "arcane/core/IVariableMng.h"
19#include "arcane/core/ISubDomain.h"
20#include "arcane/core/IParallelMng.h"
21#include "arcane/core/CommonVariables.h"
22#include "arcane/core/Timer.h"
23#include "arcane/core/VariableCollection.h"
24
25#include "arcane/utils/ITraceMng.h"
26
27#include "arcane/impl/MemoryDataReaderWriter.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
39: public IVariableFilter
40{
41 public:
42
43 virtual bool applyFilter(IVariable& var)
44 {
45 if (!var.isUsed())
46 return false;
48 return false;
49 return true;
50 }
51};
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56DefaultBackwardMng::
57DefaultBackwardMng(ITraceMng* trace, ISubDomain* sub_domain)
58: m_trace(trace)
59, m_sub_domain(sub_domain)
60, m_filter(0)
61, m_data_io(0)
62, m_backward_time(-1)
63, m_period(0)
64, m_first_save(true)
65, m_action_refused(true)
66, m_sequence(SEQNothing)
67{
68 ARCANE_ASSERT((m_trace), ("ITraceMng pointer null"));
69 ARCANE_ASSERT((m_sub_domain), ("ISubDomain pointer null"));
70
71 m_filter = new RestoreVariableFilter();
72
73 m_data_io = new MemoryDataReaderWriter(trace);
74}
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79DefaultBackwardMng::
80~DefaultBackwardMng()
81{
82 delete m_filter;
83
84 delete m_data_io;
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
91clear()
92{
93 m_data_io->free();
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
101{
102 if (!m_action_refused) {
103 m_trace->pfatal() << "Trying to go backward during an action phase";
104 }
105
106 // If already rolling back, we cannot continue
107 if (m_sequence == SEQRestore) {
108 m_trace->pfatal() << "Impossible to go backward while it is already going backward";
109 }
110
112
113 m_trace->info() << "Execution of the backward mode !";
114}
115
116/*---------------------------------------------------------------------------*/
117/*---------------------------------------------------------------------------*/
118
119void DefaultBackwardMng::
120_checkValidAction()
121{
123 throw FatalErrorException(A_FUNCINFO, "Action requested outside the authorized period");
124}
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
131{
132 _checkValidAction();
133 if (m_sequence == SEQRestore) {
134 _restore();
135 return true;
136 }
137 return false;
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
144checkAndApplySave(bool is_forced)
145{
146 _checkValidAction();
147 m_action_refused = true;
148 _checkSave(is_forced);
149 if (m_sequence == SEQSave) {
150 _save();
151 return true;
152 }
153
154 if (m_sequence == SEQForceSave) {
155 m_trace->info() << "Physical time of the last backward reached or passed. Save is forced";
156 _save();
157 return true;
158 }
159
160 return false;
161}
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165
166void DefaultBackwardMng::
167_restore()
168{
169 m_backward_time = m_sub_domain->commonVariables().globalTime();
170
171 m_sub_domain->variableMng()->readVariables(m_data_io, m_filter);
172
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179void DefaultBackwardMng::
180_save()
181{
182 Timer::Action ts_action(m_sub_domain, "RestoreSave");
183
184 m_trace->info() << "Saving variable before going backward";
185
186 m_sub_domain->variableMng()->writeVariables(m_data_io, m_filter);
187
189
190 m_backward_time = -1.;
191
192 const Real var_mem = m_sub_domain->variableMng()->exportSize(VariableList());
193 const Real max_var_mem = m_sub_domain->parallelMng()->reduce(Parallel::ReduceMax, var_mem);
194
195 m_trace->info() << "Memory allocated for the variables (in Mo): used="
196 << var_mem << " max=" << max_var_mem;
197
198 m_first_save = false;
199}
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
206{
207 if (!m_action_refused) {
208 m_trace->pfatal() << "Begin of action requested before the end";
209 }
210
211 m_action_refused = false;
212}
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217void DefaultBackwardMng::
218_checkSave(bool is_forced)
219{
220 if (m_period == 0) {
222 return;
223 }
224
225 if (m_sequence == SEQRestore)
226 return;
227
228 const Real time = m_sub_domain->commonVariables().globalTime();
229
230 if (m_backward_time >= 0. && m_backward_time < time) {
232 return;
233 }
234
235 // Do not save data while rollback is active
236 if (m_backward_time >= 0.) {
238 return;
239 }
240
241 const Integer iteration = m_sub_domain->commonVariables().globalIteration();
242
243 bool do_save = false;
244
245 if (iteration > 1) {
246 do_save |= m_first_save;
247 do_save |= (iteration % m_period) == 0;
248 }
249
250 if (is_forced)
251 do_save = true;
252
253 if (do_save) {
255 }
256 else {
258 }
259}
260
261/*---------------------------------------------------------------------------*/
262/*---------------------------------------------------------------------------*/
263
265endAction()
266{
267}
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
271
272} // namespace Arcane
273
274/*---------------------------------------------------------------------------*/
275/*---------------------------------------------------------------------------*/
Real globalTime() const
Current time.
virtual bool checkAndApplyRestore() override
Checks and applies restoration if necessary.
virtual bool checkAndApplySave(bool is_forced) override
Checks and applies variable saving if necessary. If is_forced is true, forces the save.
void beginAction() override
Indicates that the save/restore actions have started.
void endAction() override
Indicates that the save/restore actions are finished.
void clear() override
Deletes resources associated with the backward rollback.
Integer m_period
Period between two saves for backward tracking.
void goBackward() override
Signals that a backward rollback is desired.
bool m_action_refused
Actions allowed?
Real m_backward_time
Time of the last requested backward action.
virtual char reduce(eReduceType rt, char v)=0
Performs a reduction of type rt on the real v and returns the value.
virtual const CommonVariables & commonVariables() const =0
Information on standard variables.
virtual IParallelMng * parallelMng()=0
Returns the parallelism manager.
virtual IVariableMng * variableMng()=0
Returns the variable manager.
virtual TraceMessage info()=0
Stream for an information message.
Functor of a filter applicable to variables.
virtual void readVariables(IDataReader *reader, IVariableFilter *filter=0)=0
Reads all variables.
virtual void writeVariables(IDataWriter *writer, IVariableFilter *filter=0)=0
Writes the variables.
virtual Real exportSize(const VariableCollection &vars)=0
Estimated size for exporting variables.
Interface of a variable.
Definition IVariable.h:40
@ PTemporary
Indicates that the variable is temporary.
Definition IVariable.h:114
@ PNoRestore
Indicates that the variable should not be restored.
Definition IVariable.h:120
virtual int property() const =0
Returns the properties of the variable.
virtual bool isUsed() const =0
Usage state of the variable.
virtual bool applyFilter(IVariable &var)
Applies the filter to the variable var.
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.