1#ifndef SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
2#define SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
34 const long granularity_level) {
36 const long nsw = std::max<long>(1, nsubworld);
37 const long level = std::max<long>(1, granularity_level);
38 return std::min<long>(level * nsw,
long(n));
48 const long granularity_level) {
49 std::vector<Batch_1D> out;
50 if (n == 0)
return out;
52 const long bs_floor = long(n) / nbatch;
53 const long rem = long(n) - bs_floor * nbatch;
55 for (
long b = 0;
b < nbatch; ++
b) {
56 const long sz = bs_floor + (
b < rem ? 1 : 0);
57 out.emplace_back(begin, begin + sz);
80inline std::vector<std::pair<Batch_1D, Batch_1D>>
84 std::vector<std::pair<Batch_1D, Batch_1D>> out;
85 out.reserve(columns.size() * rows.size());
86 for (
const auto&
c : columns)
87 for (
const auto& r : rows) out.emplace_back(
c, r);
97template<
typename T, std::
size_t NDIM>
100 if (
a.size() !=
b.size())
return false;
101 for (std::size_t i = 0; i <
a.size(); ++i)
107template <std::
size_t NDIM>
113template <
typename T, std::
size_t NDIM>
116 if (multipliers.empty())
return sup;
119 auto build = [&sup, &multipliers](
const rangeT::iterator& it) ->
bool {
120 sup[it] = std::make_shared<const SupBoundMap<NDIM>>(multipliers[it].get_impl()->sup_bound_map());
132template <std::
size_t NDIM>
144inline std::map<std::pair<long,long>,
long>
146 std::map<std::pair<long,long>,
long> owner;
147 if (ncolumn <= 0 or nrow <= 0 or nworker <= 0)
return owner;
148 for (
long c = 0;
c < ncolumn; ++
c)
149 for (
long r = 0; r < nrow; ++r) owner[{
c, r}] =
c % nworker;
158 const long a = std::max(i, j),
b = std::min(i, j);
159 return a * (
a + 1) / 2 +
b;
180inline std::map<std::pair<long,long>,
long>
182 std::map<std::pair<long,long>,
long> owner;
183 if (n <= 0 or M <= 0)
return owner;
184 auto eligible = [n](
long i,
long j,
long t) ->
bool {
185 if (i == j)
return (i % n) == t;
186 return ((i % n) == t) or ((j % n) == t);
189 const std::size_t ntask = std::size_t(M) * std::size_t(M + 1) / 2;
190 std::vector<std::pair<long,long>> tasks;
191 tasks.reserve(ntask);
192 for (
long i = 0; i < M; ++i)
193 for (
long j = 0; j <= i; ++j)
194 tasks.emplace_back(i, j);
196 std::vector<std::vector<std::pair<long,long>>> T(n);
203 std::vector<char> taken(ntask, 0);
204 std::vector<std::size_t> cursor(n, 0);
205 std::size_t placed = 0;
206 long t = 0, misses = 0;
207 while (placed < ntask and misses < n) {
208 std::size_t&
c = cursor[t];
209 while (
c < ntask and (taken[
c] or not eligible(tasks[
c].first, tasks[
c].second, t))) ++
c;
212 T[t].push_back(tasks[
c]);
222 for (std::size_t idx = 0; idx < ntask; ++idx)
223 if (not taken[idx]) T[tasks[idx].first % n].push_back(tasks[idx]);
226 for (
long iter = 0; iter < 10000; ++iter) {
227 long big = 0,
small = 0;
228 for (
long p = 1;
p < n; ++
p) {
229 if (
long(T[
p].size()) >
long(T[big].size())) big =
p;
230 if (
long(T[
p].size()) <
long(T[
small].size()))
small =
p;
232 if (
long(T[big].size()) -
long(T[
small].size()) <= 1)
break;
234 std::vector<long> order(n);
235 for (
long p = 0;
p < n; ++
p) order[
p] =
p;
236 std::sort(order.begin(), order.end(),
237 [&T](
long a,
long b){ return T[a].size() > T[b].size(); });
238 for (
long bi = 0; bi < n and not moved; ++bi) {
239 for (
long si = n - 1; si > bi and not moved; --si) {
240 const long bt = order[bi], st = order[si];
241 if (
long(T[bt].size()) -
long(T[st].size()) <= 1)
continue;
242 for (
long idx = 0; idx < long(T[bt].size()); ++idx) {
243 const auto& tk = T[bt][idx];
244 if (tk.first == tk.second)
continue;
245 if (not eligible(tk.first, tk.second, st))
continue;
247 T[bt].erase(T[bt].begin() + idx);
253 if (not moved)
break;
256 for (
long p = 0;
p < n; ++
p)
257 for (
const auto& tk : T[
p])
274inline std::map<std::pair<long,long>,
long>
276 std::map<std::pair<long,long>,
long> owner;
277 if (n <= 0 or M <= 0)
return owner;
278 auto C = [&](
long i,
long j) ->
double {
279 const long t =
exchange_sym_tri(i, j);
return (t <
long(cost.size())) ? cost[t] : 0.0;
281 std::vector<double>
load(n, 0.0);
283 for (
long i = 0; i < M; ++i) {
const long r = i % n;
load[r] +=
C(i,i); owner[{i,i}] = r; }
285 std::vector<std::pair<long,long>> off;
286 off.reserve(std::size_t(M) * std::size_t(M > 0 ? M - 1 : 0) / 2);
287 for (
long i = 0; i < M; ++i)
for (
long j = 0; j < i; ++j) off.emplace_back(i, j);
288 std::sort(off.begin(), off.end(), [&](
const std::pair<long,long>&
A,
const std::pair<long,long>&
B){
289 const double ca = C(A.first,A.second), cb = C(B.first,B.second);
290 return (ca != cb) ? (ca > cb) : (A < B);
292 for (
const auto& [i,j] : off) {
293 const long ri = i % n, rj = j % n;
294 const long r = (ri == rj) ? ri : (
load[ri] <=
load[rj] ? ri : rj);
295 owner[{i,j}] = r;
load[r] +=
C(i,j);
298 for (
long pass = 0; pass < 50; ++pass) {
299 long hot = 0;
for (
long p = 1;
p < n; ++
p)
if (
load[
p] >
load[hot]) hot =
p;
300 bool improved =
false;
301 for (
const auto& [i,j] : off) {
302 auto it = owner.find({i,j});
303 if (it->second != hot)
continue;
304 const long ri = i % n, rj = j % n;
305 if (ri == rj)
continue;
306 const long other = (hot == ri) ? rj : ri;
307 const double c =
C(i,j);
309 load[hot] -=
c;
load[other] +=
c; it->second = other; improved =
true;
312 if (not improved)
break;
351 if (tier == 2)
waited =
true;
357 static const bool on = (std::getenv(
"MAD_EXCH_TASK_PROFILE") !=
nullptr);
368 static const bool on = (std::getenv(
"MAD_EXCH_FORCE_GENERAL") !=
nullptr);
378 static std::ofstream os;
379 if (not os.is_open()) {
380 os.open(
"exch_taskprof.r" + std::to_string(
p.universe_rank) +
".jsonl", std::ios::app);
381 if (not os.is_open())
return;
383 os <<
"{\"task\":" <<
p.task_id
384 <<
",\"rank\":" <<
p.universe_rank
385 <<
",\"subworld\":" <<
p.subworld_id
386 <<
",\"subworld_nrank\":" <<
p.subworld_nrank
387 <<
",\"thresh\":" <<
p.thresh
389 <<
",\"diagonal\":" << (
p.diagonal ?
"true" :
"false")
390 <<
",\"row\":[" <<
p.row_begin <<
"," <<
p.row_end <<
"]"
391 <<
",\"col\":[" <<
p.col_begin <<
"," <<
p.col_end <<
"]"
392 <<
",\"wall_start\":" <<
p.wall_start
393 <<
",\"wall\":" << (
p.wall_end -
p.wall_start)
394 <<
",\"wait_for_data\":" <<
p.wait_for_data_wall
395 <<
",\"compute_wall\":" <<
p.compute_wall
396 <<
",\"compute_cpu\":" <<
p.compute_cpu
397 <<
",\"compute_components_wall\":{"
398 <<
"\"mul1\":" <<
p.mul1_wall <<
",\"apply\":" <<
p.apply_wall
399 <<
",\"mul2\":" <<
p.mul2_wall <<
",\"truncate\":" <<
p.truncate_wall
400 <<
",\"other\":" << (
p.compute_wall -
p.mul1_wall -
p.apply_wall
401 -
p.mul2_wall -
p.truncate_wall)
403 <<
",\"operand_source\":" <<
p.operand_source
404 <<
",\"waited\":" << (
p.waited ?
"true" :
"false")
405 <<
",\"peak_rss_gb\":" <<
p.peak_rss_gb
418 const std::vector<double>& cost) {
419 if (M <= 0 or cost.empty())
return;
421 std::snprintf(
name,
sizeof(
name),
"Ccall%03ld_k%ld.csv", call_index,
k);
422 std::ofstream os(
name);
423 if (not os.is_open())
return;
424 os <<
"# call=" << call_index <<
" k=" <<
k <<
" M=" << M <<
"\n";
426 for (
long i = 0; i < M; ++i)
427 for (
long j = 0; j <= i; ++j) {
428 const long t = i * (i + 1) / 2 + j;
429 if (t <
long(cost.size())) os << i <<
"," << j <<
"," << cost[t] <<
"\n";
447template<
typename T, std::
size_t NDIM>
449 std::size_t
k = 0x5a17ull;
458 std::size_t
k = std::size_t(salt);
475template<
typename keyT,
typename dataT>
483 for (
const auto& s :
slots_)
if (s.key == key)
return true;
489 for (
auto it =
slots_.begin(); it !=
slots_.end(); ++it) {
490 if (it->key == key) {
492 return &
slots_.front().data;
500 slots_.push_front(Slot{key, std::move(
data), pinned});
505 if (not it->pinned) {
slots_.erase(it);
break; }
508 return slots_.front().data;
518 for (
const auto& s :
slots_)
if (not s.pinned) ++
c;
533template <
typename T, std::
size_t NDIM>
538 template <
typename Archive>
548template <
typename T, std::
size_t NDIM>
561 dests_ = std::move(dests);
566 for (
const auto& r : recs) {
570 "exchange finalize: a chunk names a destination this rank has not "
571 "been given, so its reducer targets were never set");
572 typename implT::dcT::accessor acc;
573 dests_[r.f]->get_coeffs().insert(acc, r.key);
574 acc->second.template gaxpy_inplace<T, T>(T(1.0), r.node,
beta_);
594template <
typename T, std::
size_t NDIM>
600 const std::size_t chunk_entries) {
604 std::vector<implT*> dests(dest_vec.size(),
nullptr);
605 for (std::size_t
f = 0;
f < dest_vec.size(); ++
f)
610 std::map<ProcessID, std::vector<recT>> buckets;
611 const std::size_t nf = std::min(src_vec.size(), dest_vec.size());
612 for (std::size_t
f = 0;
f < nf; ++
f) {
613 auto simpl = src_vec[
f].get_impl();
614 auto dimpl = dest_vec[
f].get_impl();
615 if (not simpl or not dimpl)
continue;
616 const implT& dref = *dimpl;
617 for (
auto it = simpl->get_coeffs().begin(); it != simpl->get_coeffs().end(); ++it) {
618 const ProcessID owner = dref.get_coeffs().owner(it->first);
619 std::vector<recT>&
b = buckets[owner];
620 b.push_back(recT{
f, it->first, it->second});
621 if (
b.size() >= chunk_entries) {
627 for (
auto& kv : buckets)
628 if (not kv.second.empty())
633template<
typename T, std::
size_t NDIM>
665 world.gop.sum(resident);
666 world.gop.sum(fetched);
667 world.gop.sum(ahead);
673 j[
"batch_cache_hits"] = long(resident);
674 j[
"batch_cache_misses"] = long(fetched);
675 j[
"batch_prefetch_hits"] = long(ahead);
681 if (
world.rank() == 0) {
682 printf(
" cpu time spent in multiply1 %8.2fs\n", timings[
"multiply1"].
template get<double>());
683 printf(
" cpu time spent in apply %8.2fs\n", timings[
"apply"].
template get<double>());
684 printf(
" cpu time spent in multiply2 %8.2fs\n", timings[
"multiply2"].
template get<double>());
685 printf(
" total wall time %8.2fs\n", timings[
"total"].
template get<double>());
688 const long resident = timings[
"batch_cache_hits"].template get<long>();
689 const long fetched = timings[
"batch_cache_misses"].template get<long>();
690 const long ahead = timings[
"batch_prefetch_hits"].template get<long>();
691 if (resident + fetched + ahead > 0)
692 printf(
" operand batches resident/ahead/fetched %6ld /%6ld /%6ld\n",
693 resident, ahead, fetched);
723 std::string
info()
const {
return "K";}
756 print(
"set macrotaskinfo to");
794 "exchange: the orbital order must be as_is, shuffle or balanced");
802 const std::size_t n =
mo_ket.size();
803 std::vector<std::size_t> perm;
804 if (
mo_order_ ==
"as_is" or n < 2)
return perm;
806 std::iota(perm.begin(), perm.end(), 0);
809 std::mt19937 rng(42);
810 std::shuffle(perm.begin(), perm.end(), rng);
817 std::vector<double> cost(n, 0.0);
818 for (std::size_t j = 0; j < n; ++j) cost[j] =
double(
mo_ket[j].
get_impl()->get_coeffs().size());
819 world.gop.sum(cost.data(), cost.size());
821 std::vector<std::size_t> order(n);
822 std::iota(order.begin(), order.end(), 0);
823 std::stable_sort(order.begin(), order.end(),
824 [&cost](
const std::size_t
a,
const std::size_t
b) { return cost[a] > cost[b]; });
825 std::vector<std::vector<std::size_t>> bins(
split.size());
826 std::vector<double>
load(
split.size(), 0.0);
827 for (
const std::size_t i : order) {
828 std::size_t best =
split.size();
829 for (std::size_t
b = 0;
b <
split.size(); ++
b) {
830 if (
long(bins[
b].size()) >=
split[
b].size())
continue;
833 bins[best].push_back(i);
834 load[best] += cost[i];
837 for (
const auto&
b : bins) perm.insert(perm.end(),
b.begin(),
b.end());
840 auto spread = [](
const std::vector<double>&
v) {
841 double mx = 0.0,
sum = 0.0;
842 for (
const double x :
v) { mx = std::max(mx, x);
sum += x; }
843 return v.empty() or
sum == 0.0 ? 0.0 : mx / (
sum / double(
v.size()));
845 std::vector<double> given(
split.size(), 0.0);
846 for (std::size_t
b = 0;
b <
split.size(); ++
b)
847 for (
long i =
split[
b].begin; i <
split[
b].end; ++i) given[
b] += cost[i];
848 printf(
"exchange mo order balanced: %ld batches, per-batch node count max/mean %.2f (as given %.2f)\n",
849 long(
split.size()), spread(
load), spread(given));
891 vecfuncT K_macrotask_efficient(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
895 double mul_tol)
const;
898 vecfuncT K_macrotask_efficient_row(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
901 vecfuncT K_small_memory(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
904 vecfuncT K_large_memory(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
908 const vecfuncT& vket, std::shared_ptr<real_convolution_3d> poisson,
909 const bool symmetric,
const double mul_tol = 0.0,
910 const bool apply_screen =
false);
1005 std::shared_ptr<Future<batch_bytesT>>
fut;
1065 const std::size_t per_node = std::size_t(1) << (2 *
NDIM);
1066 return std::max<std::size_t>(1, (1u << 20) / (per_node *
k *
k *
k *
sizeof(T) + 1));
1112 if (slot->valid and slot->key == record) {
1113 vecfuncT data = cloud.template deserialize_batch_p2p<T, NDIM>(
1114 world, *slot->fut, record,
false);
1147 const std::string
policy)
const override {
1164 const std::vector<Batch_1D> batches =
1167 for (
long i = 0; i < long(batches.size()); ++i) {
1168 for (
long j = 0; j <= i; ++j) {
1179 for (
auto i = partition1.begin(); i != partition1.end(); ++i) {
1181 for (
auto j = i; j != partition1.end(); ++j) {
1182 Batch batch(i->first.input[0], j->first.input[0],
_);
1184 result.push_back(std::make_pair(
batch,priority));
1187 for (
auto j = partition2.begin(); j != partition2.end(); ++j) {
1188 Batch batch(i->first.input[0], j->first.input[0],
_);
1190 result.push_back(std::make_pair(
batch,priority));
1202 long nrow =
batch.input[0].size();
1203 long ncol =
batch.input[1].size();
1204 return double(nrow * ncol);
1212 const int accumulation_mode = 2,
const bool cost_aware =
true,
1213 const long batch_salt = 0,
const bool bra_shares_ket =
true,
1214 const bool vf_shares_ket =
true,
const bool apply_screen =
false)
1222 name=
"MacroTaskExchangeSimple";
1250 const long nsubworld) {
1257 std::map<long,long> column_index, row_index;
1258 for (
const auto& [task_batch, priority] : partition) {
1260 "owner-pinned exchange expects two-dimensional task batches");
1261 column_index[task_batch.input[0].begin] = 0;
1262 row_index[task_batch.input[1].begin] = 0;
1265 for (
auto& [begin, index] : column_index) index = next++;
1267 for (
auto& [begin, index] : row_index) index = next++;
1270 long(row_index.size()), nsubworld);
1271 for (
const auto& [task_batch, priority] : partition) {
1272 const long c = column_index[task_batch.input[0].begin];
1273 const long r = row_index[task_batch.input[1].begin];
1274 auto it = assignment.find({
c, r});
1275 owner_map_[{task_batch.input[0].begin, task_batch.input[1].begin}] =
1276 (it != assignment.end()) ? it->second : (
c % nsubworld);
1286 const std::vector<Batch_1D>
split =
1288 const long M = long(
split.size());
1289 std::map<long,long> begin_to_index;
1290 for (
long k = 0;
k < M; ++
k) begin_to_index[
split[
k].begin] =
k;
1298 const std::size_t ntask = std::size_t(M) * std::size_t(M + 1) / 2;
1302 const std::map<std::pair<long,long>,
long> assignment =
1306 for (
const auto& [task_batch, priority] : partition) {
1308 "owner-pinned exchange expects two-dimensional task batches");
1309 const long column_begin = task_batch.input[0].begin;
1310 const long row_begin = task_batch.input[1].begin;
1311 auto ic = begin_to_index.find(column_begin);
1312 auto jr = begin_to_index.find(row_begin);
1314 "owner-pinned exchange: a task batch is not one of the split batches");
1316 const long i = std::max(ic->second, jr->second);
1317 const long j = std::min(ic->second, jr->second);
1318 auto it = assignment.find({i, j});
1320 (it != assignment.end()) ? it->second : (i % nsubworld);
1332 "owner-pinned exchange expects two-dimensional task batches");
1334 return (it !=
owner_map_.end()) ? it->second : -1;
1339 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
1340 const std::vector<Function<T, NDIM>>&,
1348 std::size_t n = std::get<0>(argtuple).size();
1371 const long nsubworld) {
1378 "owner-pinned exchange needs one subworld per rank");
1379 const vecfuncT& vf = std::get<0>(argtuple);
1392 std::vector<std::pair<long, vecfuncT>> owned;
1395 auto stage = [&](
const vecfuncT&
v,
const int dim,
const std::vector<Batch_1D>&
split) {
1396 for (
long k = 0;
k < long(
split.size()); ++
k) {
1400 if (
k % nsubworld ==
world.rank()) {
1402 for (
long i = r.
begin; i < r.
end; ++i)
1403 local[i - r.
begin] =
copy(subworld,
v[i],
false);
1404 owned.emplace_back(record, std::move(local));
1412 "exchange: bra and ket are a paired set and must have equal length");
1417 const std::vector<Batch_1D>
split =
1434 for (
auto& [record,
batch] : owned)
1456 const bool has_next)
const {
1465 if (not has_next or
cloud_ptr ==
nullptr)
return;
1472 const std::vector<Batch_1D> candidates =
1473 symmetric ? std::vector<Batch_1D>{next_col, next_row}
1474 : std::vector<Batch_1D>{next_row};
1475 for (
const auto& r : candidates) {
1545 std::vector<long> idx;
1546 auto add_range = [&](
const Batch_1D&
b) {
1547 const long s =
b.is_full_size() ? 0 :
b.begin;
1548 const long e =
b.is_full_size() ? long(
nresult) :
b.end;
1549 for (
long i = s; i <
e and i < long(
nresult); ++i) idx.push_back(i);
1551 if (
batch.input.empty()) {
1552 for (
long i = 0; i < long(
nresult); ++i) idx.push_back(i);
1555 add_range(
batch.input[0]);
1557 add_range(
batch.input[1]);
1558 std::sort(idx.begin(), idx.end());
1559 idx.erase(std::unique(idx.begin(), idx.end()), idx.end());
1565 const long wid = long(subworld.
id());
1575 rs_sub.reserve(touched.size());
1576 kf_sub.reserve(touched.size());
1577 for (
long i : touched) { rs_sub.push_back(result_subworld[i]); kf_sub.push_back(
Kf_local_[i]); }
1578 if (rs_sub.empty())
return;
1582 gaxpy(1.0, kf_sub, 1.0, rs_sub,
false);
1593 const long wid = long(nodeworld.
id());
1597 for (
long i = 0; i <
nresult; ++i)
1609 if (not nodeworld)
return;
1624 if (universe_result.empty())
return;
1625 World& universe = universe_result.front().world();
1643 std::vector<Function<T, NDIM>>
1654 bool diagonal_block =
batch.input[0] ==
batch.input[1];
1655 auto& bra_range =
batch.input[1];
1656 auto& vf_range =
batch.input[0];
1658 if (vf_range.is_full_size()) vf_range.end = vf_batch.size();
1659 if (bra_range.is_full_size()) bra_range.end = bra_batch.size();
1664 const double tile_wall_start =
wall_time();
1670 prof_.subworld_id =
static_cast<unsigned long>(
world.id());
1674 prof_.diagonal = diagonal_block;
1675 prof_.row_begin = bra_range.begin;
prof_.row_end = bra_range.end;
1676 prof_.col_begin = vf_range.begin;
prof_.col_end = vf_range.end;
1677 prof_.wall_start = tile_wall_start;
1683 vecfuncT bra_owned, vf_owned, ket_row_owned, ket_column_owned;
1684 const vecfuncT* bra_work = &bra_batch;
1685 const vecfuncT* vf_work = &vf_batch;
1702 ket_column_owned = diagonal_block
1717 bra_work = &bra_owned;
1718 vf_work = &vf_owned;
1721 const double compute_wall_start =
wall_time();
1723 if (
profiling)
prof_.wait_for_data_wall = compute_wall_start - tile_wall_start;
1733 for (
int i = vf_range.begin; i < vf_range.end; ++i){
1734 Kf[i] += resultcolumn[i - vf_range.begin];}
1736 }
else if (
symmetric and not diagonal_block) {
1738 : vf_range.copy_batch(vket);
1741 *bra_work, *vf_work);
1743 for (
int i = bra_range.begin; i < bra_range.end; ++i){
1744 Kf[i] += resultcolumn[i - bra_range.begin];}
1745 for (
int i = vf_range.begin; i < vf_range.end; ++i){
1746 Kf[i] += resultrow[i - vf_range.begin];}
1749 *bra_work, *vf_work);
1750 for (
int i = vf_range.begin; i < vf_range.end; ++i)
1751 Kf[i] += resultcolumn[i - vf_range.begin];
1795 "symmetric diagonal tile: ket/bra batch size mismatch");
1797 "symmetric diagonal tile: vf/bra batch size mismatch");
1799 const long n = long(vf_batch.size());
1809 auto tick = [&](
double& acc,
const double t0) {
if (prof_on) acc +=
wall_time() - t0; };
1811 for (
long i = 0; i < n; ++i) {
1813 double w0 = prof_on ?
wall_time() : 0.0;
1814 const vecfuncT vf_subset(vf_batch.begin(), vf_batch.begin() + i + 1);
1816 tick(
prof_.mul1_wall, w0);
1819 tick(
prof_.truncate_wall, w0);
1827 std::vector<apply_screen_t<NDIM>> screens(i + 1);
1828 for (
long j = 0; j <= i; ++j) screens[j] = exchange_apply_screen<NDIM>({sup[i], sup[j]});
1829 psif =
apply(subworld, *poisson.get(), psif, screens);
1831 psif =
apply(subworld, *poisson.get(), psif);
1833 tick(
prof_.apply_wall, w0);
1836 tick(
prof_.truncate_wall, w0);
1844 double w1 = prof_on ?
wall_time() : 0.0;
1846 tick(
prof_.mul2_wall, w1);
1848 for (
long j = 0; j <= i; ++j)
update[j] += row_contrib[j];
1849 for (
long j = 0; j < i; ++j) {
1852 tick(
prof_.mul2_wall, w1);
1854 update[i] += mirrored[0];
1861 return resultcolumn;
1886 std::pair<vecfuncT, vecfuncT> compute_offdiagonal_batch_in_symmetric_matrix(
World& subworld,
1914 name=
"MacroTaskExchangeRow";
1918 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
1919 const std::vector<Function<T, NDIM>>&,
1927 std::size_t n = std::get<0>(argtuple).size();
1936 std::vector<Function<T, NDIM>>
1940 std::vector<Function<T,NDIM>> result;
1946 MADNESS_EXCEPTION(
"unknown algorithm in Exchange::MacroTaskExchangeRow::operator()",1);
1951 std::vector<Function<T,NDIM>>
1966 MADNESS_CHECK_THROW(vket.size()==1,
"out-of-bounds error in Exchange::MacroTaskExchangeRow::operator()");
1967 size_t min_tile = 10;
1968 size_t ntile = std::min(
mo_bra.size(), min_tile);
1970 for (
size_t ilo=0; ilo<
mo_bra.size(); ilo+=ntile){
1972 size_t iend = std::min(ilo+ntile,
mo_bra.size());
1980 tmp_psif =
apply(
world, *poisson.get(), tmp_psif);
1999 std::vector<Function<T,NDIM>>
2005 double total_execution_time=0.0;
2006 double total_fetch_time=0.0;
2007 double total_fetch_spawn_time=0.0;
2015 std::shared_ptr<World> fetching_world(
new World(comm.
Clone()));
2016 std::shared_ptr<World> executing_world(
new World(comm.
Clone()));
2018 print(
"time to create two worlds:",cpu1-cpu0,
"seconds");
2019 print(
"executing_world.id()",executing_world->id(),
"fetching_world.id()",fetching_world->id(),
"in MacroTaskExchangeRow");
2030 MADNESS_CHECK_THROW(vket.size()==1,
"out-of-bounds error in Exchange::MacroTaskExchangeRow::operator()");
2031 size_t min_tile = 10;
2032 size_t ntile = std::min(
mo_bra.size(), min_tile);
2042 auto fetch_data = [&](
World&
world,
const Tile& tile) {
2044 "bra and ket size mismatch in Exchange::MacroTaskExchangeRow::execute()");
2046 std::size_t sz=tile.iend-tile.ilo;
2049 for (
size_t i=tile.ilo; i<tile.iend; ++i) {
2051 subworld_bra[i-tile.ilo]=
f;
2054 return std::make_pair(subworld_bra,subworld_ket);
2060 "bra and ket size mismatch in Exchange::MacroTaskExchangeRow::execute()");
2062 auto world_id=
world.id();
2063 auto phi_id=phi.world().id();
2064 auto bra_id=
mo_bra.front().world().id();
2065 auto ket_id=
mo_ket.front().world().id();
2066 std::string msg=
"world mismatch in Exchange::MacroTaskExchangeRow::execute(): ";
2067 msg+=
"world.id()="+std::to_string(world_id)+
", ";
2068 msg+=
"phi.world().id()="+std::to_string(phi_id)+
", ";
2069 msg+=
"bra.world().id()="+std::to_string(bra_id)+
", ";
2070 msg+=
"ket.world().id()="+std::to_string(ket_id);
2071 if (not (world_id==phi_id && world_id==bra_id && world_id==ket_id)) {
2074 MADNESS_CHECK_THROW(world_id==phi_id && world_id==bra_id && world_id==ket_id,msg.c_str());
2083 tmp_psif =
apply(
world, *poisson.get(), tmp_psif);
2093 return tmp_Kf.truncate();
2097 std::vector<Tile> tiles;
2098 for (
size_t ilo=0; ilo<
mo_bra.size(); ilo+=ntile) {
2099 tiles.push_back(Tile{ilo,std::min(ilo+ntile,
mo_bra.size())});
2105 for (
size_t itile=0; itile<tiles.size(); ++itile) {
2106 Tile& tile = tiles[itile];
2110 print(
"fetching tile",tile.ilo,
"into world",executing_world->id());
2111 std::tie(tmp_mo_bra1,tmp_mo_ket1)=fetch_data(*executing_world,tiles[itile]);
2112 fetching_world->gop.set_forbid_fence(
false);
2114 executing_world->gop.fence();
2116 total_fetch_time += (t1 - t0);
2117 total_fetch_spawn_time += (t2 - t0);
2121 fetching_world->gop.set_forbid_fence(
true);
2122 if (itile<tiles.size()-1) {
2124 print(
"fetching tile",tiles[itile+1].ilo,
"into world",fetching_world->id(),
" at time ",
wall_time());
2125 std::tie(tmp_mo_bra2,tmp_mo_ket2)=fetch_data(*fetching_world,tiles[itile+1]);
2127 fetching_world->gop.set_forbid_fence(
false);
2132 total_fetch_time += (t1 - t0);
2133 total_fetch_spawn_time += (t2 - t0);
2135 print(
"executing tile",tile.ilo,
"in world",executing_world->id());
2137 Kf[0]+=execute(*executing_world,poisson1,phi1,tmp_mo_bra1,tmp_mo_ket1);
2139 print(
"time to execute tile",tile.ilo,
"in world",executing_world->id(),dpu1-dpu0,
"seconds");
2140 total_execution_time += dpu1-dpu0;
2142 fetching_world->gop.fence();
2145 std::swap(poisson1,poisson2);
2146 std::swap(phi1,phi2);
2147 std::swap(tmp_mo_bra2,tmp_mo_bra1);
2148 std::swap(tmp_mo_ket2,tmp_mo_ket1);
2149 std::swap(executing_world,fetching_world);
2154 fetching_world->gop.fence();
2155 executing_world->gop.fence();
2157 print(
"overall time: ",cpu2-cpu0,
"seconds");
2158 print(
"total execution time:",total_execution_time,
"seconds");
2159 print(
"total fetch time:",total_fetch_time,
"seconds");
2160 print(
"total fetch spawn time:",total_fetch_spawn_time,
"seconds");
Operators for the molecular HF and DFT code.
static const double small
Definition binaryop.cc:117
Definition test_ar.cc:118
Definition test_ar.cc:141
Definition test_ar.cc:170
Wrapper around MPI_Comm. Has a shallow copy constructor; use Create(Get_group()) for deep copy.
Definition safempi.h:645
Intracomm Clone() const
Definition safempi.h:844
Definition macrotaskpartitioner.h:55
long size() const
Definition macrotaskpartitioner.h:71
long end
first and first past last index [begin,end)
Definition macrotaskpartitioner.h:59
long begin
Definition macrotaskpartitioner.h:59
a batch consists of a 2D-input batch and a 1D-output batch: K-batch <- (I-batch, J-batch)
Definition macrotaskpartitioner.h:124
std::vector< Batch_1D > input
Definition macrotaskpartitioner.h:127
cloud class
Definition cloud.h:338
Future< batch_bytesT > request_batch_bytes_async(const keyT record) const
start fetching record from its owner; the trigger is in flight on return
Definition cloud.h:1048
ProcessID batch_owner(const keyT record) const
the owner of a batch record; a pmap lookup, no communication
Definition cloud.h:1008
void register_batch_owner(const keyT record, const ProcessID owner)
Register the owner of a batch record; local map insert, no communication.
Definition cloud.h:783
keyT store_batch(madness::World &world, const std::vector< Function< T, NDIM > > &batch, const ProcessID owner, const keyT record, const bool fence=true)
Store a batch of functions as one owner-pinned record.
Definition cloud.h:797
Bounded cache of fetched exchange batches, keyed by cloud record key.
Definition exchangeoperator.h:476
std::list< Slot > slots_
Definition exchangeoperator.h:524
void set_transient_capacity(const std::size_t c)
how many non-owned entries may be resident; at least one is always allowed
Definition exchangeoperator.h:479
std::size_t size() const
Definition exchangeoperator.h:514
std::size_t transient_capacity_
Definition exchangeoperator.h:525
void clear()
drop every entry; the capacity setting survives
Definition exchangeoperator.h:512
std::size_t transient_capacity() const
Definition exchangeoperator.h:480
bool contains(const keyT &key) const
Definition exchangeoperator.h:482
std::size_t n_transient() const
Definition exchangeoperator.h:516
const dataT & insert(const keyT &key, dataT &&data, const bool pinned)
Insert as most-recently-used. pinned marks a batch this rank owns.
Definition exchangeoperator.h:499
const dataT * find(const keyT &key)
Definition exchangeoperator.h:488
custom partitioning for the exchange operator in exchangeoperator.h
Definition exchangeoperator.h:1903
MacroTaskPartitionerRow()
Definition exchangeoperator.h:1905
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:1926
std::vector< Function< T, NDIM > > row_fetch_compute(const std::vector< Function< T, NDIM > > &vket, const std::vector< Function< T, NDIM > > &mo_bra, const std::vector< Function< T, NDIM > > &mo_ket)
Definition exchangeoperator.h:2000
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:1922
std::vector< Function< T, NDIM > > operator()(const std::vector< Function< T, NDIM > > &vket, const std::vector< Function< T, NDIM > > &mo_bra, const std::vector< Function< T, NDIM > > &mo_ket)
compute exchange row-wise for a fixed orbital phi_i of vket
Definition exchangeoperator.h:1937
double lo
Definition exchangeoperator.h:1897
bool symmetric
Definition exchangeoperator.h:1899
long nresult
Definition exchangeoperator.h:1896
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:1920
std::vector< Function< T, NDIM > > row(const std::vector< Function< T, NDIM > > &vket, const std::vector< Function< T, NDIM > > &mo_bra, const std::vector< Function< T, NDIM > > &mo_ket)
Definition exchangeoperator.h:1952
Algorithm algorithm_
Definition exchangeoperator.h:1900
MacroTaskExchangeRow(const long nresult, const double lo, const double mul_tol, const Algorithm algorithm)
Definition exchangeoperator.h:1911
double mul_tol
Definition exchangeoperator.h:1898
custom partitioning for the exchange operator in exchangeoperator.h
Definition exchangeoperator.h:1131
double compute_priority(const Batch &batch) const override
compute the priority of this task for non-dumb scheduling
Definition exchangeoperator.h:1200
MacroTaskPartitionerExchange(const bool symmetric, const bool owner_pinned=false, const long granularity_level=1)
Definition exchangeoperator.h:1133
bool symmetric
Definition exchangeoperator.h:1140
long granularity_level
Definition exchangeoperator.h:1144
bool owner_pinned
Definition exchangeoperator.h:1143
partitionT do_partitioning(const std::size_t &vsize1, const std::size_t &vsize2, const std::string policy) const override
override this if you want your own partitioning
Definition exchangeoperator.h:1146
void ensure_cache_world(World &world) const
drop cached batches when the subworld changes; they belong to the old one
Definition exchangeoperator.h:1088
static std::shared_ptr< FinalizeReducer< T, NDIM > > node_reducer_
Definition exchangeoperator.h:1049
vecfuncT compute_diagonal_batch_in_symmetric_matrix(World &subworld, const vecfuncT &ket_batch, const vecfuncT &bra_batch, const vecfuncT &vf_batch) const
compute a batch of the exchange matrix, with identical ranges, exploiting the matrix symmetry
Definition exchangeoperator.h:1789
static long exchange_call_index()
Definition exchangeoperator.h:1233
static void clear_local_caches()
Definition exchangeoperator.h:1055
void finalize_stage1(World &subworld, World *nodeworld)
reduce this subworld's accumulator into the node-shared one
Definition exchangeoperator.h:1608
static void commit_cost_reference()
make this call's measured costs the reference for the next one
Definition exchangeoperator.h:1237
static long batch_cache_hits()
Definition exchangeoperator.h:1227
void cleanup() override
Drop the cached batches while the subworld holding them is still alive.
Definition exchangeoperator.h:1496
static std::vector< double > cost_reference_
What each task cost last time, to place them better this time.
Definition exchangeoperator.h:1018
bool wants_node_local_reduction() const
Definition exchangeoperator.h:1528
static ExchangeBatchLRU< long, vecfuncT > batch_cache_
Batches fetched from the cloud, reused across the tasks that run in one subworld.
Definition exchangeoperator.h:984
static FinalizeReducer< T, NDIM > & get_universe_reducer(World &world)
Definition exchangeoperator.h:1071
void finalize_stage2(World &subworld, World *nodeworld, vecfuncT &universe_result)
drain into the universe result, from the node accumulator if there is one
Definition exchangeoperator.h:1621
static vecfuncT Kf_local_
Definition exchangeoperator.h:1033
static std::vector< double > & cost_this_call()
Definition exchangeoperator.h:1232
bool profile_active() const
per-task profiling on for this task?
Definition exchangeoperator.h:1231
long nresult
Definition exchangeoperator.h:943
static std::map< long, long > batch_begin_to_index_
batch offset -> index
Definition exchangeoperator.h:1020
double mul_tol
Definition exchangeoperator.h:945
static std::atomic< long > batch_cache_hits_
Definition exchangeoperator.h:986
bool apply_screen_
screen the Poisson apply by the multipliers' sizes in the destination box (applyscreen....
Definition exchangeoperator.h:976
vecfuncT compute_batch_in_asymmetric_matrix(World &subworld, const vecfuncT &ket_batch, const vecfuncT &bra_batch, const vecfuncT &vf_batch) const
compute a batch of the exchange matrix, with non-identical ranges
Definition exchangeoperator.h:1871
void ensure_node_accumulator(World &nodeworld) const
Collectively (re)build the node-shared accumulator in the node world.
Definition exchangeoperator.h:1592
int accumulation_mode_
Definition exchangeoperator.h:967
static FinalizeReducer< T, NDIM > & get_node_reducer(World &world)
Definition exchangeoperator.h:1079
void prepare_owner_assignment(const MacroTaskPartitioner::partitionT &partition, const long nsubworld)
Assign every task to the rank that will own one of its two batches.
Definition exchangeoperator.h:1249
static bool Kf_local_initialized_
Definition exchangeoperator.h:1034
static PrefetchSlot prefetch_next_
requested during this task
Definition exchangeoperator.h:1008
long universe_rank_
this process's rank in the universe, to recognise the batches it owns
Definition exchangeoperator.h:974
bool symmetric
Definition exchangeoperator.h:946
static long universe_reducer_world_id_
Definition exchangeoperator.h:1048
void sym_pipeline_advance(World &subworld, const vecfuncT &, const Batch_1D &next_col, const Batch_1D &next_row, const bool has_next) const
Request the batch the next task will have to fetch, before computing this one.
Definition exchangeoperator.h:1454
static bool finalize_stage1_done_
each drain happens once per rank, not once per task object
Definition exchangeoperator.h:1052
bool accumulates_own_output() const override
Definition exchangeoperator.h:1522
static long batch_cache_misses()
Definition exchangeoperator.h:1228
static long prof_task_seq_
per-process task counter, for identity only
Definition exchangeoperator.h:1026
MacroTaskExchangeSimple(const long nresult, const double lo, const double mul_tol, const bool symmetric, const bool owner_pinned=false, const long granularity_level=1, const long universe_rank=0, const int accumulation_mode=2, const bool cost_aware=true, const long batch_salt=0, const bool bra_shares_ket=true, const bool vf_shares_ket=true, const bool apply_screen=false)
Definition exchangeoperator.h:1209
void accumulate_locally(World &subworld, const vecfuncT &result_subworld) const
sum one tile's result into this subworld's accumulator
Definition exchangeoperator.h:1564
std::vector< long > touched_result_indices() const
The result entries this tile actually wrote.
Definition exchangeoperator.h:1544
ExchTaskProfile prof_
Definition exchangeoperator.h:1025
bool cost_aware_
place tasks by their measured cost rather than by counting them
Definition exchangeoperator.h:969
static long exchange_call_index_
Definition exchangeoperator.h:1021
std::vector< Function< T, NDIM > > operator()(const std::vector< Function< T, NDIM > > &vf_batch, const std::vector< Function< T, NDIM > > &bra_batch, const std::vector< Function< T, NDIM > > &vket)
Definition exchangeoperator.h:1644
bool owner_pinned
pin each task to a rank that owns one of its batches, over the owner-pinned split
Definition exchangeoperator.h:948
long granularity_level
Definition exchangeoperator.h:949
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:1341
bool handles_own_data_movement() const override
the owner-pinned path fetches its operand batches from the cloud itself
Definition exchangeoperator.h:1489
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:1347
bool bra_shares_ket_
Definition exchangeoperator.h:962
static void reset_batch_cache_counters()
Definition exchangeoperator.h:1238
static long Kf_node_world_id_
Definition exchangeoperator.h:1038
std::pair< vecfuncT, vecfuncT > compute_offdiagonal_batch_in_symmetric_matrix(World &subworld, const vecfuncT &ket_rows, const vecfuncT &ket_columns, const vecfuncT &bra_batch, const vecfuncT &vf_batch) const
compute a batch of the exchange matrix, with non-identical ranges
Definition exchangeoperator.cc:416
static long Kf_local_world_id_
Definition exchangeoperator.h:1035
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:1343
static long batch_prefetch_hits()
Definition exchangeoperator.h:1229
double lo
Definition exchangeoperator.h:944
static std::atomic< long > batch_cache_misses_
Definition exchangeoperator.h:987
int bra_role() const
the record role carrying the bra, and the one carrying the vf over a shared split
Definition exchangeoperator.h:1325
static bool finalize_universe_done_
Definition exchangeoperator.h:1053
static std::vector< double > cost_this_call_
rank-local, summed after the call
Definition exchangeoperator.h:1019
std::map< std::pair< long, long >, long > owner_map_
Definition exchangeoperator.h:972
static bool Kf_node_initialized_
Definition exchangeoperator.h:1037
const vecfuncT & fetch_batch(World &world, Cloud &cloud, const long record) const
Fetch one owner-pinned batch, from the local cache if it is resident.
Definition exchangeoperator.h:1102
static long node_reducer_world_id_
Definition exchangeoperator.h:1050
static long cost_matrix_dimension()
number of batches this application split into, which squares to the cost matrix
Definition exchangeoperator.h:1235
void store_batches(World &world, World &subworld, Cloud &cloud, const argtupleT &argtuple, const long nsubworld)
Store the orbitals as owner-pinned batches, one record per batch.
Definition exchangeoperator.h:1370
bool vf_shares_ket_
Definition exchangeoperator.h:963
static PrefetchSlot prefetch_current_
promoted from the previous task
Definition exchangeoperator.h:1007
static std::atomic< long > batch_prefetch_hits_
Definition exchangeoperator.h:988
static long cache_world_id_
Definition exchangeoperator.h:985
static vecfuncT Kf_node_
Definition exchangeoperator.h:1036
static std::size_t finalize_chunk_entries()
entries per chunk in coalesced_gaxpy, sized so one message stays modest at any k
Definition exchangeoperator.h:1063
int vf_role() const
Definition exchangeoperator.h:1326
static std::shared_ptr< FinalizeReducer< T, NDIM > > universe_reducer_
Receiving endpoints for the two drains, one per world they transfer within.
Definition exchangeoperator.h:1047
long batch_salt_
Per-application salt for the batch record keys, from exchange_batch_salt.
Definition exchangeoperator.h:957
long owner_hint(const Batch &task_batch, const long nsubworld) const override
Definition exchangeoperator.h:1329
Definition exchangeoperator.h:634
long batch_granularity_
Definition exchangeoperator.h:927
static std::atomic< long > mul1_timer
timing
Definition exchangeoperator.h:640
Exchange< T, NDIM >::ExchangeAlgorithm Algorithm
Definition exchangeoperator.h:698
bool printtimings() const
Definition exchangeoperator.h:914
MacroTaskInfo macro_task_info
Definition exchangeoperator.h:700
ExchangeImpl & symmetric(const bool flag)
Definition exchangeoperator.h:743
bool cost_aware_assign_
Definition exchangeoperator.h:933
ExchangeImpl & set_printlevel(const long &level)
Definition exchangeoperator.h:767
nlohmann::json statistics
statistics of the Cloud (timings, memory) and of the parameters of this run
Definition exchangeoperator.h:939
static double elapsed_time
Definition exchangeoperator.h:641
void print_timer(World &world) const
Definition exchangeoperator.h:679
ExchangeImpl & set_batch_granularity(const long level)
Definition exchangeoperator.h:772
World & get_world() const
Definition exchangeoperator.h:869
ExchangeImpl & set_macro_task_info(const std::vector< std::string > &info)
Definition exchangeoperator.h:753
double thresh
Definition exchangeoperator.h:922
nlohmann::json get_statistics() const
Definition exchangeoperator.h:871
bool apply_screen_
skip the Poisson apply where the multiplier is negligible in the destination box (applyscreen....
Definition exchangeoperator.h:937
ExchangeImpl & set_macro_task_info(const MacroTaskInfo &info)
Definition exchangeoperator.h:748
long printlevel
Definition exchangeoperator.h:923
static void reset_timer()
Definition exchangeoperator.h:643
vecfuncT mo_bra
is the exchange matrix symmetric? K phi_i = \sum_k \phi_k \int \phi_k \phi_i
Definition exchangeoperator.h:920
double lo
Definition exchangeoperator.h:921
World & world
Definition exchangeoperator.h:917
std::shared_ptr< MacroTaskQ > taskq
Definition exchangeoperator.h:918
Algorithm algorithm_
Definition exchangeoperator.h:699
ExchangeImpl & set_mo_order(const std::string &order)
Definition exchangeoperator.h:792
int accumulation_mode_
Definition exchangeoperator.h:930
Function< T, NDIM > functionT
Definition exchangeoperator.h:635
nlohmann::json gather_statistics() const
return some statistics about the current settings
Definition exchangeoperator.h:874
std::string mo_order_
the order of the orbitals in the batches: as_is, shuffle or balanced (set_mo_order)
Definition exchangeoperator.h:935
bool is_symmetric() const
Definition exchangeoperator.h:736
ExchangeImpl & set_taskq(std::shared_ptr< MacroTaskQ > taskq1)
Definition exchangeoperator.h:738
std::vector< functionT > vecfuncT
Definition exchangeoperator.h:636
double mul_tol
Definition exchangeoperator.h:924
ExchangeImpl & set_cost_aware_assignment(const bool flag)
Definition exchangeoperator.h:778
ExchangeImpl & set_algorithm(const Algorithm &alg)
Definition exchangeoperator.h:762
bool printprogress() const
Definition exchangeoperator.h:913
static std::atomic< long > apply_timer
Definition exchangeoperator.h:638
ExchangeImpl & set_accumulation_mode(const int mode)
Definition exchangeoperator.h:861
ExchangeImpl & set_apply_screen(const bool flag)
Definition exchangeoperator.h:856
vecfuncT mo_ket
MOs for bra and ket.
Definition exchangeoperator.h:920
static vecfuncT compute_K_tile(World &world, const vecfuncT &mo_bra, const vecfuncT &mo_ket, const vecfuncT &vket, std::shared_ptr< real_convolution_3d > poisson, const bool symmetric, const double mul_tol=0.0, const bool apply_screen=false)
computing the upper triangle of the double sum (over vket and the K orbitals)
Definition exchangeoperator.cc:320
ExchangeImpl(World &world, const double lo, const double thresh)
default ctor
Definition exchangeoperator.h:703
std::string info() const
Definition exchangeoperator.h:723
nlohmann::json gather_timings(World &world) const
Definition exchangeoperator.h:652
std::vector< std::size_t > mo_order_permutation() const
Definition exchangeoperator.h:801
bool printtimings_detail() const
Definition exchangeoperator.h:915
bool symmetric_
Definition exchangeoperator.h:919
bool printdebug() const
Definition exchangeoperator.h:912
std::shared_ptr< MacroTaskQ > get_taskq() const
Definition exchangeoperator.h:867
static auto set_poisson(World &world, const double lo, const double econv=FunctionDefaults< 3 >::get_thresh())
Definition exchangeoperator.h:725
static std::atomic< long > mul2_timer
Definition exchangeoperator.h:639
void set_bra_and_ket(const vecfuncT &bra, const vecfuncT &ket)
set the bra and ket orbital spaces, and the occupation
Definition exchangeoperator.h:718
Exchange(World &world, const double lo, const double thresh=FunctionDefaults< NDIM >::get_thresh())
ctor
Definition exchangeoperator.cc:494
static std::string to_string(const ExchangeAlgorithm alg)
Definition SCFOperators.h:145
ExchangeAlgorithm
Definition SCFOperators.h:118
@ fetch_compute
Definition SCFOperators.h:119
@ multiworld_efficient_row
Definition SCFOperators.h:119
Function< T, NDIM > operator()(const Function< T, NDIM > &ket) const
Definition SCFOperators.h:223
std::vector< functionT > vecfuncT
Definition SCFOperators.h:112
Receiving end of the exchange finalize, living in the world the transfer rides on.
Definition exchangeoperator.h:549
T beta_
Definition exchangeoperator.h:580
FinalizeReducer(World &world)
Definition exchangeoperator.h:554
std::vector< implT * > dests_
Definition exchangeoperator.h:579
FinalizeNodeRec< T, NDIM > recT
Definition exchangeoperator.h:552
FunctionImpl< T, NDIM > implT
Definition exchangeoperator.h:551
void accumulate_chunk(const std::vector< recT > &recs)
Definition exchangeoperator.h:565
void set_targets(std::vector< implT * > dests, T beta)
point at the destinations for the next drain; local, called by every rank
Definition exchangeoperator.h:560
static int get_k()
Returns the default wavelet order.
Definition funcdefaults.h:170
static std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > make_default_pmap(World &world)
Makes a default process map for the given world.
Definition mraimpl.h:3840
static const double & get_thresh()
Returns the default threshold.
Definition funcdefaults.h:183
static void set_default_pmap(World &world)
Definition mraimpl.h:3847
static std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > & get_pmap()
Returns the default process map that was last initialized via set_default_pmap().
Definition funcdefaults.h:415
static void set_pmap(const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &value)
Sets the default process map (does not redistribute existing functions).
Definition funcdefaults.h:446
FunctionFactory implements the named-parameter idiom for Function.
Definition function_factory.h:86
FunctionImpl holds all Function state to facilitate shallow copy semantics.
Definition funcimpl.h:1045
FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree.
Definition funcimpl.h:138
A multiresolution adaptive numerical function.
Definition mra.h:141
T & get(bool dowork=true) &
Gets the value, waiting if necessary.
Definition future.h:571
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:71
Cloud * cloud_ptr
Definition macrotaskq.h:1639
MacroTaskOperationBase()
Definition macrotaskq.h:1642
Batch batch
Definition macrotaskq.h:1635
World * subworld_ptr
Definition macrotaskq.h:1636
std::shared_ptr< MacroTaskPartitioner > partitioner
Definition macrotaskq.h:1641
std::string name
Definition macrotaskq.h:1640
std::string policy
how to partition the batches
Definition macrotaskpartitioner.h:190
std::size_t nsubworld
number of worlds (try to have enough batches for all worlds)
Definition macrotaskpartitioner.h:189
std::list< std::pair< Batch, double > > partitionT
Definition macrotaskpartitioner.h:186
friend class Batch
Definition macrotaskpartitioner.h:183
MacroTaskPartitioner()
Definition macrotaskpartitioner.h:193
partitionT do_1d_partition(const std::size_t vsize, const std::string policy) const
Definition macrotaskpartitioner.h:251
std::size_t max_batch_size
maximum batch size (for memory management)
Definition macrotaskpartitioner.h:188
The Nemo class.
Definition nemo.h:377
Range, vaguely a la Intel TBB, to encapsulate a random-access, STL-like start and end iterator with c...
Definition range.h:64
Definition applyscreen.h:83
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:177
WorldObject(const WorldObject &other)
Definition world_object.h:500
void process_pending()
Definition world_object.h:787
detail::task_result_type< memfnT >::futureT task(ProcessID dest, memfnT memfn, const TaskAttributes &attr=TaskAttributes()) const
Sends task to derived class method returnT (this->*memfn)().
Definition world_object.h:1132
Future< bool > for_each(const rangeT &range, const opT &op)
Apply op(item) on all items in range.
Definition world_task_queue.h:572
A parallel world class.
Definition world.h:134
WorldTaskQueue & taskq
Task queue.
Definition world.h:215
unsigned long id() const
Definition world.h:324
WorldGopInterface & gop
Global operations.
Definition world.h:216
Declares the Cloud class for storing data and transfering them between worlds.
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
std::vector< Spinor > truncate(std::vector< Spinor > arg)
Definition dirac-hatom.cc:503
Fcwf apply(World &world, real_convolution_3d &op, const Fcwf &psi)
Definition fcwf.cc:317
Fcwf mul_sparse(const Fcwf &psi, const madness::real_function_3d &phi, double tol)
Definition fcwf.cc:566
Fcwf copy(Fcwf psi)
Definition fcwf.cc:374
const double beta
Definition gygi_soltion.cc:62
static const double v
Definition hatom_sf_dirac.cc:20
Declares the macrotaskq and MacroTaskBase classes.
General header file for using MADNESS.
#define MADNESS_CHECK(condition)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:182
#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
Function< double, 3 > functionT
Definition mcpfit.cc:50
vector< functionT > vecfuncT
Definition mcpfit.cc:51
void print(const tensorT &t)
Definition mcpfit.cc:140
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
std::vector< std::pair< Batch_1D, Batch_1D > > exchange_row_owner_grid(const std::size_t ncolumn, const std::size_t nrow, const long nsubworld)
The asymmetric task grid: every (column, row) pair over two independent splits.
Definition exchangeoperator.h:81
long exchange_sym_owner_nbatch(const std::size_t n, const long nsubworld, const long granularity_level)
Number of batches M for the owner-pinned symmetric exchange algorithm.
Definition exchangeoperator.h:33
double get_rss_usage_in_GB()
Definition ranks_and_hosts.cpp:18
static double process_cpu_time()
Returns the CPU time consumed by this process, in seconds.
Definition timers.h:152
static double cpu_time()
Returns the cpu time in seconds relative to an arbitrary origin.
Definition timers.h:128
void coalesced_gaxpy(World &transport_world, FinalizeReducer< T, NDIM > &reducer, std::vector< Function< T, NDIM > > &dest_vec, std::vector< Function< T, NDIM > > &src_vec, const T beta, const std::size_t chunk_entries)
Accumulate src_vec into dest_vec in bulk, one message per destination rank per chunk.
Definition exchangeoperator.h:595
std::vector< Batch_1D > exchange_sym_owner_split(const std::size_t n, const long nsubworld, const long granularity_level)
Split a vector of length n into M = exchange_sym_owner_nbatch(...) contiguous batches.
Definition exchangeoperator.h:47
std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > FunctionDefaults< NDIM >::pmap
Definition mraimpl.h:3898
Function< TENSOR_RESULT_TYPE(T, R), NDIM > dot_sparse(World &world, const std::vector< Function< T, NDIM > > &a, const std::vector< Function< R, NDIM > > &b, double tol, bool fence=true, bool do_make_redundant=true)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i].
Definition vmra.h:1884
std::map< std::pair< long, long >, long > exchange_sym_cost_aware_assign(const long n, const long M, const std::vector< double > &cost)
Cost-aware owner assignment for the symmetric algorithm's triangular task matrix.
Definition exchangeoperator.h:275
long exchange_batch_record_key(const long salt, const int dim, const Batch_1D &r)
Deterministic cloud record key for one stored batch: (salt, dimension, range).
Definition exchangeoperator.h:457
int FunctionDefaults< NDIM >::k
Definition mraimpl.h:3878
Function< TENSOR_RESULT_TYPE(T, R), NDIM > dot(World &world, const std::vector< Function< T, NDIM > > &a, const std::vector< Function< R, NDIM > > &b, bool fence=true, bool do_make_redundant=true)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i]; see dot_sparse for screening.
Definition vmra.h:1897
std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > get_impl(const std::vector< Function< T, NDIM > > &v)
Definition vmra.h:781
TreeState
Definition funcdefaults.h:60
@ reconstructed
s coeffs at the leaves only
Definition funcdefaults.h:61
@ compressed
d coeffs in internal nodes, s and d coeffs at the root, empty leaves may be present
Definition funcdefaults.h:62
long exchange_sym_tri(const long i, const long j)
Triangular index of a batch pair, collapsing (i,j) and (j,i): a*(a+1)/2 + b.
Definition exchangeoperator.h:157
exchange_sup_bounds_t< NDIM > exchange_sup_bounds(World &world, const std::vector< Function< T, NDIM > > &multipliers)
Definition exchangeoperator.h:114
void compress(World &world, const std::vector< Function< T, NDIM > > &v, bool fence=true)
Compress a vector of functions.
Definition vmra.h:150
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
void hash_combine(hashT &seed, const T &v)
Combine hash values.
Definition worldhash.h:261
std::map< std::pair< long, long >, long > exchange_row_owner_assign(const long ncolumn, const long nrow, const long nworker)
Owner of every task in the asymmetric grid: all tasks of a column go to one worker.
Definition exchangeoperator.h:145
Function< T, NDIM > sum(World &world, const std::vector< Function< T, NDIM > > &f, bool fence=true)
Returns new function — q = sum_i f[i].
Definition vmra.h:1774
static const Slice _(0,-1, 1)
bool exch_task_profile_enabled()
Is per-task exchange profiling on? Read once per process.
Definition exchangeoperator.h:356
long exchange_batch_salt(const std::vector< Function< T, NDIM > > &ket)
Per-invocation salt for the exchange batch record keys, from the ket identities.
Definition exchangeoperator.h:448
void exch_write_task_profile(const ExchTaskProfile &p)
Append one record to exch_taskprof.r<rank>.jsonl.
Definition exchangeoperator.h:377
std::vector< Batch_1D > exchange_row_owner_split(const std::size_t n, const long nsubworld)
Batch boundaries for the asymmetric row/column split: one batch per rank.
Definition exchangeoperator.h:69
@ TT_FULL
Definition gentensor.h:120
NDIM & f
Definition mra.h:2824
const Function< T, NDIM > & change_tree_state(const Function< T, NDIM > &f, const TreeState finalstate, bool fence=true)
change tree state of a function
Definition mra.h:3150
double wall_time()
Returns the wall time in seconds relative to an arbitrary origin.
Definition timers.cc:48
void print(const T &t, Ts &&... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:232
static SeparatedConvolution< double, 3 > * CoulombOperatorPtr(World &world, double lo, double eps, const std::array< LatticeRange, 3 > &lattice_ranges=FunctionDefaults< 3 >::get_bc().lattice_range(), int k=FunctionDefaults< 3 >::get_k())
Factory function generating separated kernel for convolution with 1/r in 3D.
Definition operator.h:2039
std::vector< Function< T, NDIM > > zero_functions_compressed(World &world, int n, bool fence=true)
Generates a vector of zero functions (compressed).
Definition vmra.h:450
bool exch_force_general_path()
Send a symmetric application down the general (bra, ket, vf) path? Read once per process.
Definition exchangeoperator.h:367
std::vector< std::shared_ptr< const SupBoundMap< NDIM > > > exchange_sup_bounds_t
the sup bounds (supbound.h) of a tile's multipliers, one per function
Definition exchangeoperator.h:108
void exch_write_cost_matrix(const long call_index, const long k, const long M, const std::vector< double > &cost)
Write the measured per-task cost matrix of one application, for offline inspection.
Definition exchangeoperator.h:417
void load(Function< T, NDIM > &f, const std::string name)
Definition mra.h:3188
const FunctionCommonData< T, NDIM > * FunctionCommonData< T, NDIM >::data[MAXK]
Definition mraimpl.h:3875
bool exchange_same_operands(const std::vector< Function< T, NDIM > > &a, const std::vector< Function< T, NDIM > > &b)
Are these the same functions, so that one stored record can serve both operand roles?
Definition exchangeoperator.h:98
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
madness::hashT hash_value(const std::array< T, N > &a)
Hash std::array with madness hash.
Definition array_addons.h:78
std::map< std::pair< long, long >, long > exchange_sym_round_robin_assign(const long n, const long M)
Round-robin owner assignment for the symmetric algorithm's triangular task matrix.
Definition exchangeoperator.h:181
apply_screen_t< NDIM > exchange_apply_screen(exchange_sup_bounds_t< NDIM > sup)
Definition exchangeoperator.h:133
ExchangeBatchDim
which of the three exchange operand vectors a stored batch belongs to
Definition exchangeoperator.h:434
@ EXCHANGE_BATCH_BRA
Definition exchangeoperator.h:434
@ EXCHANGE_BATCH_VF
Definition exchangeoperator.h:434
@ EXCHANGE_BATCH_KET
Definition exchangeoperator.h:434
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition mra.h:2239
void gaxpy(const double a, ScalarResult< T > &left, const double b, const T &right, const bool fence=true)
the result type of a macrotask must implement gaxpy
Definition macrotaskq.h:243
@ nemo
nemo's regularized orbitals F = psi/R
Definition Restart.h:60
static const double b
Definition nonlinschro.cc:119
static const double a
Definition nonlinschro.cc:118
double C(double a)
Additional factor when transforming gaussian exponent a from fourier to real space.
Definition relops.cc:25
static const double c
Definition relops.cc:10
static std::atomic< long > sup_build_ns
Definition applyscreen.h:62
static std::atomic< long > sup_maps
Definition applyscreen.h:62
One task's record for the exchange profiler.
Definition exchangeoperator.h:326
double mul1_wall
Definition exchangeoperator.h:342
double compute_cpu
Definition exchangeoperator.h:337
double mul2_wall
Definition exchangeoperator.h:342
double thresh
which protocol tier this task ran in
Definition exchangeoperator.h:331
long k
Definition exchangeoperator.h:332
long task_id
Definition exchangeoperator.h:327
double truncate_wall
Definition exchangeoperator.h:342
long col_end
Definition exchangeoperator.h:334
double apply_wall
Definition exchangeoperator.h:342
unsigned long subworld_id
Definition exchangeoperator.h:329
long row_begin
Definition exchangeoperator.h:334
bool waited
a cold fetch happened, so this task paid latency
Definition exchangeoperator.h:344
double wait_for_data_wall
task entry until its operands are in hand
Definition exchangeoperator.h:336
long col_begin
Definition exchangeoperator.h:334
void reset()
Definition exchangeoperator.h:347
double wall_end
Definition exchangeoperator.h:335
long universe_rank
keys the output file: one per process
Definition exchangeoperator.h:328
int subworld_nrank
Definition exchangeoperator.h:330
double compute_wall
Definition exchangeoperator.h:337
bool diagonal
Definition exchangeoperator.h:333
long row_end
Definition exchangeoperator.h:334
double wall_start
Definition exchangeoperator.h:335
int operand_source
worst of its fetches: 0 resident, 1 ahead, 2 cold
Definition exchangeoperator.h:343
void observe_fetch_tier(const int tier)
keep the worst source, since that is the one that set the task's wait
Definition exchangeoperator.h:349
double peak_rss_gb
Definition exchangeoperator.h:345
Definition exchangeoperator.h:523
dataT data
Definition exchangeoperator.h:523
bool pinned
Definition exchangeoperator.h:523
keyT key
Definition exchangeoperator.h:523
One batch requested ahead of the task that will read it.
Definition exchangeoperator.h:998
std::shared_ptr< Future< batch_bytesT > > fut
Definition exchangeoperator.h:1005
long key
Definition exchangeoperator.h:1000
bool valid
Definition exchangeoperator.h:999
One coefficient node in transit during the exchange finalize.
Definition exchangeoperator.h:534
std::size_t f
index into the destination function vector
Definition exchangeoperator.h:535
Key< NDIM > key
tree-node key
Definition exchangeoperator.h:536
void serialize(Archive &ar)
Definition exchangeoperator.h:539
FunctionNode< T, NDIM > node
the source node
Definition exchangeoperator.h:537
Definition macrotaskq.h:279
static MacroTaskInfo preset(const std::string name)
Definition macrotaskq.h:312
World & world
Memoized reference to the world to which this object belongs.
Definition world_object.h:348
class to temporarily redirect output to cout
Definition print.h:305
void split(const Range< ConcurrentHashMap< int, int >::iterator > &range)
Definition test_hashthreaded.cc:63
double cpu_time()
Definition test_list.cc:43
void e()
Definition test_sig.cc:75
std::complex< double > dataT
Definition testcomplexfunctionsolver.cc:10
vector_complex_function_3d update(World &world, const vector_complex_function_3d &psi, vector_complex_function_3d &vpsi, const tensor_real &e, int iter)
Definition testcosine.cc:210
constexpr std::size_t NDIM
Definition testgconv.cc:54
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43
Key< D > keyT
Definition writecoeff2.cc:11