Skip to content

Commit

Permalink
🐛 update src
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfram77 committed Jun 21, 2023
1 parent f991e63 commit dc4ebe2
Show file tree
Hide file tree
Showing 35 changed files with 3,447 additions and 4,591 deletions.
972 changes: 379 additions & 593 deletions src/Graph.hxx

Large diffs are not rendered by default.

906 changes: 133 additions & 773 deletions src/_algorithm.hxx

Large diffs are not rendered by default.

1,122 changes: 141 additions & 981 deletions src/_bitset.hxx

Large diffs are not rendered by default.

70 changes: 47 additions & 23 deletions src/_cmath.hxx
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
#pragma once
#include <type_traits>
#include <cmath>
#include <random>

using std::is_floating_point;
using std::uniform_int_distribution;
using std::ceil;
using std::sqrt;




// COALESCE
// --------
// Similar to JavaScript coalescing || operator.

template <class T>
T coalesce(T x, T d=T()) {
return x!=T()? x : d;
}




// CEIL-DIV
// CEIL DIV
// --------
// For kernel launch calculation.

template <class T>
T ceilDiv(T x, T y) { return (x + y-1) / y; }
template <>
float ceilDiv<float>(float x, float y) { return ceil(x/y); }
template <>
double ceilDiv<double>(double x, double y) { return ceil(x/y); }
inline T ceilDiv(T x, T y) {
if (is_floating_point<T>::value) return ceil(x/y);
return (x + y-1) / y;
}



Expand All @@ -37,29 +29,61 @@ double ceilDiv<double>(double x, double y) { return ceil(x/y); }
// https://stackoverflow.com/a/4609795/1413259

template <typename T>
int sgn(T x) {
inline int sgn(T x) {
return (T() < x) - (x < T());
}




// POW-2
// -----
// POW2
// ----

template <class T>
constexpr bool isPow2(T x) noexcept {
return !(x & (x-1));
}


template <class T>
constexpr T prevPow2(T x) noexcept {
return 1 << T(log2(x));
}


template <class T>
constexpr T nextPow2(T x) noexcept {
return 1 << T(ceil(log2(x)));
}




// PRIME
// -----

template <class T>
inline bool isPrime(T x) {
// 1. 2, 3 are prime
if (x<=3) return x>1;
// 2. Multiples of 2, 3 not prime
if (x % 2==0 || x % 3==0) return false;
// 3. Factor of 6k-1 or 6k+1 => not prime
for (T i=6, I=T(sqrt(x))+1; i<=I; i+=6)
if (x % (i-1)==0 || x % (i+1)==0) return false;
return true;
}

template <class T>
inline T nextPrime(T x) {
while (true)
if (isPrime(++x)) return x;
}

template <class T, class R>
inline T randomPrime(T begin, T end, R& rnd) {
uniform_int_distribution<T> dis(begin, end);
for (int i=128; i>0; --i) {
T a = dis(rnd);
if (isPrime(a)) return a;
}
return end-1;
}
19 changes: 14 additions & 5 deletions src/_ctypes.hxx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#pragma once
#include <cstddef>
#include <type_traits>
#include <istream>
#include <ostream>
#include <utility>

using std::pair;
using std::make_signed_t;
using std::istream;
using std::ostream;




// BASIC
// -----

using ssize_t = make_signed_t<size_t>;




// NONE
// ----
// Zero size type.
Expand All @@ -28,17 +37,17 @@ struct None {
friend ostream& operator<<(ostream& a, None x) noexcept { return a; }

// Lifetime operators.
None() {}
explicit None() {}
template <class T>
None(T _) {}
explicit None(T _) {}
};
#define NONE None
#endif




// TEMPLATE-TYPE
// TEMPLATE TYPE
// -------------
// For template classes in template :).

Expand Down
231 changes: 231 additions & 0 deletions src/_debug.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
#pragma once
#include <chrono>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#ifdef MPI
#include "_mpi.hxx"
#endif
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=1
#include <unistd.h>
#include <signal.h>
#include <execinfo.h>
#endif

using std::chrono::system_clock;
using std::time_t;
using std::tm;




// BUILD
// -----
// Build modes.

#ifndef BUILD_RELEASE
#define BUILD_RELEASE 0
#define BUILD_ERROR 1
#define BUILD_WARNING 2
#define BUILD_INFO 3
#define BUILD_DEBUG 4
#define BUILD_TRACE 5
#endif




// PERFORM
// -------

#ifndef PEFORME
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_ERROR
#define PERFORME(...) __VA_ARGS__
#else
#define PERFORME(...)
#endif
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_WARNING
#define PERFORMW(...) __VA_ARGS__
#else
#define PERFORMW(...)
#endif
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_INFO
#define PERFORMI(...) __VA_ARGS__
#else
#define PERFORMI(...)
#endif
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_DEBUG
#define PERFORMD(...) __VA_ARGS__
#else
#define PERFORMD(...)
#endif
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_TRACE
#define PERFORMT(...) __VA_ARGS__
#else
#define PERFORMT(...)
#endif
#endif




// PRINT
// -----

#ifndef FPRINTFE
#define FPRINTFE(...) PERFORME(fprintf(__VA_ARGS__))
#define FPRINTFW(...) PERFORMW(fprintf(__VA_ARGS__))
#define FPRINTFI(...) PERFORMI(fprintf(__VA_ARGS__))
#define FPRINTFD(...) PERFORMD(fprintf(__VA_ARGS__))
#define FPRINTFT(...) PERFORMT(fprintf(__VA_ARGS__))
#endif

#ifndef PRINTFE
#define PRINTFE(...) PERFORME(printf(__VA_ARGS__))
#define PRINTFW(...) PERFORMW(printf(__VA_ARGS__))
#define PRINTFI(...) PERFORMI(printf(__VA_ARGS__))
#define PRINTFD(...) PERFORMD(printf(__VA_ARGS__))
#define PRINTFT(...) PERFORMT(printf(__VA_ARGS__))
#endif

#ifndef WRITEE
#define WRITEE(...) PERFORME(write(__VA_ARGS__))
#define WRITEW(...) PERFORMW(write(__VA_ARGS__))
#define WRITEI(...) PERFORMI(write(__VA_ARGS__))
#define WRITED(...) PERFORMD(write(__VA_ARGS__))
#define WRITET(...) PERFORMT(write(__VA_ARGS__))
#endif

#ifndef PRINTE
#define PRINTE(...) PERFORME(print(__VA_ARGS__))
#define PRINTW(...) PERFORMW(print(__VA_ARGS__))
#define PRINTI(...) PERFORMI(print(__VA_ARGS__))
#define PRINTD(...) PERFORMD(print(__VA_ARGS__))
#define PRINTT(...) PERFORMT(print(__VA_ARGS__))
#endif

#ifndef PRINTLNE
#define PRINTLNE(...) PERFORME(println(__VA_ARGS__))
#define PRINTLNW(...) PERFORMW(println(__VA_ARGS__))
#define PRINTLNI(...) PERFORMI(println(__VA_ARGS__))
#define PRINTLND(...) PERFORMD(println(__VA_ARGS__))
#define PRINTLNT(...) PERFORMT(println(__VA_ARGS__))
#endif




// LOG
// ---

#ifndef LOG
void logPrefix() {
time_t s = system_clock::to_time_t(system_clock::now());
tm *t = localtime(&s);
#ifdef MPI
printf("%04d-%02d-%02d %02d:%02d:%02d P%02d:"
#else
printf("%04d-%02d-%02d %02d:%02d:%02d"
#endif
, t->tm_year + 1900
, t->tm_mon + 1
, t->tm_mday
, t->tm_hour
, t->tm_min
, t->tm_sec
#ifdef MPI
, mpi_comm_rank()
#endif
);
}
#define LOG(...) do { logPrefix(); printf(" " __VA_ARGS__); } while (0)
#endif

#ifndef LOGE
#define LOGE(...) PERFORME(LOG(__VA_ARGS__))
#define LOGW(...) PERFORMW(LOG(__VA_ARGS__))
#define LOGI(...) PERFORMI(LOG(__VA_ARGS__))
#define LOGD(...) PERFORMD(LOG(__VA_ARGS__))
#define LOGT(...) PERFORMT(LOG(__VA_ARGS__))
#endif




// TRY
// ---

#ifndef TRY_MPIE
#ifdef MPI
#define TRY_MPIE(exp) PERFORME(TRY_MPI(exp))
#define TRY_MPIW(exp) PERFORMW(TRY_MPI(exp))
#define TRY_MPII(exp) PERFORMI(TRY_MPI(exp))
#define TRY_MPID(exp) PERFORMD(TRY_MPI(exp))
#define TRY_MPIT(exp) PERFORMT(TRY_MPI(exp))
#endif
#endif


#ifndef TRY
#ifdef MPI
#define TRY(exp) TRY_MPI(exp)
#endif
#endif

#ifndef TRYE
#ifdef MPI
#define TRYE(exp) TRY_MPIE(exp)
#define TRYW(exp) TRY_MPIW(exp)
#define TRYI(exp) TRY_MPII(exp)
#define TRYD(exp) TRY_MPID(exp)
#define TRYT(exp) TRY_MPIT(exp)
#endif
#endif




// ASSERT
// ------

#ifndef ASSERT
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=BUILD_ERROR
#ifdef MPI
#define ASSERT(exp) ASSERT_MPI(exp)
#define ASSERT_THAT(exp, msg) ASSERT_MPI((exp) && (msg))
#else
#define ASSERT(exp) assert(exp)
#define ASSERT_THAT(exp, msg) assert((exp) && (msg))
#endif
#else
#define ASSERT(exp)
#define ASSERT_THAT(exp, msg)
#endif
#endif




// ON SIGNAL
// ---------

#define STACK_TRACE_SIZE 32

void on_sigsegv(int sig) {
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=1
void *entries[STACK_TRACE_SIZE];
size_t n = backtrace(entries, STACK_TRACE_SIZE);
fprintf(stderr, "ERROR: SIGNAL %d:\n", sig);
backtrace_symbols_fd(entries, n, STDERR_FILENO);
exit(1);
#endif
}
// - https://stackoverflow.com/a/77336/1413259

void install_sigsegv() {
#if !defined(NDEBUG) && defined(BUILD) && BUILD>=1
signal(SIGSEGV, on_sigsegv);
#endif
}
// - https://stackoverflow.com/a/77336/1413259
Loading

0 comments on commit dc4ebe2

Please sign in to comment.