1 | /*
|
---|
2 | * vmg - a versatile multigrid solver
|
---|
3 | * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
|
---|
4 | *
|
---|
5 | * vmg is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * vmg is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @file domain_decomposition_mpi.cpp
|
---|
21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
---|
22 | * @date Mon Jun 27 12:53:50 2011
|
---|
23 | *
|
---|
24 | * @brief Computes a domain decomposition which separates
|
---|
25 | * the finest grid equally for all processes.
|
---|
26 | *
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifdef HAVE_CONFIG_H
|
---|
30 | #include <libvmg_config.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "base/interface.hpp"
|
---|
34 | #include "comm/comm.hpp"
|
---|
35 | #include "comm/domain_decomposition_mpi.hpp"
|
---|
36 | #include "grid/grid.hpp"
|
---|
37 | #include "grid/multigrid.hpp"
|
---|
38 |
|
---|
39 | using namespace VMG;
|
---|
40 |
|
---|
41 | void DomainDecompositionMPI::Compute(Comm& comm, const Interface& interface, std::map<Index, std::vector<GlobalIndices> >& global)
|
---|
42 | {
|
---|
43 |
|
---|
44 | GlobalIndices global_l;
|
---|
45 | Index pos, remainder, procs;
|
---|
46 | Index last_procs = comm.GlobalProcs();
|
---|
47 |
|
---|
48 | global.clear();
|
---|
49 |
|
---|
50 | for (unsigned int i=0; i<interface.Global().size(); ++i) {
|
---|
51 |
|
---|
52 | for (pos.X() = 0; pos.X() < comm.GlobalProcs().X(); ++pos.X())
|
---|
53 | for (pos.Y() = 0; pos.Y() < comm.GlobalProcs().Y(); ++pos.Y())
|
---|
54 | for (pos.Z() = 0; pos.Z() < comm.GlobalProcs().Z(); ++pos.Z()) {
|
---|
55 |
|
---|
56 | global_l = interface.Global()[i];
|
---|
57 |
|
---|
58 | if (IsActive(global_l.GlobalSize(), pos, procs, comm.GlobalProcs())) {
|
---|
59 |
|
---|
60 | if (i == 0) {
|
---|
61 |
|
---|
62 | remainder = global_l.GlobalSize() % procs;
|
---|
63 |
|
---|
64 | global_l.LocalSize() = global_l.GlobalSize() / procs;
|
---|
65 | for (int j=0; j<3; ++j)
|
---|
66 | if (pos[j] < remainder[j])
|
---|
67 | ++(global_l.LocalSize()[j]);
|
---|
68 |
|
---|
69 | global_l.LocalBegin() = global_l.GlobalBegin() + pos * global_l.LocalSize();
|
---|
70 | for (int j=0; j<3; ++j)
|
---|
71 | if (pos[j] >= remainder[j])
|
---|
72 | global_l.LocalBegin()[j] += remainder[j];
|
---|
73 |
|
---|
74 | global_l.LocalEnd() = global_l.LocalBegin() + global_l.LocalSize();
|
---|
75 |
|
---|
76 | } else {
|
---|
77 |
|
---|
78 | for (int j=0; j<3; ++j) {
|
---|
79 |
|
---|
80 | if (procs[j] == last_procs[j]) {
|
---|
81 |
|
---|
82 | if (global[pos].back().LocalBegin()[j] == global[pos].back().GlobalBegin()[j])
|
---|
83 | global_l.LocalBegin()[j] = global_l.GlobalBegin()[j];
|
---|
84 | else
|
---|
85 | global_l.LocalBegin()[j] = global[pos].back().LocalBegin()[j] / 2;
|
---|
86 |
|
---|
87 | if (global[pos].back().LocalEnd()[j] == global[pos].back().GlobalEnd()[j])
|
---|
88 | global_l.LocalEnd()[j] = global_l.GlobalEnd()[j];
|
---|
89 | else
|
---|
90 | global_l.LocalEnd()[j] = global[pos].back().LocalEnd()[j] / 2;
|
---|
91 |
|
---|
92 | global_l.LocalSize()[j] = global_l.LocalEnd()[j] - global_l.LocalBegin()[j];
|
---|
93 |
|
---|
94 | } else {
|
---|
95 |
|
---|
96 | remainder[j] = global_l.GlobalSize()[j] % procs[j];
|
---|
97 |
|
---|
98 | global_l.LocalSize()[j] = global_l.GlobalSize()[j] / procs[j];
|
---|
99 | if (pos[j] < remainder[j])
|
---|
100 | ++(global_l.LocalSize()[j]);
|
---|
101 |
|
---|
102 | global_l.LocalBegin()[j] = global_l.GlobalBegin()[j] + pos[j] * global_l.LocalSize()[j];
|
---|
103 | if (pos[j] >= remainder[j])
|
---|
104 | global_l.LocalBegin()[j] += remainder[j];
|
---|
105 |
|
---|
106 | global_l.LocalEnd()[j] = global_l.LocalBegin()[j] + global_l.LocalSize()[j];
|
---|
107 |
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }else {
|
---|
112 | global_l.LocalBegin() = 0;
|
---|
113 | global_l.LocalEnd() = 0;
|
---|
114 | global_l.LocalSize() = 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | global[pos].push_back(global_l);
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | last_procs = procs;
|
---|
122 |
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool DomainDecompositionMPI::IsActive(const Index& size_global, const Index& pos, Index& procs, const Index& max_procs)
|
---|
127 | {
|
---|
128 | bool is_active = true;
|
---|
129 | const int points_min = 5;
|
---|
130 |
|
---|
131 | procs = size_global / points_min + 1;
|
---|
132 |
|
---|
133 | for (int i=0; i<3; ++i) {
|
---|
134 | procs[i] = std::min(procs[i], max_procs[i]);
|
---|
135 | is_active &= pos[i] < procs[i];
|
---|
136 | }
|
---|
137 |
|
---|
138 | return is_active;
|
---|
139 | }
|
---|
140 |
|
---|
141 | void DomainDecompositionMPI::FineToCoarse(Comm& comm, int& begin, int& end, int levels)
|
---|
142 | {
|
---|
143 | int last_point = end - 1;
|
---|
144 |
|
---|
145 | for (int i=0; i<levels; ++i) {
|
---|
146 |
|
---|
147 | if (begin % 2 == 0)
|
---|
148 | begin /= 2;
|
---|
149 | else
|
---|
150 | begin = (begin+1) / 2;
|
---|
151 |
|
---|
152 | if (last_point % 2 == 0)
|
---|
153 | last_point /= 2;
|
---|
154 | else
|
---|
155 | last_point = (last_point-1) / 2;
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | end = last_point + 1;
|
---|
160 | }
|
---|