MADNESS 0.10.1
applyscreen.h
Go to the documentation of this file.
1#ifndef MADNESS_MRA_APPLYSCREEN_H__INCLUDED
2#define MADNESS_MRA_APPLYSCREEN_H__INCLUDED
3
4/// \file applyscreen.h
5/// \brief Destination screening of the apply of an integral operator.
6///
7/// The result of an apply is often multiplied with another function afterwards (the exchange operator:
8/// K f = sum_i ket_i * P(bra_i f)). A contribution to a box in which that multiplier is negligible is then
9/// never used, and need not be computed (Yanai, Fann, Gan, Harrison, Beylkin, JCP 121, 6680 (2004), eq. 17).
10/// A screen answers, per destination box, a bound on the multiplier's largest magnitude there (supbound.h).
11/// FunctionImpl::do_apply skips a block whose contribution times that bound, capped at one, is negligible,
12/// computes a kept block with its per-term tolerance loosened by the same factor, and accumulates it only
13/// if its norm times the factor is significant; a skipped block still counts as used by the shell loop.
14/// A skipped block leaves at most the block's share of the tolerance in the product function, since
15/// ||s du||_2 <= sup|s| ||du||_2. The tasks of one apply carry the screen (ApplyScreenHandle; apply_only,
16/// and the vector apply(world, op, f, screens)); without one, do_apply is unchanged.
17
18#include <madness/mra/key.h>
21#include <madness/world/world.h>
22#include <cstdio>
23#include <algorithm>
24#include <array>
25#include <atomic>
26#include <functional>
27#include <memory>
28#include <vector>
29
30namespace madness {
31
32 /// per destination box, a bound on the size of what the result of the apply will be multiplied with
33 template <std::size_t NDIM>
34 using apply_screen_t = std::function<double(const Key<NDIM>&)>;
35
36 /// The screen as the tasks of an apply carry it: shared ownership, so the screen lives as long as
37 /// any task of the apply does, and nothing is stored on the function. Empty means no screen. The
38 /// tasks stay on the rank that creates them (apply_only refuses apply_randomize), so the handle
39 /// does not travel.
40 template <std::size_t NDIM>
42 std::shared_ptr<const apply_screen_t<NDIM>> screen_;
43 public:
44 ApplyScreenHandle() = default;
46 : screen_(screen ? std::make_shared<const apply_screen_t<NDIM>>(screen) : nullptr) {}
47
48 explicit operator bool() const { return bool(screen_); }
49 double operator()(const Key<NDIM>& key) const { return (*screen_)(key); }
50
51 template <typename Archive>
52 void serialize(Archive&) {
53 MADNESS_EXCEPTION("no serialization of ApplyScreenHandle: the apply screen stays on its rank", 1);
54 }
55 };
56
57 /// Counters of the apply screen, process-wide: the blocks that passed the source test while a screen
58 /// was installed and the ones it skipped, and the sup bounds built for screens with the time it took.
59 /// Reset and reported by whoever installs the screens (the exchange operator, summed over the ranks);
60 /// a caller that runs the exchange tasks in its own queue reports after the queue ran.
62 static inline std::atomic<long> blocks{0}, skipped{0}, sup_maps{0}, sup_build_ns{0};
63 static void reset() { blocks = 0; skipped = 0; sup_maps = 0; sup_build_ns = 0; }
64
65 /// one line on rank 0 with the counters summed over the ranks. Collective.
66 static void report(World& world) {
67 std::array<long, 4> n{long(blocks), long(skipped), long(sup_maps), long(sup_build_ns)};
68 world.gop.sum(n.data(), n.size());
69 if (world.rank() != 0) return;
70 printf(" apply screen: blocks %ld, skipped %ld (%.1f%%)", n[0], n[1],
71 n[0] > 0 ? 100.0 * double(n[1]) / double(n[0]) : 0.0);
72 if (n[2] > 0)
73 printf("; sup bounds of %ld multipliers built in %.2fs (%.1f ms each)", n[2], 1e-9 * double(n[3]),
74 1e-6 * double(n[3]) / double(n[2]));
75 printf("\n");
76 }
77 };
78
79 /// The screen built from the multipliers' sup bounds (SupBoundMap, FunctionImpl::sup_bound_map): the
80 /// largest of their bounds in the box. A box none of the maps holds (not local to this rank, or without
81 /// coefficients) answers unbounded, which screens nothing.
82 template <std::size_t NDIM>
84 std::vector<std::shared_ptr<const SupBoundMap<NDIM>>> sup_;
85 public:
86 static constexpr double unbounded() { return SupBoundMap<NDIM>::unbounded(); }
87
88 explicit SupBoundScreen(std::vector<std::shared_ptr<const SupBoundMap<NDIM>>> sup) : sup_(std::move(sup)) {
89 MADNESS_CHECK_THROW(not sup_.empty(), "SupBoundScreen: at least one multiplier");
90 for (const auto& s : sup_) MADNESS_CHECK_THROW(bool(s), "SupBoundScreen: a missing sup bound");
91 }
92
93 double operator()(const Key<NDIM>& key) const {
94 double s = 0.0;
95 for (const auto& m : sup_) s = std::max(s, (*m)(key));
96 return s;
97 }
98 };
99
100} // namespace madness
101
102#endif // MADNESS_MRA_APPLYSCREEN_H__INCLUDED
ApplyScreenHandle(const apply_screen_t< NDIM > &screen)
Definition applyscreen.h:45
double operator()(const Key< NDIM > &key) const
Definition applyscreen.h:49
std::shared_ptr< const apply_screen_t< NDIM > > screen_
Definition applyscreen.h:42
void serialize(Archive &)
Definition applyscreen.h:52
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:71
Bounds on sup|f| per box of a function's tree (supbound.h).
Definition supbound.h:38
static constexpr double unbounded()
the answer for a box the map holds no bound for
Definition supbound.h:43
static constexpr double unbounded()
Definition applyscreen.h:86
double operator()(const Key< NDIM > &key) const
Definition applyscreen.h:93
SupBoundScreen(std::vector< std::shared_ptr< const SupBoundMap< NDIM > > > sup)
Definition applyscreen.h:88
std::vector< std::shared_ptr< const SupBoundMap< NDIM > > > sup_
Definition applyscreen.h:84
void sum(T *buf, size_t nelem)
Inplace global sum while still processing AM & tasks.
Definition worldgop.h:890
A parallel world class.
Definition world.h:134
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:344
WorldGopInterface & gop
Global operations.
Definition world.h:216
Multidimension Key for MRA tree and associated iterators.
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
std::function< double(const Key< NDIM > &)> apply_screen_t
per destination box, a bound on the size of what the result of the apply will be multiplied with
Definition applyscreen.h:34
Definition mraimpl.h:53
static const double m
Definition relops.cc:9
Definition applyscreen.h:61
static void reset()
Definition applyscreen.h:63
static std::atomic< long > skipped
Definition applyscreen.h:62
static std::atomic< long > sup_build_ns
Definition applyscreen.h:62
static std::atomic< long > sup_maps
Definition applyscreen.h:62
static void report(World &world)
one line on rank 0 with the counters summed over the ranks. Collective.
Definition applyscreen.h:66
static std::atomic< long > blocks
Definition applyscreen.h:62
Bounds on the largest magnitude of a function inside a box, from its scaling coefficients.
void e()
Definition test_sig.cc:75
constexpr std::size_t NDIM
Definition testgconv.cc:54
Declares the World class for the parallel runtime environment.