From 7968fd07927df150af89d4b26c3b52e1ce72d836 Mon Sep 17 00:00:00 2001 From: pxlxingliang Date: Thu, 27 Jun 2024 13:34:43 +0800 Subject: [PATCH 1/6] refactor: remove the dependency of write_dmr on globalV::NLOCAL --- source/module_esolver/esolver_ks_lcao.cpp | 1 - .../module_hcontainer/output_hcontainer.cpp | 6 ++-- .../module_hcontainer/output_hcontainer.h | 4 +-- .../test/test_hcontainer_output.cpp | 2 +- .../test/test_hcontainer_readCSR.cpp | 2 +- source/module_io/write_dmr.cpp | 33 +++++++++---------- source/module_io/write_dmr.h | 7 ++-- 7 files changed, 24 insertions(+), 31 deletions(-) diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 0f522ab7a8..26e1c4b7ac 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -1061,7 +1061,6 @@ void ESolver_KS_LCAO::after_scf(const int istep) // 2) write density matrix for sparse matrix ModuleIO::write_dmr(dynamic_cast*>(this->pelec)->get_DM()->get_DMR_vector(), - GlobalV::NLOCAL, INPUT.out_dm1, false, GlobalV::out_app_flag, diff --git a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp index eb841e37b8..3f181cd9dd 100644 --- a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp @@ -12,9 +12,9 @@ namespace hamilt * @attention ofs should be open outside of this interface */ template -Output_HContainer::Output_HContainer(hamilt::HContainer* hcontainer, const int nrow, const int ncol, +Output_HContainer::Output_HContainer(hamilt::HContainer* hcontainer, std::ostream& ofs, double sparse_threshold, int precision) - : _hcontainer(hcontainer), _nrow(nrow), _ncol(ncol), _ofs(ofs), _sparse_threshold(sparse_threshold), _precision(precision) + : _hcontainer(hcontainer), _ofs(ofs), _sparse_threshold(sparse_threshold), _precision(precision) { } @@ -56,7 +56,7 @@ template void Output_HContainer::write_single_R(int rx, int ry, int rz) { this->_hcontainer->fix_R(rx, ry, rz); - ModuleIO::SparseMatrix sparse_matrix = ModuleIO::SparseMatrix(_nrow, _ncol); + ModuleIO::SparseMatrix sparse_matrix = ModuleIO::SparseMatrix(_hcontainer->get_nbasis(), _hcontainer->get_nbasis()); sparse_matrix.setSparseThreshold(this->_sparse_threshold); for (int iap = 0; iap < this->_hcontainer->size_atom_pairs(); ++iap) { diff --git a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h index b7924916de..6b468cd3ac 100644 --- a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h +++ b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h @@ -13,7 +13,7 @@ template class Output_HContainer { public: - Output_HContainer(hamilt::HContainer* hcontainer, const int nrow, const int ncol, std::ostream& ofs, + Output_HContainer(hamilt::HContainer* hcontainer, std::ostream& ofs, double sparse_threshold, int precision); // write the matrices of all R vectors to the output stream void write(); @@ -32,8 +32,6 @@ class Output_HContainer private: hamilt::HContainer* _hcontainer; - int _nrow=0; - int _ncol=0; std::ostream& _ofs; double _sparse_threshold; int _precision; diff --git a/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_output.cpp b/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_output.cpp index 0b56a820b1..7ed0f02703 100644 --- a/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_output.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_output.cpp @@ -131,7 +131,7 @@ TEST_F(OutputHContainerTest, Write) HR.unfix_R(); } double sparse_threshold = 0.1; - hamilt::Output_HContainer output_HR(&HR, ParaV.nrow, ParaV.ncol, std::cout, sparse_threshold, 2); + hamilt::Output_HContainer output_HR(&HR, std::cout, sparse_threshold, 2); // the first R testing::internal::CaptureStdout(); output_HR.write(0, 1, 1); diff --git a/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp b/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp index 349d793ff8..a7df9e168f 100644 --- a/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp @@ -124,7 +124,7 @@ TEST_F(ReadHContainerTest, ReadAndOutputHContainer) // output SR std::ofstream ofs_out("SR.out"); double sparse_threshold = 1e-10; - hamilt::Output_HContainer output_SR(&SR, paraV.nrow, paraV.ncol, ofs_out, sparse_threshold, 8); + hamilt::Output_HContainer output_SR(&SR, ofs_out, sparse_threshold, 8); // std::cout << SR.size_R_loop() << std::endl; // output_SR.write(-2, -1, 0); ofs_out << "STEP: " << 0 << std::endl; diff --git a/source/module_io/write_dmr.cpp b/source/module_io/write_dmr.cpp index 7f2f26cc3a..64060dcabb 100644 --- a/source/module_io/write_dmr.cpp +++ b/source/module_io/write_dmr.cpp @@ -33,23 +33,23 @@ std::string dmr_gen_fname(const int out_type, const int ispin, const bool append return fname; } -void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, const int nbasis, const int istep) +void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, const int istep) { // write the head: ION step number, basis number and R loop number std::ofstream ofs(fname, std::ios::app); ofs << "STEP: " << istep << std::endl; - ofs << "Matrix Dimension of DM(R): " << nbasis << std::endl; + ofs << "Matrix Dimension of DM(R): " << dm_serial->get_nbasis() << std::endl; ofs << "Matrix number of DM(R): " << dm_serial->size_R_loop() << std::endl; // write HR_serial to ofs double sparse_threshold = 1e-10; int precision = 8; - hamilt::Output_HContainer out_dm(dm_serial, nbasis, nbasis, ofs, sparse_threshold, precision); + hamilt::Output_HContainer out_dm(dm_serial, ofs, sparse_threshold, precision); out_dm.write(); ofs.close(); } -void write_dmr(const std::vector*> dmr, const int nbasis,const bool out_csr, const bool out_npz, +void write_dmr(const std::vector*> dmr, const bool out_csr, const bool out_npz, const bool append, const int istep) { if (!out_csr && !out_npz) @@ -60,30 +60,29 @@ void write_dmr(const std::vector*> dmr, const int nba for (int ispin = 0; ispin < dmr.size(); ispin++) { - // gather the parallel matrix to serial matrix + if(out_csr) + { + int nbasis = dmr[ispin]->get_nbasis(); + // gather the parallel matrix to serial matrix #ifdef __MPI - Parallel_Orbitals serialV; - serialV.set_serial(nbasis, nbasis); - serialV.set_atomic_trace(GlobalC::ucell.get_iat2iwt(), GlobalC::ucell.nat, nbasis); - hamilt::HContainer dm_serial(&serialV); - hamilt::gatherParallels(*dmr[ispin], &dm_serial, 0); + Parallel_Orbitals serialV; + serialV.set_serial(nbasis, nbasis); + serialV.set_atomic_trace(GlobalC::ucell.get_iat2iwt(), GlobalC::ucell.nat, nbasis); + hamilt::HContainer dm_serial(&serialV); + hamilt::gatherParallels(*dmr[ispin], &dm_serial, 0); #else - hamilt::HContainer dm_serial(*dmr[ispin]); + hamilt::HContainer dm_serial(*dmr[ispin]); #endif - - if(out_csr) - { if (GlobalV::MY_RANK == 0) { std::string fname = GlobalV::global_out_dir + dmr_gen_fname(1, ispin, append, istep); - write_dmr_csr(fname, &dm_serial, nbasis, istep); + write_dmr_csr(fname, &dm_serial, istep); } } if(out_npz) { - // std::string fname = GlobalV::global_out_dir + dmr_gen_fname(2, ispin, append, istep); - // write_dmr_npz(fname, *dm_serial, istep); + } } } diff --git a/source/module_io/write_dmr.h b/source/module_io/write_dmr.h index 68ae7e842a..1e29ad60fc 100644 --- a/source/module_io/write_dmr.h +++ b/source/module_io/write_dmr.h @@ -26,25 +26,22 @@ std::string dmr_gen_fname(const int out_type, const int ispin, const bool append * * @param fname The name of the file to write the CSR representation to. * @param dm_serial A pointer to the Hamiltonian container. - * @param nbasis The number of basis functions. * @param istep The current step number. */ -void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, const int nbasis, const int istep); +void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, const int istep); /** * Writes DMR to a file. * * @param dm The 2D block parallel matrix representing the density matrix. - * @param nbasis The number of basis functions. * @param out_type The output file type. 1: csr, 2: npz. * @param sparse Whether output the sparse DM. * @param ispin The spin index, starting from 0. * @param append Whether to append the data to an existing file or create a new file. The file name is related to this * flag. * @param istep The ION step, starting from 0. - * @param pv The Parallel_Orbitals object. */ -void write_dmr(const std::vector*> dmr, const int nbasis, const bool out_csr, const bool out_npz, +void write_dmr(const std::vector*> dmr, const bool out_csr, const bool out_npz, const bool append, const int istep); } // namespace ModuleIO From 6336c1a6bc16ada426fa0e445bee2a181f77526e Mon Sep 17 00:00:00 2001 From: pxlxingliang Date: Thu, 27 Jun 2024 13:43:44 +0800 Subject: [PATCH 2/6] correct annotaion --- source/module_io/write_dmr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/module_io/write_dmr.h b/source/module_io/write_dmr.h index 1e29ad60fc..f1d1820072 100644 --- a/source/module_io/write_dmr.h +++ b/source/module_io/write_dmr.h @@ -33,7 +33,7 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co /** * Writes DMR to a file. * - * @param dm The 2D block parallel matrix representing the density matrix. + * @param dmr The 2D block parallel matrix representing the density matrix. The first dimension is the spin index. * @param out_type The output file type. 1: csr, 2: npz. * @param sparse Whether output the sparse DM. * @param ispin The spin index, starting from 0. From 76052eb1e0c82551db3932a27ffd12c2ab778010 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:59:21 +0000 Subject: [PATCH 3/6] [pre-commit.ci lite] apply automatic fixes --- source/module_esolver/esolver_ks_lcao.cpp | 2 +- .../module_hcontainer/output_hcontainer.cpp | 7 +++++-- .../module_hcontainer/output_hcontainer.h | 3 +-- source/module_io/write_dmr.cpp | 12 +++++++----- source/module_io/write_dmr.h | 8 ++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 26e1c4b7ac..75bbde3ba5 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -390,7 +390,7 @@ void ESolver_KS_LCAO::cal_stress(ModuleBase::matrix& stress) //! mohan add 2024-05-11 //------------------------------------------------------------------------------ template -void ESolver_KS_LCAO::after_all_runners(void) +void ESolver_KS_LCAO::after_all_runners() { ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); diff --git a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp index 3f181cd9dd..4a27da237b 100644 --- a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.cpp @@ -13,7 +13,9 @@ namespace hamilt */ template Output_HContainer::Output_HContainer(hamilt::HContainer* hcontainer, - std::ostream& ofs, double sparse_threshold, int precision) + std::ostream& ofs, + double sparse_threshold, + int precision) : _hcontainer(hcontainer), _ofs(ofs), _sparse_threshold(sparse_threshold), _precision(precision) { } @@ -56,7 +58,8 @@ template void Output_HContainer::write_single_R(int rx, int ry, int rz) { this->_hcontainer->fix_R(rx, ry, rz); - ModuleIO::SparseMatrix sparse_matrix = ModuleIO::SparseMatrix(_hcontainer->get_nbasis(), _hcontainer->get_nbasis()); + ModuleIO::SparseMatrix sparse_matrix + = ModuleIO::SparseMatrix(_hcontainer->get_nbasis(), _hcontainer->get_nbasis()); sparse_matrix.setSparseThreshold(this->_sparse_threshold); for (int iap = 0; iap < this->_hcontainer->size_atom_pairs(); ++iap) { diff --git a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h index 6b468cd3ac..42f5d97789 100644 --- a/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h +++ b/source/module_hamilt_lcao/module_hcontainer/output_hcontainer.h @@ -13,8 +13,7 @@ template class Output_HContainer { public: - Output_HContainer(hamilt::HContainer* hcontainer, std::ostream& ofs, - double sparse_threshold, int precision); + Output_HContainer(hamilt::HContainer* hcontainer, std::ostream& ofs, double sparse_threshold, int precision); // write the matrices of all R vectors to the output stream void write(); diff --git a/source/module_io/write_dmr.cpp b/source/module_io/write_dmr.cpp index 64060dcabb..8dbbd6f32f 100644 --- a/source/module_io/write_dmr.cpp +++ b/source/module_io/write_dmr.cpp @@ -49,8 +49,11 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co ofs.close(); } -void write_dmr(const std::vector*> dmr, const bool out_csr, const bool out_npz, - const bool append, const int istep) +void write_dmr(const std::vector*> dmr, + const bool out_csr, + const bool out_npz, + const bool append, + const int istep) { if (!out_csr && !out_npz) { @@ -60,7 +63,7 @@ void write_dmr(const std::vector*> dmr, const bool ou for (int ispin = 0; ispin < dmr.size(); ispin++) { - if(out_csr) + if (out_csr) { int nbasis = dmr[ispin]->get_nbasis(); // gather the parallel matrix to serial matrix @@ -80,9 +83,8 @@ void write_dmr(const std::vector*> dmr, const bool ou } } - if(out_npz) + if (out_npz) { - } } } diff --git a/source/module_io/write_dmr.h b/source/module_io/write_dmr.h index f1d1820072..3eac4d421b 100644 --- a/source/module_io/write_dmr.h +++ b/source/module_io/write_dmr.h @@ -17,9 +17,7 @@ namespace ModuleIO * @param istep The ION step (default: -1), starting from 0. * @return The generated filename as a string. */ -std::string dmr_gen_fname(const int out_type, const int ispin, const bool append = true, - const int istep = -1); - +std::string dmr_gen_fname(const int out_type, const int ispin, const bool append = true, const int istep = -1); /** * Writes HContainer to a csr file. @@ -41,7 +39,9 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co * flag. * @param istep The ION step, starting from 0. */ -void write_dmr(const std::vector*> dmr, const bool out_csr, const bool out_npz, +void write_dmr(const std::vector*> dmr, + const bool out_csr, + const bool out_npz, const bool append, const int istep); } // namespace ModuleIO From e8c0a1e97fce9dc3e9a12edd253a1a9905c60bac Mon Sep 17 00:00:00 2001 From: pxlxingliang Date: Fri, 28 Jun 2024 16:52:48 +0800 Subject: [PATCH 4/6] fix the init of serialV --- source/module_io/write_dmr.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/module_io/write_dmr.cpp b/source/module_io/write_dmr.cpp index 64060dcabb..28f7740e6b 100644 --- a/source/module_io/write_dmr.cpp +++ b/source/module_io/write_dmr.cpp @@ -65,7 +65,9 @@ void write_dmr(const std::vector*> dmr, const bool ou int nbasis = dmr[ispin]->get_nbasis(); // gather the parallel matrix to serial matrix #ifdef __MPI + // This is not safe, because we do not know the comm_2D of dmr. Need refactor the gather function. Parallel_Orbitals serialV; + serialV.init(nbasis, nbasis, nbasis, MPI_COMM_WORLD); serialV.set_serial(nbasis, nbasis); serialV.set_atomic_trace(GlobalC::ucell.get_iat2iwt(), GlobalC::ucell.nat, nbasis); hamilt::HContainer dm_serial(&serialV); From 635d0c597d7278a8b8207f6ae4b54294882f0c99 Mon Sep 17 00:00:00 2001 From: pxlxingliang Date: Fri, 28 Jun 2024 17:22:10 +0800 Subject: [PATCH 5/6] add parallel_2D as the input parameter of write_dmr --- source/module_esolver/esolver_ks_lcao.cpp | 1 + source/module_io/write_dmr.cpp | 4 ++-- source/module_io/write_dmr.h | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 75bbde3ba5..ead283ba9b 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -1061,6 +1061,7 @@ void ESolver_KS_LCAO::after_scf(const int istep) // 2) write density matrix for sparse matrix ModuleIO::write_dmr(dynamic_cast*>(this->pelec)->get_DM()->get_DMR_vector(), + this->orb_con.ParaV, INPUT.out_dm1, false, GlobalV::out_app_flag, diff --git a/source/module_io/write_dmr.cpp b/source/module_io/write_dmr.cpp index 22b20320ce..c4b764dc5f 100644 --- a/source/module_io/write_dmr.cpp +++ b/source/module_io/write_dmr.cpp @@ -50,6 +50,7 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co } void write_dmr(const std::vector*> dmr, + const Parallel_2D& paraV, const bool out_csr, const bool out_npz, const bool append, @@ -68,9 +69,8 @@ void write_dmr(const std::vector*> dmr, int nbasis = dmr[ispin]->get_nbasis(); // gather the parallel matrix to serial matrix #ifdef __MPI - // This is not safe, because we do not know the comm_2D of dmr. Need refactor the gather function. Parallel_Orbitals serialV; - serialV.init(nbasis, nbasis, nbasis, MPI_COMM_WORLD); + serialV.init(nbasis, nbasis, nbasis, paraV.comm_2D); serialV.set_serial(nbasis, nbasis); serialV.set_atomic_trace(GlobalC::ucell.get_iat2iwt(), GlobalC::ucell.nat, nbasis); hamilt::HContainer dm_serial(&serialV); diff --git a/source/module_io/write_dmr.h b/source/module_io/write_dmr.h index 3eac4d421b..98fa587ee4 100644 --- a/source/module_io/write_dmr.h +++ b/source/module_io/write_dmr.h @@ -2,6 +2,7 @@ #define MODULE_IO_WRITE_DMR_H #include "module_basis/module_ao/parallel_orbitals.h" #include "module_hamilt_lcao/module_hcontainer/hcontainer.h" +#include "module_basis/module_ao/parallel_orbitals.h" #include @@ -32,6 +33,7 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co * Writes DMR to a file. * * @param dmr The 2D block parallel matrix representing the density matrix. The first dimension is the spin index. + * @param paraV The parallel 2D object. * @param out_type The output file type. 1: csr, 2: npz. * @param sparse Whether output the sparse DM. * @param ispin The spin index, starting from 0. @@ -40,6 +42,7 @@ void write_dmr_csr(std::string& fname, hamilt::HContainer* dm_serial, co * @param istep The ION step, starting from 0. */ void write_dmr(const std::vector*> dmr, + const Parallel_2D& paraV, const bool out_csr, const bool out_npz, const bool append, From 7664ac826f8c4ed183cd801fca02c1282e905eed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:41:50 +0000 Subject: [PATCH 6/6] [pre-commit.ci lite] apply automatic fixes --- source/module_io/write_dmr.h | 1 - 1 file changed, 1 deletion(-) diff --git a/source/module_io/write_dmr.h b/source/module_io/write_dmr.h index 98fa587ee4..d3cc1b4573 100644 --- a/source/module_io/write_dmr.h +++ b/source/module_io/write_dmr.h @@ -2,7 +2,6 @@ #define MODULE_IO_WRITE_DMR_H #include "module_basis/module_ao/parallel_orbitals.h" #include "module_hamilt_lcao/module_hcontainer/hcontainer.h" -#include "module_basis/module_ao/parallel_orbitals.h" #include