Skip to content

Commit

Permalink
Merge pull request #4962 from ye-luo/opt-output
Browse files Browse the repository at this point in the history
Refine WFOpt batched driver timer reporting
  • Loading branch information
prckent committed Apr 21, 2024
2 parents 34c64b0 + d46906e commit 7e45c96
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ QMCFixedSampleLinearOptimize::QMCFixedSampleLinearOptimize(const ProjectData& pr
Max_iterations(1),
wfNode(NULL),
param_tol(1e-4),
generate_samples_timer_(createGlobalTimer("QMCLinearOptimize::GenerateSamples", timer_level_medium)),
generate_samples_timer_(createGlobalTimer("QMCLinearOptimize::generateSamples", timer_level_medium)),
initialize_timer_(createGlobalTimer("QMCLinearOptimize::Initialize", timer_level_medium)),
eigenvalue_timer_(createGlobalTimer("QMCLinearOptimize::EigenvalueSolve", timer_level_medium)),
involvmat_timer_(createGlobalTimer("QMCLinearOptimize::invertOverlapMat", timer_level_medium)),
Expand Down
141 changes: 74 additions & 67 deletions src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ QMCFixedSampleLinearOptimizeBatched::QMCFixedSampleLinearOptimizeBatched(
do_output_matrices_hdf_(false),
output_matrices_initialized_(false),
freeze_parameters_(false),
generate_samples_timer_(createGlobalTimer("QMCLinearOptimizeBatched::GenerateSamples", timer_level_medium)),
initialize_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Initialize", timer_level_medium)),
generate_samples_timer_(createGlobalTimer("QMCLinearOptimizeBatched::generateSamples", timer_level_medium)),
build_olv_ham_timer_(createGlobalTimer("QMCLinearOptimizedBatched::build_ovl_ham_matrix", timer_level_medium)),
invert_olvmat_timer_(createGlobalTimer("QMCLinearOptimizedBatched::invert_ovlMat", timer_level_medium)),
eigenvalue_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Eigenvalue", timer_level_medium)),
involvmat_timer_(createGlobalTimer("QMCLinearOptimizedBatched::invert_matrix", timer_level_medium)),
line_min_timer_(createGlobalTimer("QMCLinearOptimizeBatched::Line_Minimization", timer_level_medium)),
cost_function_timer_(createGlobalTimer("QMCLinearOptimizeBatched::CostFunction", timer_level_medium)),
wfNode(NULL),
Expand Down Expand Up @@ -1587,13 +1588,6 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run()
// compute the initial cost
const RealType initCost = optTarget->computedCost();

// say what we are doing
app_log() << std::endl
<< "*****************************************" << std::endl
<< "Building overlap and Hamiltonian matrices" << std::endl
<< "*****************************************" << std::endl;

Timer t_build_matrices;
// allocate the matrices we will need
Matrix<RealType> ovlMat(N, N);
ovlMat = 0.0;
Expand All @@ -1604,79 +1598,92 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run()
Matrix<RealType> prdMat(N, N);
prdMat = 0.0;

// build the overlap and hamiltonian matrices
optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);
// for outputing matrices and eigenvalue/vectors to disk
hdf_archive hout;

if (do_output_matrices_csv_)
{
output_overlap_.output(ovlMat);
output_hamiltonian_.output(hamMat);
}
ScopedTimer local(build_olv_ham_timer_);
Timer t_build_matrices;
// say what we are doing
app_log() << std::endl
<< "*****************************************" << std::endl
<< "Building overlap and Hamiltonian matrices" << std::endl
<< "*****************************************" << std::endl;

hdf_archive hout;
if (do_output_matrices_hdf_)
{
std::string newh5 = get_root_name() + ".linear_matrices.h5";
hout.create(newh5, H5F_ACC_TRUNC);
hout.write(ovlMat, "overlap");
hout.write(hamMat, "Hamiltonian");
hout.write(bestShift_i, "bestShift_i");
hout.write(bestShift_s, "bestShift_s");
}
app_log() << " Execution time (building matrices) = " << std::setprecision(4) << t_build_matrices.elapsed() << std::endl;
// build the overlap and hamiltonian matrices
optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);

app_log() << std::endl
<< "**************************" << std::endl
<< "Solving eigenvalue problem" << std::endl
<< "**************************" << std::endl;
if (do_output_matrices_csv_)
{
output_overlap_.output(ovlMat);
output_hamiltonian_.output(hamMat);
}

Timer t_eigen;
invMat.copy(ovlMat);
// apply the identity shift
for (int i = 1; i < N; i++)
{
hamMat(i, i) += bestShift_i;
if (invMat(i, i) == 0)
invMat(i, i) = bestShift_i * bestShift_s;
if (do_output_matrices_hdf_)
{
std::string newh5 = get_root_name() + ".linear_matrices.h5";
hout.create(newh5, H5F_ACC_TRUNC);
hout.write(ovlMat, "overlap");
hout.write(hamMat, "Hamiltonian");
hout.write(bestShift_i, "bestShift_i");
hout.write(bestShift_s, "bestShift_s");
}
app_log() << " Execution time (building matrices) = " << std::setprecision(4) << t_build_matrices.elapsed()
<< std::endl;
}

// compute the inverse of the overlap matrix
{
ScopedTimer local(involvmat_timer_);
invert_matrix(invMat, false);
}
ScopedTimer local(eigenvalue_timer_);
Timer t_eigen;
app_log() << std::endl
<< "**************************" << std::endl
<< "Solving eigenvalue problem" << std::endl
<< "**************************" << std::endl;

// apply the overlap shift
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
hamMat(i, j) += bestShift_s * ovlMat(i, j);
invMat.copy(ovlMat);
// apply the identity shift
for (int i = 1; i < N; i++)
{
hamMat(i, i) += bestShift_i;
if (invMat(i, i) == 0)
invMat(i, i) = bestShift_i * bestShift_s;
}

// multiply the shifted hamiltonian matrix by the inverse of the overlap matrix
qmcplusplus::MatrixOperators::product(invMat, hamMat, prdMat);
// compute the inverse of the overlap matrix
{
ScopedTimer local(invert_olvmat_timer_);
invert_matrix(invMat, false);
}

// transpose the result (why?)
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
std::swap(prdMat(i, j), prdMat(j, i));
// apply the overlap shift
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++)
hamMat(i, j) += bestShift_s * ovlMat(i, j);

// compute the lowest eigenvalue of the product matrix and the corresponding eigenvector
RealType lowestEV = 0.;
{
ScopedTimer local(eigenvalue_timer_);
lowestEV = getLowestEigenvector(prdMat, parameterDirections);
}
// multiply the shifted hamiltonian matrix by the inverse of the overlap matrix
qmcplusplus::MatrixOperators::product(invMat, hamMat, prdMat);

// compute the scaling constant to apply to the update
objFuncWrapper_.Lambda = getNonLinearRescale(parameterDirections, ovlMat, *optTarget);
// transpose the result (why?)
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
std::swap(prdMat(i, j), prdMat(j, i));

if (do_output_matrices_hdf_)
{
hout.write(lowestEV, "lowest_eigenvalue");
hout.write(parameterDirections, "scaled_eigenvector");
hout.write(objFuncWrapper_.Lambda, "non_linear_rescale");
hout.close();
// compute the lowest eigenvalue of the product matrix and the corresponding eigenvector
RealType lowestEV = 0.;
lowestEV = getLowestEigenvector(prdMat, parameterDirections);

// compute the scaling constant to apply to the update
objFuncWrapper_.Lambda = getNonLinearRescale(parameterDirections, ovlMat, *optTarget);

if (do_output_matrices_hdf_)
{
hout.write(lowestEV, "lowest_eigenvalue");
hout.write(parameterDirections, "scaled_eigenvector");
hout.write(objFuncWrapper_.Lambda, "non_linear_rescale");
hout.close();
}
app_log() << " Execution time (eigenvalue) = " << std::setprecision(4) << t_eigen.elapsed() << std::endl;
}
app_log() << " Execution time (eigenvalue) = " << std::setprecision(4) << t_eigen.elapsed() << std::endl;

// scale the update by the scaling constant
for (int i = 0; i < numParams; i++)
Expand Down
5 changes: 3 additions & 2 deletions src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ class QMCFixedSampleLinearOptimizeBatched : public QMCDriverNew, LinearMethod
// Freeze variational parameters. Do not update them during each step.
bool freeze_parameters_;

NewTimer& generate_samples_timer_;
NewTimer& initialize_timer_;
NewTimer& generate_samples_timer_;
NewTimer& build_olv_ham_timer_;
NewTimer& invert_olvmat_timer_;
NewTimer& eigenvalue_timer_;
NewTimer& involvmat_timer_;
NewTimer& line_min_timer_;
NewTimer& cost_function_timer_;

Expand Down

0 comments on commit 7e45c96

Please sign in to comment.