MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
maia.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7// Copyright (C) 2024 The m-AIA AUTHORS
8//
9// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
10//
11// SPDX-License-Identifier: LGPL-3.0-only
12
13// Copyright (C) 2024 The m-AIA AUTHORS
14//
15// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
16//
17// SPDX-License-Identifier: LGPL-3.0-only
18
19
20#include "maia.h"
21
22#ifdef _OPENMP
23#include <omp.h>
24#endif
25#include <climits>
26#include <cstdlib>
27#include <fftw3-mpi.h>
28#include "INCLUDE/maialikwid.h"
29#include "compiler_config.h"
30#include "environment.h"
31#include "globals.h"
32
33// GCC parallel STL uses the Intel TBB library
34// Check that it can be included here.
35#if defined(MAIA_GCC_COMPILER) && defined(MAIA_PSTL)
36#define TBB_USE_EXCEPTIONS 0
37#include <tbb/parallel_for.h>
38#endif
39
40using namespace std;
41
43
44int main(int argc, char* argv[]) {
45 // Create MAIA instance
46 MAIA maia(argc, argv);
47
48 // Run main loop and save exit code
49 const int exitCode = maia.run();
50
51 // Return exit code to OS
52 return exitCode;
53}
54
56int MAIA::run() {
57 // Initialize MPI communication
58 // Profiling uses MPI_Wtime, i.e. the first DEBUG call requires MPI to be initialized already
59#ifdef _OPENMP
60
61 /* If OMP_NUM_THREADS is not set, limit number of threads to one, unless another environment
62 // variable is set appropriately
63 if (!getenv("OMP_NUM_THREADS")) {
64 const char* env = getenv("MAIA_AUTO_NUM_THREADS");
65 if (!env || MString(env) != MString("1")) {
66 omp_set_num_threads(1);
67 m_log << "Set number of openMP-Threads to 1" << endl;
68 cerr0 << "Set number of openMP-Threads to 1" << endl;
69 }
70 }
71 */
72
73 int provided;
74 MPI_Init_thread(&m_argc, &m_argv, MPI_THREAD_FUNNELED, &provided);
75 // The check for the provided thread level is omitted until OpenMPI reports
76 // the correct level and not just MPI_THREAD_SINGLE no matter what
77 // if (provided < MPI_THREAD_FUNNELED) {
78 // cerr << "OpenMP requires a threaded MPI library, but the used library is "
79 // << "not." << endl;
80 // MPI_Finalize();
81 // return EXIT_FAILURE;
82 // }
83#else
84 MPI_Init(&m_argc, &m_argv);
85#endif
86 int domainId, noDomains;
87#ifndef MAIA_WINDOWS
88 fftw_mpi_init();
89#endif
90 MPI_Comm_rank(MPI_COMM_WORLD, &domainId);
91 MPI_Comm_size(MPI_COMM_WORLD, &noDomains);
92 g_mpiInformation.init(domainId, noDomains);
93
94 // Set MPI error handling (return error and handle in code)
95 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
96
97 // Open cerr0 on MPI root
98 if(domainId == 0) {
99 cerr0.rdbuf(cerr.rdbuf());
100 } else {
101 cerr0.rdbuf(&nullBuffer);
102 }
103
104 // Check type sizes to make sure they have the expected size
105 // If an error is produced but you want to proceed anyways, just comment the
106 // offending line(s) below
107 static_assert(CHAR_BIT == 8, "Unexpected type size: char has " XSTRINGIFY(CHAR_BIT) " bits (expected: 8)");
108 static_assert(sizeof(MChar) == 1, "MChar has unexpected type size");
109#ifndef _SX
110 static_assert(sizeof(MBool) == 1, "MBool has unexpected type size");
111#endif
112 static_assert(sizeof(MInt) == 4, "MInt has unexpected type size");
113 static_assert(sizeof(MInt) == 4, "MInt has unexpected type size");
114 static_assert(sizeof(MLong) == 8, "MLong has unexpected type size");
115 static_assert(sizeof(MFloat) == 8, "MFloat has unexpected type size");
116
117 // Needed for m_log
120
121 // Open log file
122#if(MAIA_INFOOUT_FILE_TYPE == 1 && MAIA_INFOOUT_ROOT_ONLY == false)
123 m_log.open("m_log_" + to_string(globalDomainId()), MAIA_INFOOUT_PROJECT_NAME, MAIA_INFOOUT_FILE_TYPE, MPI_COMM_WORLD,
124 MAIA_INFOOUT_ROOT_ONLY);
125#else
126 m_log.open("m_log", MAIA_INFOOUT_PROJECT_NAME, MAIA_INFOOUT_FILE_TYPE, MPI_COMM_WORLD, MAIA_INFOOUT_ROOT_ONLY);
127#endif
128
129 // Set root-only writing property
130 m_log.setRootOnly(MAIA_INFOOUT_ROOT_ONLY);
131
132 // Set minimum flush size
133 m_log.setMinFlushSize(MAIA_INFOOUT_MIN_FLUSH_SIZE);
134
135 // Reset global debug level (may be overridden using the "-d <lvl>" command line parameter (cf. debug.h)
136 SET_DEBUG_LEVEL(0);
137 // SET_DEBUG_LEVEL(MAIA_DEBUG_TRACE_IN | MAIA_DEBUG_TRACE_OUT | MAIA_DEBUG_TRACE);
138 // Note: do not trace this, since m_log is closed before this trace is finished the output to m_log results in a
139 // segmentation fault!
140 // TRACE();
141 DEBUG("main:: domainId " << globalDomainId(), MAIA_DEBUG_LEVEL1);
142 DEBUG("main:: noDomains " << globalNoDomains(), MAIA_DEBUG_LEVEL1);
143
144 // Initialize likwid if likwid is enabled
145#ifdef WITH_LIKWID
146 LIKWID_MARKER_INIT;
147 LIKWID_MARKER_THREADINIT;
148#endif
149
150 // Reset all timers
151 DEBUG("main:: reset timer", MAIA_DEBUG_LEVEL1);
152 RESET_TIMERS();
153
154 NEW_TIMER_GROUP(tg, "MAIA");
155
156 // Start timer for the total program execution
157 NEW_TIMER(timertotal, "Total", tg);
158 RECORD_TIMER_START(timertotal);
159
160 // Create environment
161 NEW_SUB_TIMER(timer1, "New Environment", timertotal);
162 RECORD_TIMER_START(timer1);
163
164 DEBUG("main:: new Environment", MAIA_DEBUG_LEVEL1);
166 RECORD_TIMER_STOP(timer1);
167
168 // Run MAIA... YEHAW!
169 DEBUG("main:: mEnvironment->run", MAIA_DEBUG_LEVEL1);
170 NEW_SUB_TIMER(timer2, "Run Environment", timertotal);
171 RECORD_TIMER_START(timer2);
172 mEnvironment->run();
173 RECORD_TIMER_STOP(timer2);
174
175 // Once mEnvironment has finished, call the cleanup function
176 NEW_SUB_TIMER(timer3, "End Environment", timertotal);
177 DEBUG("main:: mEnvironment->end", MAIA_DEBUG_LEVEL1);
178 RECORD_TIMER_START(timer3);
179 mEnvironment->end();
180 RECORD_TIMER_STOP(timer3);
181
182 // Finalize likwid if likwid is enabled
183#ifdef WITH_LIKWID
184 LIKWID_MARKER_CLOSE;
185#endif
186
187 // Delete mEnvironment
188 DEBUG("main:: delete environment", MAIA_DEBUG_LEVEL1);
189 if(mEnvironment != nullptr) {
190 delete mEnvironment;
191 mEnvironment = nullptr;
192 }
193
194 // Stop the timer for total program execution
195 RECORD_TIMER_STOP(timertotal);
196
197 // Show timer output
198 DEBUG("main:: display all timers", MAIA_DEBUG_LEVEL1);
199 STOP_ALL_RECORD_TIMERS();
200 DISPLAY_ALL_TIMERS();
201
202 // Save memory report to log file
204
205 // Deallocate all memory
206 DEBUG("main:: mDealloc", MAIA_DEBUG_LEVEL1);
207 mDealloc();
208
209 // Close log file streams
210 DEBUG("main:: close streams", MAIA_DEBUG_LEVEL1);
211 m_log.close();
212 maia_res.close();
213
214 // Stop MPI - after MPI_Finalize, no more calls to any MPI-related functions are allowed!
215 // Note: do not use DEBUG() after m_log has been closed!
216 // DEBUG("main:: MPI_Finalize", MAIA_DEBUG_LEVEL1);
217#ifndef MAIA_WINDOWS
218 fftw_mpi_cleanup();
219#endif
220 MPI_Finalize();
221
222 return EXIT_SUCCESS;
223}
void mDealloc()
Deallocates all memory allocated previously by mAlloc(...)
Definition: alloc.cpp:20
Environment for the program.
Definition: environment.h:21
static MChar ** m_argv
Definition: environment.h:29
MInt run()
Runs the Environment, makes the Application run.
MInt end()
Ends the Environment.
static MInt m_argc
Reads the name of the property-file and creates a new Application.
Definition: environment.h:28
void init(const MInt domainId, const MInt noDomains)
Definition: globalmpiinfo.h:24
void open(const MString &filename, const MString &projectName, MInt fileType=0, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
Opens a file by passing the parameters to InfoOut_<xyz>FileBuffer::open(...).
Definition: infoout.cpp:975
void close(MBool forceClose=false)
Pass the close call to the respective internal buffer.
Definition: infoout.cpp:1011
MBool setRootOnly(MBool rootOnly=true)
Sets interal state of whether only the root domain (rank 0) should write to file.
Definition: infoout.cpp:1043
MInt setMinFlushSize(MInt minFlushSize)
Sets the minimum buffer length that has to be reached before the buffer is flushed.
Definition: infoout.cpp:1055
Definition: maia.h:49
int run()
Main controlling method for MAIA. Calls everything else.
Definition: maia.cpp:56
NullBuffer nullBuffer
Definition: maia.h:56
char ** m_argv
Definition: maia.h:59
int m_argc
Definition: maia.h:58
static MString printSelfReport()
Returns a shortened string summing up the scratch space state information.
Definition: scratch.cpp:129
GlobalMpiInformation g_mpiInformation
MInt globalNoDomains()
Return global number of domains.
MInt globalDomainId()
Return global domain id.
InfoOutFile maia_res
InfoOutFile m_log
std::ostream cerr0
int main(int argc, char *argv[])
Definition: maia.cpp:44
Environment * mEnvironment
Definition: maia.cpp:42
int32_t MInt
Definition: maiatypes.h:62
double MFloat
Definition: maiatypes.h:52
int64_t MLong
Definition: maiatypes.h:64
bool MBool
Definition: maiatypes.h:58
char MChar
Definition: maiatypes.h:56
Namespace for auxiliary functions/classes.