Skip to content

Commit d0a094b

Browse files
linpeizePeizeLin
andauthored
Feature: add OpenMP in XC_Functional_Libxc::v_xc_libxc() (#7130)
* Feature: add OpenMP in XC_Functional_Libxc::v_xc_libxc() * update OpenMP in XC_Functional_Libxc::v_xc_libxc() * remove OpenMP in XC_Functional_Libxc::v_xc_meta() * update OpenMP in XC_Functional_Libxc::v_xc_libxc() * fix bug in OpenMP XC_Functional_Libxc::v_xc_libxc() --------- Co-authored-by: linpz <linpz@mail.ustc.edu.cn>
1 parent ba3122f commit d0a094b

1 file changed

Lines changed: 73 additions & 19 deletions

File tree

source/source_hamilt/module_xc/xc_functional_libxc_vxc.cpp

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,67 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
9393
std::vector<double> exc ( nrxx );
9494
std::vector<double> vrho ( nrxx * nspin );
9595
std::vector<double> vsigma( nrxx * ((1==nspin)?1:3) );
96+
97+
ModuleBase::timer::start("Libxc","xc_lda/gga_exc_vxc");
9698
switch( func.info->family )
9799
{
98100
case XC_FAMILY_LDA:
99-
// call Libxc function: xc_lda_exc_vxc
100-
xc_lda_exc_vxc( &func, nrxx, rho.data(),
101-
exc.data(), vrho.data() );
101+
{
102+
constexpr int nr_batch_size = 1024;
103+
#ifdef _OPENMP
104+
#pragma omp parallel for schedule(static, nr_batch_size)
105+
#endif
106+
for( int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size )
107+
{
108+
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
109+
const int nrxx_thread = ir_end - ir_start;
110+
xc_lda_exc_vxc(
111+
&func,
112+
nrxx_thread,
113+
rho.data() + ir_start * nspin,
114+
exc.data() + ir_start,
115+
vrho.data() + ir_start * nspin );
116+
}
102117
break;
118+
}
103119
case XC_FAMILY_GGA:
104120
case XC_FAMILY_HYB_GGA:
105-
// call Libxc function: xc_gga_exc_vxc
106-
xc_gga_exc_vxc( &func, nrxx, rho.data(), sigma.data(),
107-
exc.data(), vrho.data(), vsigma.data() );
121+
{
122+
constexpr int nr_batch_size = 1024;
123+
#ifdef _OPENMP
124+
#pragma omp parallel for schedule(static, nr_batch_size)
125+
#endif
126+
for( int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size )
127+
{
128+
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
129+
const int nrxx_thread = ir_end - ir_start;
130+
xc_gga_exc_vxc(
131+
&func,
132+
nrxx_thread,
133+
rho.data() + ir_start * nspin,
134+
sigma.data() + ir_start * ((1==nspin)?1:3),
135+
exc.data() + ir_start,
136+
vrho.data() + ir_start * nspin,
137+
vsigma.data() + ir_start * ((1==nspin)?1:3) );
138+
}
108139
break;
140+
}
109141
default:
142+
{
110143
throw std::domain_error("func.info->family ="+std::to_string(func.info->family)
111144
+" unfinished in "+std::string(__FILE__)+" line "+std::to_string(__LINE__));
112-
break;
145+
146+
}
113147
}
148+
ModuleBase::timer::end("Libxc","xc_lda/gga_exc_vxc");
114149

115150
// added by jghan, 2024-10-10
116151
double factor = 1.0;
117-
if( scaling_factor == nullptr ) { ;
118-
} else
152+
if( scaling_factor )
119153
{
120154
auto pair_factor = scaling_factor->find(func.info->number);
121-
if( pair_factor != scaling_factor->end() ) { factor = pair_factor->second;
122-
}
155+
if( pair_factor != scaling_factor->end() )
156+
{ factor = pair_factor->second; }
123157
}
124158

125159
// time factor is added by jghan, 2024-10-10
@@ -256,20 +290,40 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
256290
#endif
257291
for( int ir=0; ir<nrxx; ++ir )
258292
{
259-
if ( rho[ir*2]<rho_th || sqrt(std::abs(sigma[ir*3]))<grho_th || std::abs(kin_r[ir*2])<tau_th) {
260-
sgn[ir*2] = 0.0;
261-
}
262-
if ( rho[ir*2+1]<rho_th || sqrt(std::abs(sigma[ir*3+2]))<grho_th || std::abs(kin_r[ir*2+1])<tau_th) {
263-
sgn[ir*2+1] = 0.0;
264-
}
293+
if ( rho[ir*2]<rho_th || sqrt(std::abs(sigma[ir*3]))<grho_th || std::abs(kin_r[ir*2])<tau_th)
294+
{ sgn[ir*2] = 0.0; }
295+
if ( rho[ir*2+1]<rho_th || sqrt(std::abs(sigma[ir*3+2]))<grho_th || std::abs(kin_r[ir*2+1])<tau_th)
296+
{ sgn[ir*2+1] = 0.0; }
265297
}
266298
}
267299

268300
for ( xc_func_type &func : funcs )
269301
{
270302
assert(func.info->family == XC_FAMILY_MGGA);
271-
xc_mgga_exc_vxc(&func, nrxx, rho.data(), sigma.data(), sigma.data(),
272-
kin_r.data(), exc.data(), vrho.data(), vsigma.data(), vlapl.data(), vtau.data());
303+
304+
ModuleBase::timer::start("Libxc","xc_mgga_exc_vxc");
305+
constexpr int nr_batch_size = 1024;
306+
#ifdef _OPENMP
307+
#pragma omp parallel for schedule(static, nr_batch_size)
308+
#endif
309+
for( int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size )
310+
{
311+
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
312+
const int nrxx_thread = ir_end - ir_start;
313+
xc_mgga_exc_vxc(
314+
&func,
315+
nrxx_thread,
316+
rho.data() + ir_start * nspin,
317+
sigma.data() + ir_start * ((1==nspin)?1:3),
318+
sigma.data() + ir_start * ((1==nspin)?1:3),
319+
kin_r.data() + ir_start * nspin,
320+
exc.data() + ir_start,
321+
vrho.data() + ir_start * nspin,
322+
vsigma.data() + ir_start * ((1==nspin)?1:3),
323+
vlapl.data() + ir_start * nspin,
324+
vtau.data() + ir_start * nspin);
325+
}
326+
ModuleBase::timer::end("Libxc","xc_mgga_exc_vxc");
273327

274328
//process etxc
275329
for( int is=0; is!=nspin; ++is )

0 commit comments

Comments
 (0)