Triumvirate C++ API 0.5.0.post1.dev301+g026f21751
Three-point clustering measurements in large-scale structure analyses.
Loading...
Searching...
No Matches
monitor.hpp
Go to the documentation of this file.
1// Triumvirate: Three-Point Clustering Measurements in LSS
2//
3// Copyright (C) 2023 Mike S Wang & Naonori S Sugiyama [GPL-3.0-or-later]
4//
5// This file is part of the Triumvirate program. See the COPYRIGHT
6// and LICENCE files at the top-level directory of this distribution
7// for details of copyright and licensing.
8//
9// This program is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17// See the GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program. If not, see <https://www.gnu.org/licenses/>.
21
33
34#ifndef TRIUMVIRATE_INCLUDE_MONITOR_HPP_INCLUDED_
35#define TRIUMVIRATE_INCLUDE_MONITOR_HPP_INCLUDED_
36
37#include <gsl/gsl_version.h>
38
39#if defined(TRV_USE_HIP)
40#include <hipfft/hipfftXt.h>
41#elif defined(TRV_USE_CUDA) // !TRV_USE_HIP && TRV_USE_CUDA
42#include <cufftXt.h>
43#endif // TRV_USE_HIP
44#include <fftw3.h>
45
46#ifdef TRV_USE_OMP
47#include <omp.h>
48#endif // TRV_USE_OMP
49
50#include <chrono>
51#include <cstdarg>
52#include <cstdio>
53#include <cstdlib>
54#include <cstring>
55#include <iostream>
56#include <regex>
57#include <sstream>
58#include <stdexcept>
59#include <string>
60#include <vector>
61
62#ifdef TRV_USE_H5
63#include "highfive/H5File.hpp"
64#endif // TRV_USE_H5
65
66// Enter debugging mode.
67#ifdef DBG_MODE
68#include <iostream>
69#endif // DBG_MODE
70
72// Declares OMP macros.
73#ifdef TRV_USE_OMP
74#define OMP_ATOMIC _Pragma("omp atomic")
75#define OMP_CRITICAL _Pragma("omp critical")
76// NOTE: OpenMP is unavailabled in separate GPU compilation pass.
77#ifndef __HIP_DEVICE_COMPILE__
78#define _OMP_VERSION std::to_string(_OPENMP)
79#else // !__HIP_DEVICE_COMPILE__
80#define _OMP_VERSION std::string("unknown")
81#endif // __HIP_DEVICE_COMPILE__
82#define _OMP_NTHREADS omp_get_max_threads()
83#else // !TRV_USE_OMP
84#define OMP_ATOMIC
85#define OMP_CRITICAL
86#define _OMP_VERSION std::string("unknown")
87#define _OMP_NTHREADS 1
88#endif // TRV_USE_OMP
89
90#ifndef GSL_VERSION
91#define GSL_VERSION "unknown"
92#endif // !GSL_VERSION
93
94// Record (fallback) version number.
95#ifndef __TRV_VERSION__
96#define __TRV_VERSION__ "0.6+"
97#endif // !__TRV_VERSION__
98
99// Indicate external interface call.
100#ifdef TRV_EXTCALL
101#define SHOW_CPPSTATE " C++"
102#else // !TRV_EXTCALL
103#define SHOW_CPPSTATE ""
104#endif // TRV_EXTCALL
105
106// Record (fallback) build timezone offset.
107#ifndef __TZOFFSET__
108#define __TZOFFSET__ "+0000"
109#endif // !__TZOFFSET__
111
112#if defined(TRV_USE_HIP)
113#ifndef HIP_EXEC
119#define HIP_EXEC(hip_call) { \
120 auto ret_status = static_cast<hipError_t>(hip_call); \
121 if (ret_status != hipSuccess) { \
122 std::fprintf( \
123 stderr, \
124 "HIP error: %s. " \
125 "Function <%s> returned error code %d " \
126 "in file \"%s\", line %d.\n", \
127 hipGetErrorString(ret_status), \
128 #hip_call, ret_status, \
129 __FILE__, __LINE__ \
130 ); \
131 } \
132}
133#endif // !HIP_EXEC
134
135#ifndef HIPFFT_EXEC
141#define HIPFFT_EXEC(hipfft_call) { \
142 auto ret_status = static_cast<hipfftResult>(hipfft_call); \
143 if (ret_status != HIPFFT_SUCCESS) { \
144 std::fprintf( \
145 stderr, \
146 "hipFFT error: function <%s> returned error code %d " \
147 "in file \"%s\", line %d.\n", \
148 #hipfft_call, ret_status, \
149 __FILE__, __LINE__ \
150 ); \
151 } \
152}
153#endif // !HIPFFT_EXEC
154
155#elif defined(TRV_USE_CUDA)
156#ifndef CUDA_EXEC
162#define CUDA_EXEC(cuda_call) { \
163 auto ret_status = static_cast<cudaError_t>(cuda_call); \
164 if (ret_status != cudaSuccess) { \
165 std::fprintf( \
166 stderr, \
167 "CUDA error: %s. " \
168 "Function <%s> returned error code %d " \
169 "in file \"%s\", line %d.\n", \
170 cudaGetErrorString(ret_status), \
171 #cuda_call, ret_status, \
172 __FILE__, __LINE__ \
173 ); \
174 } \
175}
176#endif // !CUDA_EXEC
177
178#ifndef CUFFT_EXEC
184#define CUFFT_EXEC(cufft_call) { \
185 auto ret_status = static_cast<cufftResult>(cufft_call); \
186 if (ret_status != CUFFT_SUCCESS) { \
187 std::fprintf( \
188 stderr, \
189 "cuFFT error: function <%s> returned error code %d " \
190 "in file \"%s\", line %d.\n", \
191 #cufft_call, ret_status, \
192 __FILE__, __LINE__ \
193 ); \
194 } \
195}
196#endif // !CUFFT_EXEC
197
198#endif // TRV_USE_HIP
199
200namespace trv {
201namespace sys {
202
203// ***********************************************************************
204// Program helpers
205// ***********************************************************************
206
208const std::string fn_delimiter = "::";
210
218bool has_extension(const std::string& fname, const std::string& fext);
219
227std::string join_strings(
228 const std::vector<std::string>& strings, const std::string& delimiter
229);
230
238std::vector<std::string> split_string(
239 const std::string& str, const std::string& delimiter
240);
241
242
243// ***********************************************************************
244// Program tracking
245// ***********************************************************************
246
247// STYLE: Standard naming convention is not followed below.
248
249// RFE: Implement MPI.
250extern int currTask;
251
252extern double gbytesMem;
253extern double gbytesMaxMem;
254
255extern double gbytesMemGPU;
256extern double gbytesMaxMemGPU;
257
258extern int count_rgrid;
259extern int count_cgrid;
260extern float count_grid;
261extern int max_count_rgrid;
262extern int max_count_cgrid;
263extern float max_count_grid;
264
265extern int count_fft;
266extern int count_ifft;
267
269extern bool fftw_wisdom_f_imported;
271extern bool fftw_wisdom_b_imported;
272
280template <typename T>
281double size_in_gb(long long num) {
282 const double BYTES_PER_GBYTES = 1073741824.; // 1024³ bytes per gibibyte
283 return double(num) * sizeof(T) / BYTES_PER_GBYTES;
284}
285
295template <typename T>
296double size_in_gb(int num) {
297 const double BYTES_PER_GBYTES = 1073741824.; // 1024³ bytes per gibibyte
298 return double(num) * sizeof(T) / BYTES_PER_GBYTES;
299}
300
301#if defined(TRV_USE_HIP)
311double worksize_in_gb(
312 hipfftHandle plan,
313 hipfftType ffttype,
314 int nx, int ny, int nz,
315 std::vector<int> gpus
316);
317#elif defined(TRV_USE_CUDA) // !TRV_USE_HIP && TRV_USE_CUDA
328double worksize_in_gb(
329 cufftHandle plan,
330 cufftType ffttype,
331 int nx, int ny, int nz,
332 std::size_t* worksizes,
333 std::vector<int> gpus
334);
335#endif // TRV_USE_HIP
336
342void update_maxmem(bool gpu = false);
343
348void update_maxcntgrid();
349
357std::string show_current_datetime(bool utc = false);
358
365std::string show_elapsed_time(double duration_in_seconds);
366
372std::string show_timestamp();
373
385int get_gpu_count(bool sys = false);
386
392std::vector<int> get_gpu_ids();
393
399bool is_gpu_available();
400
411bool is_gpu_enabled();
412
418bool is_gpu_single();
419
420// Determine CUDA stream support for multi-GPU cuFFT (in cuFFT 10.4.0+).
421#if defined(TRV_USE_CUDA) && CUFFT_VERSION >= 10400
422#define _CUDA_STREAM
423#endif // TRV_USE_CUDA && CUFFT_VERSION >= 10400
424
429void exit_fatal(const std::string& msg);
430
436 // CAVEAT: Discretionary choices.
437 NSET = 0,
438 DBUG = 10,
439 STAT = 20,
440 INFO = 30,
441 WARN = 40,
442 ERRO = 50
443};
444
449class Logger {
450 public:
452
459 Logger(LogLevel level);
460
469 Logger(int level);
470
476 void reset_level(LogLevel level);
477
485 void reset_level(int level);
486
497 void log(LogLevel level_entry, const char* fmt_string, ...);
498
511 void log(int level_entry, const char* fmt_string, ...);
512
521 void debug(const char* fmt_string, ...);
522
531 void stat(const char* fmt_string, ...);
532
541 void info(const char* fmt_string, ...);
542
551 void warn(const char* fmt_string, ...);
552
561 void error(const char* fmt_string, ...);
562
563 private:
564 void emit(std::string log_type, const char* fmt_string, std::va_list args);
565};
566
575bool is_colourable();
576
577extern Logger logger;
578
584 public:
585 std::string name;
586 int task_count = 1;
587 int task_idx = 0;
588 float progress = 0.;
589
596 explicit ProgressBar(int task_count, std::string name = "");
597
603 void set_bar_width(int bar_width);
604
611 void set_nodes(std::vector<float> nodes);
612
620 void set_task_idx(int task_idx);
621
627 void set_progress(float progress);
628
636 void update(int task_idx_now);
637
645 void update(float progress_now);
646
647 private:
648 int bar_width = 50;
649
650 std::vector<float> nodes;
651 int next_node_idx = 0;
652
657 void set_default_pcpt_nodes();
658};
659
672std::vector<float> set_nodes_by_str(std::string interval_str);
673
674
675// ***********************************************************************
676// Program exceptions
677// ***********************************************************************
678
683class UnimplementedError: public std::logic_error {
684 public:
685 std::string err_mesg;
686
693 UnimplementedError(const char* fmt_string, ...);
694
700 virtual const char* what() const noexcept;
701};
702
707class IOError: public std::runtime_error {
708 public:
709 std::string err_mesg;
710
717 IOError(const char* fmt_string, ...);
718
724 virtual const char* what() const noexcept;
725};
726
731class InvalidParameterError: public std::invalid_argument {
732 public:
733 std::string err_mesg;
734
741 InvalidParameterError(const char* fmt_string, ...);
742
748 virtual const char* what() const noexcept;
749};
750
755class InvalidDataError: public std::runtime_error {
756 public:
757 std::string err_mesg;
758
765 InvalidDataError(const char* fmt_string, ...);
766
772 virtual const char* what() const noexcept;
773};
774
775
776// ***********************************************************************
777// Program notices
778// ***********************************************************************
779
785std::string get_build_datetime();
786
791void display_help();
792
797void display_prog_logo();
798
804void display_prog_licence(bool brief = false);
805
811void display_prog_info(bool runtime = false);
812
818void display_prog_logbars(int endpoint);
819
820
821// ***********************************************************************
822// Program utilities
823// ***********************************************************************
824
830void expand_envar_in_path(std::string& path_str);
831
832} // namespace trv::sys
833} // namespace trv
834
835#endif // !TRIUMVIRATE_INCLUDE_MONITOR_HPP_INCLUDED_
IOError(const char *fmt_string,...)
Construct an trv::sys::IOError exception.
Definition monitor.cpp:701
std::string err_mesg
error message
Definition monitor.hpp:709
std::string err_mesg
error message
Definition monitor.hpp:757
InvalidDataError(const char *fmt_string,...)
Construct an trv::sys::InvalidDataError exception.
Definition monitor.cpp:734
InvalidParameterError(const char *fmt_string,...)
Construct an trv::sys::InvalidParameterError exception.
Definition monitor.cpp:716
std::string err_mesg
error message
Definition monitor.hpp:733
Logger with logging level differentiation.
Definition monitor.hpp:449
Logger(LogLevel level)
Construct the logger with the specified threshold level.
Definition monitor.cpp:336
void info(const char *fmt_string,...)
Emit a information-level message.
Definition monitor.cpp:467
void debug(const char *fmt_string,...)
Emit a debugging-level message.
Definition monitor.cpp:441
void error(const char *fmt_string,...)
Emit a warning-level message.
Definition monitor.cpp:493
void warn(const char *fmt_string,...)
Emit a warning-level message.
Definition monitor.cpp:480
void stat(const char *fmt_string,...)
Emit a status-level message.
Definition monitor.cpp:454
int level_limit
logger threshold level
Definition monitor.hpp:451
void log(LogLevel level_entry, const char *fmt_string,...)
Log a message at the specified level.
Definition monitor.cpp:373
void reset_level(LogLevel level)
Reset the logger threshold level.
Definition monitor.cpp:344
float progress
progress value in [0, 1] interval
Definition monitor.hpp:588
int task_count
total task count
Definition monitor.hpp:586
ProgressBar(int task_count, std::string name="")
Construct a progress bar.
Definition monitor.cpp:524
void set_task_idx(int task_idx)
Set current (or initial) task index.
Definition monitor.cpp:553
void set_nodes(std::vector< float > nodes)
Set progress bar width.
Definition monitor.cpp:544
int task_idx
task index
Definition monitor.hpp:587
void set_bar_width(int bar_width)
Set progress bar width.
Definition monitor.cpp:537
std::string name
progress bar name
Definition monitor.hpp:585
void update(int task_idx_now)
Update the progress bar.
Definition monitor.cpp:580
void set_progress(float progress)
Set current (or initial) progress value.
Definition monitor.cpp:566
virtual const char * what() const noexcept
Exception string representation.
Definition monitor.cpp:697
std::string err_mesg
error message
Definition monitor.hpp:685
UnimplementedError(const char *fmt_string,...)
Construct an trv::sys::UnimplementedError exception.
Definition monitor.cpp:683
double gbytesMem
current memory usage in gibibytes
Definition monitor.cpp:94
bool has_extension(const std::string &fname, const std::string &fext)
Check if a file has a given extension.
Definition monitor.cpp:38
void display_help()
Display help message in stdout.
Definition monitor.cpp:856
void update_maxmem(bool gpu=false)
Update the maximum memory usage estimate.
Definition monitor.cpp:165
std::string show_timestamp()
Return the timestamp string including the elapsed time.
Definition monitor.cpp:238
double size_in_gb(long long num)
Return size in gibibytes.
Definition monitor.hpp:281
void expand_envar_in_path(std::string &path_str)
Expand environment variables in a path string.
Definition monitor.cpp:1012
std::vector< int > get_gpu_ids()
Get the indices of GPUs available for use.
Definition monitor.cpp:284
bool fftw_wisdom_b_imported
wisdom import status for backward transform
Definition monitor.cpp:111
bool is_gpu_enabled()
Check if GPU mode is enabled.
Definition monitor.cpp:297
bool is_gpu_available()
Check if GPUs are available in the system.
Definition monitor.cpp:292
void display_prog_logo()
Display program logo in stdout.
Definition monitor.cpp:871
bool fftw_wisdom_f_imported
wisdom import status for forward transform
Definition monitor.cpp:110
float max_count_grid
maximum number of grids
Definition monitor.cpp:105
Logger logger
default logger (at NSET logging level)
int max_count_rgrid
maximum number of 3-d real grids
Definition monitor.cpp:103
std::string show_current_datetime(bool utc=false)
Return the current datetime string.
Definition monitor.cpp:189
float count_grid
number of grids
Definition monitor.cpp:102
void display_prog_licence(bool brief=false)
Display program licence in stdout.
Definition monitor.cpp:890
double gbytesMemGPU
current (GPU) memory usage in gibibytes
Definition monitor.cpp:97
double gbytesMaxMemGPU
maximum (GPU) memory usage in gibibytes
Definition monitor.cpp:98
int max_count_cgrid
maximum number of 3-d complex grids
Definition monitor.cpp:104
int currTask
current task
Definition monitor.cpp:92
void display_prog_logbars(int endpoint)
Display program log bars in stdout.
Definition monitor.cpp:994
std::vector< float > set_nodes_by_str(std::string interval_str)
Set a node list possibly from a string.
Definition monitor.cpp:649
LogLevel
Logging levels.
Definition monitor.hpp:435
@ NSET
0: unset
Definition monitor.hpp:437
@ INFO
30: info
Definition monitor.hpp:440
@ STAT
20: status
Definition monitor.hpp:439
@ WARN
40: warning
Definition monitor.hpp:441
@ ERRO
50: error/critical
Definition monitor.hpp:442
@ DBUG
10: debugging
Definition monitor.hpp:438
int get_gpu_count(bool sys=false)
Get the number of GPUs available.
Definition monitor.cpp:258
bool is_gpu_single()
Check if in single-GPU mode.
Definition monitor.cpp:322
int count_fft
number of FFTs
Definition monitor.cpp:107
int count_cgrid
number of 3-d complex grids
Definition monitor.cpp:101
double gbytesMaxMem
maximum memory usage in gibibytes
Definition monitor.cpp:95
std::vector< std::string > split_string(const std::string &str, const std::string &delimiter)
Split a string into a vector of strings.
Definition monitor.cpp:59
int count_rgrid
number of 3-d real grids
Definition monitor.cpp:100
std::string join_strings(const std::vector< std::string > &strings, const std::string &delimiter)
Join a vector of strings with a delimiter.
Definition monitor.cpp:45
int count_ifft
number of IFFTs
Definition monitor.cpp:108
void update_maxcntgrid()
Update the maximum 3-d grid counts.
Definition monitor.cpp:177
void display_prog_info(bool runtime=false)
Display program information in stdout.
Definition monitor.cpp:930
void exit_fatal(const std::string &msg)
Terminate the program with exit status EXIT_FAILURE.
Definition monitor.cpp:326
bool is_colourable()
Check if the program stdout is colourable.
Definition monitor.cpp:506
std::string show_elapsed_time(double duration_in_seconds)
Return the elapsed-time string in 'HH:MM:SS' format.
Definition monitor.cpp:212
std::string get_build_datetime()
Return the build datetime string in ISO 8601–like format.
Definition monitor.cpp:757