Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ArcaneSimpleExecutor.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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/* ArcaneSimpleExecutor.cc (C) 2000-2023 */
9/* */
10/* Classe permettant d'exécuter du code directement via Arcane. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/ArcaneSimpleExecutor.h"
15
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/FatalErrorException.h"
18#include "arcane/utils/TraceClassConfig.h"
19#include "arcane/utils/ITraceMng.h"
20#include "arcane/utils/ApplicationInfo.h"
21
22#include "arcane/Parallel.h"
23
24#include "arcane/impl/ArcaneMain.h"
25#include "arcane/impl/MainFactory.h"
26#include "arcane/impl/internal/ArcaneMainExecInfo.h"
27
28#include "arcane/SubDomainBuildInfo.h"
29#include "arcane/ISession.h"
30#include "arcane/ICodeService.h"
31#include "arcane/IApplication.h"
32#include "arcane/IParallelSuperMng.h"
33#include "arcane/IParallelMng.h"
34#include "arcane/ITimeStats.h"
35#include "arcane/IIOMng.h"
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40namespace Arcane
41{
42extern "C++" ARCANE_IMPL_EXPORT Ref<ICodeService>
43createArcaneCodeService(IApplication* app);
44
46: public MainFactory
47{
48 public:
49 // NOTE: Il faut implémenter cette méthode pour hériter de MainFactory
50 // mais elle ne sera pas utilisée dans le cas d'une exécution directe
51 // (c'est toujours une instance de ArcaneMain() qui sera créée)
53 {
54 return new ArcaneMain(app_info,this);
55 }
56};
57
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
62{
63 public:
64 Impl()
65 {
67 }
68 ~Impl() noexcept(false)
69 {
70 for (ITimeStats* ts : m_time_stats_list){
71 ts->endGatherStats();
72 delete ts;
73 }
74 if (m_arcane_main){
75 m_arcane_main->finalize();
76 delete m_arcane_main;
77 }
78 delete m_main_factory;
80 }
81 public:
82 ArcaneMainExecInfo* m_arcane_main = nullptr;
83 ArcaneSimpleExecutorMainFactory* m_main_factory = nullptr;
84 ApplicationBuildInfo m_application_build_info;
85 bool m_has_minimal_verbosity_level = false;
86 bool m_has_output_level = false;
87 UniqueArray<ITimeStats*> m_time_stats_list;
88};
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93ArcaneSimpleExecutor::
94ArcaneSimpleExecutor()
95: m_p(new Impl())
96{
98 const CommandLineArguments& cmd_line_args = app_info.commandLineArguments();
99 auto& build_info = m_p->m_application_build_info;
100 build_info.parseArguments(cmd_line_args);
101 // Par défaut il n'y a pas de fichiers de configuration dans le cas
102 // d'une exécution directe.
103 build_info.setConfigFileName(String());
104
105 // Par défaut, limite le niveau de verbosité de l'initialisation
106 // Cela permet d'éviter d'afficher les infos sur les versions
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113ArcaneSimpleExecutor::
114~ArcaneSimpleExecutor()
115{
116 delete m_p;
117}
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
121
122void ArcaneSimpleExecutor::
123_checkInit()
124{
125 if (!m_p->m_arcane_main)
126 ARCANE_FATAL("This instance is not initialized. Call initialize() method");
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
136_setDefaultVerbosityLevel(Integer level)
137{
138 auto& build_info = m_p->m_application_build_info;
139
140 m_p->m_has_minimal_verbosity_level = (build_info.minimalVerbosityLevel()!=Trace::UNSPECIFIED_VERBOSITY_LEVEL);
141 m_p->m_has_output_level = (build_info.outputLevel()!=Trace::UNSPECIFIED_VERBOSITY_LEVEL);
142
143 if (!m_p->m_has_minimal_verbosity_level)
144 build_info.setMinimalVerbosityLevel(level);
145 if (!m_p->m_has_output_level)
146 build_info.setOutputLevel(level);
147}
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152int ArcaneSimpleExecutor::
153initialize()
154{
155 if (m_p->m_arcane_main)
156 ARCANE_FATAL("This instance is already initialized");
157
159 auto factory = new ArcaneSimpleExecutorMainFactory();
160 m_p->m_main_factory = factory;
161 m_p->m_arcane_main = new ArcaneMainExecInfo(app_info,m_p->m_application_build_info,factory);
162
163 int r = m_p->m_arcane_main->initialize();
164 return r;
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
170int ArcaneSimpleExecutor::
171runCode(IFunctor* f)
172{
173 if (!m_p->m_arcane_main)
174 ARCANE_FATAL("This instance is not yet initialized");
175 if (!f)
176 return 0;
177 IArcaneMain* am = m_p->m_arcane_main->arcaneMainClass();
178 bool clean_abort = false;
179 bool is_print = true;
180 return ArcaneMain::callFunctorWithCatchedException(f,am,&clean_abort,is_print);
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186ApplicationBuildInfo& ArcaneSimpleExecutor::
187applicationBuildInfo()
188{
189 return m_p->m_application_build_info;
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
195const ApplicationBuildInfo& ArcaneSimpleExecutor::
196applicationBuildInfo() const
197{
198 return m_p->m_application_build_info;
199}
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204ISubDomain* ArcaneSimpleExecutor::
205createSubDomain(const String& case_file_name)
206{
207 _checkInit();
208 IApplication* app = m_p->m_arcane_main->arcaneMainClass()->application();
209 ITraceMng* tr = app->traceMng();
210 if (!m_p->m_has_minimal_verbosity_level)
211 tr->setVerbosityLevel(Trace::DEFAULT_VERBOSITY_LEVEL);
212 if (!m_p->m_has_output_level)
213 tr->setStandardOutputVerbosityLevel(Trace::DEFAULT_VERBOSITY_LEVEL);
214 IMainFactory* main_factory = app->mainFactory();
215 // TODO: utiliser le service de code spécifié dans ApplicationInfo.
216 Ref<ICodeService> code_service = createArcaneCodeService(app);
217 ISession* session(code_service->createSession());
218 IParallelSuperMng* psm = app->parallelSuperMng();
219 Ref<IParallelMng> world_pm = psm->internalCreateWorldParallelMng(0);
220
221 SubDomainBuildInfo sdbi(world_pm,0);
222 UniqueArray<Byte> case_bytes;
223 bool has_case_file = !case_file_name.empty();
224 if (has_case_file){
225 bool is_bad = app->ioMng()->collectiveRead(case_file_name,case_bytes);
226 if (is_bad)
227 ARCANE_FATAL("Can not read case file '{0}'",case_file_name);
228 sdbi.setCaseFileName(case_file_name);
229 sdbi.setCaseBytes(case_bytes);
230 tr->info() << "Create sub domain with case file '" << case_file_name << "'";
231 }
232 else{
233 sdbi.setCaseFileName(String());
234 sdbi.setCaseBytes(ByteConstArrayView());
235 }
236 // Le service de statistiques doit être détruit explicitement
237 ITimeStats* time_stat = main_factory->createTimeStats(world_pm->timerMng(),tr,"Stats");
238 time_stat->beginGatherStats();
239 m_p->m_time_stats_list.add(time_stat);
240 world_pm->setTimeStats(time_stat);
241
242 ISubDomain* sub_domain(session->createSubDomain(sdbi));
243 if (has_case_file){
244 code_service->initCase(sub_domain,false);
245 }
246 return sub_domain;
247}
248
249/*---------------------------------------------------------------------------*/
250/*---------------------------------------------------------------------------*/
251
252} // End namespace Arcane
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Informations pour construire une instance de IApplication.
Informations sur une application.
int initialize()
Création de l'instance 'IArcaneMain'.
Classe de gestion de l'exécution.
Definition ArcaneMain.h:80
static void arcaneInitialize()
Initialise Arcane.
static void arcaneFinalize()
Termine l'utilisation Arcane.
static int callFunctorWithCatchedException(IFunctor *functor, IArcaneMain *amain, bool *clean_abort, bool is_print=true)
Appelle le fonctor functor en récupérant les éventuelles exceptions.
static ApplicationInfo & defaultApplicationInfo()
Infos par défaut de l'application.
IArcaneMain * createArcaneMain(const ApplicationInfo &app_info) override
Crée une instance de IArcaneMain.
void _setDefaultVerbosityLevel(Integer level)
Positionne le niveau des traces à level si elles ne sont pas positionnées.
Arguments de la ligne de commande.
Interface de la classe de gestion du code.
Definition IArcaneMain.h:54
virtual IApplication * application() const =0
Application.
Interface gérant les statistiques sur les temps d'exécution.
Definition ITimeStats.h:48
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Manufacture des classes d'Arcane.
Definition MainFactory.h:53
virtual void setVerbosityLevel(Int32 level)=0
Positionne le niveau de verbosité des sorties.
Chaîne de caractères unicode.
ITraceMng * traceMng() const
Gestionnaire de trace.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
ConstArrayView< Byte > ByteConstArrayView
Equivalent C d'un tableau à une dimension de caractères.
Definition UtilsTypes.h:634