Skip to content

C++ API reference

Everything is in the namespace deblais and comes with #include <deblais/deblais.hpp>. The names of deblais::detail are not part of the interface and may change between versions.

The library needs C++17. It is developed and tested with GCC on x86-64 Linux.

This page is the guided tour. The exhaustive reference, generated by Doxygen from the comments of the headers, is at https://keckj.github.io/deblais/doxygen/. To build it locally:

doxygen docs/doxygen/Doxyfile   # output in build/doxygen/html

Solving

// Dimension known at compile time
template <int D>
Plan solve(const Cloud& source, const Cloud& target, const Options& options = {});

// Dimension known at run time, 1 <= dim <= DEBLAIS_MAX_DIM
Plan solve(int dim, const Cloud& source, const Cloud& target, const Options& options = {});

// Containers: points stored point after point, uniform masses or masses a and b
Plan solve(int dim, const std::vector<double>& x, const std::vector<double>& y, const Options& = {});
Plan solve(int dim, const std::vector<double>& x, const std::vector<double>& a,
           const std::vector<double>& y, const std::vector<double>& b, const Options& = {});

template <std::size_t D>
Plan solve(const std::vector<std::array<double, D>>& x, const std::vector<std::array<double, D>>& y,
           const Options& = {});
template <std::size_t D>
Plan solve(const std::vector<std::array<double, D>>& x, const std::vector<double>& a,
           const std::vector<std::array<double, D>>& y, const std::vector<double>& b, const Options& = {});

All of them compute an optimal plan between the two clouds for the cost \(|x - y|^p\), \(p = 2\) by default (Options::p). They throw std::invalid_argument when the input is invalid: empty cloud, null pointer, negative or non finite mass, zero total mass, totals that differ by more than Options::mass_tolerance, non finite coordinate, dimension out of range, sizes of the containers that do not match.

The run-time overloads instantiate the solver for each dimension from 1 to DEBLAIS_MAX_DIM, 4 by default. Define the macro before the include to change it; the compile time grows with it. solve<D> has no such limit.

The calls are reentrant, and a solve uses its own threads: do not call solve from inside an OpenMP parallel region expecting nested speed-ups.

Cloud

struct Cloud {
  const double* points;   // size * D coordinates: x0 y0 x1 y1 ... in 2D
  std::size_t size;       // number of points
  const double* masses;   // size masses >= 0, or nullptr: uniform, total 1

  Cloud(const double* points, std::size_t size, const double* masses = nullptr);
};

A view: nothing is copied or owned, the arrays must outlive the call. The target masses are rescaled to the total of the source masses.

Options

Field Default Meaning
Method method automatic automatic, line, simplex, multiscale, decomposition: see Concepts and Automatic mode
Flows flows automatic automatic, real (doubles) or integer (throws when the masses are neither uniform nor integers)
int threads 0 threads of the solve, 0: the OpenMP default
bool potentials true compute the potentials; only Method::line saves time without them
double p 2 exponent of the cost \(\lvert x - y \rvert^p\), any \(p > 0\); Method::line needs \(p \ge 1\). The automatic mode is tuned for 2, see Other costs
double mass_tolerance 1e-6 largest relative difference between the two total masses
int sweeps -1 sweeps of the domain decomposition per level, < 0: automatic (2). 0 turns decomposition into multiscale
int cell 0 sources per basic cell of the domain decomposition, 0: automatic (384 below 300 000 points, 512 above)
bool auction true use an auction on the levels of assignment problems
int pool 0 arcs kept per source by the simplex, 0: automatic
int helpers -1 pricing helper threads of the simplex, < 0: automatic
int min_size 0 size of the coarsest level of the multiscale start, 0: automatic (100 to 3000, a sixth of the points at most)

The last six are tuning knobs. The defaults are the measured best on the problems of the Performance page; none of them changes the result. tune measures them on your clouds: see Tuning.

Plan

struct Plan {
  Status status;                        // optimal, or stalled (optimal up to rounding)
  Method method;                        // the method that ran, never automatic
  double cost;                          // sum of mass[k] |x_source[k] - y_target[k]|^p
  double p;                             // exponent of the cost, Options::p
  std::vector<std::int32_t> source, target;
  std::vector<double> mass;             // arcs sorted by source, then target
  std::vector<double> potential_source; // f, size n (empty without potentials)
  std::vector<double> potential_target; // g, size m
  Stats stats;
  std::size_t size() const;             // number of arcs, at most n + m - 1
};

The potentials satisfy \(f_i + g_j \le |x_i - y_j|^p\) for every pair, with equality on the arcs, and cost \(= \sum_i a_i f_i + \sum_j b_j g_j\) (with the rescaled \(b\)).

Stats holds the counters of the solve: seconds, seconds_start (ordering of the points and multiscale start), pivots and passes of the final simplex, levels, local_problems, auction_bids, threads, integer_flows, max_violation.

Automatic mode

struct Recipe { Method method; int sweeps; int cell; int min_size; };

Recipe recipe(int dim, std::size_t n1, std::size_t n2, const Options& options = {});

What solve will run for these sizes and options: see Automatic mode.

Tuning

struct TuneOptions { double max_seconds = 0; double margin = 0.03; };
struct Trial { Options options; double seconds; };
struct Tuning {
  Options options;            // the fastest options, every tuning field explicit
  double seconds;             // time of a solve with them
  double seconds_automatic;   // time of a solve in the automatic mode
  Plan plan;                  // the plan of the clouds
  std::vector<Trial> trials;  // every configuration timed
};

template <int D>
Tuning tune(const Cloud& source, const Cloud& target, const Options& base = {}, const TuneOptions& bounds = {});
Tuning tune(int dim, const Cloud& source, const Cloud& target, const Options& base = {}, const TuneOptions& bounds = {});

The fastest options for these clouds on this machine, found by timing solves: see Automatic mode.

Certificates

struct Certificate {
  double primal, dual, gap, marginal_error, dual_violation;
  bool optimal(double tol = 1e-9) const;
};

template <int D>
Certificate certify(const Cloud& source, const Cloud& target, const Plan& plan, bool all_pairs = true);

An independent check of a plan with its potentials: see Concepts. With all_pairs, the dual constraint is evaluated on the \(n\,m\) pairs, in parallel: keep it for tests and moderate sizes. Without it, dual_violation is NaN and only the marginals and the gap are checked.

Version

DEBLAIS_VERSION_STRING (macro) and deblais::version (const char*), in <deblais/version.hpp>.

Compile-time configuration

Macro Effect
DEBLAIS_MAX_DIM largest dimension of the run-time overloads (default 4)
_OPENMP (set by -fopenmp) parallel pricing, sweeps and certificates; without it the solvers run on one thread

CMake

Option Default Effect
DEBLAIS_BUILD_TESTS ON at top level build deblais_tests (run with ctest)
DEBLAIS_BUILD_EXAMPLES ON at top level build deblais_quickstart, deblais_certify, deblais_bench
DEBLAIS_BUILD_PYTHON OFF build the Python extension module (pybind11)
DEBLAIS_OPENMP ON link OpenMP when it is found
DEBLAIS_NATIVE OFF -march=native for the tests, examples and Python module
DEBLAIS_ARCH empty -march=<value> for the same targets, e.g. x86-64-v3
DEBLAIS_PYTHON_MAX_DIM 8 largest dimension of the Python module

The target deblais::deblais itself adds no optimization flag: the flags of your own targets apply. The library is much faster with -O3 and the instruction set of the machine (-march=native).