MADNESS 0.10.1
exchangeoperator.h
Go to the documentation of this file.
1#ifndef SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
2#define SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
3
4#include<madness.h>
8
9namespace madness {
10
11// forward declaration
12class SCF;
13class Nemo;
14
15
16template<typename T, std::size_t NDIM>
19 typedef std::vector<functionT> vecfuncT;
20
21 static inline std::atomic<long> apply_timer;
22 static inline std::atomic<long> mul2_timer;
23 static inline std::atomic<long> mul1_timer; ///< timing
24 static inline double elapsed_time;
25
26 static void reset_timer() {
27 mul1_timer = 0l;
28 mul2_timer = 0l;
29 apply_timer = 0l;
30 elapsed_time = 0.0;
31 }
32
33public:
34 nlohmann::json gather_timings(World& world) const {
35 double t1 = double(mul1_timer) * 0.001;
36 double t2 = double(apply_timer) * 0.001;
37 double t3 = double(mul2_timer) * 0.001;
38 world.gop.sum(t1);
39 world.gop.sum(t2);
40 world.gop.sum(t3);
41 nlohmann::json j;
42 j["multiply1"] = t1;
43 j["apply"] = t2;
44 j["multiply2"] = t3;
45 j["total"] = elapsed_time;
46 return j;
47 }
48
49 void print_timer(World& world) const {
50 auto timings= gather_timings(world);
51 if (world.rank() == 0) {
52 printf(" cpu time spent in multiply1 %8.2fs\n", timings["multiply1"].template get<double>());
53 printf(" cpu time spent in apply %8.2fs\n", timings["apply"].template get<double>());
54 printf(" cpu time spent in multiply2 %8.2fs\n", timings["multiply2"].template get<double>());
55 printf(" total wall time %8.2fs\n", timings["total"].template get<double>());
56 }
57 }
58
59
63
64 /// default ctor
65 ExchangeImpl(World& world, const double lo, const double thresh) : world(world), lo(lo), thresh(thresh) {}
66
67 /// ctor with a conventional calculation
68 ExchangeImpl(World& world, const SCF *calc, const int ispin) ;
69
70 /// ctor with a nemo calculation
71 ExchangeImpl(World& world, const Nemo *nemo, const int ispin);
72
73 /// set the bra and ket orbital spaces, and the occupation
74
75 /// @param[in] bra bra space, must be provided as complex conjugate
76 /// @param[in] ket ket space
77 void set_bra_and_ket(const vecfuncT& bra, const vecfuncT& ket) {
78 mo_bra = copy(world, bra);
79 mo_ket = copy(world, ket);
80 }
81
82 std::string info() const {return "K";}
83
84 static auto set_poisson(World& world, const double lo, const double econv = FunctionDefaults<3>::get_thresh()) {
85 return std::shared_ptr<real_convolution_3d>(CoulombOperatorPtr(world, lo, econv));
86 }
87
88 /// apply the exchange operator on a vector of functions
89
90 /// note that only one spin is used (either alpha or beta orbitals)
91 /// @param[in] vket the orbitals |i> that the operator is applied on
92 /// @return a vector of orbitals K| i>
93 vecfuncT operator()(const vecfuncT& vket) const;
94
95 bool is_symmetric() const { return symmetric_; }
96
97 ExchangeImpl& set_taskq(std::shared_ptr<MacroTaskQ> taskq1) {
98 this->taskq=taskq1;
99 return *this;
100 }
101
102 ExchangeImpl& symmetric(const bool flag) {
103 symmetric_ = flag;
104 return *this;
105 }
106
109 return *this;
110 }
111
113 algorithm_ = alg;
114 return *this;
115 }
116
117 ExchangeImpl& set_printlevel(const long& level) {
118 printlevel=level;
119 return *this;
120 }
121
122 std::shared_ptr<MacroTaskQ> get_taskq() const {return taskq;}
123
124 World& get_world() const {return world;}
125
126 nlohmann::json get_statistics() const {return statistics;}
127
128 /// return some statistics about the current settings
129 nlohmann::json gather_statistics() const {
130 nlohmann::json j;
131 j["symmetric"] = symmetric_;
132 j["lo"] = lo;
133 j["thresh"] = thresh;
134 j["mul_tol"] = mul_tol;
135 j["printlevel"] = printlevel;
136 j["algorithm"] = to_string(algorithm_);
137 j["macro_task_info"] = macro_task_info.to_json();
138 auto timings = gather_timings(world);
139 j.update(timings);
140 return j;
141 }
142
143private:
144
145 /// exchange using macrotasks, i.e. apply K on a function in individual worlds
146 vecfuncT K_macrotask_efficient(const vecfuncT& vket, const double mul_tol = 0.0) const;
147
148 /// exchange using macrotasks, i.e. apply K on a function in individual worlds row-wise
149 vecfuncT K_macrotask_efficient_row(const vecfuncT& vket, const double mul_tol = 0.0) const;
150
151 /// computing the full square of the double sum (over vket and the K orbitals)
152 vecfuncT K_small_memory(const vecfuncT& vket, const double mul_tol = 0.0) const;
153
154 /// computing the upper triangle of the double sum (over vket and the K orbitals)
155 vecfuncT K_large_memory(const vecfuncT& vket, const double mul_tol = 0.0) const;
156
157 /// computing the upper triangle of the double sum (over vket and the K orbitals)
158 static vecfuncT compute_K_tile(World& world, const vecfuncT& mo_bra, const vecfuncT& mo_ket,
159 const vecfuncT& vket, std::shared_ptr<real_convolution_3d> poisson,
160 const bool symmetric, const double mul_tol = 0.0);
161
162 inline bool printdebug() const {return printlevel >= 10; }
163 inline bool printprogress() const {return (printlevel>=4) and (not (printdebug()));}
164 inline bool printtimings() const {return printlevel>=3;}
165 inline bool printtimings_detail() const {return printlevel>=4;}
166
168 std::shared_ptr<MacroTaskQ> taskq;
169 bool symmetric_ = false; /// is the exchange matrix symmetric? K phi_i = \sum_k \phi_k \int \phi_k \phi_i
170 vecfuncT mo_bra, mo_ket; ///< MOs for bra and ket
171 double lo = 1.e-4;
173 long printlevel = 0;
175
176 mutable nlohmann::json statistics; ///< statistics of the Cloud (timings, memory) and of the parameters of this run
177
179
181 double lo = 1.e-4;
182 double mul_tol = 1.e-7;
183 bool symmetric = false;
184
185 /// custom partitioning for the exchange operator in exchangeoperator.h
186
187 /// arguments are: result[i] += sum_k vket[k] \int 1/r vbra[k] f[i]
188 /// with f and vbra being batched, result and vket being passed on as a whole
190 public:
191 MacroTaskPartitionerExchange(const bool symmetric) : symmetric(symmetric) {
192 max_batch_size=30;
193 }
194
195 bool symmetric = false;
196
197 partitionT do_partitioning(const std::size_t& vsize1, const std::size_t& vsize2,
198 const std::string policy) const override {
199
200 partitionT partition1 = do_1d_partition(vsize1, policy);
201 partitionT partition2 = do_1d_partition(vsize2, policy);
202 partitionT result;
203 for (auto i = partition1.begin(); i != partition1.end(); ++i) {
204 if (symmetric) {
205 for (auto j = i; j != partition1.end(); ++j) {
206 Batch batch(i->first.input[0], j->first.input[0], _);
207 double priority=compute_priority(batch);
208 result.push_back(std::make_pair(batch,priority));
209 }
210 } else {
211 for (auto j = partition2.begin(); j != partition2.end(); ++j) {
212 Batch batch(i->first.input[0], j->first.input[0], _);
213 double priority=compute_priority(batch);
214 result.push_back(std::make_pair(batch,priority));
215 }
216 }
217 }
218 return result;
219 }
220
221 /// compute the priority of this task for non-dumb scheduling
222
223 /// \return the priority as double number (no limits)
224 double compute_priority(const Batch& batch) const override {
225 MADNESS_CHECK(batch.input.size() == 2); // must be quadratic batches
226 long nrow = batch.input[0].size();
227 long ncol = batch.input[1].size();
228 return double(nrow * ncol);
229 }
230 };
231
232 public:
233 MacroTaskExchangeSimple(const long nresult, const double lo, const double mul_tol, const bool symmetric)
234 : nresult(nresult), lo(lo), mul_tol(mul_tol), symmetric(symmetric) {
235 partitioner.reset(new MacroTaskPartitionerExchange(symmetric));
236 }
237
238
239 // you need to define the exact argument(s) of operator() as tuple
240 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
241 const std::vector<Function<T, NDIM>>&,
242 const std::vector<Function<T, NDIM>>&> argtupleT;
243
244 using resultT = std::vector<Function<T, NDIM>>;
245
246 // you need to define an empty constructor for the result
247 // resultT must implement operator+=(const resultT&)
248 resultT allocator(World& world, const argtupleT& argtuple) const {
249 std::size_t n = std::get<0>(argtuple).size();
250 resultT result = zero_functions_compressed<T, NDIM>(world, n);
251 return result;
252 }
253
254 std::vector<Function<T, NDIM>>
255 operator()(const std::vector<Function<T, NDIM>>& vf_batch, // will be batched (column)
256 const std::vector<Function<T, NDIM>>& bra_batch, // will be batched (row)
257 const std::vector<Function<T, NDIM>>& vket) { // will not be batched
258
259 World& world = vf_batch.front().world();
260 resultT Kf = zero_functions_compressed<T, NDIM>(world, nresult);
261
262 bool diagonal_block = batch.input[0] == batch.input[1];
263 auto& bra_range = batch.input[1]; // corresponds to vbra
264 auto& vf_range = batch.input[0]; // corresponds to vf_batch
265
266 if (vf_range.is_full_size()) vf_range.end = vf_batch.size();
267 if (bra_range.is_full_size()) bra_range.end = bra_batch.size();
268
269 MADNESS_CHECK(vf_range.end <= nresult);
270 if (symmetric) MADNESS_CHECK(bra_range.end <= nresult);
271
272 if (symmetric and diagonal_block) {
273 auto ket_batch = bra_range.copy_batch(vket);
274 vecfuncT resultcolumn = compute_diagonal_batch_in_symmetric_matrix(world, ket_batch, bra_batch,
275 vf_batch);
276
277 for (int i = vf_range.begin; i < vf_range.end; ++i){
278 Kf[i] += resultcolumn[i - vf_range.begin];}
279
280 } else if (symmetric and not diagonal_block) {
281 auto[resultcolumn, resultrow]=compute_offdiagonal_batch_in_symmetric_matrix(world, vket, bra_batch,
282 vf_batch);
283
284 for (int i = bra_range.begin; i < bra_range.end; ++i){
285 Kf[i] += resultcolumn[i - bra_range.begin];}
286 for (int i = vf_range.begin; i < vf_range.end; ++i){
287 Kf[i] += resultrow[i - vf_range.begin];}
288 } else {
289 auto ket_batch = bra_range.copy_batch(vket);
290 vecfuncT resultcolumn = compute_batch_in_asymmetric_matrix(world, ket_batch, bra_batch, vf_batch);
291 for (int i = vf_range.begin; i < vf_range.end; ++i)
292 Kf[i] += resultcolumn[i - vf_range.begin];
293 }
294 return Kf;
295 }
296
297 /// compute a batch of the exchange matrix, with identical ranges, exploiting the matrix symmetry
298
299 /// \param subworld the world we're computing in
300 /// \param cloud where to store the results
301 /// \param bra_batch the bra batch of orbitals (including the nuclear correlation factor square)
302 /// \param ket_batch the ket batch of orbitals, i.e. the orbitals to premultiply with
303 /// \param vf_batch the argument of the exchange operator
305 const vecfuncT& ket_batch, // is batched
306 const vecfuncT& bra_batch, // is batched
307 const vecfuncT& vf_batch // is batched
308 ) const {
309 double mul_tol = 0.0;
310 double symmetric = true;
311 auto poisson = Exchange<double, 3>::ExchangeImpl::set_poisson(subworld, lo);
312 return Exchange<T, NDIM>::ExchangeImpl::compute_K_tile(subworld, bra_batch, ket_batch, vf_batch, poisson, symmetric,
313 mul_tol);
314 }
315
316 /// compute a batch of the exchange matrix, with non-identical ranges
317
318 /// \param subworld the world we're computing in
319 /// \param cloud where to store the results
320 /// \param bra_batch the bra batch of orbitals (including the nuclear correlation factor square)
321 /// \param ket_batch the ket batch of orbitals, i.e. the orbitals to premultiply with
322 /// \param vf_batch the argument of the exchange operator
324 const vecfuncT& ket_batch,
325 const vecfuncT& bra_batch,
326 const vecfuncT& vf_batch) const {
327 double mul_tol = 0.0;
328 double symmetric = false;
329 auto poisson = Exchange<double, 3>::ExchangeImpl::set_poisson(subworld, lo);
330 return Exchange<T, NDIM>::ExchangeImpl::compute_K_tile(subworld, bra_batch, ket_batch, vf_batch, poisson, symmetric,
331 mul_tol);
332 }
333
334 /// compute a batch of the exchange matrix, with non-identical ranges
335
336 /// \param subworld the world we're computing in
337 /// \param cloud where to store the results
338 /// \param bra_batch the bra batch of orbitals (including the nuclear correlation factor square)
339 /// \param ket_batch the ket batch of orbitals, i.e. the orbitals to premultiply with
340 /// \param vf_batch the argument of the exchange operator
341 std::pair<vecfuncT, vecfuncT> compute_offdiagonal_batch_in_symmetric_matrix(World& subworld,
342 const vecfuncT& ket, // not batched
343 const vecfuncT& bra_batch, // batched
344 const vecfuncT& vf_batch) const; // batched
345
346 };
347
349
351 double lo = 1.e-4;
352 double mul_tol = 1.e-7;
353 bool symmetric = false;
355
356 /// custom partitioning for the exchange operator in exchangeoperator.h
358 public:
360 max_batch_size=1;
361 }
362 };
363
364 public:
365 MacroTaskExchangeRow(const long nresult, const double lo, const double mul_tol, const Algorithm algorithm)
366 : nresult(nresult), lo(lo), mul_tol(mul_tol), algorithm_(algorithm) {
367 partitioner.reset(new MacroTaskPartitionerRow());
368 name="MacroTaskExchangeRow";
369 }
370
371 // you need to define the exact argument(s) of operator() as tuple
372 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
373 const std::vector<Function<T, NDIM>>&,
374 const std::vector<Function<T, NDIM>>&> argtupleT;
375
376 using resultT = std::vector<Function<T, NDIM>>;
377
378 // you need to define an empty constructor for the result
379 // resultT must implement operator+=(const resultT&)
380 resultT allocator(World& world, const argtupleT& argtuple) const {
381 std::size_t n = std::get<0>(argtuple).size();
382 resultT result = zero_functions_compressed<T, NDIM>(world, n);
383 return result;
384 }
385
386 /// compute exchange row-wise for a fixed orbital phi_i of vket
387
388 /// create 2 worlds: one fetches the function coefficients from the universe, the other
389 /// does the computation, then swap. The result is copied back to the universe
390 std::vector<Function<T, NDIM>>
391 operator()(const std::vector<Function<T, NDIM>>& vket,
392 const std::vector<Function<T, NDIM>>& mo_bra,
393 const std::vector<Function<T, NDIM>>& mo_ket) {
394 std::vector<Function<T,NDIM>> result;
395 if (algorithm_==fetch_compute) {
396 result=row_fetch_compute(vket,mo_bra,mo_ket);
397 } else if (algorithm_==multiworld_efficient_row) {
398 result=row(vket,mo_bra,mo_ket);
399 } else {
400 MADNESS_EXCEPTION("unknown algorithm in Exchange::MacroTaskExchangeRow::operator()",1);
401 }
402 return result;
403 }
404
405 std::vector<Function<T,NDIM>>
406 row(const std::vector<Function<T, NDIM>>& vket,
407 const std::vector<Function<T, NDIM>>& mo_bra,
408 const std::vector<Function<T, NDIM>>& mo_ket) {
409
410 double cpu0, cpu1;
411 World& world = vket.front().world();
412 mul_tol = 0.0;
413
414 resultT Kf = zero_functions_compressed<T, NDIM>(world, 1);
415 vecfuncT psif = zero_functions_compressed<T,NDIM>(world, mo_bra.size());
417
418 // !! NO !! vket is batched, starts at batch.input[0].begin
419 // auto& i = batch.input[0].begin;
420 long i=0;
421 MADNESS_CHECK_THROW(vket.size()==1,"out-of-bounds error in Exchange::MacroTaskExchangeRow::operator()");
422 size_t min_tile = 10;
423 size_t ntile = std::min(mo_bra.size(), min_tile);
424
425 for (size_t ilo=0; ilo<mo_bra.size(); ilo+=ntile){
426 cpu0 = cpu_time();
427 size_t iend = std::min(ilo+ntile,mo_bra.size());
428 vecfuncT tmp_mo_bra(mo_bra.begin()+ilo,mo_bra.begin()+iend);
429 auto tmp_psif = mul_sparse(world, vket[i], tmp_mo_bra, mul_tol);
430 truncate(world, tmp_psif);
431 cpu1 = cpu_time();
432 mul1_timer += long((cpu1 - cpu0) * 1000l);
433
434 cpu0 = cpu_time();
435 tmp_psif = apply(world, *poisson.get(), tmp_psif);
436 truncate(world, tmp_psif);
437 cpu1 = cpu_time();
438 apply_timer += long((cpu1 - cpu0) * 1000l);
439
440 cpu0 = cpu_time();
441 vecfuncT tmp_mo_ket(mo_ket.begin()+ilo,mo_ket.begin()+iend);
442 auto tmp_Kf = dot(world, tmp_mo_ket, tmp_psif);
443 cpu1 = cpu_time();
444 mul2_timer += long((cpu1 - cpu0) * 1000l);
445
446 Kf[0] += tmp_Kf;
447 truncate(world, Kf);
448 }
449
450 return Kf;
451 }
452
453 std::vector<Function<T,NDIM>>
454 row_fetch_compute(const std::vector<Function<T, NDIM>>& vket,
455 const std::vector<Function<T, NDIM>>& mo_bra,
456 const std::vector<Function<T, NDIM>>& mo_ket) {
457
459 double total_execution_time=0.0;
460 double total_fetch_time=0.0;
461 double total_fetch_spawn_time=0.0;
462
463 resultT Kf = zero_functions_compressed<T, NDIM>(*subworld_ptr, 1);
464 {
465 // create the two worlds that will be used for fetching and computing
466 // std::shared_ptr<World> executing_world(subworld_ptr);
467 double cpu0=cpu_time();
468 SafeMPI::Intracomm comm = subworld_ptr->mpi.comm();
469 std::shared_ptr<World> fetching_world(new World(comm.Clone()));
470 std::shared_ptr<World> executing_world(new World(comm.Clone()));
471 double cpu1=cpu_time();
472 print("time to create two worlds:",cpu1-cpu0,"seconds");
473 print("executing_world.id()",executing_world->id(),"fetching_world.id()",fetching_world->id(),"in MacroTaskExchangeRow");
474
475 {
476 auto poisson1 = Exchange<double, 3>::ExchangeImpl::set_poisson(*executing_world, lo);
477 auto poisson2 = Exchange<double, 3>::ExchangeImpl::set_poisson(*fetching_world, lo);
478
479 functionT phi1=copy(*executing_world,vket[0]);
480 functionT phi2=copy(*fetching_world,vket[0]);
481
482 // !! NO !! vket is batched, starts at batch.input[0].begin
483 // auto& i = batch.input[0].begin;
484 MADNESS_CHECK_THROW(vket.size()==1,"out-of-bounds error in Exchange::MacroTaskExchangeRow::operator()");
485 size_t min_tile = 10;
486 size_t ntile = std::min(mo_bra.size(), min_tile);
487
488 struct Tile {
489 size_t ilo;
490 size_t iend;
491 };
492
493
494 // copy the data from the universe bra and ket to subworld bra and ket
495 // returns a pair of vectors in the subworld which are still awaiting the function coefficients
496 auto fetch_data = [&](World& world, const Tile& tile) {
497 MADNESS_CHECK_THROW(mo_bra.size()==mo_ket.size(),
498 "bra and ket size mismatch in Exchange::MacroTaskExchangeRow::execute()");
499
500 std::size_t sz=tile.iend-tile.ilo;
501 vecfuncT subworld_bra(sz);
502 vecfuncT subworld_ket;
503 for (int i=tile.ilo; i<tile.iend; ++i) {
504 auto f=copy(world,mo_bra[i],false);
505 subworld_bra[i-tile.ilo]=f;
506 subworld_ket.push_back(copy(world, mo_ket[i],false));
507 }
508 return std::make_pair(subworld_bra,subworld_ket);
509 };
510
511 // apply the exchange operator on phi for a a tile of mo_bra and mo_ket
512 auto execute = [&](World& world, auto poisson, const functionT& phi, const vecfuncT& mo_bra, const vecfuncT& mo_ket) {
513 MADNESS_CHECK_THROW(mo_bra.size()==mo_ket.size(),
514 "bra and ket size mismatch in Exchange::MacroTaskExchangeRow::execute()");
515
516 auto world_id=world.id();
517 auto phi_id=phi.world().id();
518 auto bra_id=mo_bra.front().world().id();
519 auto ket_id=mo_ket.front().world().id();
520 std::string msg="world mismatch in Exchange::MacroTaskExchangeRow::execute(): ";
521 msg+="world.id()="+std::to_string(world_id)+", ";
522 msg+="phi.world().id()="+std::to_string(phi_id)+", ";
523 msg+="bra.world().id()="+std::to_string(bra_id)+", ";
524 msg+="ket.world().id()="+std::to_string(ket_id);
525 if (not (world_id==phi_id && world_id==bra_id && world_id==ket_id)) {
526 print(msg);
527 }
528 MADNESS_CHECK_THROW(world_id==phi_id && world_id==bra_id && world_id==ket_id,msg.c_str());
529
530 double cpu0 = cpu_time();
531 auto tmp_psif = mul_sparse(world, phi, mo_bra, mul_tol);
532 truncate(world, tmp_psif);
533 double cpu1 = cpu_time();
534 mul1_timer += long((cpu1 - cpu0) * 1000l);
535
536 cpu0 = cpu_time();
537 tmp_psif = apply(world, *poisson.get(), tmp_psif);
538 truncate(world, tmp_psif);
539 cpu1 = cpu_time();
540 apply_timer += long((cpu1 - cpu0) * 1000l);
541
542 cpu0 = cpu_time();
543 auto tmp_Kf = dot(world, mo_ket, tmp_psif);
544 cpu1 = cpu_time();
545 mul2_timer += long((cpu1 - cpu0) * 1000l);
546
547 return tmp_Kf.truncate();
548
549 };
550
551 std::vector<Tile> tiles;
552 for (size_t ilo=0; ilo<mo_bra.size(); ilo+=ntile) {
553 tiles.push_back(Tile{ilo,std::min(ilo+ntile,mo_bra.size())});
554 }
555
556 vecfuncT tmp_mo_bra1,tmp_mo_ket1;
557 vecfuncT tmp_mo_bra2,tmp_mo_ket2;
558
559 for (int itile=0; itile<tiles.size(); ++itile) {
560 Tile& tile = tiles[itile];
561
562 if (itile==0) {
563 double t0=cpu_time();
564 print("fetching tile",tile.ilo,"into world",executing_world->id());
565 std::tie(tmp_mo_bra1,tmp_mo_ket1)=fetch_data(*executing_world,tiles[itile]);
566 fetching_world->gop.set_forbid_fence(false);
567 double t2=cpu_time();
568 executing_world->gop.fence();
569 double t1=cpu_time();
570 total_fetch_time += (t1 - t0);
571 total_fetch_spawn_time += (t2 - t0);
572 }
573
574 if (itile>=0) {
575 double t0=cpu_time();
576 fetching_world->gop.set_forbid_fence(true);
577 if (itile<tiles.size()-1) {
578 // fetch data into fetching_world while computing in executing_world
579 print("fetching tile",tiles[itile+1].ilo,"into world",fetching_world->id()," at time ",wall_time());
580 std::tie(tmp_mo_bra2,tmp_mo_ket2)=fetch_data(*fetching_world,tiles[itile+1]);
581 }
582 fetching_world->gop.set_forbid_fence(false);
583 double t2=cpu_time();
584 // uncomment the next line to enforce that fetching is finished before executing
585 // fetching_world->gop.fence();
586 double t1=cpu_time();
587 total_fetch_time += (t1 - t0);
588 total_fetch_spawn_time += (t2 - t0);
589
590 print("executing tile",tile.ilo,"in world",executing_world->id());
591 double dpu0=cpu_time();
592 Kf[0]+=execute(*executing_world,poisson1,phi1,tmp_mo_bra1,tmp_mo_ket1);
593 double dpu1=cpu_time();
594 print("time to execute tile",tile.ilo,"in world",executing_world->id(),dpu1-dpu0,"seconds");
595 total_execution_time += dpu1-dpu0;
596
597 fetching_world->gop.fence();
598
599 // change roles of the two worlds
600 std::swap(poisson1,poisson2);
601 std::swap(phi1,phi2);
602 std::swap(tmp_mo_bra2,tmp_mo_bra1);
603 std::swap(tmp_mo_ket2,tmp_mo_ket1);
604 std::swap(executing_world,fetching_world);
605 }
606 }
607 } // objects living in the two worlds must be destroyed before the worlds are freed
608
609 // deferred destruction of WorldObjects happens here
610 fetching_world->gop.fence();
611 executing_world->gop.fence();
612 double cpu2=cpu_time();
613 print("overall time: ",cpu2-cpu0,"seconds");
614 print("total execution time:",total_execution_time,"seconds");
615 print("total fetch time:",total_fetch_time,"seconds");
616 print("total fetch spawn time:",total_fetch_spawn_time,"seconds");
617 } // worlds are destroyed here
618
619 return Kf;
620 }
621 };
622};
623
624} /* namespace madness */
625
626#endif /* SRC_APPS_CHEM_EXCHANGEOPERATOR_H_ */
Operators for the molecular HF and DFT code.
Wrapper around MPI_Comm. Has a shallow copy constructor; use Create(Get_group()) for deep copy.
Definition safempi.h:490
Intracomm Clone() const
Definition safempi.h:689
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
custom partitioning for the exchange operator in exchangeoperator.h
Definition exchangeoperator.h:357
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:380
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:454
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:391
long nresult
Definition exchangeoperator.h:350
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:374
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:406
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:376
Algorithm algorithm_
Definition exchangeoperator.h:354
MacroTaskExchangeRow(const long nresult, const double lo, const double mul_tol, const Algorithm algorithm)
Definition exchangeoperator.h:365
custom partitioning for the exchange operator in exchangeoperator.h
Definition exchangeoperator.h:189
double compute_priority(const Batch &batch) const override
compute the priority of this task for non-dumb scheduling
Definition exchangeoperator.h:224
MacroTaskPartitionerExchange(const bool symmetric)
Definition exchangeoperator.h:191
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:197
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:304
MacroTaskExchangeSimple(const long nresult, const double lo, const double mul_tol, const bool symmetric)
Definition exchangeoperator.h:233
long nresult
Definition exchangeoperator.h:180
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:244
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:323
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:255
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:242
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:248
Definition exchangeoperator.h:17
static std::atomic< long > mul1_timer
timing
Definition exchangeoperator.h:23
Exchange< T, NDIM >::ExchangeAlgorithm Algorithm
Definition exchangeoperator.h:60
bool printtimings() const
Definition exchangeoperator.h:164
ExchangeImpl & symmetric(const bool flag)
Definition exchangeoperator.h:102
ExchangeImpl & set_printlevel(const long &level)
Definition exchangeoperator.h:117
nlohmann::json statistics
statistics of the Cloud (timings, memory) and of the parameters of this run
Definition exchangeoperator.h:176
static double elapsed_time
Definition exchangeoperator.h:24
void print_timer(World &world) const
Definition exchangeoperator.h:49
World & get_world() const
Definition exchangeoperator.h:124
nlohmann::json get_statistics() const
Definition exchangeoperator.h:126
ExchangeImpl & set_macro_task_info(const MacroTaskInfo &info)
Definition exchangeoperator.h:107
static void reset_timer()
Definition exchangeoperator.h:26
vecfuncT mo_bra
is the exchange matrix symmetric? K phi_i = \sum_k \phi_k \int \phi_k \phi_i
Definition exchangeoperator.h:170
World & world
Definition exchangeoperator.h:167
std::shared_ptr< MacroTaskQ > taskq
Definition exchangeoperator.h:168
Function< T, NDIM > functionT
Definition exchangeoperator.h:18
nlohmann::json gather_statistics() const
return some statistics about the current settings
Definition exchangeoperator.h:129
bool is_symmetric() const
Definition exchangeoperator.h:95
ExchangeImpl & set_taskq(std::shared_ptr< MacroTaskQ > taskq1)
Definition exchangeoperator.h:97
std::vector< functionT > vecfuncT
Definition exchangeoperator.h:19
ExchangeImpl & set_algorithm(const Algorithm &alg)
Definition exchangeoperator.h:112
bool printprogress() const
Definition exchangeoperator.h:163
static std::atomic< long > apply_timer
Definition exchangeoperator.h:21
ExchangeImpl(World &world, const double lo, const double thresh)
default ctor
Definition exchangeoperator.h:65
std::string info() const
Definition exchangeoperator.h:82
nlohmann::json gather_timings(World &world) const
Definition exchangeoperator.h:34
bool printtimings_detail() const
Definition exchangeoperator.h:165
bool printdebug() const
Definition exchangeoperator.h:162
std::shared_ptr< MacroTaskQ > get_taskq() const
Definition exchangeoperator.h:122
static auto set_poisson(World &world, const double lo, const double econv=FunctionDefaults< 3 >::get_thresh())
Definition exchangeoperator.h:84
static std::atomic< long > mul2_timer
Definition exchangeoperator.h:22
void set_bra_and_ket(const vecfuncT &bra, const vecfuncT &ket)
set the bra and ket orbital spaces, and the occupation
Definition exchangeoperator.h:77
Definition SCFOperators.h:105
MacroTaskInfo macro_task_info
Definition SCFOperators.h:165
static std::string to_string(const ExchangeAlgorithm alg)
Definition SCFOperators.h:144
ExchangeAlgorithm
Definition SCFOperators.h:117
@ multiworld_efficient_row
Definition SCFOperators.h:118
Function< T, NDIM > operator()(const Function< T, NDIM > &ket) const
Definition SCFOperators.h:195
std::vector< functionT > vecfuncT
Definition SCFOperators.h:111
std::string info() const
print some information about this operator
Definition SCFOperators.h:175
FunctionDefaults holds default paramaters as static class members.
Definition funcdefaults.h:100
static const double & get_thresh()
Returns the default threshold.
Definition funcdefaults.h:177
A multiresolution adaptive numerical function.
Definition mra.h:139
Definition macrotaskq.h:1240
partition one (two) vectors into 1D (2D) batches.
Definition macrotaskpartitioner.h:182
std::list< std::pair< Batch, double > > partitionT
Definition macrotaskpartitioner.h:186
The Nemo class.
Definition nemo.h:326
nlohmann::json statistics
Definition SCFOperators.h:64
std::shared_ptr< MacroTaskQ > taskq
Definition SCFOperators.h:71
Definition SCF.h:191
void sum(T *buf, size_t nelem)
Inplace global sum while still processing AM & tasks.
Definition worldgop.h:872
A parallel world class.
Definition world.h:132
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:320
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:330
unsigned long id() const
Definition world.h:315
WorldGopInterface & gop
Global operations.
Definition world.h:207
Declares the Cloud class for storing data and transfering them between worlds.
double(* f)(const coord_3d &)
Definition derivatives.cc:54
static double lo
Definition dirac-hatom.cc:23
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:281
Fcwf copy(Fcwf psi)
Definition fcwf.cc:338
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:28
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
void print(const tensorT &t)
Definition mcpfit.cc:140
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
static SeparatedConvolution< double, 3 > * CoulombOperatorPtr(World &world, double lo, double eps, const array_of_bools< 3 > &lattice_sum=FunctionDefaults< 3 >::get_bc().is_periodic(), int k=FunctionDefaults< 3 >::get_k())
Factory function generating separated kernel for convolution with 1/r in 3D.
Definition operator.h:1818
Function< TENSOR_RESULT_TYPE(L, R), NDIM > mul_sparse(const Function< L, NDIM > &left, const Function< R, NDIM > &right, double tol, bool fence=true)
Sparse multiplication — left and right must be reconstructed and if tol!=0 have tree of norms already...
Definition mra.h:1836
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)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i].
Definition vmra.h:1565
double wall_time()
Returns the wall time in seconds relative to an arbitrary origin.
Definition timers.cc:48
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
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:2096
static const double thresh
Definition rk.cc:45
Definition macrotaskq.h:280
static MacroTaskInfo preset(const std::string name)
Definition macrotaskq.h:313
nlohmann::json to_json() const
Definition macrotaskq.h:400
class to temporarily redirect output to cout
Definition print.h:277
double cpu_time()
Definition test_list.cc:43
constexpr std::size_t NDIM
Definition testgconv.cc:54