ParSortableList.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ParSortableList.H"
00028 #include "SortableList.H"
00029 #include "Pstream.H"
00030 #include "ListListOps.H"
00031 #include "PstreamReduceOps.H"
00032
00033
00034
00035 template <class Type>
00036 void Foam::ParSortableList<Type>::write
00037 (
00038 const List<Type>& elems,
00039 Ostream& os
00040 ) const
00041 {
00042 os << '(';
00043
00044 forAll(elems, elemI)
00045 {
00046 os << ' ' << elems[elemI];
00047 }
00048 os << ')';
00049 }
00050
00051
00052
00053 template <class Type>
00054 void Foam::ParSortableList<Type>::copyInto
00055 (
00056 const List<Type>& values,
00057 const labelList& indices,
00058 const label fromProcNo,
00059 label& destI,
00060 List<taggedValue>& dest
00061 ) const
00062 {
00063 forAll(values, elemI)
00064 {
00065 taggedValue& tagVal = dest[destI];
00066
00067 tagVal.value() = values[elemI];
00068 tagVal.index() = indices[elemI];
00069 tagVal.procID() = fromProcNo;
00070
00071 destI++;
00072 }
00073 }
00074
00075
00076 template <class Type>
00077 void Foam::ParSortableList<Type>::getPivots
00078 (
00079 const List<Type>& elems,
00080 List<Type>& pivots
00081 ) const
00082 {
00083 pivots.setSize(Pstream::nProcs());
00084
00085 label pivotPos = 0;
00086
00087 forAll(pivots, pivotI)
00088 {
00089 pivots[pivotI] = elems[pivotPos];
00090
00091 pivotPos += elems.size()/Pstream::nProcs();
00092 }
00093 }
00094
00095
00096 template <class Type>
00097 void Foam::ParSortableList<Type>::checkAndSend
00098 (
00099 List<Type>& values,
00100 labelList& indices,
00101 const label bufSize,
00102 const label destProcI
00103 ) const
00104 {
00105 if (destProcI != Pstream::myProcNo())
00106 {
00107 values.setSize(bufSize);
00108 indices.setSize(bufSize);
00109
00110 if (debug)
00111 {
00112 Pout<< "Sending to " << destProcI << " elements:" << values
00113 << endl;
00114 }
00115
00116 {
00117 OPstream toSlave(destProcI);
00118 toSlave << values << indices;
00119 }
00120 }
00121 }
00122
00123
00124
00125
00126
00127 template <class Type>
00128 Foam::ParSortableList<Type>::ParSortableList(const UList<Type>& values)
00129 :
00130 List<Type>(values),
00131 indices_(0),
00132 procs_(0)
00133 {
00134 sort();
00135 }
00136
00137
00138
00139 template <class Type>
00140 Foam::ParSortableList<Type>::ParSortableList(const label size)
00141 :
00142 List<Type>(size),
00143 indices_(0),
00144 procs_(0)
00145 {}
00146
00147
00148
00149
00150
00151 template <class Type>
00152 void Foam::ParSortableList<Type>::sort()
00153 {
00154
00155
00156
00157
00158 label n = this->size();
00159
00160 reduce(n, sumOp<label>());
00161
00162
00163
00164 SortableList<Type> sorted(*this);
00165
00166
00167 labelListList sortedGatherList(Pstream::nProcs());
00168
00169 labelList& pivots = sortedGatherList[Pstream::myProcNo()];
00170
00171 getPivots(sorted, pivots);
00172
00173 if (debug)
00174 {
00175 Pout<< "pivots:";
00176 write(pivots, Pout);
00177 Pout<< endl;
00178 }
00179
00180
00181
00182
00183
00184
00185 Pstream::gatherList(sortedGatherList);
00186
00187 if (Pstream::master())
00188 {
00189 labelList allPivots =
00190 ListListOps::combine<labelList>
00191 (
00192 sortedGatherList,
00193 accessOp<labelList>()
00194 );
00195
00196 SortableList<Type> sortedPivots(allPivots);
00197
00198 if (debug)
00199 {
00200 Pout<< "allPivots:";
00201 write(allPivots, Pout);
00202 Pout<< endl;
00203 }
00204
00205 getPivots(sortedPivots, pivots);
00206 }
00207 Pstream::scatter(pivots);
00208
00209 if (debug)
00210 {
00211 Pout<< "new pivots:";
00212 write(pivots, Pout);
00213 Pout<< endl;
00214 }
00215
00216
00217
00218
00219
00220
00221 label pivotI = 1;
00222 label destProcI = 0;
00223
00224
00225 labelList ownValues(sorted.size());
00226 labelList ownIndices(sorted.size());
00227 label ownI = 0;
00228
00229
00230 labelList sendValues(sorted.size());
00231 labelList sendIndices(sorted.size());
00232 label sendI = 0;
00233
00234 forAll(sorted, sortedI)
00235 {
00236 if ((pivotI < Pstream::nProcs()) && (sorted[sortedI] > pivots[pivotI]))
00237 {
00238 checkAndSend(sendValues, sendIndices, sendI, destProcI);
00239
00240
00241 sendValues.setSize(sorted.size());
00242 sendIndices.setSize(sorted.size());
00243 sendI = 0;
00244
00245 pivotI++;
00246 destProcI++;
00247 }
00248
00249 if (destProcI != Pstream::myProcNo())
00250 {
00251 sendValues[sendI] = sorted[sortedI];
00252 sendIndices[sendI] = sorted.indices()[sortedI];
00253 sendI++;
00254 }
00255 else
00256 {
00257 ownValues[ownI] = sorted[sortedI];
00258 ownIndices[ownI] = sorted.indices()[sortedI];
00259 ownI++;
00260 }
00261 }
00262
00263
00264
00265 if (sendI != 0)
00266 {
00267 checkAndSend(sendValues, sendIndices, sendI, destProcI);
00268 }
00269
00270
00271 ownValues.setSize(ownI);
00272 ownIndices.setSize(ownI);
00273
00274 if (debug & 2)
00275 {
00276 Pout<< "Not sending (to myself) elements "
00277 << ownValues << endl;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287 List<taggedValue> combinedValues(2 * n/Pstream::nProcs());
00288
00289 label combinedI = 0;
00290
00291 for (label procI = 0; procI < Pstream::nProcs(); procI++)
00292 {
00293 if (procI == Pstream::myProcNo())
00294 {
00295 if (debug & 2)
00296 {
00297 Pout<< "Copying from own:" << ownValues << endl;
00298 }
00299
00300
00301 copyInto(ownValues, ownIndices, procI, combinedI, combinedValues);
00302 }
00303 else
00304 {
00305 labelList recValues;
00306 labelList recIndices;
00307
00308 {
00309 if (debug)
00310 {
00311 Pout<< "Receiving from " << procI << endl;
00312 }
00313
00314 IPstream fromSlave(procI);
00315
00316 fromSlave >> recValues >> recIndices;
00317
00318 if (debug & 2)
00319 {
00320 Pout<< "Received from " << procI
00321 << " elements:" << recValues << endl;
00322 }
00323 }
00324
00325 if (debug)
00326 {
00327 Pout<< "Copying starting at:" << combinedI << endl;
00328 }
00329 copyInto(recValues, recIndices, procI, combinedI, combinedValues);
00330 }
00331 }
00332 combinedValues.setSize(combinedI);
00333
00334
00335 Foam::sort(combinedValues);
00336
00337
00338 this->setSize(combinedI);
00339 indices_.setSize(combinedI);
00340 procs_.setSize(combinedI);
00341
00342 forAll(combinedValues, elemI)
00343 {
00344 this->operator[](elemI) = combinedValues[elemI].value();
00345 indices_[elemI] = combinedValues[elemI].index();
00346 procs_[elemI] = combinedValues[elemI].procID();
00347 }
00348 }
00349
00350
00351
Copyright © 2000-2009 OpenCFD Ltd