Upload files to "utils"
This commit is contained in:
parent
394e1be746
commit
ba60cbdcb5
5 changed files with 3346 additions and 0 deletions
2875
utils/Doxyfile
Normal file
2875
utils/Doxyfile
Normal file
File diff suppressed because it is too large
Load diff
15
utils/Makefile
Normal file
15
utils/Makefile
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#
|
||||
# use GNU-Compiler tools
|
||||
COMPILER=GCC_
|
||||
|
||||
MAIN = main
|
||||
SOURCES = ${MAIN}.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
PROGRAM = ${MAIN}.${COMPILER}
|
||||
|
||||
default:doc
|
||||
|
||||
|
||||
include ../${COMPILER}default.mk
|
||||
|
||||
145
utils/info.h
Normal file
145
utils/info.h
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
//
|
||||
// Gundolf Haase, Oct 18 2024
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifdef __NVCC__
|
||||
#include <cuda_runtime.h>
|
||||
#endif
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
//#####################################
|
||||
// G.Haase
|
||||
// See https://sourceforge.net/p/predef/wiki/Compilers/
|
||||
// http://www.cplusplus.com/doc/tutorial/preprocessor/
|
||||
// also: export OMP_DISPLAY_ENV=VERBOSE
|
||||
//#####################################
|
||||
/** Checks for compilers, its versions, threads etc. on CPU
|
||||
*
|
||||
@param[in] argc number of command line arguments
|
||||
@param[in] argv command line arguments as array of C-strings
|
||||
*/
|
||||
template <class T>
|
||||
void check_env(T argc, char const *argv[])
|
||||
{
|
||||
std::cout << "\n#######################################################################\n";
|
||||
std::cout << "Code :";
|
||||
for (T k = 0; k < argc; ++k) std::cout << " " << argv[k];
|
||||
std::cout << std::endl;
|
||||
|
||||
// compiler: https://sourceforge.net/p/predef/wiki/Compilers/
|
||||
std::cout << "Compiler: ";
|
||||
#if defined __INTEL_COMPILER
|
||||
#pragma message(" ########## INTEL ###############")
|
||||
std::cout << "INTEL " << __INTEL_COMPILER;
|
||||
// Ignore warnings for #pragma acc unrecognice
|
||||
#pragma warning disable 161
|
||||
// Ignore warnings for #pragma omp unrecognice
|
||||
#pragma warning disable 3180
|
||||
|
||||
#elif defined __NVCC__
|
||||
//#pragma message(" ########## NVCC ###############")
|
||||
// https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/
|
||||
std::cout << "NVCC " << __CUDACC_VER_MAJOR__ << "." << __CUDACC_VER_MINOR__ ;
|
||||
#elif defined __clang__
|
||||
#pragma message(" ########## CLANG ###############")
|
||||
std::cout << "CLANG " << __clang_major__ << "." << __clang_minor__ << "."; // << __clang_patchlevel__;
|
||||
#elif defined __GNUC__
|
||||
//#pragma message(" ########## Gnu ###############")
|
||||
std::cout << "Gnu " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__;
|
||||
#else
|
||||
#pragma message(" ########## unknown Compiler ###############")
|
||||
std::cout << "unknown";
|
||||
#endif
|
||||
std::cout << " C++ standard: " << __cplusplus << std::endl;
|
||||
|
||||
// Parallel environments
|
||||
std::cout << "Parallel: ";
|
||||
#if defined MPI_VERSION
|
||||
#ifndef __GNUC__
|
||||
#pragma message(" ########## MPI ###############")
|
||||
#endif
|
||||
#ifdef OPEN_MPI
|
||||
std::cout << "OpenMPI ";
|
||||
#else
|
||||
std::cout << "MPI ";
|
||||
#endif
|
||||
std::cout << MPI_VERSION << "." << MPI_SUBVERSION << " ";
|
||||
#endif
|
||||
|
||||
#ifdef _OPENMP
|
||||
//https://www.openmp.org/specifications/
|
||||
//https://stackoverflow.com/questions/1304363/how-to-check-the-version-of-openmp-on-linux
|
||||
std::unordered_map<unsigned, std::string> const map{
|
||||
{200505, "2.5"}, {200805, "3.0"}, {201107, "3.1"}, {201307, "4.0"}, {201511, "4.5"}, {201611, "5.0"}, {201811, "5.0"}};
|
||||
#ifndef __GNUC__
|
||||
#pragma message(" ########## OPENMP ###############")
|
||||
#endif
|
||||
std::cout << "OpenMP ";
|
||||
try {
|
||||
std::cout << map.at(_OPENMP);
|
||||
}
|
||||
catch (...) {
|
||||
std::cout << _OPENMP;
|
||||
}
|
||||
#pragma omp parallel
|
||||
{
|
||||
#pragma omp master
|
||||
{
|
||||
const int nn = omp_get_num_threads(); // OpenMP
|
||||
std::cout << " ---> " << nn << " Threads ";
|
||||
}
|
||||
#pragma omp barrier
|
||||
}
|
||||
|
||||
#endif
|
||||
#ifdef _OPENACC
|
||||
#pragma message(" ########## OPENACC ###############")
|
||||
std::cout << "OpenACC ";
|
||||
#endif
|
||||
std::cout << std::endl;
|
||||
std::cout << "Date : " << __DATE__ << " " << __TIME__;
|
||||
std::cout << "\n#######################################################################\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @brief Lists basic properties of GPU
|
||||
*/
|
||||
inline
|
||||
void printGPUInfo()
|
||||
{
|
||||
using std::cout, std::endl, std::boolalpha;
|
||||
#ifdef __NVCC__
|
||||
cout <<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
|
||||
// https://devblogs.nvidia.com/how-query-device-properties-and-handle-errors-cuda-cc/
|
||||
int nDevices;
|
||||
cudaError_t err = cudaGetDeviceCount(&nDevices);
|
||||
if (err != cudaSuccess) printf("%i : %s\n", err, cudaGetErrorString(err));
|
||||
|
||||
// https://docs.nvidia.com/cuda/cuda-runtime-api/structcudaDeviceProp.html
|
||||
cudaDeviceProp prop;
|
||||
// https://www.cs.cmu.edu/afs/cs/academic/class/15668-s11/www/cuda-doc/html/group__CUDART__DEVICE_g5aa4f47938af8276f08074d09b7d520c.html
|
||||
err = cudaGetDeviceProperties (&prop, 0);
|
||||
if (err != cudaSuccess) printf("%i : %s\n", err, cudaGetErrorString(err));
|
||||
cout << "We work on " << nDevices << " x " << prop.name << " GPU \n with "
|
||||
<< prop.multiProcessorCount << " Multiprocessors (SME)"
|
||||
<< ", Compute capability " << prop.major << "." << prop.minor << endl;
|
||||
cout << "global Mem: " << prop.totalGlobalMem/1024/1000/1000 << " GB" << endl;
|
||||
cout << "shared Mem per SME: " << prop.sharedMemPerMultiprocessor/1024 << "kB"
|
||||
<< " shared Mem per Block: " << prop.sharedMemPerBlock/1024 << " kB"
|
||||
<< " 32b-Registers per Block: " << prop.regsPerBlock << endl;
|
||||
cout << "global L2 cache: " << prop.l2CacheSize/1024 << " kB"
|
||||
<< " local L1 cache supported: " << boolalpha << bool(prop.localL1CacheSupported) << endl;
|
||||
cout << "max Threads per Block:" << prop.maxThreadsPerBlock << endl;
|
||||
cout <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
|
||||
#else
|
||||
cout << endl << "No compiler support for GPU" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
260
utils/std_lib_facilities.h
Normal file
260
utils/std_lib_facilities.h
Normal file
|
|
@ -0,0 +1,260 @@
|
|||
/*
|
||||
std_lib_facilities.h
|
||||
*/
|
||||
|
||||
/*
|
||||
simple "Programming: Principles and Practice using C++ (second edition)" course header to
|
||||
be used for the first few weeks.
|
||||
It provides the most common standard headers (in the global namespace)
|
||||
and minimal exception/error support.
|
||||
|
||||
Students: please don't try to understand the details of headers just yet.
|
||||
All will be explained. This header is primarily used so that you don't have
|
||||
to understand every concept all at once.
|
||||
|
||||
By Chapter 10, you don't need this file and after Chapter 21, you'll understand it
|
||||
|
||||
Revised April 25, 2010: simple_error() added
|
||||
|
||||
Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
|
||||
Revised November 28 2013: add a few container algorithms
|
||||
Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
|
||||
Revised Febrary 2 2015: randint() can now be seeded (see exercise 5.13).
|
||||
Revised August 3, 2020: a cleanup removing support for ancient compilers
|
||||
*/
|
||||
|
||||
#ifndef H112
|
||||
#define H112 080315L
|
||||
|
||||
|
||||
#include<iostream>
|
||||
#include<iomanip>
|
||||
#include<fstream>
|
||||
#include<sstream>
|
||||
#include<cmath>
|
||||
#include<cstdlib>
|
||||
#include<string>
|
||||
#include<list>
|
||||
#include <forward_list>
|
||||
#include<vector>
|
||||
#include<unordered_map>
|
||||
#include<algorithm>
|
||||
#include <array>
|
||||
#include <regex>
|
||||
#include<random>
|
||||
#include<stdexcept>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
typedef long Unicode;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<class T> string to_string(const T& t)
|
||||
{
|
||||
ostringstream os;
|
||||
os << t;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
struct Range_error : out_of_range { // enhanced vector range error reporting
|
||||
int index;
|
||||
Range_error(int i) :out_of_range("Range error: " + to_string(i)), index(i) { }
|
||||
};
|
||||
|
||||
|
||||
// trivially range-checked vector (no iterator checking):
|
||||
template< class T> struct Vector : public std::vector<T> {
|
||||
using size_type = typename std::vector<T>::size_type;
|
||||
|
||||
/* #ifdef _MSC_VER
|
||||
// microsoft doesn't yet support C++11 inheriting constructors
|
||||
Vector() { }
|
||||
explicit Vector(size_type n) :std::vector<T>(n) {}
|
||||
Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
|
||||
template <class I>
|
||||
Vector(I first, I last) : std::vector<T>(first, last) {}
|
||||
Vector(initializer_list<T> list) : std::vector<T>(list) {}
|
||||
*/
|
||||
using std::vector<T>::vector; // inheriting constructor
|
||||
|
||||
T& operator[](unsigned int i) // rather than return at(i);
|
||||
{
|
||||
if (/*i<0 || */ this->size() <= i) throw Range_error(i);
|
||||
return std::vector<T>::operator[](i);
|
||||
}
|
||||
const T& operator[](unsigned int i) const
|
||||
{
|
||||
if (/*i<0 || */ this->size() <= i) throw Range_error(i);
|
||||
return std::vector<T>::operator[](i);
|
||||
}
|
||||
};
|
||||
|
||||
// disgusting macro hack to get a range checked vector:
|
||||
#define vector Vector
|
||||
|
||||
// trivially range-checked string (no iterator checking):
|
||||
struct String : std::string {
|
||||
using size_type = std::string::size_type;
|
||||
// using string::string;
|
||||
|
||||
char& operator[](unsigned int i) // rather than return at(i);
|
||||
{
|
||||
if (/*i<0 || */ size() <= i) throw Range_error(i);
|
||||
return std::string::operator[](i);
|
||||
}
|
||||
|
||||
const char& operator[](unsigned int i) const
|
||||
{
|
||||
if (/*i<0 || */ size() <= i) throw Range_error(i);
|
||||
return std::string::operator[](i);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
namespace std {
|
||||
|
||||
template<> struct hash<String>
|
||||
{
|
||||
size_t operator()(const String& s) const
|
||||
{
|
||||
return hash<std::string>()(s);
|
||||
}
|
||||
};
|
||||
|
||||
} // of namespace std
|
||||
|
||||
|
||||
struct Exit : runtime_error {
|
||||
Exit() : runtime_error("Exit") {}
|
||||
};
|
||||
|
||||
// error() simply disguises throws:
|
||||
inline void error(const string& s)
|
||||
{
|
||||
throw runtime_error(s);
|
||||
}
|
||||
|
||||
inline void error(const string& s, const string& s2)
|
||||
{
|
||||
error(s + s2);
|
||||
}
|
||||
|
||||
inline void error(const string& s, int i)
|
||||
{
|
||||
ostringstream os;
|
||||
os << s << ": " << i;
|
||||
error(os.str());
|
||||
}
|
||||
|
||||
|
||||
template<class T> char* as_bytes(T& i) // needed for binary I/O
|
||||
{
|
||||
void* addr = &i; // get the address of the first byte
|
||||
// of memory used to store the object
|
||||
return static_cast<char*>(addr); // treat that memory as bytes
|
||||
}
|
||||
|
||||
|
||||
inline void keep_window_open()
|
||||
{
|
||||
cin.clear();
|
||||
cout << "Please enter a character to exit\n";
|
||||
char ch;
|
||||
cin >> ch;
|
||||
return;
|
||||
}
|
||||
|
||||
inline void keep_window_open(string s)
|
||||
{
|
||||
if (s == "") return;
|
||||
cin.clear();
|
||||
cin.ignore(120, '\n');
|
||||
for (;;) {
|
||||
cout << "Please enter " << s << " to exit\n";
|
||||
string ss;
|
||||
while (cin >> ss && ss != s)
|
||||
cout << "Please enter " << s << " to exit\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// error function to be used (only) until error() is introduced in Chapter 5:
|
||||
inline void simple_error(string s) // write ``error: s and exit program
|
||||
{
|
||||
cerr << "error: " << s << '\n';
|
||||
keep_window_open(); // for some Windows environments
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// make std::min() and std::max() accessible on systems with antisocial macros:
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
|
||||
// run-time checked narrowing cast (type conversion). See ???.
|
||||
template<class R, class A> R narrow_cast(const A& a)
|
||||
{
|
||||
R r = R(a);
|
||||
if (A(r) != a) error(string("info loss"));
|
||||
return r;
|
||||
}
|
||||
|
||||
// random number generators. See 24.7.
|
||||
|
||||
inline default_random_engine& get_rand()
|
||||
{
|
||||
static default_random_engine ran; // note: not thread_local
|
||||
return ran;
|
||||
};
|
||||
|
||||
inline void seed_randint(int s) { get_rand().seed(s); }
|
||||
|
||||
inline int randint(int min, int max) { return uniform_int_distribution<>{min, max}(get_rand()); }
|
||||
|
||||
inline int randint(int max) { return randint(0, max); }
|
||||
|
||||
//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x
|
||||
|
||||
// container algorithms. See 21.9. // C++ has better versions of this:
|
||||
|
||||
template<typename C>
|
||||
using Value_type = typename C::value_type;
|
||||
|
||||
template<typename C>
|
||||
using Iterator = typename C::iterator;
|
||||
|
||||
template<typename C>
|
||||
// requires Container<C>()
|
||||
void sort(C& c)
|
||||
{
|
||||
std::sort(c.begin(), c.end());
|
||||
}
|
||||
|
||||
template<typename C, typename Pred>
|
||||
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
|
||||
void sort(C& c, Pred p)
|
||||
{
|
||||
std::sort(c.begin(), c.end(), p);
|
||||
}
|
||||
|
||||
template<typename C, typename Val>
|
||||
// requires Container<C>() && Equality_comparable<C,Val>()
|
||||
Iterator<C> find(C& c, Val v)
|
||||
{
|
||||
return std::find(c.begin(), c.end(), v);
|
||||
}
|
||||
|
||||
template<typename C, typename Pred>
|
||||
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
|
||||
Iterator<C> find_if(C& c, Pred p)
|
||||
{
|
||||
return std::find_if(c.begin(), c.end(), p);
|
||||
}
|
||||
|
||||
#endif //H112
|
||||
51
utils/timing.h
Normal file
51
utils/timing.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
//
|
||||
// Gundolf Haase, Oct 18 2024
|
||||
//
|
||||
#pragma once
|
||||
#include <chrono> // timing
|
||||
#include <stack>
|
||||
|
||||
//using Clock = std::chrono::system_clock; //!< The wall clock timer chosen
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
using TPoint= std::chrono::time_point<Clock>;
|
||||
|
||||
// [Galowicz, C++17 STL Cookbook, p. 29]
|
||||
inline
|
||||
std::stack<TPoint> MyStopWatch; //!< starting time of stopwatch
|
||||
|
||||
/** Starts stopwatch timer.
|
||||
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
|
||||
*
|
||||
* The timining can be nested and the recent time point is stored on top of the stack.
|
||||
*
|
||||
* @return recent time point
|
||||
* @see toc
|
||||
*/
|
||||
inline auto tic()
|
||||
{
|
||||
MyStopWatch.push(Clock::now());
|
||||
return MyStopWatch.top();
|
||||
}
|
||||
|
||||
/** Returns the elapsed time from stopwatch.
|
||||
*
|
||||
* The time point from top of the stack is used
|
||||
* if time point @p t_b is not passed as input parameter.
|
||||
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
|
||||
* or as @code auto t_b = tic(); myfunction(...) ; double tsec = toc(t_b); @endcode
|
||||
* The last option is to be used in the case of
|
||||
* non-nested but overlapping time measurements.
|
||||
*
|
||||
* @param[in] t_b start time of some stop watch
|
||||
* @return elapsed time in seconds.
|
||||
*
|
||||
*/
|
||||
inline double toc(TPoint const &t_b = MyStopWatch.top())
|
||||
{
|
||||
// https://en.cppreference.com/w/cpp/chrono/treat_as_floating_point
|
||||
using Unit = std::chrono::seconds;
|
||||
using FpSeconds = std::chrono::duration<double, Unit::period>;
|
||||
auto t_e = Clock::now();
|
||||
MyStopWatch.pop();
|
||||
return FpSeconds(t_e-t_b).count();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue