Skip to content

Commit 22c985e

Browse files
[Refactor] Remove unnecessary manual MPI_Reduce in hsolver dav code (#7314)
reduce_pool template already has single precision (std::complex<float>) instantiation with MPI_IN_PLACE, so manually creating swap buffers and calling MPI_Reduce for complex types is unnecessary. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent 4ef3500 commit 22c985e

5 files changed

Lines changed: 13 additions & 78 deletions

File tree

source/source_hsolver/diago_dav_subspace.cpp

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#ifdef __MPI
2121
#include <mpi.h>
22+
#include "source_base/parallel_comm.h"
2223
#endif
2324

2425
using namespace hsolver;
@@ -583,62 +584,9 @@ void Diago_DavSubspace<T, Device>::cal_elem(const int& dim,
583584
// Only on dsp hardware need an extra space to reduce data
584585
mtfunc::dsp_dav_subspace_reduce(hcc, scc, nbase, this->nbase_x, this->notconv, this->diag_comm.comm);
585586
#else
586-
auto* swap = new T[notconv * this->nbase_x];
587-
588-
syncmem_complex_op()(swap, hcc + nbase * this->nbase_x, notconv * this->nbase_x);
589-
590-
if (std::is_same<T, double>::value)
591-
{
592-
Parallel_Reduce::reduce_pool(hcc + nbase * this->nbase_x, notconv * this->nbase_x);
593-
Parallel_Reduce::reduce_pool(scc + nbase * this->nbase_x, notconv * this->nbase_x);
594-
}
595-
else
596-
{
597-
if (base_device::get_current_precision(swap) == "single")
598-
{
599-
MPI_Reduce(swap,
600-
hcc + nbase * this->nbase_x,
601-
notconv * this->nbase_x,
602-
MPI_COMPLEX,
603-
MPI_SUM,
604-
0,
605-
this->diag_comm.comm);
606-
}
607-
else
608-
{
609-
MPI_Reduce(swap,
610-
hcc + nbase * this->nbase_x,
611-
notconv * this->nbase_x,
612-
MPI_DOUBLE_COMPLEX,
613-
MPI_SUM,
614-
0,
615-
this->diag_comm.comm);
616-
}
617-
618-
syncmem_complex_op()(swap, scc + nbase * this->nbase_x, notconv * this->nbase_x);
619-
620-
if (base_device::get_current_precision(swap) == "single")
621-
{
622-
MPI_Reduce(swap,
623-
scc + nbase * this->nbase_x,
624-
notconv * this->nbase_x,
625-
MPI_COMPLEX,
626-
MPI_SUM,
627-
0,
628-
this->diag_comm.comm);
629-
}
630-
else
631-
{
632-
MPI_Reduce(swap,
633-
scc + nbase * this->nbase_x,
634-
notconv * this->nbase_x,
635-
MPI_DOUBLE_COMPLEX,
636-
MPI_SUM,
637-
0,
638-
this->diag_comm.comm);
639-
}
640-
}
641-
delete[] swap;
587+
assert(this->diag_comm.comm == POOL_WORLD);
588+
Parallel_Reduce::reduce_pool(hcc + nbase * this->nbase_x, notconv * this->nbase_x);
589+
Parallel_Reduce::reduce_pool(scc + nbase * this->nbase_x, notconv * this->nbase_x);
642590
#endif
643591
}
644592
#endif

source/source_hsolver/diago_david.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "source_hsolver/kernels/hegvd_op.h"
88
#include "source_base/kernels/math_kernel_op.h"
9+
#include "source_base/parallel_comm.h"
910

1011

1112
using namespace hsolver;
@@ -613,25 +614,8 @@ void DiagoDavid<T, Device>::cal_elem(const int& dim,
613614
{
614615
ModuleBase::matrixTranspose_op<T, Device>()(nbase_x, nbase_x, hcc, hcc);
615616

616-
auto* swap = new T[notconv * nbase_x];
617-
syncmem_complex_op()(swap, hcc + nbase * nbase_x, notconv * nbase_x);
618-
if (std::is_same<T, double>::value)
619-
{
620-
Parallel_Reduce::reduce_pool(hcc + nbase * nbase_x, notconv * nbase_x);
621-
}
622-
else
623-
{
624-
if (base_device::get_current_precision(swap) == "single") {
625-
MPI_Reduce(swap, hcc + nbase * nbase_x, notconv * nbase_x, MPI_COMPLEX, MPI_SUM, 0, diag_comm.comm);
626-
}
627-
else {
628-
MPI_Reduce(swap, hcc + nbase * nbase_x, notconv * nbase_x, MPI_DOUBLE_COMPLEX, MPI_SUM, 0, diag_comm.comm);
629-
}
630-
631-
}
632-
delete[] swap;
633-
634-
// Parallel_Reduce::reduce_complex_double_pool( hcc + nbase * nbase_x, notconv * nbase_x );
617+
assert(diag_comm.comm == POOL_WORLD);
618+
Parallel_Reduce::reduce_pool(hcc + nbase * nbase_x, notconv * nbase_x);
635619

636620
ModuleBase::matrixTranspose_op<T, Device>()(nbase_x, nbase_x, hcc, hcc);
637621
}

source/source_hsolver/test/diago_david_float_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include"source_hsolver/diago_david.h"
22
#include"source_hsolver/diago_iter_assist.h"
3+
#include "source_base/parallel_comm.h"
34
#include"source_pw/module_pwdft/hamilt_pw.h"
45
#include"diago_mock.h"
56
#include "source_psi/psi.h"
@@ -85,7 +86,7 @@ class DiagoDavPrepare
8586
phm = new hamilt::HamiltPW<std::complex<float>>(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
8687

8788
#ifdef __MPI
88-
const hsolver::diag_comm_info comm_info = {MPI_COMM_WORLD, mypnum, nprocs};
89+
const hsolver::diag_comm_info comm_info = {POOL_WORLD, mypnum, nprocs};
8990
#else
9091
const hsolver::diag_comm_info comm_info = {mypnum, nprocs};
9192
#endif

source/source_hsolver/test/diago_david_real_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include"source_hsolver/diago_david.h"
22
#include"source_hsolver/diago_iter_assist.h"
3+
#include "source_base/parallel_comm.h"
34
#include"source_pw/module_pwdft/hamilt_pw.h"
45
#include"diago_mock.h"
56
#include "source_psi/psi.h"
@@ -84,7 +85,7 @@ class DiagoDavPrepare
8485
phm = new hamilt::HamiltPW<double>(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
8586

8687
#ifdef __MPI
87-
const hsolver::diag_comm_info comm_info = {MPI_COMM_WORLD, mypnum, nprocs};
88+
const hsolver::diag_comm_info comm_info = {POOL_WORLD, mypnum, nprocs};
8889
#else
8990
const hsolver::diag_comm_info comm_info = {mypnum, nprocs};
9091
#endif

source/source_hsolver/test/diago_david_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include"source_hsolver/diago_david.h"
22
#include"source_hsolver/diago_iter_assist.h"
3+
#include "source_base/parallel_comm.h"
34
#include"source_pw/module_pwdft/hamilt_pw.h"
45
#include"diago_mock.h"
56
#include "source_psi/psi.h"
@@ -89,7 +90,7 @@ class DiagoDavPrepare
8990
phm = new hamilt::HamiltPW<std::complex<double>>(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
9091

9192
#ifdef __MPI
92-
const hsolver::diag_comm_info comm_info = {MPI_COMM_WORLD, mypnum, nprocs};
93+
const hsolver::diag_comm_info comm_info = {POOL_WORLD, mypnum, nprocs};
9394
#else
9495
const hsolver::diag_comm_info comm_info = {mypnum, nprocs};
9596
#endif

0 commit comments

Comments
 (0)