Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions source/source_base/module_out/filename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ std::string filename_output(
const int out_app_flag,
const bool gamma_only,
const int istep,
const int iter)
const int iter,
const std::string &tag)
{
// output filename = "{global_out_dir}/property{s}{spin index}
// {k(optional)}{k-point index}{g(optional)}{geometry index1}{_basis(nao|pw)}
Expand Down Expand Up @@ -126,7 +127,7 @@ std::string filename_output(
}

std::string fn_out
= directory + property + kpoint_block + spin_block
= directory + property + tag + kpoint_block + spin_block
+ istep_block + iter_block + "_" + basis + suffix_block;

return fn_out;
Expand Down
5 changes: 4 additions & 1 deletion source/source_base/module_out/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace ModuleIO
* filename in mode 2 (out_app_flag == 2), and only when istep >= 0.
* @param iter: index of the electronic step starting from 0. The step number is only embedded in
* the filename in mode 2 (out_app_flag == 2), and only when iter >= 0.
* @param tag: optional tag inserted after the property, used to write several independent dumps of
* the same property in one run (e.g. the stochastic orbitals of an SDFT run).
* @return The generated filename.
*/
std::string filename_output(
Expand All @@ -37,7 +39,8 @@ std::string filename_output(
const int out_app_flag,
const bool gamma_only,
const int istep=-1,
const int iter=-1);
const int iter=-1,
const std::string &tag="");

}
#endif
111 changes: 111 additions & 0 deletions source/source_esolver/esolver_sdft_pw.cpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output_wfc_sdft should not belong to 'esolver_sdft_pw', it should be put inside the function 'after_scf'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: output_wfc_sdft() removed, the dump is inside after_scf().

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "esolver_sdft_pw.h"

#include "source_base/global_variable.h"
#include "source_base/fs_compat.h"
#include "source_base/module_device/memory_op.h"
#include "source_base/memory_recorder.h"
#include "source_base/parallel_comm.h"
#include "source_estate/module_charge/chg_symm.h"
#include "source_hsolver/diago_iter_assist.h"
#include "source_hsolver/diago_params.h"
#include "source_io/module_parameter/parameter.h"
#include "source_io/module_wf/write_wfc_pw.h"
#include "source_pw/module_stodft/sto_dos.h"
#include "source_pw/module_stodft/sto_elecond.h"
#include "source_pw/module_stodft/sto_forces.h"
Expand Down Expand Up @@ -129,6 +132,114 @@ void ESolver_SDFT_PW<T, Device>::after_scf(UnitCell &ucell, const int istep, con
// 1) call after_scf() of ESolver_KS_PW
ESolver_KS_PW<T, Device>::after_scf(ucell, istep, conv_esolver);

// 2) dump the SDFT/MDFT wavefunctions through the same writer that the
// KSDFT path uses (ModuleIO::write_wfc_pw), so the file layout, the
// binary mode and the collective pattern are shared. `out_wfc_pw` is
// reused as the switch; nothing is written for other values.
//
// Every band group enters the writer even when it owns no orbital,
// because the writer synchronises the whole world: returning early for
// an empty group would leave the other groups waiting in MPI_Barrier.
if (istep != 0 || this->inp_->basis_type != "pw"
|| (this->inp_->out_wfc_pw != 1 && this->inp_->out_wfc_pw != 2))
{
ModuleBase::timer::end("ESolver_SDFT_PW", "after_scf");
return;
}

ModuleBase::TITLE("ESolver_SDFT_PW", "after_scf_dump_wfc");

// On device builds the master copy of the deterministic orbitals lives on
// the device; bring the host copy up to date before reading it, exactly as
// the KSDFT output path does (ModuleIO::ctrl_iter_pw).
this->stp.sync_cpu();
const std::string psi_dir = PARAM.globalv.global_psi_dir;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] The global-dependency exception no longer matches this implementation: the PR now adds five global references in after_scf(), including PARAM.globalv.npol, while the body describes four reads in the deleted output_wfc_sdft(). Please pass/remove these dependencies or update the exception to the current count and scope for maintainer approval.

ModuleBase::ensure_directory(psi_dir);

// The number of stochastic orbitals can differ between k points when
// `nbands_sto all` is used; dump only the rows that every k point owns, so
// that the header and the coefficients agree and no uncomputed padding row
// is written.
int nsto = 0;
if (this->stowf.nchip_max > 0 && this->stowf.nchip != nullptr)
{
nsto = this->stowf.nchip[0];
for (int ik = 1; ik < this->kv.get_nks(); ++ik)
{
nsto = std::min(nsto, this->stowf.nchip[ik]);
}
}

// At most four orbitals of each set are dumped: enough to inspect the
// orbitals in real space without writing hundreds of files per run.
const int max_dump = 4;
const int nband_ks = std::min(this->stp.psi_cpu->get_nbands(), max_dump);
const int nband_sto = std::min(nsto, max_dump);
const int nband_filtered = std::min(nsto, max_dump);
const int nband_chiortho = std::min(this->stp.psi_cpu->get_nbands(), 2);

auto dump = [&](const std::string& tag, const psi::Psi<std::complex<double>>& psi, const int nbands_out) {
ModuleIO::write_wfc_pw(-1, // -1 keeps the ionic step out of the file name
-1, // same for the electronic step
-1, // a single-point dump always truncates
this->inp_->kpar,
GlobalV::MY_POOL,
0, // my_rank: unused by the writer
nbands_out,
this->inp_->nspin,
PARAM.globalv.npol,
this->pw_wfc->poolrank,
this->pw_wfc->poolnproc,
this->inp_->out_wfc_pw,
0, // out_app_flag: one frame, no append
this->inp_->ecutwfc,
psi_dir,
psi,
this->kv,
this->pw_wfc,
GlobalV::ofs_running,
tag);
};

// With band parallelism every band group owns a different subset of the
// orbitals, so the files of the groups must not share a name.
std::string bg = "";
if (this->inp_->bndpar > 1)
{
bg = "_bg" + std::to_string(GlobalV::MY_BNDGROUP);
}

if (this->inp_->nbands > 0 && this->stp.psi_cpu != nullptr)
{
dump(bg + "_ks", *this->stp.psi_cpu, nband_ks);
}
if (this->inp_->nbands_sto > 0 && this->stowf.chi0_cpu != nullptr)
{
dump(bg + "_sto", *this->stowf.chi0_cpu, nband_sto);
}
// The filtered and the KS-orthogonalized stochastic orbitals live on the
// wavefunction device (and may use a single-precision type), while the
// writer works on host double-precision data. Copying through
// cast_memory_op handles device -> host and float -> double in one step, so
// all four dump sets are available on CPU and GPU builds alike.
auto dump_on_host = [&](const std::string& tag, const psi::Psi<T, Device>& src, const int nbands_out) {
psi::Psi<std::complex<double>> host(
this->kv.get_nks(), src.get_nbands(), this->pw_wfc->npwk_max, this->kv.ngk, true);
base_device::memory::cast_memory_op<std::complex<double>, T, base_device::DEVICE_CPU, Device>()(
host.get_pointer() - host.get_psi_bias(),
src.get_pointer() - src.get_psi_bias(),
host.size());
Comment on lines +226 to +231

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this allocates and copies every stochastic row even though the dump writes at most four or two. It also duplicates CPU data that the writer can already read directly. Please reuse host storage on CPU and stage only the selected per-k rows on GPU.

dump(tag, host, nbands_out);
};
if (this->stowf.shchi != nullptr)
{
dump_on_host(bg + "_shchi", *this->stowf.shchi, nband_filtered);
}
if (this->inp_->nbands > 0 && this->stowf.chiortho != nullptr)
{
dump_on_host(bg + "_chiortho", *this->stowf.chiortho, nband_chiortho);
}

ModuleBase::timer::end("ESolver_SDFT_PW", "after_scf");
}

Expand Down
12 changes: 10 additions & 2 deletions source/source_io/module_parameter/read_inp_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,15 @@ The optional second integer controls the output precision. If not provided, the
item.annotation = "output wave functions";
item.category = "Output information";
item.type = "Integer";
item.description = R"(Controls whether plane-wave Kohn-Sham wavefunction coefficients are written to `OUT.${suffix}/`.
item.description = R"(Controls whether plane-wave wavefunction coefficients are written to `OUT.${suffix}/`.
For `esolver_type=ksdft` the Kohn-Sham coefficients are written. For `esolver_type=sdft` this
parameter switches on the same writer for the SDFT/MDFT orbitals, once, at the first ionic step:
at most four deterministic (Kohn-Sham) orbitals (`wf_ks...`), at most four raw stochastic
orbitals (`wf_sto...`), at most four occupation-filtered stochastic orbitals (`wf_shchi...`, the
ones that enter the charge density without a separate diagonalization) and at most two stochastic
orbitals orthogonalized against the Kohn-Sham subspace (`wf_chiortho...`). The two last sets are
only written when the wavefunction device is the CPU, and with `bndpar > 1` every band group
writes its own `bg<i>` files (a band group without orbitals writes a header-only file).

Available values are:
* `0`: Do not write wavefunction coefficients.
Expand All @@ -332,7 +340,7 @@ With `init_wfc file binary`, ABACUS reads only unindexed binary `wf*_pw.dat` fil
[NOTE] In the 3.10-LTS version, the binary files are named `WAVEFUNC1.dat`, `WAVEFUNC2.dat`, etc.)";
item.default_value = "0";
item.unit = "";
item.set_availability("basis_type==pw and esolver_type==ksdft");
item.set_availability("basis_type==pw and (esolver_type==ksdft or esolver_type==sdft)");
read_sync_int(input.out_wfc_pw);
this->add_item(item);
}
Expand Down
5 changes: 3 additions & 2 deletions source/source_io/module_wf/write_wfc_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void ModuleIO::write_wfc_pw(
const psi::Psi<std::complex<double>>& psi,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* wfcpw,
std::ofstream &ofs_running)
std::ofstream &ofs_running,
const std::string& property_tag)
{
ModuleBase::TITLE("ModuleIO", "write_wfc_pw");

Expand All @@ -58,7 +59,7 @@ void ModuleIO::write_wfc_pw(
{
std::string fn = filename_output(global_out_dir,"wf","pw",
ik_local,kv.ik2iktot,nspin,nkstot,
out_wfc_pw,out_app_flag,gamma_only,istep,iter);
out_wfc_pw,out_app_flag,gamma_only,istep,iter,property_tag);

ModuleIO::write_notice(ofs_running, "G-space wave functions", fn);

Expand Down
3 changes: 2 additions & 1 deletion source/source_io/module_wf/write_wfc_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void write_wfc_pw(
const psi::Psi<std::complex<double>>& psi,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* wfcpw,
std::ofstream &ofs_running);
std::ofstream &ofs_running,
const std::string& property_tag = "");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this and filename_output() add default arguments to existing interfaces, which root rule 5 prohibits. Preserve the empty-tag behavior with a forwarding overload, or make tag selection explicit at callers.


}

Expand Down
Loading