Peano
Loading...
Searching...
No Matches
DefaultSimulationMain.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import peano4.api
4
5from ..Project import Project
6
7
9 """!
10
11 Default realisation of exahype2.api.Project.mainfile_generator
12
13 Mirrors SolverToCPPFiles on the solver side: turns the project
14 configuration into the header/implementation file pair that ends up
15 holding main(), similar in spirit to
16 python/exahype2/ExaHyPEMain.template.h/.cpp. Rather than emitting that
17 file pair itself, it replaces peano_project's generic peano4-level main
18 file generator (peano4.api.runner.DefaultSequence) with
19 _ExaHyPEMainFileSequence below, which speaks the same
20 construct_output(output, main_name) protocol but fills the file pair
21 with ExaHyPE-flavoured content instead of the mesh-agnostic default.
22
23 The generated main() contains only what is specific to the project:
24 the runners, the data types, the domain, the log filter and the load
25 balancing. The runtime configuration, the main loop over the runners
26 and the shutdown are plain C++, i.e. exahype2::configureRuntime(),
27 exahype2::runSimulation() and exahype2::shutdownRuntime().
28
29 """
30
31 def __init__(self, project: Project):
32 super().__init__(project)
33
34 def construct_output(self, peano_project):
36 "Create main file for {}".format(self.project._project_name)
37 )
38 peano_project.main = _ExaHyPEMainFileSequence(peano_project, self.project)
39
40
42 """!
43
44 Adapter that speaks the construct_output(output, main_name) protocol
45 peano4.api.Project.generate() expects from its main attribute (see
46 peano4.api.runner.DefaultSequence, whose job this class takes over for
47 an ExaHyPE 2 project), but fills the header/implementation pair with
48 ExaHyPE-flavoured content instead of the mesh-agnostic default.
49
50 project is the peano4-level project (peano4.api.Project) this sequence
51 is attached to as .main - it is where SUBDIRECTORIES/SUBNAMESPACE/
52 rootnamespace come from, mirroring peano4.api.runner.DefaultSequence.
53
54 exahype_project is the exahype2.api.Project this main file is generated
55 for - it is where the runtime settings (TIMEOUT, NUMBER_OF_THREADS,
56 GPUS, MULTICORE_ORCHESTRATION) come from, as those live on the ExaHyPE
57 2 project rather than on the plain peano4 one.
58
59 """
60
61 HEADER_TEMPLATE = """
62// This file is part of the ExaHyPE2 project. For conditions of distribution and
63// use, please see the copyright notice at www.peano-framework.org
64#pragma once
65
66int main(int argc, char** argv);
67"""
68
69 CPP_TEMPLATE = """
70// This file is part of the ExaHyPE2 project. For conditions of distribution and
71// use, please see the copyright notice at www.peano-framework.org
72#include "{{CLASSNAME}}.h"
73
74#include "tarch/logging/Log.h"
75#include "tarch/logging/LogFilterFileReader.h"
76#include "tarch/mpi/Rank.h"
77#include "tarch/multicore/Core.h"
78#include "tarch/multicore/orchestration/Strategy.h"
79#include "peano4/peano.h"
80#include "peano4/DefaultStepReport.h"
81#include "exahype2/LoadBalancingConfiguration.h"
82#include "exahype2/Simulation.h"
83#include "toolbox/loadbalancing/LoadBalancingService.h"
84{% if LOAD_BALANCER!="" -%}
85#include "{{LOAD_BALANCER | replace('::', '/') }}.h"
86{% else -%}
87#include "toolbox/loadbalancing/strategies/NoLoadBalancing.h"
88{% endif %}
89
90{% if SUBDIRECTORIES -%}
91{% for subdirectory in SUBDIRECTORIES %}
92#include "{{subdirectory}}Constants.h"
93#include "{{subdirectory}}repositories/DataRepository.h"
94#include "{{subdirectory}}repositories/StepRepository.h"
95#include "{{subdirectory}}repositories/SolverRepository.h"
96#include "{{subdirectory}}runners/SetupSimulation.h"
97#include "{{subdirectory}}runners/TimeStep.h"
98#include "{{subdirectory}}runners/Plot.h"
99#include "{{subdirectory}}runners/MeshAdaptation.h"
100{% endfor -%}
101{% else %}
102#include "Constants.h"
103#include "repositories/DataRepository.h"
104#include "repositories/StepRepository.h"
105#include "repositories/SolverRepository.h"
106#include "runners/SetupSimulation.h"
107#include "runners/TimeStep.h"
108#include "runners/Plot.h"
109#include "runners/MeshAdaptation.h"
110{%- endif %}
111
112tarch::logging::Log _log( "{{NAMESPACE | join("::")}}" );
113
114
115{{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}runners::SetupSimulation SetupSimulation;
116{{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}runners::Plot Plot;
117{{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}runners::TimeStep TimeStep;
118{{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}runners::MeshAdaptation MeshAdaptation;
119
120{{STEP_REPORT_CLASS}} stepReport;
121
122
123int main(int argc, char** argv) {
124 constexpr int ExitCodeSuccess = 0;
125 constexpr int ExitCodeUnitTestsFailed = 1;
126 constexpr int ExitCodeInvalidArguments = 2;
127 constexpr int ExitCodeInvalidBuild = 3;
128
129 peano4::initParallelEnvironment(&argc, &argv);
130
131 // Do this early, so people can use logInfo/logWarning/... properly from
132 // here on.
133 {% if LOG_FILTER_FILE!="" %}
134 if ( not tarch::logging::LogFilterFileReader::parsePlainTextFile( "{{LOG_FILTER_FILE}}" ) ) {
135 if (tarch::mpi::Rank::getInstance().isGlobalMaster()) {
136 logWarning( "main()", "filter file {{LOG_FILTER_FILE}} not found or file has been corrupted. Use default logging configuration" );
137 }
138 }
139 {% endif %}
140
141 peano4::fillLookupTables();
142 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::DataRepository::initDatatypes();
143 peano4::initSingletons(
144 {{NAMESPACE | join("::")}}::DomainOffset,
145 {{NAMESPACE | join("::")}}::DomainSize,
146 {{NAMESPACE | join("::")}}::PeriodicBC
147 );
148
149 if ( not ::exahype2::configureRuntime(
150 {{TIMEOUT}},
151 {{NUMBER_OF_THREADS}},
152 { {{GPUS|join(",")}} },
153 {{MULTICORE_ORCHESTRATION}}
154 ) ) {
155 return ExitCodeInvalidBuild;
156 }
157
158 {% if LOAD_BALANCER!="" -%}
159 toolbox::loadbalancing::LoadBalancingService::getInstance().setLoadBalancing(
160 new {{LOAD_BALANCER}}({{LOAD_BALANCER_ARGUMENTS}})
161 );
162 {% else -%}
163 toolbox::loadbalancing::LoadBalancingService::getInstance().setLoadBalancing(
164 new toolbox::loadbalancing::strategies::NoLoadBalancing()
165 );
166 {% endif %}
167
168 {% if FENV_ARGS is defined and FENV_ARGS != "" -%}
169 feenableexcept({{FENV_ARGS}} );
170 {% endif -%}
171
172 ::exahype2::runSimulation(
173 SetupSimulation,
174 Plot,
175 TimeStep,
176 MeshAdaptation,
177 stepReport,
178 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::wantsToPlot,
179 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::getMinTimeStamp,
180 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::getMaxTimeStamp,
181 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::getMaxTimeStepSize,
182 {{NAMESPACE | join("::")}}::MinTerminalTime,
183 {{NAMESPACE | join("::")}}::MaxTerminalTime
184 );
185
186 ::exahype2::shutdownRuntime(
187 {{NAMESPACE | join("::")}}::{{SUBNAMESPACE}}repositories::DataRepository::shutdownDatatypes
188 );
189
190 return ExitCodeSuccess;
191}
192"""
193
194 def __init__(self, project, exahype_project):
195 self.project = project
196 self.exahype_project = exahype_project
197
198 def construct_output(self, output, main_name):
199 d = {
200 "SUBDIRECTORIES": list(self.project.subdirectories),
201 "SUBNAMESPACE": self.project._subdirectory + "::" if self.project._subdirectory else "",
202 "TIMEOUT": self.exahype_project._timeout,
203 "NUMBER_OF_THREADS": self.exahype_project._number_of_threads,
204 "MULTICORE_ORCHESTRATION": self.exahype_project._multicore_orchestration,
205 "GPUS": self.exahype_project._gpus,
206 "LOG_FILTER_FILE": self.exahype_project._log_filter_file,
207 "STEP_REPORT_CLASS": self.exahype_project._step_report_class,
208 "LOAD_BALANCER": self.exahype_project._load_balancer_name,
209 "LOAD_BALANCER_ARGUMENTS": self.exahype_project._load_balancer_arguments,
210 }
211
213 headerfile_template = self.HEADER_TEMPLATE,
214 cppfile_template = self.CPP_TEMPLATE,
215 classname = main_name,
216 namespace = self.project.rootnamespace,
217 subdirectory = "./",
218 dictionary = d,
219 default_overwrite = True,
220 )
221
222 output.makefile.add_h_file(main_name + ".h", generated=True)
223 output.makefile.add_cpp_file(main_name + ".cpp", generated=True)
224 output.add(generated_files)
Base class for the code generation steps that turn an ExaHyPE 2 Project's configuration into actual g...
Definition Project.py:35
Default realisation of exahype2.api.Project.mainfile_generator.
construct_output(self, peano_project)
Add this generator's output to peano_project (a peano4.api.Project-compatible object,...
Adapter that speaks the construct_output(output, main_name) protocol peano4.api.Project....
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37