Skip to content

Commit 3af6673

Browse files
committed
1. add Distribute_Equally::distribute_periods()
1 parent 4b2f3c3 commit 3af6673

File tree

4 files changed

+123
-28
lines changed

4 files changed

+123
-28
lines changed

include/RI/distribute/Distribute_Equally.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Distribute_Equally
3232
const std::size_t num_index,
3333
const bool flag_task_repeatable);
3434

35-
// 第0维按照atoms、剩余维按照{atom,period},尽可能均分
35+
// 第0维按照atoms、剩余维按照{atom,period},尽可能均分
3636
template<typename TA, typename Tcell, std::size_t Ndim>
3737
extern std::pair<std::vector<TA>,
3838
std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
@@ -42,6 +42,22 @@ namespace Distribute_Equally
4242
const std::array<Tcell,Ndim> &period,
4343
const std::size_t num_index,
4444
const bool flag_task_repeatable);
45+
46+
47+
48+
// num_index 维张量,每维皆为{atom,cell}。
49+
// 返回值为每维上本进程被分配到的值列表,[i]为第i维。
50+
// 当 任务数<进程数 时,部分进程会被分配到重复任务。if(!flag_task_repeatable),重复进程只保留一个,其他进程将任务删空。
51+
52+
// 全部维按照{atom,period},尽可能均分
53+
template<typename TA, typename Tcell, std::size_t Ndim>
54+
extern std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>
55+
distribute_periods(
56+
const MPI_Comm &mpi_comm,
57+
const std::vector<TA> &atoms,
58+
const std::array<Tcell,Ndim> &period,
59+
const std::size_t num_index,
60+
const bool flag_task_repeatable);
4561
}
4662

4763
}

include/RI/distribute/Distribute_Equally.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace RI
1414

1515
namespace Distribute_Equally
1616
{
17+
// 全部维按照atoms,尽可能均分
1718
template<typename TA, typename Tcell, std::size_t Ndim>
1819
std::pair<std::vector<TA>,
1920
std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
@@ -53,6 +54,7 @@ namespace Distribute_Equally
5354
return atoms_split_list;
5455
}
5556

57+
// 第0维按照atoms、剩余维按照{atom,period},尽可能均分
5658
template<typename TA, typename Tcell, std::size_t Ndim>
5759
std::pair<std::vector<TA>,
5860
std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
@@ -91,6 +93,39 @@ namespace Distribute_Equally
9193
period);
9294
return atoms_split_list;
9395
}
96+
97+
// 全部维按照{atom,period},尽可能均分
98+
template<typename TA, typename Tcell, std::size_t Ndim>
99+
extern std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>
100+
distribute_periods(
101+
const MPI_Comm &mpi_comm,
102+
const std::vector<TA> &atoms,
103+
const std::array<Tcell,Ndim> &period,
104+
const std::size_t num_index,
105+
const bool flag_task_repeatable)
106+
{
107+
assert(num_index>=1);
108+
using TAC = std::pair<TA,std::array<Tcell,Ndim>>;
109+
110+
const std::size_t task_size_period = atoms.size() * std::accumulate( period.begin(), period.end(), 1, std::multiplies<Tcell>() );
111+
std::vector<std::size_t> task_sizes(num_index, task_size_period);
112+
const std::vector<std::tuple<MPI_Wrapper::mpi_comm, std::size_t, std::size_t>>
113+
comm_color_sizes = Split_Processes::split_all(mpi_comm, task_sizes);
114+
115+
std::vector<std::vector<TAC>> atoms_split_list(num_index);
116+
117+
if(!flag_task_repeatable)
118+
if(RI::MPI_Wrapper::mpi_get_rank(std::get<0>(comm_color_sizes.back())()))
119+
return atoms_split_list;
120+
121+
for(std::size_t i=0; i<num_index; ++i)
122+
atoms_split_list[i] = Divide_Atoms::divide_atoms_periods(
123+
std::get<1>(comm_color_sizes[i+1]),
124+
std::get<2>(comm_color_sizes[i+1]),
125+
atoms,
126+
period);
127+
return atoms_split_list;
128+
}
94129
}
95130

96131
}

unittests/Test_All.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace Test_All
3636

3737
Distribute_Equally_Test::test_distribute_atoms(argc, argv);
3838
Distribute_Equally_Test::test_distribute_atoms_periods(argc, argv);
39+
Distribute_Equally_Test::test_distribute_atoms_repeatable(argc, argv);
40+
Distribute_Equally_Test::test_distribute_periods(argc, argv);
3941

4042
Divide_Atoms_Test::test_divide_atoms();
4143
Divide_Atoms_Test::test_divide_atoms_with_period();

unittests/distribute/Distribute_Equally-test.hpp

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ namespace Distribute_Equally_Test
3535
/*
3636
mpirun -n 7
3737
rank 0
38-
0| 1|
39-
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
38+
0| 1|
39+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
4040
rank 1
4141
0| 1|
4242
{ 2, 0 }| { 2, -1 }| { 3, 0 }| { 3, -1 }|
@@ -79,26 +79,26 @@ namespace Distribute_Equally_Test
7979
/*
8080
mpirun -n 7
8181
rank 0
82-
0| 1| 2|
83-
{ 0, 0 }| { 0, -1 }| { 1, 0 }|
82+
0| 1| 2|
83+
{ 0, 0 }| { 0, -1 }| { 1, 0 }|
8484
rank 1
85-
0| 1| 2|
86-
{ 1, -1 }| { 2, 0 }| { 2, -1 }|
85+
0| 1| 2|
86+
{ 1, -1 }| { 2, 0 }| { 2, -1 }|
8787
rank 2
88-
0| 1| 2|
89-
{ 3, 0 }| { 3, -1 }|
88+
0| 1| 2|
89+
{ 3, 0 }| { 3, -1 }|
9090
rank 3
91-
0| 1| 2|
92-
{ 4, 0 }| { 4, -1 }|
91+
0| 1| 2|
92+
{ 4, 0 }| { 4, -1 }|
9393
rank 4
94-
3| 4|
95-
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
94+
3| 4|
95+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
9696
rank 5
97-
3| 4|
98-
{ 2, 0 }| { 2, -1 }| { 3, 0 }|
97+
3| 4|
98+
{ 2, 0 }| { 2, -1 }| { 3, 0 }|
9999
rank 6
100-
3| 4|
101-
{ 3, -1 }| { 4, 0 }| { 4, -1 }|
100+
3| 4|
101+
{ 3, -1 }| { 4, 0 }| { 4, -1 }|
102102
*/
103103

104104
void test_distribute_atoms_repeatable(int argc, char *argv[])
@@ -122,32 +122,74 @@ namespace Distribute_Equally_Test
122122

123123
MPI_Finalize();
124124
};
125-
125+
126126
//test_repeatable(true);
127127
/*
128128
mpirun -n 4
129129
rank 0
130-
0|
130+
0|
131131
rank 1
132-
0|
132+
0|
133133
rank 2
134-
0|
134+
0|
135135
rank 3
136-
1|
136+
1|
137137
rank 4
138-
1|
138+
1|
139139
*/
140140

141141
test_repeatable(false);
142142
/*
143143
mpirun -n 4
144144
rank 0
145-
0|
146-
rank 1
147-
rank 2
145+
0|
146+
rank 1
147+
rank 2
148148
rank 3
149-
1|
150-
rank 4
149+
1|
150+
rank 4
151151
*/
152152
}
153+
154+
void test_distribute_periods(int argc, char *argv[])
155+
{
156+
MPI_Init(&argc, &argv);
157+
158+
const std::vector<int> atoms = {0,1,2,3,4};
159+
const std::array<int,1> period = {2};
160+
const std::size_t num_index = 2;
161+
const std::vector<std::vector<std::pair<int,std::array<int,1>>>>
162+
atoms_split_list
163+
= RI::Distribute_Equally::distribute_periods(MPI_COMM_WORLD, atoms, period, num_index, false);
164+
165+
std::ofstream ofs("out."+std::to_string(RI::MPI_Wrapper::mpi_get_rank(MPI_COMM_WORLD)));
166+
for(const auto &a : atoms_split_list)
167+
ofs<<a<<std::endl;
168+
169+
MPI_Finalize();
170+
}
171+
/*
172+
mpirun -n 7
173+
rank 0
174+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
175+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
176+
rank 1
177+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
178+
{ 2, 0 }| { 2, -1 }| { 3, 0 }|
179+
rank 2
180+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }|
181+
{ 3, -1 }| { 4, 0 }| { 4, -1 }|
182+
rank 3
183+
{ 2, 0 }| { 2, -1 }| { 3, 0 }|
184+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }| { 2, 0 }|
185+
rank 4
186+
{ 2, 0 }| { 2, -1 }| { 3, 0 }|
187+
{ 2, -1 }| { 3, 0 }| { 3, -1 }| { 4, 0 }| { 4, -1 }|
188+
rank 5
189+
{ 3, -1 }| { 4, 0 }| { 4, -1 }|
190+
{ 0, 0 }| { 0, -1 }| { 1, 0 }| { 1, -1 }| { 2, 0 }|
191+
rank 6
192+
{ 3, -1 }| { 4, 0 }| { 4, -1 }|
193+
{ 2, -1 }| { 3, 0 }| { 3, -1 }| { 4, 0 }| { 4, -1 }|
194+
*/
153195
}

0 commit comments

Comments
 (0)