124 overwrite=Overwrite.Default,
125 throw_away_data_after_generation=False):
127 Generate all code. If you add stuff to your project after a
128 build, you have to (re-)generate the code. If you compile only
129 once, you don't have to invoke this routine explicitly. It is
130 lazily called by the other project routines - the latest before
133 It is important that I reset the output after each generate
134 call before you change parameter settings and call generate
135 again. To do so, invoke cleanup(). If you forget this, two
136 subsequent generate calls enrich the output twice.
138 throw_away_data_after_generation: Bool
139 The Peano 4 memory footprint can become quite substantial
140 effectively reducing the translation capabilities (as compilers
141 tend to require a lot of memory, too). So pass in True if you
142 want the script to throw away the data structures (and run a
143 garbage collection) after all files have been generated. Please
144 note that it invalidates both this object (and maybe another
145 object that you've used to generate the present one - such as
146 ExaHyPE). It really strips everything down to the stuff you
147 absolutely need to translate and run the code.
149 Logger().new_section(
"Generate code")
155 os.makedirs(self.
directory, exist_ok=
True)
157 self.
output.readme.add_package_description( self.
constants.readme_entry() )
183 for root, dirs, files
in os.walk(temporary_subdirectory):
185 temporary_file_path = os.path.join(root, file)
186 relative_path = os.path.relpath(temporary_file_path, temporary_subdirectory)
187 target_file_path = os.path.join(self.
directory, subdirectory, relative_path)
189 if not os.path.exists(target_file_path)
or not filecmp.cmp(temporary_file_path, target_file_path):
191 os.makedirs(os.path.dirname(target_file_path), exist_ok=
True)
193 shutil.copy(temporary_file_path, target_file_path)
202 Logger().log(f
"Updated {target_file_path} from {temporary_file_path}")
205 temporary_file_path = os.path.join(root, file)
209 if not os.path.exists(target_file_path)
or not filecmp.cmp(temporary_file_path, target_file_path):
211 os.makedirs(os.path.dirname(target_file_path), exist_ok=
True)
213 shutil.copy(temporary_file_path, target_file_path)
214 Logger().log(f
"Updated {target_file_path} from {temporary_file_path}")
216 if throw_away_data_after_generation:
221 print(
"threw away all data and ran garbage collection")
225 make_clean_first=True,
226 throw_away_data_after_build=False,
227 number_of_parallel_builds=-1):
229 Invokes the underlying make build mechanism on the project.
231 number_of_parallel_builds: int
232 This is mapped onto make -jnumber_of_parallel_builds, i.e., it
233 determines how many parallel make instances the code spawns.
234 Usually, a lot of the generated code is quite lengthy. Therefore
235 compile time can be high. If you pass something smaller or equal
236 to zero, it will use the core count as guideline how to compile.
238 throw_away_data_after_build: Bool
241 if number_of_parallel_builds <= 0:
242 number_of_parallel_builds = multiprocessing.cpu_count()
249 print(
"clean up project ... in {}".format(self.
directory))
252 subprocess.check_call([
"make",
"clean"], cwd=self.
directory)
254 print(
"clean complete")
255 except Exception
as e:
256 print(
"clean failed (" + str(e) +
") - continue anyway")
259 print(
"start to compile with concurrency level of " + str(number_of_parallel_builds) +
" ...")
261 process = subprocess.run([
"make",
"-j"+str(number_of_parallel_builds)], cwd=self.
directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
263 error_message = process.stderr.decode().strip()
264 warnings = re.findall(
r"(?i)warning:.*", error_message)
265 errors = re.findall(
r"(?i)error:.*", error_message)
268 print(
"\nWarnings ({}):".format(len(warnings)))
269 print(
"\n".join(warnings))
271 print(
"\nErrors ({}):".format(len(errors)))
272 error_message =
"\n".join(errors)
275 if process.returncode != 0:
276 linker_error_message = process.stderr.decode().strip() +
"\n" + error_message
277 raise Exception(linker_error_message)
278 print(
"compile completed successfully")
281 except Exception
as e:
285 print(
"compile was not successful")
288 print(
"cannot build as code generation has not been successful")
290 if throw_away_data_after_build:
296 print(
"threw away all data and ran garbage collection")
298 def run(self, executable, args=[], prefix=None, pipefile=None, rebuild_if_required=True):
300 Runs the code. args should be a list of strings or the empty list.
301 prefix is an array, too. A typical invocation looks alike
302 project.run( ["16.0", "32.0"], ["/opt/mpi/mpirun", "-n", "1"] )
303 The operation returns True if the run had been successful
305 pipefile: string or None
312 print(
"run executable " + str(executable))
317 invocation += [
"./" + executable]
323 result = subprocess.run( invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
325 result = subprocess.run( invocation, stdout=open( pipefile,
"w" ), stderr=subprocess.PIPE )
326 if result.returncode==0:
328 print( result.stdout.decode(
"utf-8") )
329 if result.stderr!=
None:
330 print( result.stderr.decode(
"utf-8") )
331 print(
"run completed without error code, but check for warnings and numerical assertions/errors" )
333 print( result.stderr.decode(
"utf-8") )
334 print(
"run failed" )
336 except Exception
as e:
337 print(
"run of application was not successful: " + str(e) )
338 print(
"invocation: " + str(invocation) )
340 print(
"cannot run as code compilation has not been successful" )
345 subproject:
'Project',
348 Adds a new Peano4 project into this Peano 4 project
352 lhs.namespace == rhs.namespace
353 ), "namespaces of the projects being merged don't match"
356 lhs.directory == rhs.directory
357 ), "directories of the projects being merged don't match"
360 lhs.output.makefile.d["CXX"] == lhs.output.makefile.d["CXX"]
361 ), "CXX compilers of the projects being merged don't match"
364 lhs.output.makefile.d["FC"] == lhs.output.makefile.d["FC"]
365 ), "FC compilers of the projects being merged don't match"
368 lhs.output.makefile.d["DIM"] == lhs.output.makefile.d["DIM"]
369 ), "dimensions of the projects being merged don't match"
372 lhs.output.makefile.d["MODE"] == lhs.output.makefile.d["MODE"]
373 ), "compile modes of the projects being merged don't match"
392 assert(subproject._subdirectory !=
""),
"subdirectory of a subproject mustn't be empty"
396 self.
datamodel.namespace = subproject.datamodel.namespace
397 self.
datamodel._subdirectory = subproject.datamodel._subdirectory
410 for data
in subproject.datamodel.cell_data:
412 for data
in subproject.datamodel.face_data:
414 for particle
in subproject.datamodel.vertex_data:
416 for particle
in subproject.datamodel.global_data:
425 added_step = subproject.solversteps._steps[0]
426 step.cell_data.extend(added_step.cell_data)
427 step.face_data.extend(added_step.face_data)
428 step.vertex_data.extend(added_step.vertex_data)
432 for added_step
in subproject.solversteps._steps:
434 added_step.cell_data.clear()
435 added_step.cell_data.extend(step.cell_data)
436 added_step.face_data.clear()
437 added_step.face_data.extend(step.face_data)
438 added_step.vertex_data.clear()
439 added_step.vertex_data.extend(step.vertex_data)
442 for step
in subproject.solversteps._steps:
450 if (self.
output.makefile.d[
"CXX"]):
452 self.
output.makefile.d[
"CXX"] == subproject.output.makefile.d[
"CXX"]
453 ),
"the CXX compiler of the subproject being added doesn't match the one of the main project"
455 self.
output.makefile.d[
"CXX"] = subproject.output.makefile.d[
"CXX"]
457 if (self.
output.makefile.d[
"FC"]):
459 self.
output.makefile.d[
"FC"] == subproject.output.makefile.d[
"FC"]
460 ),
"the FC compiler of the subproject being added doesn't match the one of the main project"
462 self.
output.makefile.d[
"FC"] = subproject.output.makefile.d[
"FC"]
464 for flag
in subproject.output.makefile.d[
"CXXFLAGS"].split():
465 self.
output.makefile.add_CXX_flag(flag)
467 for flag
in subproject.output.makefile.d[
"FCFLAGS"].split():
468 self.
output.makefile.add_Fortran_flag(flag)
470 for flag
in subproject.output.makefile.d[
"LDFLAGS"].split():
471 self.
output.makefile.add_linker_flag(flag)
473 self.
output.makefile.d[
"LIBS"] += subproject.output.makefile.d[
"LIBS"]
474 self.
output.makefile.d[
"GENERATED_INCLUDE_DIRS"] |= subproject.output.makefile.d[
"GENERATED_INCLUDE_DIRS"]
475 self.
output.makefile.d[
"MODE"] = subproject.output.makefile.d[
"MODE"]
477 for module
in subproject.output.makefile.d[
"FORTRAN_MODULES"]:
478 self.
output.makefile.add_Fortran_module(module)
481 self.
output.makefile.d[
"EXECUTABLENAME"]
482 ),
"the name of the main project is empty"
483 if not self.
output.makefile.d[
"DIM"]:
484 self.
output.makefile.set_dimension(subproject.output.makefile.d[
"DIM"])
487 self.
output.makefile.d[
"DIM"] == subproject.output.makefile.d[
"DIM"]
488 ),
"the dimensions of the added subproject doesn't match the current one of the main project"
491 for filename
in subproject.output.makefile.hfiles:
492 self.
output.makefile.add_h_file(filename,
False)
493 for filename
in subproject.output.makefile.cppfiles:
494 self.
output.makefile.add_cpp_file(filename,
False)
495 for filename
in subproject.output.makefile.fortranfiles:
496 self.
output.makefile.add_Fortran_file(filename,
False)
498 for filename
in subproject.output.makefile.generated_hfiles:
499 self.
output.makefile.add_h_file(filename,
True)
500 for filename
in subproject.output.makefile.generated_cppfiles:
501 self.
output.makefile.add_cpp_file(filename,
True)
502 for filename
in subproject.output.makefile.generated_fortranfiles:
503 self.
output.makefile.add_Fortran_file(filename,
True)
508 for i
in subproject.output.readme._entries:
509 self.
output.readme.add_entry(i)
511 for i
in subproject.output.readme._package_descriptions:
512 self.
output.readme.add_package_description(i)
514 for i
in subproject.output.readme._entries:
515 self.
output.readme.add_entry(i)
517 for i
in subproject.output.artefacts:
524 self.
main = subproject.main
528 self.
main.merge(subproject.main)