FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
runtime.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
7#include <kernel/runtime.hpp>
9#include <kernel/util/dist.hpp>
10#include <kernel/util/memory_arbiter.hpp>
11#include <kernel/util/os_windows.hpp>
13#ifdef FEAT_HAVE_DEATH_HANDLER
14#include <death_handler.h>
15#endif
16
17#include <cstdlib>
18#include <fstream>
19#include <cstring>
20#include <cstdlib>
21
22#if defined(__linux) || defined(__unix__)
23#include <sys/types.h>
24#include <unistd.h>
25#include <execinfo.h>
26#endif
27
28#ifdef FEAT_HAVE_MPI
29#include <mpi.h>
30#endif
31
32#ifdef FEAT_HAVE_CUDA
33#include <kernel/util/cuda_util.hpp>
34#endif
35
36#ifdef FEAT_HAVE_CUDSS
37#include <cudss.h>
38#endif
39
40using namespace FEAT;
41
42// static member initialization
43bool Runtime::_initialized = false;
44bool Runtime::_finalized = false;
45
46#ifdef FEAT_HAVE_CUDSS
47static cudssHandle_t feat_cudss_handle = nullptr;
48#endif
49
50void Runtime::initialize(int& argc, char**& argv)
51{
56#if defined(_WIN32) && defined(FEAT_TESTING_VC)
59#endif
60
61 if (_initialized)
62 {
63 std::cerr << "ERROR: Runtime::initialize called twice!\n";
64 std::cerr.flush();
66 }
67 if (_finalized)
68 {
69 std::cerr << "ERROR: Runtime::initialize called after Runtime::finalize!\n";
70 std::cerr.flush();
72 }
73
74 // initialize Dist operations
75 if(!Dist::initialize(argc, argv))
76 {
77 std::cerr << "ERROR: Failed to initialize Dist operations!\n";
78 std::cerr.flush();
80 }
81
82 // get the MPI world comm rank of this process
83 const int my_rank = Dist::Comm::world().rank();
84
85 // initialize likwid marker api
87
88#ifdef FEAT_HAVE_CUDA
89 // initialize memory pool for CUDA memory
90 Util::cuda_initialize(my_rank, 1, 1, Util::cuda_get_device_count());
91 Util::cuda_set_blocksize(256, 256, 256, 256, 256, 128);
92#endif
93
94 // initialize memory manager (after CUDA)
96
97#ifdef FEAT_HAVE_CUDSS
98 if(CUDSS_STATUS_SUCCESS != cudssCreate(&feat_cudss_handle))
99 {
100 std::cerr << "ERROR: Failed to initialize cuDSS handle!\n";
101 std::cerr.flush();
103 }
104#ifdef FEAT_HAVE_MPI
105 // set communication layer to nullptr, so cuDSS will read its path from $CUDSS_COMM_LIB; this
106 // must *not* be called in non-MPI mode, because cuDSS would try to call MPI routines otherwise
107 cudssSetCommLayer(feat_cudss_handle, nullptr);
108#endif // FEAT_HAVE_MPI
109#endif // FEAT_HAVE_CUDSS
110
111 // check whether '---debug [<ranks...>]' option is given
112 // if so, then trigger a breakpoint for the specified ranks
113 for(int iarg = 1; iarg < argc; ++iarg)
114 {
115 if(strcmp(argv[iarg], "---build-info") == 0)
116 {
117 // dump some information about our build to the console
118 if(my_rank == 0)
119 {
120 std::cout << "--- FEAT BUILD INFORMATION ---\n";
121 // 123456789-123456789-123456789-
122 std::cout << "__cplusplus...................: " << __cplusplus << "\n";
123#ifdef __STDC_VERSION__
124 std::cout << "__STDC_VERSION__..............: " << __STDC_VERSION__ << "\n";
125#else
126 std::cout << "__STDC_VERSION__..............: -N/A-\n";
127#endif
128#ifdef __STDC_HOSTED__
129 std::cout << "__STDC_HOSTED__...............: " << __STDC_HOSTED__ << "\n";
130#else
131 std::cout << "__STDC_HOSTED__...............: -N/A-\n";
132#endif
133#ifdef __STDCPP_THREADS__
134 std::cout << "__STDCPP_THREADS__............: " << __STDCPP_THREADS__ << "\n";
135#else
136 std::cout << "__STDCPP_THREADS__............: -N/A-\n";
137#endif
138#ifdef _OPENMP
139 std::cout << "_OPENMP.......................: yes\n";
140#else
141 std::cout << "_OPENMP.......................: no\n";
142#endif
143 std::cout << "FEAT Version..................: " << FEAT::version_major << "." << FEAT::version_minor << "." << FEAT::version_patch << "\n";
144#ifdef FEAT_GIT_SHA1
145 std::cout << "FEAT_GIT_SHA1.................: " << FEAT_GIT_SHA1 << "\n";
146#else
147 std::cout << "FEAT_GIT_SHA1.................: -N/A-\n";
148#endif
149#ifdef FEAT_BUILD_ID
150 std::cout << "FEAT_BUILD_ID.................: " << FEAT_BUILD_ID << "\n";
151#else
152 std::cout << "FEAT_BUILD_ID.................: -N/A-\n";
153#endif
154#ifdef FEAT_SOURCE_DIR
155 std::cout << "FEAT_SOURCE_DIR...............: " << FEAT_SOURCE_DIR << "\n";
156#else
157 std::cout << "FEAT_SOURCE_DIR...............: -N/A-\n";
158#endif
159#ifdef FEAT_BUILD_DIR
160 std::cout << "FEAT_BUILD_DIR................: " << FEAT_BUILD_DIR << "\n";
161#else
162 std::cout << "FEAT_BUILD_DIR................: -N/A-\n";
163#endif
164#ifdef FEAT_COMPILER
165 std::cout << "FEAT_COMPILER.................: " << FEAT_COMPILER << "\n";
166#else
167 std::cout << "FEAT_COMPILER.................: -N/A-\n";
168#endif
169#ifdef FEAT_COMPILER_GNU
170 std::cout << "FEAT_COMPILER_GNU.............: " << FEAT_COMPILER_GNU << "\n";
171#else
172 std::cout << "FEAT_COMPILER_GNU.............: -N/A-\n";
173#endif
174#ifdef FEAT_COMPILER_CLANG
175 std::cout << "FEAT_COMPILER_CLANG...........: " << FEAT_COMPILER_CLANG << "\n";
176#else
177 std::cout << "FEAT_COMPILER_CLANG...........: -N/A-\n";
178#endif
179#ifdef FEAT_COMPILER_CRAY
180 std::cout << "FEAT_COMPILER_CRAY............: " << FEAT_COMPILER_CRAY << "\n";
181#else
182 std::cout << "FEAT_COMPILER_CRAY............: -N/A-\n";
183#endif
184#ifdef FEAT_COMPILER_INTEL
185 std::cout << "FEAT_COMPILER_INTEL...........: " << FEAT_COMPILER_INTEL << "\n";
186#else
187 std::cout << "FEAT_COMPILER_INTEL...........: -N/A-\n";
188#endif
189#ifdef FEAT_COMPILER_MICROSOFT
190 std::cout << "FEAT_COMPILER_MICROSOFT.......: " << FEAT_COMPILER_MICROSOFT << "\n";
191#else
192 std::cout << "FEAT_COMPILER_MICROSOFT.......: -N/A-\n";
193#endif
194#ifdef FEAT_DEBUG_MODE
195 std::cout << "FEAT_DEBUG_MODE...............: yes\n";
196#else
197 std::cout << "FEAT_DEBUG_MODE...............: no\n";
198#endif
199#ifdef FEAT_EICKT
200 std::cout << "FEAT_EICKT....................: yes\n";
201#else
202 std::cout << "FEAT_EICKT....................: no\n";
203#endif
204#ifdef FEAT_INDEX_U32
205 std::cout << "FEAT_INDEX_U32................: yes\n";
206#else
207 std::cout << "FEAT_INDEX_U32................: no\n";
208#endif
209#ifdef FEAT_MPI_THREAD_MULTIPLE
210 std::cout << "FEAT_MPI_THREAD_MULTIPLE......: yes\n";
211#else
212 std::cout << "FEAT_MPI_THREAD_MULTIPLE......: no\n";
213#endif
214#ifdef FEAT_NO_CONFIG
215 std::cout << "FEAT_NO_CONFIG................: yes\n";
216#else
217 std::cout << "FEAT_NO_CONFIG................: no\n";
218#endif
219#ifdef FEAT_OVERRIDE_MPI_OPS
220 std::cout << "FEAT_OVERRIDE_MPI_OPS.........: yes\n";
221#else
222 std::cout << "FEAT_OVERRIDE_MPI_OPS.........: no\n";
223#endif
224#ifdef FEAT_USE_MKL_LEGACY_SPMV
225 std::cout << "FEAT_USE_MKL_LEGACY_SPMV......: yes\n";
226#else
227 std::cout << "FEAT_USE_MKL_LEGACY_SPMV......: no\n";
228#endif
229#ifdef FEAT_UNROLL_BANDED
230 std::cout << "FEAT_UNROLL_BANDED............: yes\n";
231#else
232 std::cout << "FEAT_UNROLL_BANDED............: no\n";
233#endif
234#ifdef FEAT_HAVE_ALGLIB
235 std::cout << "FEAT_HAVE_ALGLIB..............: yes\n";
236#else
237 std::cout << "FEAT_HAVE_ALGLIB..............: no\n";
238#endif
239#ifdef FEAT_HAVE_BOOST
240 std::cout << "FEAT_HAVE_BOOST...............: yes\n";
241#else
242 std::cout << "FEAT_HAVE_BOOST...............: no\n";
243#endif
244#ifdef FEAT_HAVE_CGAL
245 std::cout << "FEAT_HAVE_CGAL................: yes\n";
246#else
247 std::cout << "FEAT_HAVE_CGAL................: no\n";
248#endif
249#ifdef FEAT_HAVE_CUDA
250 std::cout << "FEAT_HAVE_CUDA................: yes\n";
251#else
252 std::cout << "FEAT_HAVE_CUDA................: no\n";
253#endif
254#ifdef FEAT_HAVE_CUDSS
255 std::cout << "FEAT_HAVE_CUDSS...............: yes\n";
256#else
257 std::cout << "FEAT_HAVE_CUDSS...............: no\n";
258#endif
259#ifdef FEAT_HAVE_DEATH_HANDLER
260 std::cout << "FEAT_HAVE_DEATH_HANDLER.......: yes\n";
261#else
262 std::cout << "FEAT_HAVE_DEATH_HANDLER.......: no\n";
263#endif
264#ifdef FEAT_HAVE_FPARSER
265 std::cout << "FEAT_HAVE_FPARSER.............: yes\n";
266#else
267 std::cout << "FEAT_HAVE_FPARSER.............: no\n";
268#endif
269#ifdef FEAT_HAVE_FLOATX
270 std::cout << "FEAT_HAVE_FLOATX..............: yes\n";
271#else
272 std::cout << "FEAT_HAVE_FLOATX..............: no\n";
273#endif
274#ifdef FEAT_HAVE_HALFMATH
275 std::cout << "FEAT_HAVE_HALFMATH............: yes\n";
276#else
277 std::cout << "FEAT_HAVE_HALFMATH............: no\n";
278#endif
279#ifdef FEAT_HAVE_HYPRE
280 std::cout << "FEAT_HAVE_HYPRE...............: yes\n";
281#else
282 std::cout << "FEAT_HAVE_HYPRE...............: no\n";
283#endif
284#ifdef FEAT_HAVE_MESHHEXER
285 std::cout << "FEAT_HAVE_MESHHEXER...........: yes\n";
286#else
287 std::cout << "FEAT_HAVE_MESHHEXER...........: no\n";
288#endif
289#ifdef FEAT_HAVE_METIS
290 std::cout << "FEAT_HAVE_METIS...............: yes\n";
291#else
292 std::cout << "FEAT_HAVE_METIS...............: no\n";
293#endif
294#ifdef FEAT_HAVE_MKL
295 std::cout << "FEAT_HAVE_MKL.................: yes\n";
296#else
297 std::cout << "FEAT_HAVE_MKL.................: no\n";
298#endif
299#ifdef FEAT_HAVE_MUMPS
300 std::cout << "FEAT_HAVE_MUMPS...............: yes\n";
301#else
302 std::cout << "FEAT_HAVE_MUMPS...............: no\n";
303#endif
304#ifdef FEAT_HAVE_MPI
305 std::cout << "FEAT_HAVE_MPI.................: yes\n";
306#else
307 std::cout << "FEAT_HAVE_MPI.................: no\n";
308#endif
309#ifdef FEAT_HAVE_OMP
310 std::cout << "FEAT_HAVE_OMP.................: yes\n";
311#else
312 std::cout << "FEAT_HAVE_OMP.................: no\n";
313#endif
314#ifdef FEAT_HAVE_PARMETIS
315 std::cout << "FEAT_HAVE_PARMETIS............: yes\n";
316#else
317 std::cout << "FEAT_HAVE_PARMETIS............: no\n";
318#endif
319#ifdef FEAT_HAVE_PMP
320 std::cout << "FEAT_HAVE_PMP.................: yes\n";
321#else
322 std::cout << "FEAT_HAVE_PMP.................: no\n";
323#endif
324#ifdef FEAT_HAVE_QUADMATH
325 std::cout << "FEAT_HAVE_QUADMATH............: yes\n";
326#else
327 std::cout << "FEAT_HAVE_QUADMATH............: no\n";
328#endif
329#ifdef FEAT_HAVE_SUITESPARSE
330 std::cout << "FEAT_HAVE_SUITESPARSE.........: yes\n";
331#else
332 std::cout << "FEAT_HAVE_SUITESPARSE.........: no\n";
333#endif
334#ifdef FEAT_HAVE_SCOTCH
335 std::cout << "FEAT_HAVE_SCOTCH..............: yes\n";
336#else
337 std::cout << "FEAT_HAVE_SCOTCH..............: no\n";
338#endif
339#ifdef FEAT_HAVE_SUPERLU_DIST
340 std::cout << "FEAT_HAVE_SUPERLU_DIST........: yes\n";
341#else
342 std::cout << "FEAT_HAVE_SUPERLU_DIST........: no\n";
343#endif
344#ifdef FEAT_HAVE_TRIANGLE
345 std::cout << "FEAT_HAVE_TRIANGLE............: yes\n";
346#else
347 std::cout << "FEAT_HAVE_TRIANGLE............: no\n";
348#endif
349#ifdef FEAT_HAVE_TRILINOS
350 std::cout << "FEAT_HAVE_TRILINOS............: yes\n";
351#else
352 std::cout << "FEAT_HAVE_TRILINOS............: no\n";
353#endif
354#ifdef FEAT_HAVE_UMFPACK
355 std::cout << "FEAT_HAVE_UMFPACK.............: yes\n";
356#else
357 std::cout << "FEAT_HAVE_UMFPACK.............: no\n";
358#endif
359#ifdef FEAT_HAVE_ZFP
360 std::cout << "FEAT_HAVE_ZFP.................: yes\n";
361#else
362 std::cout << "FEAT_HAVE_ZFP.................: no\n";
363#endif
364#ifdef FEAT_HAVE_ZLIB
365 std::cout << "FEAT_HAVE_ZLIB................: yes\n";
366#else
367 std::cout << "FEAT_HAVE_ZLIB................: no\n";
368#endif
369#ifdef FEAT_HAVE_ZOLTAN
370 std::cout << "FEAT_HAVE_ZOLTAN..............: yes\n";
371#else
372 std::cout << "FEAT_HAVE_ZOLTAN..............: no\n";
373#endif
374 std::cout << "--- END OF FEAT BUILD INFORMATION ---\n";
375 std::cout.flush();
376 }
377 }
378
379 if(strcmp(argv[iarg], "---print-pid") == 0)
380 {
381#ifdef FEAT_HAVE_MPI
382#if defined(_WIN32)
383 std::cout << "Process ID " << std::setw(6) << Windows::get_current_process_id() << " runs rank " << my_rank << "\n";
384#elif defined(__linux) || defined(__unix__)
385 std::cout << "Process ID " << std::setw(6) << getpid() << " runs rank " << my_rank << "\n";
386#endif // defined(_WIN32)
387#else // no FEAT_HAVE_MPI
388#if defined(_WIN32)
389 std::cout << "Process ID " << std::setw(6) << Windows::get_current_process_id() << "\n";
390#elif defined(__linux) || defined(__unix__)
391 std::cout << "Process ID " << std::setw(6) << getpid() << "\n";
392#endif // defined(_WIN32)
393#endif // FEAT_HAVE_MPI
394 std::cout.flush();
395 }
396
397#ifdef FEAT_COMPILER_MICROSOFT
398 if(strcmp(argv[iarg], "---debug-break") == 0)
399 {
400#ifdef FEAT_HAVE_MPI
401 // in an MPI case, the ranks to debug have to be specified
402 for(++iarg; iarg < argc; ++iarg)
403 {
404 char* endptr = nullptr;
405 int irank = int(std::strtol(argv[iarg], &endptr, 0));
406 if((endptr != argv[iarg]) && (*endptr == '\0'))
407 {
408 if(irank == my_rank)
409 {
410 __debugbreak();
411 break;
412 }
413 }
414 else // argv[iarg] could not be parsed as a number
415 {
416 --iarg;
417 break;
418 }
419 }
420#else // no FEAT_HAVE_MPI
421 __debugbreak();
422#endif // FEAT_HAVE_MPI
423 continue;
424 }
425#endif // FEAT_COMPILER_MICROSOFT
426 }
427
428 _initialized = true;
429}
430
431void Runtime::abort(bool dump_call_stack)
432{
433 if(dump_call_stack)
434 {
435#if defined(__linux) || defined(__unix__)
436#if defined(FEAT_HAVE_DEATH_HANDLER) and not defined(FEAT_HAVE_MPI)
437 Debug::DeathHandler death_handler;
438#else
439 // https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
440 void* buffer[1024];
441 auto bt_size = backtrace(buffer, 1024);
442 char** bt_symb = backtrace_symbols(buffer, bt_size);
443 if((bt_size > 0) && (bt_symb != nullptr))
444 {
445 fprintf(stderr, "\nCall-Stack Back-Trace:\n");
446 fprintf(stderr, "----------------------\n");
447 for(decltype(bt_size) i(0); i < bt_size; ++i)
448 fprintf(stderr, "%s\n", bt_symb[i]);
449 fflush(stderr);
450 }
451#endif
452#elif defined(_WIN32)
454#endif
455 }
456
457#ifdef FEAT_HAVE_MPI
458 ::MPI_Abort(MPI_COMM_WORLD, 1);
459#endif
460 std::abort();
461}
462
464{
465 if (!_initialized)
466 {
467 std::cerr << "ERROR: Runtime::finalize called before Runtime::initialize!\n";
468 std::cerr.flush();
470 }
471 if (_finalized)
472 {
473 std::cerr << "ERROR: Runtime::finalize called twice!\n";
474 std::cerr.flush();
476 }
477
478#ifdef FEAT_HAVE_CUDSS
479 cudssDestroy(feat_cudss_handle);
480#endif
481
482 // finalize memory manager (before CUDA)
484
485#ifdef FEAT_HAVE_CUDA
486 Util::cuda_finalize();
487#endif
488
489 // flush streams before we finalize the MPI runtime
490 std::cout.flush();
491 std::clog.flush();
492 std::cerr.flush();
493
494 // finalize Dist operations
496
497 // finalize Likwid markerAPI
499
500 // reset device, which is/can be nesessary to have sanitizer and profiler work properly
501 // this should not be called, if there are other cuda contexts live while FEAT is finalized,
502 // for example if MPI is initilized by another library which shares the same process, or a feat app
503 // is called by another app without reinitializing the cuda device
504#if defined(FEAT_HAVE_CUDA) && defined(FEAT_FINALIZE_RESETS_DEVICE)
505 Util::cuda_reset_device();
506#endif
507 _finalized = true;
508
509 // return successful exit code
510 return EXIT_SUCCESS;
511}
512
514{
515#ifdef FEAT_HAVE_CUDSS
516 return feat_cudss_handle;
517#else
518 return nullptr;
519#endif
520}
521
523{
524#ifdef FEAT_HAVE_CUDA
525 Util::cuda_synchronize();
526#endif
527}
FEAT Kernel base header.
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
static Comm world()
Returns a copy of the world communicator.
Definition: dist.cpp:432
static void initialize(int argc, char **argv)
Initializes FEAT's memory management.
static void finalize()
Finalizes FEAT's memory management.
static int finalize()
FEAT finalization.
Definition: runtime.cpp:463
static void synchronize_devices()
Synchronizes with all available devices (CUDA, HIP, etc...)
Definition: runtime.cpp:522
static bool _finalized
signals, if finalize was called
Definition: runtime.hpp:25
static void * get_cudss_handle()
Returns the unique cuDSS third-party library handle.
Definition: runtime.cpp:513
static bool _initialized
signals, if initialize was called
Definition: runtime.hpp:22
static void initialize(int &argc, char **&argv)
FEAT initialization.
Definition: runtime.cpp:50
static void abort(bool dump_call_stack=true)
FEAT abortion.
Definition: runtime.cpp:431
#define FEAT_MARKER_INIT
Init the marker api.
#define FEAT_MARKER_CLOSE
Finalize marker api, writes data to file, only call once.
bool initialize(int &argc, char **&argv)
Initializes the distributed communication system.
Definition: dist.cpp:105
void finalize()
Finalizes the distributed communication system.
Definition: dist.cpp:148
void install_seh_filter()
Installs custom Windows Structured-Exception-Handler filter.
Definition: os_windows.cpp:208
void dump_call_stack_to_stderr()
Dumps the call-stack to stderr.
Definition: os_windows.cpp:152
void disable_error_prompts()
Disables Windows error dialog boxes.
Definition: os_windows.cpp:214
unsigned long get_current_process_id()
Returns the Windows process ID for the current process.
Definition: os_windows.cpp:222
FEAT namespace.
Definition: adjactor.hpp:12
static constexpr int version_major
FEAT major version number.
static constexpr int version_patch
FEAT patch version number (month)
static constexpr int version_minor
FEAT minor version number (year)