1#ifndef SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
2#define SRC_APPS_CHEM_EXCHANGEOPERATOR_H_
16template<
typename T, std::
size_t NDIM>
32 double t1 = double(mul1_timer) * 0.001;
33 double t2 = double(apply_timer) * 0.001;
34 double t3 = double(mul2_timer) * 0.001;
38 if (world.
rank() == 0) {
39 printf(
" cpu time spent in multiply1 %8.2fs\n", t1);
40 printf(
" cpu time spent in apply %8.2fs\n", t2);
41 printf(
" cpu time spent in multiply2 %8.2fs\n", t3);
64 mo_bra =
copy(world, bra);
65 mo_ket =
copy(world, ket);
68 std::string
info()
const {
return "K";}
106 vecfuncT K_macrotask_efficient(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
109 vecfuncT K_macrotask_efficient_row(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
112 vecfuncT K_small_memory(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
115 vecfuncT K_large_memory(
const vecfuncT& vket,
const double mul_tol = 0.0)
const;
119 const vecfuncT& vket, std::shared_ptr<real_convolution_3d> poisson,
120 const bool symmetric,
const double mul_tol = 0.0);
128 bool symmetric_ =
false;
139 double mul_tol = 1.e-7;
140 bool symmetric =
false;
152 bool symmetric =
false;
155 const std::string policy)
const override {
157 partitionT partition1 = do_1d_partition(vsize1, policy);
158 partitionT partition2 = do_1d_partition(vsize2, policy);
160 for (
auto i = partition1.begin(); i != partition1.end(); ++i) {
162 for (
auto j = i; j != partition1.end(); ++j) {
163 Batch batch(i->first.input[0], j->first.input[0], _);
164 double priority=compute_priority(batch);
165 result.push_back(std::make_pair(batch,priority));
168 for (
auto j = partition2.begin(); j != partition2.end(); ++j) {
169 Batch batch(i->first.input[0], j->first.input[0], _);
170 double priority=compute_priority(batch);
171 result.push_back(std::make_pair(batch,priority));
183 long nrow = batch.
input[0].size();
184 long ncol = batch.
input[1].size();
185 return double(nrow * ncol);
191 : nresult(nresult),
lo(
lo), mul_tol(mul_tol), symmetric(symmetric) {
197 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
198 const std::vector<Function<T, NDIM>>&,
201 using resultT = std::vector<Function<T, NDIM>>;
206 std::size_t n = std::get<0>(argtuple).size();
207 resultT result = zero_functions_compressed<T, NDIM>(world, n);
211 std::vector<Function<T, NDIM>>
216 World& world = vf_batch.front().world();
217 resultT Kf = zero_functions_compressed<T, NDIM>(world, nresult);
219 bool diagonal_block = batch.input[0] == batch.input[1];
220 auto& bra_range = batch.input[1];
221 auto& vf_range = batch.input[0];
223 if (vf_range.is_full_size()) vf_range.end = vf_batch.size();
224 if (bra_range.is_full_size()) bra_range.end = bra_batch.size();
229 if (symmetric and diagonal_block) {
230 auto ket_batch = bra_range.copy_batch(vket);
231 vecfuncT resultcolumn = compute_diagonal_batch_in_symmetric_matrix(world, ket_batch, bra_batch,
234 for (
int i = vf_range.begin; i < vf_range.end; ++i){
235 Kf[i] += resultcolumn[i - vf_range.begin];}
237 }
else if (symmetric and not diagonal_block) {
238 auto[resultcolumn, resultrow]=compute_offdiagonal_batch_in_symmetric_matrix(world, vket, bra_batch,
241 for (
int i = bra_range.begin; i < bra_range.end; ++i){
242 Kf[i] += resultcolumn[i - bra_range.begin];}
243 for (
int i = vf_range.begin; i < vf_range.end; ++i){
244 Kf[i] += resultrow[i - vf_range.begin];}
246 auto ket_batch = bra_range.copy_batch(vket);
247 vecfuncT resultcolumn = compute_batch_in_asymmetric_matrix(world, ket_batch, bra_batch, vf_batch);
248 for (
int i = vf_range.begin; i < vf_range.end; ++i)
249 Kf[i] += resultcolumn[i - vf_range.begin];
266 double mul_tol = 0.0;
267 double symmetric =
true;
284 double mul_tol = 0.0;
285 double symmetric =
false;
298 std::pair<vecfuncT, vecfuncT> compute_offdiagonal_batch_in_symmetric_matrix(
World& subworld,
309 double mul_tol = 1.e-7;
310 bool symmetric =
false;
322 : nresult(nresult),
lo(
lo), mul_tol(mul_tol) {
327 typedef std::tuple<const std::vector<Function<T, NDIM>>&,
328 const std::vector<Function<T, NDIM>>&,
331 using resultT = std::vector<Function<T, NDIM>>;
336 std::size_t n = std::get<0>(argtuple).size();
337 resultT result = zero_functions_compressed<T, NDIM>(world, n);
342 std::vector<Function<T, NDIM>>
347 World& world = vket.front().world();
350 resultT Kf = zero_functions_compressed<T, NDIM>(world, 1);
351 vecfuncT psif = zero_functions_compressed<T,NDIM>(world, mo_bra.
size());
354 auto& i = batch.input[0].begin;
355 size_t min_tile = 10;
356 size_t ntile = std::min(mo_bra.size(), min_tile);
358 for (
size_t ilo=0; ilo<mo_bra.size(); ilo+=ntile){
359 size_t iend = std::min(ilo+ntile,mo_bra.size());
361 vecfuncT tmp_mo_bra(mo_bra.begin()+ilo,mo_bra.begin()+iend);
362 auto tmp_psif =
mul_sparse(world, vket[i], tmp_mo_bra, mul_tol);
365 tmp_psif =
apply(world, *poisson.get(), tmp_psif);
368 vecfuncT tmp_mo_ket(mo_ket.begin()+ilo,mo_ket.begin()+iend);
369 auto tmp_Kf =
dot(world, tmp_mo_ket, tmp_psif);
Operators for the molecular HF and DFT code.
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:313
MacroTaskPartitionerRow()
Definition exchangeoperator.h:315
Definition exchangeoperator.h:305
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:335
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:343
MacroTaskExchangeRow(const long nresult, const double lo, const double mul_tol)
Definition exchangeoperator.h:321
long nresult
Definition exchangeoperator.h:307
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:329
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:331
custom partitioning for the exchange operator in exchangeoperator.h
Definition exchangeoperator.h:146
double compute_priority(const Batch &batch) const override
compute the priority of this task for non-dumb scheduling
Definition exchangeoperator.h:181
MacroTaskPartitionerExchange(const bool symmetric)
Definition exchangeoperator.h:148
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:154
Definition exchangeoperator.h:135
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:261
MacroTaskExchangeSimple(const long nresult, const double lo, const double mul_tol, const bool symmetric)
Definition exchangeoperator.h:190
long nresult
Definition exchangeoperator.h:137
std::vector< Function< T, NDIM > > resultT
Definition exchangeoperator.h:201
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:280
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:212
std::tuple< const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > &, const std::vector< Function< T, NDIM > > & > argtupleT
Definition exchangeoperator.h:199
resultT allocator(World &world, const argtupleT &argtuple) const
Definition exchangeoperator.h:205
Definition exchangeoperator.h:17
static std::atomic< long > mul1_timer
timing
Definition exchangeoperator.h:23
ExchangeImpl & symmetric(const bool flag)
Definition exchangeoperator.h:88
static void print_timer(World &world)
Definition exchangeoperator.h:31
ExchangeImpl & set_printlevel(const long &level)
Definition exchangeoperator.h:98
Exchange< T, NDIM >::Algorithm Algorithm
Definition exchangeoperator.h:47
static void reset_timer()
Definition exchangeoperator.h:25
vecfuncT mo_bra
is the exchange matrix symmetric? K phi_i = \sum_k \phi_k \int \phi_k \phi_i
Definition exchangeoperator.h:129
World & world
Definition exchangeoperator.h:126
std::shared_ptr< MacroTaskQ > taskq
Definition exchangeoperator.h:127
Function< T, NDIM > functionT
Definition exchangeoperator.h:18
bool is_symmetric() const
Definition exchangeoperator.h:81
ExchangeImpl & set_taskq(std::shared_ptr< MacroTaskQ > taskq1)
Definition exchangeoperator.h:83
std::vector< functionT > vecfuncT
Definition exchangeoperator.h:19
ExchangeImpl & set_algorithm(const Algorithm &alg)
Definition exchangeoperator.h:93
static std::atomic< long > apply_timer
Definition exchangeoperator.h:21
ExchangeImpl(World &world, const double lo, const double thresh)
default ctor
Definition exchangeoperator.h:51
std::string info() const
Definition exchangeoperator.h:68
bool do_print_timings() const
Definition exchangeoperator.h:122
bool printdebug() const
Definition exchangeoperator.h:124
static auto set_poisson(World &world, const double lo, const double econv=FunctionDefaults< 3 >::get_thresh())
Definition exchangeoperator.h:70
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:63
Definition SCFOperators.h:104
Algorithm
Definition SCFOperators.h:116
@ multiworld_efficient_row
Definition SCFOperators.h:117
Function< T, NDIM > operator()(const Function< T, NDIM > &ket) const
Definition SCFOperators.h:150
std::vector< functionT > vecfuncT
Definition SCFOperators.h:110
FunctionDefaults holds default paramaters as static class members.
Definition funcdefaults.h:100
static const double & get_thresh()
Returns the default threshold.
Definition funcdefaults.h:176
A multiresolution adaptive numerical function.
Definition mra.h:139
Definition macrotaskq.h:978
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
std::shared_ptr< MacroTaskQ > taskq
Definition SCFOperators.h:70
void sum(T *buf, size_t nelem)
Inplace global sum while still processing AM & tasks.
Definition worldgop.h:870
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
WorldGopInterface & gop
Global operations.
Definition world.h:207
Declares the Cloud class for storing data and transfering them between worlds.
static double lo
Definition dirac-hatom.cc:23
std::vector< Spinor > truncate(std::vector< Spinor > arg)
Definition dirac-hatom.cc:499
Fcwf apply(World &world, real_convolution_3d &op, const Fcwf &psi)
Definition fcwf.cc:281
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
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
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:1806
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
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:2066
static const double thresh
Definition rk.cc:45
constexpr std::size_t NDIM
Definition testgconv.cc:54