ProcessorTopology.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ProcessorTopology.H"
27 #include "ListOps.H"
28 #include "Pstream.H"
29 #include "commSchedule.H"
30 #include "boolList.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class Container, class ProcPatch>
36 (
37  const label nProcs,
38  const Container& patches
39 )
40 {
41  // Determine number of processor neighbours and max neighbour id.
42 
43  label nNeighbours = 0;
44 
45  label maxNb = 0;
46 
47  boolList isNeighbourProc(nProcs, false);
48 
49  forAll(patches, patchi)
50  {
51  const typename Container::const_reference patch = patches[patchi];
52 
53  if (isA<ProcPatch>(patch))
54  {
55  const ProcPatch& procPatch =
56  refCast<const ProcPatch>(patch);
57 
58  label pNeighbProcNo = procPatch.neighbProcNo();
59 
60  if (!isNeighbourProc[pNeighbProcNo])
61  {
62  nNeighbours++;
63 
64  maxNb = max(maxNb, procPatch.neighbProcNo());
65 
66  isNeighbourProc[pNeighbProcNo] = true;
67  }
68  }
69  }
70 
71  labelList neighbours(nNeighbours, -1);
72 
73  nNeighbours = 0;
74 
75  forAll(isNeighbourProc, proci)
76  {
77  if (isNeighbourProc[proci])
78  {
79  neighbours[nNeighbours++] = proci;
80  }
81  }
82 
83  procPatchMap_.setSize(maxNb + 1);
84  procPatchMap_ = -1;
85 
86  forAll(patches, patchi)
87  {
88  const typename Container::const_reference patch = patches[patchi];
89 
90  if (isA<ProcPatch>(patch))
91  {
92  const ProcPatch& procPatch =
93  refCast<const ProcPatch>(patch);
94 
95  // Construct reverse map
96  procPatchMap_[procPatch.neighbProcNo()] = patchi;
97  }
98  }
99 
100  return neighbours;
101 }
102 
103 
104 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
105 
106 template<class Container, class ProcPatch>
108 (
109  const Container& patches,
110  const label comm
111 )
112 :
113  labelListList(Pstream::nProcs(comm)),
114  patchSchedule_(2*patches.size())
115 {
116  if (Pstream::parRun())
117  {
118  // Fill my 'slot' with my neighbours
119  operator[](Pstream::myProcNo(comm)) =
120  procNeighbours(this->size(), patches);
121 
122  // Distribute to all processors
123  Pstream::gatherList(*this, Pstream::msgType(), comm);
124  Pstream::scatterList(*this, Pstream::msgType(), comm);
125  }
126 
127  if (Pstream::parRun() && Pstream::defaultCommsType == Pstream::scheduled)
128  {
129  label patchEvali = 0;
130 
131  // 1. All non-processor patches
132  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133 
134  forAll(patches, patchi)
135  {
136  if (!isA<ProcPatch>(patches[patchi]))
137  {
138  patchSchedule_[patchEvali].patch = patchi;
139  patchSchedule_[patchEvali++].init = true;
140  patchSchedule_[patchEvali].patch = patchi;
141  patchSchedule_[patchEvali++].init = false;
142  }
143  }
144 
145  // 2. All processor patches
146  // ~~~~~~~~~~~~~~~~~~~~~~~~
147 
148  // Determine the schedule for all. Insert processor pair once
149  // to determine the schedule. Each processor pair stands for both
150  // send and receive.
151  label nComms = 0;
152  forAll(*this, proci)
153  {
154  nComms += operator[](proci).size();
155  }
156  DynamicList<labelPair> comms(nComms);
157 
158  forAll(*this, proci)
159  {
160  const labelList& nbrs = operator[](proci);
161 
162  forAll(nbrs, i)
163  {
164  if (proci < nbrs[i])
165  {
166  comms.append(labelPair(proci, nbrs[i]));
167  }
168  }
169  }
170  comms.shrink();
171 
172  // Determine a schedule.
173  labelList mySchedule
174  (
176  (
177  Pstream::nProcs(comm),
178  comms
179  ).procSchedule()[Pstream::myProcNo(comm)]
180  );
181 
182  forAll(mySchedule, iter)
183  {
184  label commI = mySchedule[iter];
185 
186  // Get the other processor
187  label nb = comms[commI][0];
188  if (nb == Pstream::myProcNo(comm))
189  {
190  nb = comms[commI][1];
191  }
192  label patchi = procPatchMap_[nb];
193 
194  if (Pstream::myProcNo(comm) > nb)
195  {
196  patchSchedule_[patchEvali].patch = patchi;
197  patchSchedule_[patchEvali++].init = true;
198  patchSchedule_[patchEvali].patch = patchi;
199  patchSchedule_[patchEvali++].init = false;
200  }
201  else
202  {
203  patchSchedule_[patchEvali].patch = patchi;
204  patchSchedule_[patchEvali++].init = false;
205  patchSchedule_[patchEvali].patch = patchi;
206  patchSchedule_[patchEvali++].init = true;
207  }
208  }
209  }
210  else
211  {
212  patchSchedule_ = nonBlockingSchedule(patches);
213  }
214 }
215 
216 
217 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
218 
219 template<class Container, class ProcPatch>
222 (
223  const Container& patches
224 )
225 {
226  lduSchedule patchSchedule(2*patches.size());
227 
228  label patchEvali = 0;
229 
230  // 1. All non-processor patches
231  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232 
233  // Have evaluate directly after initEvaluate. Could have them separated
234  // as long as they're not intermingled with processor patches since
235  // then e.g. any reduce parallel traffic would interfere with the
236  // processor swaps.
237 
238  forAll(patches, patchi)
239  {
240  if (!isA<ProcPatch>(patches[patchi]))
241  {
242  patchSchedule[patchEvali].patch = patchi;
243  patchSchedule[patchEvali++].init = true;
244  patchSchedule[patchEvali].patch = patchi;
245  patchSchedule[patchEvali++].init = false;
246  }
247  }
248 
249  // 2. All processor patches
250  // ~~~~~~~~~~~~~~~~~~~~~~~~
251 
252  // 2a. initEvaluate
253  forAll(patches, patchi)
254  {
255  if (isA<ProcPatch>(patches[patchi]))
256  {
257  patchSchedule[patchEvali].patch = patchi;
258  patchSchedule[patchEvali++].init = true;
259  }
260  }
261 
262  // 2b. evaluate
263  forAll(patches, patchi)
264  {
265  if (isA<ProcPatch>(patches[patchi]))
266  {
267  patchSchedule[patchEvali].patch = patchi;
268  patchSchedule[patchEvali++].init = false;
269  }
270  }
271 
272  return patchSchedule;
273 }
274 
275 
276 // ************************************************************************* //
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Determines processor-processor connection. After instantiation contains on all processors the process...
Various functions to operate on Lists.
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
ProcessorTopology(const Container &patches, const label comm)
Construct from boundaryMesh.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
static lduSchedule nonBlockingSchedule(const Container &patches)
Calculate non-blocking (i.e. unscheduled) schedule.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
Pair< label > labelPair
Label pair.
Definition: labelPair.H:48
List< label > labelList
A List of labels.
Definition: labelList.H:56
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:240
Determines the order in which a set of processors should communicate with one another.
Definition: commSchedule.H:65
label patchi