1#ifndef included_AMP_Utilities
2#define included_AMP_Utilities
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"
11#include "StackTrace/Utilities.h"
34inline T type_default_tol()
36 if constexpr ( std::is_integral_v<T> )
38 else if constexpr ( std::is_same_v<T, double> )
40 else if constexpr ( std::is_floating_point_v<T> )
41 return std::pow( std::numeric_limits<T>::epsilon(), (T) 0.77 );
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;
90void setenv(
const char *name,
const char *value );
97std::string
getenv(
const char *name );
121std::string
strrep(
const std::string &str,
const std::string &s,
const std::string &r );
146inline bool approx_equal(
const T &v1,
const T &v2,
const T tol = type_default_tol<T>() )
149 T tol2 =
static_cast<T
>( tol * std::max( fabs( (
double) ( v1 ) ), fabs( (
double) ( v2 ) ) ) );
151 return fabs( (
double) ( v1 - v2 ) ) <= tol2;
162inline bool approx_equal_abs(
const T &v1,
const T &v2,
const T tol = type_default_tol<T>() )
164 return fabs( (
double) ( v1 - v2 ) ) <= tol;
175template<
typename T1,
typename T2, Backend,
class Allocator>
176void copyCast(
const size_t len,
const T1 *vec_in, T2 *vec_out );
178template<
typename T1,
typename T2, Backend>
179void copyCast(
const size_t len,
const T1 *vec_in, T2 *vec_out );
206template<
class T1,
class T2>
214template<
class T1,
class T2>
215inline void quicksort( std::vector<T1> &x, std::vector<T2> &y )
217 AMP_INSIST( x.size() == y.size(),
"x and y must be the same size" );
218 quicksort( x.size(), x.data(), y.data() );
228template<
class T1,
class T2,
class T3>
247void unique( std::vector<T> &
X, std::vector<size_t> &I );
259size_t findfirst(
size_t N,
const T *x,
const T &value );
269inline size_t findfirst(
const std::vector<T> &x,
const T &value )
271 return findfirst( x.size(), x.data(), value );
281double linear(
const std::vector<double> &x,
const std::vector<double> &f,
double xi );
292 const std::vector<double> &y,
293 const std::vector<double> &f,
308 const std::vector<double> &y,
309 const std::vector<double> &z,
310 const std::vector<double> &f,
317constexpr unsigned int hash_char(
const std::string_view &str )
319 uint32_t hash = 5381;
320 for (
unsigned char c : str ) {
322 hash = ( ( hash << 5 ) + hash ) ^ c;
348inline void sleep_ms(
int N ) { std::this_thread::sleep_for( std::chrono::milliseconds( N ) ); }
354inline void sleep_s(
int N ) { std::this_thread::sleep_for( std::chrono::seconds( N ) ); }
397inline std::string
stringf(
const char *format, ... )
400 va_start( ap, format );
402 int n = vsnprintf( tmp,
sizeof tmp, format, 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 );
416[[deprecated(
"This function will be removed soon, use Database::print" )]]
void
421template<
class TYPE, std::
size_t CAPACITY>
428 if ( x.size() > CAPACITY )
429 throw std::bad_alloc();
430 for (
const auto &y : x )
438 throw std::bad_alloc();
443 for (
size_t i = 0; i <
d_size; i++ )
457 template<
class... Args>
461 throw std::bad_alloc();
472 std::string outerOperatorName,
473 std::vector<std::string> nestedOperatorNames );
Provides C++ wrapper around MPI routines.
stackVector(std::initializer_list< TYPE > x)
const TYPE * begin() const
const TYPE & operator[](size_t i) const
TYPE & operator[](size_t i)
const TYPE & back() const
void emplace_back(Args &&...args)
void push_back(const TYPE &v)
#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
void fillRandom(std::vector< double > &)
Fill with random values in [0,1].
void printBanner()
Print AMP Banner.
void unique(std::vector< T > &x)
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.
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 >())
std::string intToString(int num, int min_width=1)
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.
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 >())
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 quicksort(size_t N, T *x)
void abort(const std::string &message, const source_location &source)