Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
CodeService.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/* CodeService.cc (C) 2000-2024 */
9/* */
10/* Code service. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/CodeService.h"
15
16#include "arcane/utils/List.h"
17#include "arcane/utils/ScopedPtr.h"
18#include "arcane/utils/Exception.h"
19#include "arcane/utils/String.h"
20#include "arcane/utils/ITraceMng.h"
21#include "arcane/utils/IProfilingService.h"
22#include "arcane/utils/PlatformUtils.h"
23#include "arcane/utils/Array.h"
24#include "arcane/utils/ValueConvert.h"
25#include "arcane/utils/JSONWriter.h"
26
27#include "arcane/core/IApplication.h"
28#include "arcane/core/ISession.h"
29#include "arcane/core/IServiceLoader.h"
30#include "arcane/core/IMainFactory.h"
31#include "arcane/core/ISubDomain.h"
32#include "arcane/core/ServiceBuildInfo.h"
33#include "arcane/core/ICheckpointMng.h"
34#include "arcane/core/ICaseMng.h"
35#include "arcane/core/ITimeLoopMng.h"
36#include "arcane/core/IVariableMng.h"
37#include "arcane/core/CheckpointInfo.h"
38#include "arcane/core/internal/IVariableMngInternal.h"
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane
44{
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49class CodeServicePrivate
50{
51 public:
52
53 CodeServicePrivate(IApplication* application, IServiceInfo* si);
54
55 public:
56
57 IServiceInfo* m_service_info;
58 IApplication* m_application;
59 StringList m_extensions;
60};
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
65CodeServicePrivate::
66CodeServicePrivate(IApplication* application, IServiceInfo* si)
67: m_service_info(si)
68, m_application(application)
69{
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78CodeService::
79CodeService(const ServiceBuildInfo& sbi)
80: m_p(new CodeServicePrivate(sbi.application(), sbi.serviceInfo()))
81{
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
87CodeService::
88~CodeService()
89{
90 delete m_p;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
97allowExecution() const
98{
99 return true;
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
106validExtensions() const
107{
108 return m_p->m_extensions;
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
115createAndLoadCase(ISession* session, const SubDomainBuildInfo& sdbi)
116{
117 ITraceMng* trace = session->traceMng();
118
119 ISubDomain* sub_domain = session->createSubDomain(sdbi);
120
121 // Allows the derived class to make its modifications
122 _preInitializeSubDomain(sub_domain);
123
124 try {
125 sub_domain->readCaseMeshes();
126 }
127 catch (const Exception& ex) {
128 trace->error() << ex;
129 throw;
130 }
131 catch (...) {
132 trace->error() << "Unknown exception thrown";
133 throw;
134 }
135
136 return sub_domain;
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
143initCase(ISubDomain* sub_domain, bool is_continue)
144{
145 IProfilingService* ps = nullptr;
146 bool is_profile_init = false;
147 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ARCANE_PROFILE_INIT", true))
148 is_profile_init = (v.value() != 0);
149 if (is_profile_init)
151 {
152 ProfilingSentryWithInitialize ps_sentry(ps);
153 ps_sentry.setPrintAtEnd(true);
154 /*
155 * The different phases of initialization are:
156 * - allocation of structures managing meshes.
157 * - rereading protections (if restarting).
158 * - reading (phase1, so without mesh elements) the dataset.
159 * - calling 'build' type entry points.
160 * - reading meshes (if init) or rereading (if restart).
161 */
162 if (is_continue)
163 sub_domain->setIsContinue();
164 sub_domain->allocateMeshes();
165 if (is_continue) {
166 ICheckpointMng* cm = sub_domain->checkpointMng();
168 ci.setIsRestart(true);
169 cm->readCheckpoint(ci);
170 }
171 ICaseMng* case_mng = sub_domain->caseMng();
172 // Reading the dataset (phase1)
173 case_mng->readOptions(true);
174 ITimeLoopMng* loop_mng = sub_domain->timeLoopMng();
175 loop_mng->execBuildEntryPoints();
176 sub_domain->readOrReloadMeshes();
177
178 IVariableMng* vm = sub_domain->variableMng();
179 vm->_internalApi()->initializeVariables(is_continue);
180 if (!is_continue)
182 // Reading the dataset (phase2)
183 case_mng->readOptions(false);
184 case_mng->printOptions();
185 // Performs initial or restart partitioning
186 sub_domain->doInitMeshPartition();
187 loop_mng->execInitEntryPoints(is_continue);
188 sub_domain->setIsInitialized();
189 }
190
191 if (is_profile_init && ps) {
192 // Generates a JSON file with profiling information
193 // of the initialization.
194 JSONWriter json_writer(JSONWriter::FormatFlags::None);
195 json_writer.beginObject();
196 ps->dumpJSON(json_writer);
197 json_writer.endObject();
198 String file_name = String("profiling_init-") + platform::getProcessId() + String(".json");
199 std::ofstream ofile(file_name.localstr());
200 ofile << json_writer.getBuffer();
201 }
202}
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
208serviceInfo() const
209{
210 return m_p->m_service_info;
211}
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
217serviceParent() const
218{
219 return m_p->m_application;
220}
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224
225void CodeService::
226_addExtension(const String& extension)
227{
228 m_p->m_extensions.add(extension);
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234IApplication* CodeService::
235_application() const
236{
237 return m_p->m_application;
238}
239
240/*---------------------------------------------------------------------------*/
241/*---------------------------------------------------------------------------*/
242
243} // namespace Arcane
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
Information about a checkpoint.
IServiceInfo * serviceInfo() const override
Service information.
IBase * serviceParent() const override
Parent of this service.
void initCase(ISubDomain *sub_domain, bool is_continue) override
Initializes the session session.
StringCollection validExtensions() const override
Returns the list of file extensions processed by the instance. The extension does not include the '....
bool allowExecution() const override
Returns whether the code allows execution.
ISubDomain * createAndLoadCase(ISession *session, const SubDomainBuildInfo &sdbi) override
Creates and loads the case using the info sdbi for the session session.
Template class for converting a type.
Application interface.
Interface of the base class for main arcane objects.
Definition IBase.h:32
virtual ITraceMng * traceMng() const =0
Trace manager.
Case manager interface.
Definition ICaseMng.h:57
virtual void printOptions()=0
Prints the option values.
virtual void readOptions(bool is_phase1)=0
Reads the dataset options corresponding to the used modules.
Interface of the checkpoint information manager.
virtual ARCANE_DEPRECATED_122 void readCheckpoint()=0
Reads a checkpoint.
virtual CheckpointInfo readDefaultCheckpointInfo()=0
Reads default checkpoint information.
Interface of a profiling service.
virtual void dumpJSON(JSONWriter &writer)=0
Writes the profiling information to the writer writer.
Interface for service or module information.
Interface for a case execution session.
Definition ISession.h:38
virtual ISubDomain * createSubDomain(const SubDomainBuildInfo &sdbi)=0
Creates a sub-domain with the parameters contained in sdbi.
Interface of the subdomain manager.
Definition ISubDomain.h:75
virtual void doInitMeshPartition()=0
Applies the initialization mesh partitioning.
virtual ICheckpointMng * checkpointMng() const =0
Protection manager.
virtual void setIsInitialized()=0
Indicates that the subdomain is initialized.
virtual ITimeLoopMng * timeLoopMng()=0
Returns the time loop manager.
virtual void initializeMeshVariablesFromCaseFile()=0
Initializes variables whose values are specified in the dataset.
virtual IVariableMng * variableMng()=0
Returns the variable manager.
virtual void readOrReloadMeshes()=0
Reads or re-reads the meshes.
virtual void readCaseMeshes()=0
Reads the mesh information from the dataset.
virtual void allocateMeshes()=0
Allocates the instances.
virtual ICaseMng * caseMng()=0
Returns the dataset manager.
virtual void setIsContinue()=0
Sets a flag indicating that a restart is being performed.
Interface for the time loop manager.
virtual void execBuildEntryPoints()=0
Executes the build entry points.
virtual void execInitEntryPoints(bool is_continue)=0
Executes the initialization entry points.
virtual TraceMessage error()=0
Stream for an error message.
virtual void initializeVariables(bool is_continue)=0
Initializes the variables.
Variable manager interface.
virtual IVariableMngInternal * _internalApi()=0
Internal Arcane API.
Class allowing automatic start and stop of a service.
void setPrintAtEnd(bool v)
Indicates if results are printed at the end of profiling.
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:229
Parameters necessary for building a subdomain.
IProfilingService * getProfilingService()
Service used to obtain profiling information.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Collection< String > StringCollection
Collection of strings.
Definition UtilsTypes.h:506
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509