34 const std::vector<int>& walkers_per_crowd,
38 walkers_per_crowd_(walkers_per_crowd),
44 app_log() <<
" Using QMCCostFunctionBatched::QMCCostFunctionBatched" << std::endl;
52 const std::vector<Return_rt>& PM,
66 PGradient[i] = (CostPlus - CostMinus) * dh;
86 Return_rt delE_bar = 0;
91 Return_rt weight = saved[
REWEIGHT] * wgtinv;
96 HD_avg[
pm] += HDsaved[
pm];
107 Return_rt weight = saved[
REWEIGHT] * wgtinv;
109 Return_rt delta_l = (eloc_new -
curAvg_w);
121 EDtotals_w[
pm] += weight * (HDsaved[
pm] + 2.0 *
std::real(Dsaved[
pm]) * delta_l);
122 URV[
pm] += 2.0 * (eloc_new * HDsaved[
pm] -
curAvg * HD_avg[
pm]);
124 EDtotals[
pm] += weight * (2.0 *
std::real(Dsaved[
pm]) * (delE - delE_bar) + ddelE * HDsaved[
pm]);
126 EDtotals[
pm] += weight * (2.0 *
std::real(Dsaved[
pm]) * (delE - delE_bar) - ddelE * HDsaved[
pm]);
133 Return_rt smpinv = 1.0 /
static_cast<Return_rt
>(
NumSamples);
138 Return_rt weight = saved[
REWEIGHT] * wgtinv;
140 Return_rt delta_l = (eloc_new -
curAvg_w);
141 Return_rt sigma_l = delta_l * delta_l;
158 PGradient[j] +=
w_var * E2Dtotals_w[j];
160 PGradient[j] +=
w_en * EDtotals_w[j];
162 PGradient[j] +=
w_w * URV[j];
164 PGradient[j] +=
w_abs * EDtotals[j];
174 app_log() <<
" Found " << components.size() <<
" wavefunction dependent components in the Hamiltonian";
175 if (components.size())
177 app_log() <<
" '" << component.getName() <<
"'";
259 std::vector<std::unique_ptr<CostFunctionCrowdData>> opt_eval(opt_num_crowds);
260 for (
int i = 0; i < opt_num_crowds; i++)
269 std::vector<int> samples_per_crowd_offsets(opt_num_crowds + 1);
275 const std::vector<int>& samples_per_crowd_offsets,
const std::vector<int>& walkers_per_crowd,
276 std::vector<ParticleGradient*>& gradPsi, std::vector<ParticleLaplacian*>& lapPsi,
282 const int local_samples = samples_per_crowd_offsets[crowd_id + 1] - samples_per_crowd_offsets[crowd_id];
284 int final_batch_size;
288 for (
int inb = 0; inb < num_batches; inb++)
290 int current_batch_size = walkers_per_crowd[crowd_id];
291 if (inb == num_batches - 1)
292 current_batch_size = final_batch_size;
294 const int base_sample_index = inb * walkers_per_crowd[crowd_id] + samples_per_crowd_offsets[crowd_id];
296 auto wf_list_no_leader = opt_data.
get_wf_list(current_batch_size);
297 auto p_list_no_leader = opt_data.
get_p_list(current_batch_size);
298 auto h_list_no_leader = opt_data.
get_h_list(current_batch_size);
311 for (
int ib = 0; ib < current_batch_size; ib++)
313 samples.loadSample(p_list[ib], base_sample_index + ib);
338 std::vector<QMCHamiltonian::FullPrecRealType> energy_list;
342 const size_t nparams = optVars.size();
349 handle.
takeSample(energy_list, dlogpsi_array, dhpsioverpsi_array, base_sample_index);
351 for (
int ib = 0; ib < current_batch_size; ib++)
353 const int is = base_sample_index + ib;
354 for (
int j = 0; j < nparams; j++)
357 DerivRecords[is][j] = dlogpsi_array[ib][j];
359 HDerivRecords[is][j] =
std::real(dhpsioverpsi_array[ib][j]);
369 for (
int ib = 0; ib < current_batch_size; ib++)
371 const int is = base_sample_index + ib;
372 auto etmp = energy_list[ib];
373 opt_data.
get_e0() += etmp;
374 opt_data.
get_e2() += etmp * etmp;
381 const auto twf_dependent_components = h_list[ib].getTWFDependentComponents();
382 for (
const OperatorBase& component : twf_dependent_components)
383 RecordsOnNode[is][
ENERGY_FIXED] -= component.getValue();
392 for (
int i = 0; i < opt_eval.size(); i++)
394 et_tot += opt_eval[i]->get_e0();
395 e2_tot += opt_eval[i]->get_e2();
401 std::vector<Return_rt> etemp(3);
407 Etarget =
static_cast<Return_rt
>(etemp[0] / etemp[1]);
411 app_log() <<
" Total weights = " << etemp[1] << std::endl;
430 #ifdef HAVE_LMY_ENGINE 431 void QMCCostFunctionBatched::engine_checkConfigurations(cqmc::engine::LMYEngine<Return_t>* EngineObj,
433 const std::string& MinMethod)
435 APP_ABORT(
"LMYEngine not implemented with batch optimization");
466 Return_rt wgt_tot = 0.0;
467 Return_rt wgt_tot2 = 0.0;
476 std::vector<int> samples_per_crowd_offsets(opt_num_crowds + 1);
481 std::vector<std::unique_ptr<CostFunctionCrowdData>> opt_eval(opt_num_crowds);
482 for (
int i = 0; i < opt_num_crowds; i++)
488 auto evalOptCorrelated =
490 const std::vector<int>& walkers_per_crowd, std::vector<ParticleGradient*>& gradPsi,
493 bool compute_all_from_scratch, Return_rt
vmc_or_dmc,
bool needGrad) {
496 const int local_samples = samples_per_crowd_offsets[crowd_id + 1] - samples_per_crowd_offsets[crowd_id];
499 int final_batch_size;
502 for (
int inb = 0; inb < num_batches; inb++)
504 int current_batch_size = walkers_per_crowd[crowd_id];
505 if (inb == num_batches - 1)
507 current_batch_size = final_batch_size;
510 const int base_sample_index = inb * walkers_per_crowd[crowd_id] + samples_per_crowd_offsets[crowd_id];
512 auto p_list_no_leader = opt_data.
get_p_list(current_batch_size);
513 auto wf_list_no_leader = opt_data.
get_wf_list(current_batch_size);
514 auto h0_list_no_leader = opt_data.
get_h0_list(current_batch_size);
524 for (
int ib = 0; ib < current_batch_size; ib++)
526 samples.loadSample(p_list[ib], base_sample_index + ib);
537 std::vector<std::unique_ptr<ParticleSet::ParticleGradient>> dummyG_ptr_list;
538 std::vector<std::unique_ptr<ParticleSet::ParticleLaplacian>> dummyL_ptr_list;
541 if (compute_all_from_scratch)
543 int nptcl = gradPsi[0]->size();
544 dummyG_ptr_list.reserve(current_batch_size);
545 dummyL_ptr_list.reserve(current_batch_size);
546 for (
int i = 0; i < current_batch_size; i++)
548 dummyG_ptr_list.emplace_back(std::make_unique<ParticleGradient>(nptcl));
549 dummyL_ptr_list.emplace_back(std::make_unique<ParticleLaplacian>(nptcl));
557 compute_all_from_scratch);
559 Return_rt inv_n_samples = 1.0 / samples.getGlobalNumSamples();
561 for (
int ib = 0; ib < current_batch_size; ib++)
563 const int is = base_sample_index + ib;
564 wf_list[ib].G += *gradPsi[is];
565 wf_list[ib].L += *lapPsi[is];
567 p_list[ib].G += *gradPsi[is];
568 p_list[ib].L += *lapPsi[is];
570 RecordsOnNode[is][
REWEIGHT] = weight;
572 opt_data.
get_wgt() += inv_n_samples * weight;
573 opt_data.
get_wgt2() += inv_n_samples * weight * weight;
579 const size_t nparams = optVars.size();
585 dlogpsi_array, dhpsioverpsi_array);
587 for (
int ib = 0; ib < current_batch_size; ib++)
589 const int is = base_sample_index + ib;
590 auto etmp = energy_list[ib];
592 for (
int j = 0; j < nparams; j++)
594 if (optVars.recompute(j))
597 DerivRecords[is][j] = dlogpsi_array[ib][j];
599 HDerivRecords[is][j] =
std::real(dhpsioverpsi_array[ib][j]);
608 for (
int ib = 0; ib < current_batch_size; ib++)
610 const int is = base_sample_index + ib;
611 auto etmp = energy_list[ib];
623 compute_all_from_scratch,
vmc_or_dmc, needGrad);
625 for (
int i = 0; i < opt_eval.size(); i++)
627 wgt_tot += opt_eval[i]->get_wgt();
628 wgt_tot2 += opt_eval[i]->get_wgt2();
637 Return_rt wgtnorm = (wgt_tot == 0) ? 0 : wgt_tot;
645 wgt_tot += inv_n_samples * saved[
REWEIGHT];
650 wgtnorm = (wgt_tot == 0) ? 1 : 1.0 / wgt_tot;
657 wgt_tot += inv_n_samples * saved[
REWEIGHT];
662 for (
int i = 0; i <
SumValue.size(); i++)
715 Return_rt weight = saved[
REWEIGHT] * wgtinv;
719 D_avg[
pm] += Dsaved[
pm] * weight;
728 Return_rt weight = saved[
REWEIGHT] * wgtinv;
734 std::vector<int> params_per_crowd(opt_num_crowds + 1);
738 auto constructMatrices = [](
int crowd_id, std::vector<int>& crowd_ranges,
int numParams,
const Return_t* Dsaved,
739 const Return_rt* HDsaved, Return_rt weight, Return_rt eloc_new,
RealType V_avg,
742 int local_pm_start = crowd_ranges[crowd_id];
743 int local_pm_end = crowd_ranges[crowd_id + 1];
745 for (
int pm = local_pm_start;
pm < local_pm_end;
pm++)
747 Return_t wfe = (HDsaved[
pm] + (Dsaved[
pm] - D_avg[
pm]) * eloc_new) * weight;
756 Left(
pm + 1, 0) += (1 - b2) *
std::real(wfd) * eloc_new;
757 for (
int pm2 = 0; pm2 < numParams; pm2++)
760 Left(
pm + 1, pm2 + 1) +=
761 std::real((1 - b2) *
std::conj(wfd) * (HDsaved[pm2] + (Dsaved[pm2] - D_avg[pm2]) * eloc_new));
764 Right(
pm + 1, pm2 + 1) += ovlij;
768 (HDsaved[pm2] -
RealType(2.0) * (Dsaved[pm2] - D_avg[pm2]) * eloc_new));
769 Left(
pm + 1, pm2 + 1) += b2 * (varij + V_avg * ovlij);
775 crowd_tasks(opt_num_crowds, constructMatrices, params_per_crowd,
getNumParams(), Dsaved, HDsaved, weight, eloc_new,
Return_rt Cost(bool needGrad=true) override
return the cost value for CGMinimization
int rank_local_num_samples_
Return_rt Etarget
target energy
std::vector< ParticleLaplacian * > d2LogPsi
Fixed Laplacian , , components.
DriverWalkerResourceCollection & getSharedResource()
void pause()
Pause the summary and log streams.
void delete_iter(IT first, IT last)
delete the pointers in [first,last)
helper functions for EinsplineSetBuilder
~QMCCostFunctionBatched() override
Destructor.
int NumSamples
global number of samples to use in correlated sampling
void checkConfigurations(EngineHandle &handle) override
evaluate everything before optimization
RefVector< OperatorBase > getTWFDependentComponents()
return components, auxH not included, depending on TWF.
QTBase::RealType RealType
std::vector< Return_rt > & get_log_psi_fixed()
Abstraction for running concurrent tasks in parallel by an executor executor workers can be OpenMP th...
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
size_t getTotalNum() const
std::vector< TinyVector< int, 2 > > equalVarMap
index mapping for <equal> constraints
static RefVector< T > convertPtrToRefVectorSubset(const std::vector< T *> &ptr_list, int offset, int len)
RandomBase< FullPrecRealType > & get_rng_save()
int NumOptimizables
total number of optimizable variables
Collection of Local Energy Operators.
QMCTraits::QTFull::RealType EffectiveWeight
Return_rt curAvg_w
current weighted average (correlated sampling)
size_t getNumSamples() const
opt_variables_type OptVariables
list of optimizables
std::vector< std::unique_ptr< T > > UPtrVector
ResourceCollection & get_h0_res()
UPtrVector< RandomBase< FullPrecRealType > > RngSaved
Random number generators.
void resize(size_type n, size_type m)
Resize the container.
ParticleSet::ParticleLaplacian ParticleLaplacian
Communicate * Controller
Global Communicator for a process.
ParticleSet::ParticleGradient ParticleGradient
Saved derivative properties and Hderivative properties of all the walkers.
static RefVector< T > convertUPtrToRefVector(const UPtrVector< T > &ptr_list)
convert a vector of std::unique_ptrs<T> to a refvector<T>
void GradCost(std::vector< Return_rt > &PGradient, const std::vector< Return_rt > &PM, Return_rt FiniteDiff=0) override
static std::vector< QMCHamiltonian::FullPrecRealType > mw_evaluate(const RefVectorWithLeader< QMCHamiltonian > &ham_list, const RefVectorWithLeader< TrialWaveFunction > &wf_list, const RefVectorWithLeader< ParticleSet > &p_list)
batched version of evaluate for LocalEnergy
OutputManagerClass outputManager(Verbosity::HIGH)
static std::vector< QMCHamiltonian::FullPrecRealType > mw_evaluateValueAndDerivatives(const RefVectorWithLeader< QMCHamiltonian > &ham_list, const RefVectorWithLeader< TrialWaveFunction > &wf_list, const RefVectorWithLeader< ParticleSet > &p_list, const opt_variables_type &optvars, RecordArray< ValueType > &dlogpsi, RecordArray< ValueType > &dhpsioverpsi)
Wrapping information on parallelism.
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
RefVector< QMCHamiltonian > get_h_list(int len)
void resume()
Resume the summary and log streams.
Specialized paritlce class for atomistic simulations.
std::vector< ParticleGradient * > dLogPsi
Fixed Gradients , , components.
void compute_batch_parameters(int sample_size, int batch_size, int &num_batches, int &final_batch_size)
Compute number of batches and final batch size given the number of samples and a batch size...
Communicate * myComm
pointer to Communicate
ResourceCollection twf_res
Return_rt curAvg
current Average
ResourceCollection pset_res
RefVector< ParticleSet > get_p_list(int len)
NewTimer & createGlobalTimer(const std::string &myname, timer_levels mylevel)
opt_variables_type OptVariablesForPsi
full list of optimizables
class to handle a set of variables that can be modified during optimizations
static void mw_evaluateDeltaLog(const RefVectorWithLeader< TrialWaveFunction > &wf_list, const RefVectorWithLeader< ParticleSet > &p_list, std::vector< RealType > &logpsi_list, RefVector< ParticleSet::ParticleGradient > &dummyG_list, RefVector< ParticleSet::ParticleLaplacian > &dummyL_list, bool recompute=false)
evaluate the log value for optimizable parts of a many-body wave function
QMCHamiltonian & H
Hamiltonian.
Return_rt w_en
weights for energy and variance in the cost function
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Implements wave-function optimization.
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
ParticleSet & W
Particle set.
static void mw_evaluateDeltaLogSetup(const RefVectorWithLeader< TrialWaveFunction > &wf_list, const RefVectorWithLeader< ParticleSet > &p_list, std::vector< RealType > &logpsi_fixed_list, std::vector< RealType > &logpsi_opt_list, RefVector< ParticleSet::ParticleGradient > &fixedG_list, RefVector< ParticleSet::ParticleLaplacian > &fixedL_list)
evaluate the sum of log value of optimizable many-body wavefunctions
bool isEffectiveWeightValid(EffectiveWeight effective_weight) const
check the validity of the effective weight calculated by correlatedSampling
size_type size() const
return the size
void resetPsi(bool final_reset=false) override
reset the wavefunction
float conj(const float &c)
Workaround to allow conj on scalar to return real instead of complex.
EffectiveWeight correlatedSampling(bool needGrad=true) override
run correlated sampling return effective walkers ( w_i)^2/(Nw * w^2_i)
Declaration of a TrialWaveFunction.
ResourceCollection ham_res
Implements wave-function optimization.
int ReportCounter
counter for output
An abstract class for Local Energy operators.
std::vector< std::reference_wrapper< T > > RefVector
void FairDivide(int ntot, int npart, IV &adist)
Partition ntot over npart.
virtual void prepareSampling(int num_params, int num_samples)=0
Function for preparing derivative ratio vectors used by optimizer engines.
Matrix< Return_rt > HDerivRecords_
Class to represent a many-body trial wave function.
RefVector< QMCHamiltonian > get_h0_list(int len)
NewTimer & corr_sampling_timer_
std::vector< Return_rt > SumValue
Sum of energies and weights for averages.
static void mw_update(const RefVectorWithLeader< ParticleSet > &p_list, bool skipSK=false)
batched version of update
std::vector< Return_rt > & get_log_psi_opt()
virtual void takeSample(const std::vector< FullPrecReal > &energy_list, const RecordArray< Value > &dlogpsi_array, const RecordArray< Value > &dhpsioverpsi_array, int base_sample_index)=0
Function for passing derivative ratios to optimizer engines.
RefVector< TrialWaveFunction > get_wf_list(int len)
void resetOptimizableObjects(TrialWaveFunction &psi, const opt_variables_type &opt_variables) const
TrialWaveFunction & Psi
Trial function.
int getNumParams() const override
return the number of optimizable parameters
Matrix< Return_rt > RecordsOnNode_
handles acquire/release resource by the consumer (RefVectorWithLeader type).
Return_rt MaxWeight
maximum weight beyond which the weight is set to 1
virtual void finishSampling()=0
Function for having optimizer engines execute their sample_finish functions.
std::vector< int > walkers_per_crowd_
QMCCostFunctionBatched(ParticleSet &w, TrialWaveFunction &psi, QMCHamiltonian &h, SampleStack &samples, const std::vector< int > &walkers_per_crowd, Communicate *comm)
Constructor.
UPtrVector< RandomBase< FullPrecRealType > > & get_rng_ptr_list()
int PowerE
|E-E_T|^PowerE is used for the cost function
void setRandomGenerator(RandomBase< FullPrecRealType > *rng)
Matrix< Return_t > DerivRecords_
Temp derivative properties and Hderivative properties of all the walkers.
void zero_log_psi()
Set the log_psi_* arrays to zero.
size_t getGlobalNumSamples() const
Global number of samples is number of samples per rank * number of ranks.
Return_rt curVar_w
current weighted variance (correlated sampling)
Declaration of a MCWalkerConfiguration.
Return_rt fillOverlapHamiltonianMatrices(Matrix< Return_rt > &Left, Matrix< Return_rt > &Right) override
void getConfigurations(const std::string &aroot) override
BareKineticEnergy::Return_t Return_t
NewTimer & check_config_timer_
void setTargetEnergy(Return_rt et)
Return_rt EtargetEff
real target energy with the Correlation Factor
std::vector< RandomBase< FullPrecRealType > * > MoverRng