Skip to content

Commit 76018dc

Browse files
committed
1. add parallel for loop3
1 parent 11893f1 commit 76018dc

File tree

17 files changed

+218
-82
lines changed

17 files changed

+218
-82
lines changed

include/RI/comm/mix/Communicate_Tensors_Map_Judge.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ namespace Communicate_Tensors_Map_Judge
136136
> judge_combine;
137137

138138
std::get<0>(judge_combine.judge_list).resize(std::get<0>(s_list).size());
139-
for(int j=0; j<std::get<0>(s_list).size(); ++j)
139+
for(std::size_t j=0; j<std::get<0>(s_list).size(); ++j)
140140
{
141141
std::get<0>(judge_combine.judge_list)[j].s0 = std::get<0>(std::get<0>(s_list)[j]);
142142
std::get<0>(judge_combine.judge_list)[j].s1 = std::get<1>(std::get<0>(s_list)[j]);
143143
}
144144

145145
std::get<1>(judge_combine.judge_list).resize(std::get<1>(s_list).size());
146-
for(int j=0; j<std::get<1>(s_list).size(); ++j)
146+
for(std::size_t j=0; j<std::get<1>(s_list).size(); ++j)
147147
{
148148
std::get<1>(judge_combine.judge_list)[j].s0 = std::get<0>(std::get<1>(s_list)[j]);
149149
std::get<1>(judge_combine.judge_list)[j].s1 = std::get<1>(std::get<1>(s_list)[j]);

include/RI/distribute/Divide_Atoms.hpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ namespace Divide_Atoms
6767
return cells;
6868
}
6969

70+
/*
71+
traversal_atom_period
72+
In: [A0,A1] [3]
73+
Out: [[A0,0], [A0,1], [A0,2], [A1,0], [A1,1], [A1,2]]
74+
*/
75+
template<typename TA, typename Tcell, std::size_t Ndim>
76+
std::vector<std::pair<TA,std::array<Tcell,Ndim>>> traversal_atom_period(
77+
const std::vector<TA> &atoms,
78+
const std::array<Tcell,Ndim> &period)
79+
{
80+
using TC = std::array<Tcell,Ndim>;
81+
using TAC = std::pair<TA,TC>;
82+
const std::vector<TC> cells_origin = traversal_period(period);
83+
const std::vector<TC> cells = Global_Func::mod_period(cells_origin, period);
84+
std::vector<TAC> atoms_periods;
85+
atoms_periods.reserve( atoms.size() * cells.size() );
86+
for(const TA &atom : atoms)
87+
for(const TC &cell : cells)
88+
atoms_periods.push_back(std::make_pair(atom,cell));
89+
return atoms_periods;
90+
}
91+
7092
template<typename TA>
7193
std::vector<TA> divide_atoms(
7294
const std::size_t group_rank,
@@ -92,17 +114,8 @@ namespace Divide_Atoms
92114
const std::vector<TA> &atoms,
93115
const std::array<Tcell,Ndim> &period)
94116
{
95-
using TC = std::array<Tcell,Ndim>;
96-
using TAC = std::pair<TA,TC>;
97117
const std::vector<TA> atoms_divide = divide_atoms(group_rank, group_size, atoms);
98-
const std::vector<TC> cells_origin = traversal_period(period);
99-
const std::vector<TC> cells = Global_Func::mod_period(cells_origin, period);
100-
std::vector<TAC> atoms_periods_divide;
101-
atoms_periods_divide.reserve( atoms_divide.size() * cells.size() );
102-
for(const TA &atom : atoms_divide)
103-
for(const TC &cell : cells)
104-
atoms_periods_divide.push_back(std::make_pair(atom,cell));
105-
return atoms_periods_divide;
118+
return traversal_atom_period(atoms_divide, period);
106119
}
107120

108121
template<typename TA, typename Tcell, std::size_t Ndim>

include/RI/parallel/Parallel_LRI.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313
#include <array>
1414
#include <map>
15+
#include <unordered_map>
1516
#include <set>
1617

1718
namespace RI
@@ -21,7 +22,7 @@ template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
2122
//template<typename TA, typename Tcell, std::size_t Ndim>
2223
class Parallel_LRI
2324
{
24-
public:
25+
public:
2526
using TC = std::array<Tcell,Ndim>;
2627
using TAC = std::pair<TA,TC>;
2728
using Tatom_pos = std::array<double,Ndim>; // tmp
@@ -31,21 +32,31 @@ class Parallel_LRI
3132
const MPI_Comm &mpi_comm,
3233
const std::map<TA,Tatom_pos> &atoms_pos,
3334
const std::array<Tatom_pos,Ndim> &latvec,
34-
const std::array<Tcell,Ndim> &period) =0;
35+
const std::array<Tcell,Ndim> &period,
36+
const std::set<Label::Aab_Aab> &labels) =0;
3537
// atom_pos[iA][{cell}] = atoms_pos[iA] + \sum_x cell_x * latvec[cell_x]
3638

3739
//template<typename Tdata>
38-
virtual std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
39-
const Label::ab &label,
40-
const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const =0;
40+
// virtual std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
41+
// const Label::ab &label,
42+
// const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const =0;
4143
virtual std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
4244
const std::vector<Label::ab> &label,
4345
const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const =0;
4446

4547
virtual const std::vector<TA >& get_list_Aa01() const =0;
46-
virtual const std::vector<TAC>& get_list_Aa2 () const =0;
47-
virtual const std::vector<TAC>& get_list_Ab01() const =0;
48-
virtual const std::vector<TAC>& get_list_Ab2 () const =0;
48+
virtual const std::vector<TAC>& get_list_Aa2 (const TA &Aa01) const =0;
49+
virtual const std::vector<TAC>& get_list_Ab01(const TA &Aa01, const TAC &Aa2) const =0;
50+
virtual const std::vector<TAC>& get_list_Ab2 (const TA &Aa01, const TAC &Aa2, const TAC &Ab01) const =0;
51+
52+
struct List_A
53+
{
54+
std::vector<TA > a01;
55+
std::vector<TAC> a2;
56+
std::vector<TAC> b01;
57+
std::vector<TAC> b2;
58+
};
59+
std::unordered_map<Label::Aab_Aab, List_A> list_A;
4960
};
5061

5162
}

include/RI/parallel/Parallel_LRI_Equally.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace RI
1313
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
1414
class Parallel_LRI_Equally: public Parallel_LRI<TA,Tcell,Ndim,Tdata>
1515
{
16-
public:
16+
public:
1717
using TC = std::array<Tcell,Ndim>;
1818
using TAC = std::pair<TA,TC>;
1919
using Tatom_pos = std::array<double,Ndim>; // tmp
@@ -22,29 +22,36 @@ class Parallel_LRI_Equally: public Parallel_LRI<TA,Tcell,Ndim,Tdata>
2222
const MPI_Comm &mpi_comm_in,
2323
const std::map<TA,Tatom_pos> &atoms_pos,
2424
const std::array<Tatom_pos,Ndim> &latvec,
25-
const std::array<Tcell,Ndim> &period_in) override;
25+
const std::array<Tcell,Ndim> &period_in,
26+
const std::set<Label::Aab_Aab> &labels) override;
2627

27-
std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
28-
const Label::ab &label,
29-
const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const override;
28+
// std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
29+
// const Label::ab &label,
30+
// const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const override;
3031
std::map<TA,std::map<TAC,Tensor<Tdata>>> comm_tensors_map2(
3132
const std::vector<Label::ab> &label,
3233
const std::map<TA,std::map<TAC,Tensor<Tdata>>> &Ds) const override;
3334

3435
const std::vector<TA >& get_list_Aa01() const override { return this->list_Aa01; }
35-
const std::vector<TAC>& get_list_Aa2 () const override { return this->list_Aa2; }
36-
const std::vector<TAC>& get_list_Ab01() const override { return this->list_Ab01; }
37-
const std::vector<TAC>& get_list_Ab2 () const override { return this->list_Ab2; }
36+
const std::vector<TAC>& get_list_Aa2 (const TA &Aa01) const override { return this->list_Aa2; }
37+
const std::vector<TAC>& get_list_Ab01(const TA &Aa01, const TAC &Aa2) const override { return this->list_Ab01; }
38+
const std::vector<TAC>& get_list_Ab2 (const TA &Aa01, const TAC &Aa2, const TAC &Ab01) const override { return this->list_Ab2; }
3839

39-
// private:
40-
public:
40+
public: // private:
4141
std::vector<TA> list_Aa01;
4242
std::vector<TAC> list_Aa2;
4343
std::vector<TAC> list_Ab01;
4444
std::vector<TAC> list_Ab2;
4545

4646
MPI_Comm mpi_comm;
4747
std::array<Tcell,Ndim> period;
48+
49+
public: // private:
50+
void set_parallel_loop4(
51+
const std::vector<TA> &atoms_vec);
52+
void set_parallel_loop3(
53+
const std::vector<TA> &atoms_vec,
54+
const std::set<Label::Aab_Aab> &labels);
4855
};
4956

5057
}

include/RI/parallel/Parallel_LRI_Equally.hpp

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,84 @@ void Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::set_parallel(
1818
const MPI_Comm &mpi_comm_in,
1919
const std::map<TA,Tatom_pos> &atoms_pos,
2020
const std::array<Tatom_pos,Ndim> &latvec,
21-
const std::array<Tcell,Ndim> &period_in)
21+
const std::array<Tcell,Ndim> &period_in,
22+
const std::set<Label::Aab_Aab> &labels)
2223
{
2324
this->mpi_comm = mpi_comm_in;
2425
this->period = period_in;
26+
const std::vector<TA> atoms_vec = Global_Func::map_key_to_vec(atoms_pos);
27+
28+
this->set_parallel_loop4(atoms_vec);
29+
this->set_parallel_loop3(atoms_vec, labels);
30+
}
2531

32+
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
33+
void Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::set_parallel_loop4(
34+
const std::vector<TA> &atoms_vec)
35+
{
2636
constexpr std::size_t num_index = 4;
27-
const std::vector<TA> atoms_vec = Global_Func::map_key_to_vec(atoms_pos);
2837

29-
std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,TC>>>>
38+
const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,TC>>>>
3039
atoms_split_list = Distribute_Equally::distribute_atoms_periods(
31-
mpi_comm, atoms_vec, period, num_index, false);
40+
this->mpi_comm, atoms_vec, this->period, num_index, false);
3241

3342
this->list_Aa01 = atoms_split_list.first;
3443
this->list_Aa2 = atoms_split_list.second[0];
3544
this->list_Ab01 = atoms_split_list.second[1];
3645
this->list_Ab2 = atoms_split_list.second[2];
3746
}
3847

48+
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
49+
void Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::set_parallel_loop3(
50+
const std::vector<TA> &atoms_vec,
51+
const std::set<Label::Aab_Aab> &labels)
52+
{
53+
constexpr std::size_t num_index = 2;
54+
const std::vector<TAC> atoms_period_vec = Divide_Atoms::traversal_atom_period(atoms_vec, this->period);
55+
56+
const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,TC>>>>
57+
atoms_split_list1 = Distribute_Equally::distribute_atoms_periods(
58+
this->mpi_comm, atoms_vec, this->period, num_index, false);
59+
const std::vector<std::vector<std::pair<TA,TC>>>
60+
atoms_split_list2 = Distribute_Equally::distribute_periods(
61+
this->mpi_comm, atoms_vec, this->period, num_index, false);
62+
for(const Label::Aab_Aab &label : labels)
63+
{
64+
typename Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::List_A &atoms = this->list_A[label];
65+
atoms.a01 = atoms_vec;
66+
atoms.a2 = atoms_period_vec;
67+
atoms.b01 = atoms_period_vec;
68+
atoms.b2 = atoms_period_vec;
69+
switch(label)
70+
{
71+
case Label::Aab_Aab::a01b01_a01b01:
72+
atoms.a2 = atoms_split_list2[0];
73+
atoms.b01 = atoms_split_list2[1];
74+
break;
75+
case Label::Aab_Aab::a01b01_a2b01:
76+
atoms.a01 = atoms_split_list1.first;
77+
atoms.b01 = atoms_split_list1.second[0];
78+
break;
79+
case Label::Aab_Aab::a01b01_a01b2:
80+
atoms.b01 = atoms_split_list1.second[0];
81+
atoms.a01 = atoms_split_list1.first;
82+
break;
83+
case Label::Aab_Aab::a01b01_a2b2:
84+
atoms.a01 = atoms_split_list1.first;
85+
atoms.b2 = atoms_split_list1.second[0];
86+
break;
87+
case Label::Aab_Aab::a01b2_a2b01:
88+
atoms.a01 = atoms_split_list1.first;
89+
atoms.b01 = atoms_split_list1.second[0];
90+
break;
91+
default:
92+
throw std::invalid_argument(std::string(__FILE__)+" line "+std::to_string(__LINE__));
93+
}
94+
}
95+
}
96+
3997

98+
/*
4099
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
41100
auto Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::comm_tensors_map2(
42101
const Label::ab &label,
@@ -62,6 +121,7 @@ auto Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::comm_tensors_map2(
62121
throw std::invalid_argument(std::string(__FILE__)+" line "+std::to_string(__LINE__));
63122
}
64123
}
124+
*/
65125

66126
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
67127
auto Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::comm_tensors_map2(
@@ -97,6 +157,18 @@ auto Parallel_LRI_Equally<TA,Tcell,Ndim,Tdata>::comm_tensors_map2(
97157
}
98158
}
99159

160+
// used in loop3
161+
for(const auto &list_atom : this->list_A)
162+
{
163+
if(flags[0]) std::get<0>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.a01), Global_Func::to_set(list_atom.second.a2) ));
164+
if(flags[1]) std::get<1>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.b01), Global_Func::to_set(list_atom.second.b2) ));
165+
if(flags[2]) std::get<0>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.a01), Global_Func::to_set(list_atom.second.b01) ));
166+
if(flags[3]) std::get<0>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.a01), Global_Func::to_set(list_atom.second.b2) ));
167+
if(flags[4]) std::get<1>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.a2), Global_Func::to_set(list_atom.second.b01) ));
168+
if(flags[5]) std::get<1>(s_list).push_back(std::make_tuple( Global_Func::to_set(list_atom.second.a2), Global_Func::to_set(list_atom.second.b2) ));
169+
}
170+
171+
// used in loop4
100172
if(flags[0]) std::get<0>(s_list).push_back(std::make_tuple( Global_Func::to_set(this->list_Aa01), Global_Func::to_set(this->list_Aa2) ));
101173
if(flags[1]) std::get<1>(s_list).push_back(std::make_tuple( Global_Func::to_set(this->list_Ab01), Global_Func::to_set(this->list_Ab2) ));
102174
if(flags[2]) std::get<0>(s_list).push_back(std::make_tuple( Global_Func::to_set(this->list_Aa01), Global_Func::to_set(this->list_Ab01) ));

include/RI/physics/Exx.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ void Exx<TA,Tcell,Ndim,Tdata>::set_parallel(
2727
this->latvec = latvec_in;
2828
this->period = period_in;
2929

30-
this->lri.set_parallel(this->mpi_comm, this->atoms_pos, this->latvec, this->period);
30+
this->lri.set_parallel(
31+
this->mpi_comm, this->atoms_pos, this->latvec, this->period,
32+
{Label::ab_ab::a0b0_a1b1, Label::ab_ab::a0b0_a1b2, Label::ab_ab::a0b0_a2b1, Label::ab_ab::a0b0_a2b2});
3133
this->flag_finish.stru = true;
3234
//if()
3335
this->post_2D.set_parallel(this->mpi_comm, this->atoms_pos, this->period);
@@ -55,7 +57,7 @@ void Exx<TA,Tcell,Ndim,Tdata>::set_Vs(
5557
{
5658
this->lri.set_tensors_map2(
5759
Vs,
58-
Label::ab::a0b0,
60+
{Label::ab::a0b0},
5961
{{"threshold_filter", threshold_V}},
6062
"Vs_"+save_name_suffix );
6163
this->flag_finish.V = true;
@@ -137,7 +139,7 @@ void Exx<TA,Tcell,Ndim,Tdata>::set_dVs(
137139
for(std::size_t ipos=0; ipos<Npos; ++ipos)
138140
this->lri.set_tensors_map2(
139141
dVs[ipos],
140-
Label::ab::a0b0,
142+
{Label::ab::a0b0},
141143
{{"threshold_filter", threshold_dV}},
142144
"dVs_"+std::to_string(ipos)+"_"+save_name_suffix );
143145
this->flag_finish.dV = true;

include/RI/physics/GW.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ void G0W0<TA,Tcell,Ndim,Tdata>::set_parallel(
2222
this->latvec = latvec_in;
2323
this->period = period_in;
2424

25-
this->lri.set_parallel(this->mpi_comm, this->atoms_pos, this->latvec, this->period);
25+
this->lri.set_parallel(
26+
this->mpi_comm, this->atoms_pos, this->latvec, this->period,
27+
{Label::ab_ab::a0b0_a1b1, Label::ab_ab::a0b0_a1b2, Label::ab_ab::a0b0_a2b1, Label::ab_ab::a0b0_a2b2});
2628
this->flag_finish.stru = true;
2729
//if()
2830
// this->post_2D.set_parallel(this->mpi_comm, this->atoms_pos, this->period);
@@ -59,7 +61,7 @@ void G0W0<TA, Tcell, Ndim, Tdata>::cal_Sigc(
5961
// setup screened Coulomb interaction
6062
this->lri.set_tensors_map2(
6163
Wc_tau,
62-
Label::ab::a0b0,
64+
{Label::ab::a0b0},
6365
{{"threshold_filter", threshold_W}} );
6466

6567
std::vector<std::map<TA, std::map<TAC, Tensor<Tdata>>>> Sigc_vec(1);

include/RI/physics/RPA.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ void RPA<TA,Tcell,Ndim,Tdata>::set_parallel(
2121
const std::array<Tatom_pos,Ndim> &latvec,
2222
const std::array<Tcell,Ndim> &period)
2323
{
24-
this->lri.set_parallel(mpi_comm, atoms_pos, latvec, period);
24+
this->lri.set_parallel(
25+
mpi_comm, atoms_pos, latvec, period,
26+
{Label::ab_ab::a1b1_a2b2, Label::ab_ab::a1b2_a2b1});
2527
this->flag_finish.stru = true;
2628
}
2729

include/RI/ri/CS_Matrix-set.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void CS_Matrix<TA,TC,Tdata>::cal_uplimits(
5858
}
5959

6060
template<typename TA, typename TC, typename Tdata> template<typename T>
61-
CS_Matrix<TA,TC,Tdata>::Uplimits CS_Matrix<TA,TC,Tdata>::cal_uplimits(
61+
typename CS_Matrix<TA,TC,Tdata>::Uplimits CS_Matrix<TA,TC,Tdata>::cal_uplimits(
6262
const Label::ab &label,
6363
const T &Ds)
6464
{
@@ -68,7 +68,7 @@ CS_Matrix<TA,TC,Tdata>::Uplimits CS_Matrix<TA,TC,Tdata>::cal_uplimits(
6868
}
6969

7070
template<typename TA, typename TC, typename Tdata> template<typename T>
71-
CS_Matrix<TA,TC,Tdata>::Uplimits CS_Matrix<TA,TC,Tdata>::cal_uplimits(
71+
typename CS_Matrix<TA,TC,Tdata>::Uplimits CS_Matrix<TA,TC,Tdata>::cal_uplimits(
7272
const std::vector<Label::ab> &label_list,
7373
const T &Ds)
7474
{

include/RI/ri/LRI-cal.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ void LRI<TA,Tcell,Ndim,Tdata>::cal(
6060

6161
for(const TA &Aa01 : this->parallel->get_list_Aa01())
6262
{
63-
for(const TAC &Aa2 : this->parallel->get_list_Aa2())
63+
for(const TAC &Aa2 : this->parallel->get_list_Aa2(Aa01))
6464
{
6565
const Tensor<Tdata> D_a = tools.get_Ds_ab(Label::ab::a, Aa01, Aa2);
6666
if(D_a.empty()) continue;
6767
const Tensor<Tdata> D_a_transpose = LRI_Cal_Aux::tensor3_transpose(D_a);
6868

69-
const std::vector<TAC> &list_Ab01 = this->parallel->get_list_Ab01();
69+
const std::vector<TAC> &list_Ab01 = this->parallel->get_list_Ab01(Aa01, Aa2);
7070
#pragma omp for schedule(dynamic) nowait
7171
for(std::size_t ib01=0; ib01<list_Ab01.size(); ++ib01)
7272
{
@@ -79,7 +79,7 @@ void LRI<TA,Tcell,Ndim,Tdata>::cal(
7979
Ds_b01.reserve(Label::array_ab_ab.size());
8080
Ds_b01_csm.reserve(Label::array_ab_ab.size());
8181

82-
for(const TAC &Ab2 : this->parallel->get_list_Ab2())
82+
for(const TAC &Ab2 : this->parallel->get_list_Ab2(Aa01, Aa2, Ab01))
8383
{
8484
const Tensor<Tdata> D_b = tools.get_Ds_ab(Label::ab::b, Ab01, Ab2);
8585
if(D_b.empty()) continue;

0 commit comments

Comments
 (0)