Advanced Multi-Physics (AMP)
On-Line Documentation
Utilities.h
Go to the documentation of this file.
1#ifndef included_AMP_Utilities
2#define included_AMP_Utilities
3
4
5#include "AMP/AMP_TPLs.h"
6#include "AMP/utils/AMP_MPI.h"
7#include "AMP/utils/Backend.h"
8#include "AMP/utils/Memory.h"
9#include "AMP/utils/UtilityMacros.h"
10
11#include "StackTrace/Utilities.h"
12
13#include <algorithm>
14#include <cctype>
15#include <chrono>
16#include <cstdarg>
17#include <limits>
18#include <math.h>
19#include <sstream>
20#include <stdio.h>
21#include <string>
22#include <string_view>
23#include <vector>
24
25
26namespace AMP {
27
28
29class Database;
30
31
32// \cond HIDDEN_SYMBOLS
33template<class T>
34inline T type_default_tol()
35{
36 if constexpr ( std::is_integral_v<T> )
37 return 0;
38 else if constexpr ( std::is_same_v<T, double> )
39 return 1e-12;
40 else if constexpr ( std::is_floating_point_v<T> )
41 return std::pow( std::numeric_limits<T>::epsilon(), (T) 0.77 );
42 else
43 return T();
44}
45// \endcond
46
47
53namespace Utilities {
54
55
56// Include functions from StackTrace
58using StackTrace::Utilities::exec;
59using StackTrace::Utilities::getMemoryUsage;
60using StackTrace::Utilities::getOS;
61using StackTrace::Utilities::getSystemMemory;
62using StackTrace::Utilities::OS;
63using StackTrace::Utilities::tick;
64using StackTrace::Utilities::time;
65
66
68std::string getLastErrnoString();
69
70
73
74
76template<class TYPE>
77bool isInf( TYPE x );
78
79
81template<class TYPE>
82bool isNaN( TYPE x );
83
84
90void setenv( const char *name, const char *value );
91
97std::string getenv( const char *name );
98
99
112std::string intToString( int num, int min_width = 1 );
113
114
121std::string strrep( const std::string &str, const std::string &s, const std::string &r );
122
123
131std::string nodeToString( int num );
132std::string processorToString( int num );
133std::string patchToString( int num );
134std::string levelToString( int num );
135std::string blockToString( int num );
136
137
145template<class T>
146inline bool approx_equal( const T &v1, const T &v2, const T tol = type_default_tol<T>() )
147{
148 // Compute the absolute tolerance
149 T tol2 = static_cast<T>( tol * std::max( fabs( (double) ( v1 ) ), fabs( (double) ( v2 ) ) ) );
150 // Check if the two value are less than tolerance
151 return fabs( (double) ( v1 - v2 ) ) <= tol2;
152}
153
161template<class T>
162inline bool approx_equal_abs( const T &v1, const T &v2, const T tol = type_default_tol<T>() )
163{
164 return fabs( (double) ( v1 - v2 ) ) <= tol; // Check if the two value are less than tolerance
165}
166
167
175template<typename T1, typename T2, Backend, class Allocator>
176void copyCast( const size_t len, const T1 *vec_in, T2 *vec_out );
177
178template<typename T1, typename T2, Backend>
179void copyCast( const size_t len, const T1 *vec_in, T2 *vec_out );
180
181
187template<class T>
188void quicksort( size_t N, T *x );
189
194template<class T>
195inline void quicksort( std::vector<T> &x )
196{
197 quicksort( x.size(), x.data() );
198}
199
206template<class T1, class T2>
207void quicksort( size_t N, T1 *x, T2 *y );
208
214template<class T1, class T2>
215inline void quicksort( std::vector<T1> &x, std::vector<T2> &y )
216{
217 AMP_INSIST( x.size() == y.size(), "x and y must be the same size" );
218 quicksort( x.size(), x.data(), y.data() );
219}
220
228template<class T1, class T2, class T3>
229void quicksort( size_t N, T1 *x, T2 *y, T3 *z );
230
231
236template<class T>
237void unique( std::vector<T> &x );
238
246template<class T>
247void unique( std::vector<T> &X, std::vector<size_t> &I );
248
249
258template<class T>
259size_t findfirst( size_t N, const T *x, const T &value );
260
268template<class T>
269inline size_t findfirst( const std::vector<T> &x, const T &value )
270{
271 return findfirst( x.size(), x.data(), value );
272}
273
274
281double linear( const std::vector<double> &x, const std::vector<double> &f, double xi );
282
291double bilinear( const std::vector<double> &x,
292 const std::vector<double> &y,
293 const std::vector<double> &f,
294 double xi,
295 double yi );
296
307double trilinear( const std::vector<double> &x,
308 const std::vector<double> &y,
309 const std::vector<double> &z,
310 const std::vector<double> &f,
311 double xi,
312 double yi,
313 double zi );
314
315
317constexpr unsigned int hash_char( const std::string_view &str )
318{
319 uint32_t hash = 5381;
320 for ( unsigned char c : str ) {
321 // hash = hash * 33 ^ c
322 hash = ( ( hash << 5 ) + hash ) ^ c;
323 }
324 return hash;
325}
326
327
328// Function to demangle a string (e.g. from typeid)
329std::string demangle( const std::string &name );
330
331
333std::vector<int> factor( uint64_t );
334
335
337bool isPrime( uint64_t );
338
339
341std::vector<uint64_t> primes( uint64_t );
342
343
348inline void sleep_ms( int N ) { std::this_thread::sleep_for( std::chrono::milliseconds( N ) ); }
349
354inline void sleep_s( int N ) { std::this_thread::sleep_for( std::chrono::seconds( N ) ); }
355
360void busy_ms( int N );
361
366inline void busy_s( int N ) { busy_ms( 1000 * N ); }
367
368
371
372
374void nullUse( const void * );
375
376
384std::string randomString( const AMP::AMP_MPI &comm = AMP_COMM_NULL );
385
386
388void fillRandom( std::vector<double> & );
389
390
392template<size_t N>
393void fillRandom( std::vector<std::array<double, N>> & );
394
395
397inline std::string stringf( const char *format, ... )
398{
399 va_list ap;
400 va_start( ap, format );
401 char tmp[4096];
402 int n = vsnprintf( tmp, sizeof tmp, format, ap );
403 va_end( ap );
404 AMP_INSIST( n >= 0, "Error using stringf: encoding error" );
405 AMP_INSIST( n < (int) sizeof tmp, "Error using stringf: internal buffer size" );
406 return std::string( tmp );
407}
408
409
411template<class TYPE>
412std::string to_string( const std::vector<TYPE> &x );
413
414
416[[deprecated( "This function will be removed soon, use Database::print" )]] void
417printDatabase( const Database &, std::ostream &, const std::string &indent = "" );
418
419
421template<class TYPE, std::size_t CAPACITY>
422class stackVector final
423{
424public:
425 stackVector() : d_size( 0 ) {}
426 stackVector( std::initializer_list<TYPE> x ) : d_size( 0 )
427 {
428 if ( x.size() > CAPACITY )
429 throw std::bad_alloc();
430 for ( const auto &y : x )
431 d_data[d_size++] = y;
432 }
433 size_t size() const { return d_size; }
434 bool empty() const { return d_size == 0; }
435 void push_back( const TYPE &v )
436 {
437 if ( d_size == CAPACITY )
438 throw std::bad_alloc();
439 d_data[d_size++] = v;
440 }
441 void clear()
442 {
443 for ( size_t i = 0; i < d_size; i++ )
444 d_data[i] = TYPE();
445 d_size = 0;
446 }
447 TYPE &operator[]( size_t i ) { return d_data[i]; }
448 TYPE *begin() { return d_data; }
449 TYPE *end() { return d_data + d_size; }
450 TYPE &back() { return d_data[d_size - 1]; }
451 TYPE *data() { return d_size == 0 ? nullptr : d_data; }
452 void pop_back() { d_size = std::max<size_t>( d_size, 1 ) - 1; }
453 const TYPE &operator[]( size_t i ) const { return d_data[i]; }
454 const TYPE *begin() const { return d_data; }
455 const TYPE *end() const { return d_data + d_size; }
456 const TYPE &back() const { return d_data[d_size - 1]; }
457 template<class... Args>
458 void emplace_back( Args &&...args )
459 {
460 if ( d_size == CAPACITY )
461 throw std::bad_alloc();
462 d_data[d_size++] = TYPE( args... );
463 }
464
465private:
466 uint32_t d_size;
467 TYPE d_data[CAPACITY];
468};
469
470
471void setNestedOperatorMemoryLocations( std::shared_ptr<AMP::Database> input_db,
472 std::string outerOperatorName,
473 std::vector<std::string> nestedOperatorNames );
474
475} // namespace Utilities
476} // namespace AMP
477
478
479#endif
#define AMP_COMM_NULL
Definition AMP_MPI.h:30
#define X(C)
Provides C++ wrapper around MPI routines.
Definition AMP_MPI.h:63
Class to a database.
Definition Database.h:111
Stack based vector.
Definition Utilities.h:423
stackVector(std::initializer_list< TYPE > x)
Definition Utilities.h:426
const TYPE * begin() const
Definition Utilities.h:454
const TYPE & operator[](size_t i) const
Definition Utilities.h:453
TYPE & operator[](size_t i)
Definition Utilities.h:447
const TYPE * end() const
Definition Utilities.h:455
const TYPE & back() const
Definition Utilities.h:456
void emplace_back(Args &&...args)
Definition Utilities.h:458
void push_back(const TYPE &v)
Definition Utilities.h:435
#define AMP_INSIST(EXP, MSG)
Insist error.
std::string getenv(const char *name)
std::string levelToString(int num)
std::string processorToString(int num)
double trilinear(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const std::vector< double > &f, double xi, double yi, double zi)
double linear(const std::vector< double > &x, const std::vector< double > &f, double xi)
void setNestedOperatorMemoryLocations(std::shared_ptr< AMP::Database > input_db, std::string outerOperatorName, std::vector< std::string > nestedOperatorNames)
std::string stringf(const char *format,...)
std::string version of sprintf
Definition Utilities.h:397
void fillRandom(std::vector< double > &)
Fill with random values in [0,1].
void printBanner()
Print AMP Banner.
void unique(std::vector< T > &x)
void sleep_s(int N)
Definition Utilities.h:354
std::string patchToString(int num)
std::string randomString(const AMP::AMP_MPI &comm=AMP_COMM_NULL)
Function to return a unique alpha-numeric string across a given communicator.
std::string strrep(const std::string &str, const std::string &s, const std::string &r)
bool running_valgrind()
Check if valgrind is running.
bool isInf(TYPE x)
Check if a number infinity.
std::string to_string(const std::vector< TYPE > &x)
Print a vector.
bool isNaN(TYPE x)
Check if a number NaN.
void busy_ms(int N)
std::string demangle(const std::string &name)
std::string nodeToString(int num)
bool approx_equal(const T &v1, const T &v2, const T tol=type_default_tol< T >())
Definition Utilities.h:146
std::string intToString(int num, int min_width=1)
void busy_s(int N)
Definition Utilities.h:366
std::string blockToString(int num)
void setenv(const char *name, const char *value)
constexpr unsigned int hash_char(const std::string_view &str)
Create a hash key from a char array.
Definition Utilities.h:317
bool isPrime(uint64_t)
Check if a number is prime.
size_t findfirst(size_t N, const T *x, const T &value)
std::vector< uint64_t > primes(uint64_t)
Return all prime numbers <= x.
void copyCast(const size_t len, const T1 *vec_in, T2 *vec_out)
bool approx_equal_abs(const T &v1, const T &v2, const T tol=type_default_tol< T >())
Definition Utilities.h:162
void nullUse(const void *)
Null use function.
std::string getLastErrnoString()
Return the string description for the last value in errno (thread-safe)
void printDatabase(const Database &, std::ostream &, const std::string &indent="")
Print a database.
double bilinear(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &f, double xi, double yi)
std::vector< int > factor(uint64_t)
Get the prime factors for a number.
void sleep_ms(int N)
Definition Utilities.h:348
void quicksort(size_t N, T *x)
void abort(const std::string &message, const source_location &source)



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page