Actual source code: Numbering.hh
1: #ifndef included_ALE_Numbering_hh
2: #define included_ALE_Numbering_hh
4: #ifndef included_ALE_SectionCompletion_hh
5: #include <SectionCompletion.hh>
6: #endif
9: namespace ALE {
10: // We have a dichotomy between \emph{types}, describing the structure of objects,
11: // and \emph{concepts}, describing the role these objects play in the algorithm.
12: // Below we identify concepts with potential implementing types.
13: //
14: // Concept Type
15: // ------- ----
16: // Overlap Sifter
17: // Atlas ConstantSection, UniformSection
18: // Numbering UniformSection
19: // GlobalOrder UniformSection
20: //
21: // We will use factory types to create objects which satisfy a given concept.
22: template<typename Point_, typename Value_ = int, typename Alloc_ = malloc_allocator<Point_> >
23: class Numbering : public UniformSection<Point_, Value_, 1, Alloc_> {
24: public:
25: typedef UniformSection<Point_, Value_, 1, Alloc_> base_type;
26: typedef typename base_type::point_type point_type;
27: typedef typename base_type::value_type value_type;
28: typedef typename base_type::atlas_type atlas_type;
29: protected:
30: int _localSize;
31: int *_offsets;
32: std::map<int, point_type> _invOrder;
33: public:
34: Numbering(MPI_Comm comm, const int debug = 0) : UniformSection<Point_, Value_, 1, Alloc_>(comm, debug), _localSize(0) {
35: this->_offsets = new int[this->commSize()+1];
36: this->_offsets[0] = 0;
37: };
38: virtual ~Numbering() {
39: delete [] this->_offsets;
40: };
41: public: // Sizes
42: int getLocalSize() const {return this->_localSize;};
43: void setLocalSize(const int size) {this->_localSize = size;};
44: int getGlobalSize() const {return this->_offsets[this->commSize()];};
45: int getGlobalOffset(const int p) const {return this->_offsets[p];};
46: const int *getGlobalOffsets() const {return this->_offsets;};
47: void setGlobalOffsets(const int offsets[]) {
48: for(int p = 0; p <= this->commSize(); ++p) {
49: this->_offsets[p] = offsets[p];
50: }
51: };
52: public: // Indices
53: virtual int getIndex(const point_type& point) {
54: const value_type& idx = this->restrictPoint(point)[0];
55: if (idx >= 0) {
56: return idx;
57: }
58: return -(idx+1);
59: };
60: virtual void setIndex(const point_type& point, const int index) {this->updatePoint(point, &index);};
61: virtual bool isLocal(const point_type& point) {return this->restrictPoint(point)[0] >= 0;};
62: virtual bool isRemote(const point_type& point) {return this->restrictPoint(point)[0] < 0;};
63: point_type getPoint(const int& index) {return this->_invOrder[index];};
64: void setPoint(const int& index, const point_type& point) {this->_invOrder[index] = point;};
65: };
66: template<typename Point_, typename Value_ = ALE::Point>
67: class GlobalOrder : public UniformSection<Point_, Value_> {
68: public:
69: typedef UniformSection<Point_, Value_> base_type;
70: typedef typename base_type::point_type point_type;
71: typedef typename base_type::value_type value_type;
72: typedef typename base_type::atlas_type atlas_type;
73: protected:
74: int _localSize;
75: int *_offsets;
76: public:
77: GlobalOrder(MPI_Comm comm, const int debug = 0) : UniformSection<Point_, Value_>(comm, debug), _localSize(0) {
78: this->_offsets = new int[this->commSize()+1];
79: this->_offsets[0] = 0;
80: };
81: ~GlobalOrder() {
82: delete [] this->_offsets;
83: };
84: public: // Sizes
85: int getLocalSize() const {return this->_localSize;};
86: void setLocalSize(const int size) {this->_localSize = size;};
87: int getGlobalSize() const {return this->_offsets[this->commSize()];};
88: int getGlobalOffset(const int p) const {return this->_offsets[p];};
89: const int *getGlobalOffsets() const {return this->_offsets;};
90: void setGlobalOffsets(const int offsets[]) {
91: for(int p = 0; p <= this->commSize(); ++p) {
92: this->_offsets[p] = offsets[p];
93: }
94: };
95: public: // Indices
96: virtual int getIndex(const point_type& p) {
97: const int idx = this->restrictPoint(p)[0].prefix;
98: if (idx >= 0) {
99: return idx;
100: }
101: return -(idx+1);
102: };
103: virtual void setIndex(const point_type& p, const int index) {
104: const value_type idx(index, this->restrictPoint(p)[0].index);
105: this->updatePoint(p, &idx);
106: };
107: virtual bool isLocal(const point_type& p) {return this->restrictPoint(p)[0].prefix >= 0;};
108: virtual bool isRemote(const point_type& p) {return this->restrictPoint(p)[0].prefix < 0;};
109: };
110: template<typename Bundle_, typename Value_ = int, typename Alloc_ = typename Bundle_::alloc_type>
111: class NumberingFactory : ALE::ParallelObject {
112: public:
113: typedef Bundle_ bundle_type;
114: typedef Alloc_ alloc_type;
115: typedef Value_ value_type;
116: typedef typename bundle_type::sieve_type sieve_type;
117: typedef typename bundle_type::point_type point_type;
118: typedef typename bundle_type::rank_type rank_type;
119: typedef typename bundle_type::send_overlap_type send_overlap_type;
120: typedef typename bundle_type::recv_overlap_type recv_overlap_type;
121: typedef Numbering<point_type, value_type, alloc_type> numbering_type;
122: typedef typename alloc_type::template rebind<value_type>::other value_alloc_type;
123: typedef std::map<bundle_type*, std::map<std::string, std::map<int, Obj<numbering_type> > > > numberings_type;
124: typedef GlobalOrder<point_type> order_type;
125: typedef typename order_type::value_type oValue_type;
126: typedef typename alloc_type::template rebind<oValue_type>::other oValue_alloc_type;
127: typedef std::map<bundle_type*, std::map<std::string, Obj<order_type> > > orders_type;
128: protected:
129: numberings_type _localNumberings;
130: numberings_type _numberings;
131: orders_type _orders;
132: const value_type _unknownNumber;
133: const oValue_type _unknownOrder;
134: protected:
135: NumberingFactory(MPI_Comm comm, const int debug = 0) : ALE::ParallelObject(comm, debug), _unknownNumber(-1), _unknownOrder(-1, 0) {};
136: public:
137: ~NumberingFactory() {};
138: public:
139: static const Obj<NumberingFactory>& singleton(MPI_Comm comm, const int debug, bool cleanup = false) {
140: static Obj<NumberingFactory> *_singleton = NULL;
142: if (cleanup) {
143: if (debug) {std::cout << "Destroying NumberingFactory" << std::endl;}
144: if (_singleton) {delete _singleton;}
145: _singleton = NULL;
146: } else if (_singleton == NULL) {
147: if (debug) {std::cout << "Creating new NumberingFactory" << std::endl;}
148: _singleton = new Obj<NumberingFactory>();
149: *_singleton = new NumberingFactory(comm, debug);
150: }
151: return *_singleton;
152: };
153: void clear() {
154: this->_localNumberings.clear();
155: this->_numberings.clear();
156: this->_orders.clear();
157: };
158: public: // Dof ordering
159: template<typename Section_>
160: void orderPointNew(const Obj<Section_>& section, const Obj<sieve_type>& sieve, const typename Section_::point_type& point, value_type& offset, value_type& bcOffset, const Obj<send_overlap_type>& sendOverlap = NULL) {
161: const typename Section_::chart_type& chart = section->getChart();
162: int& idx = section->getIndex(point);
164: // If the point does not exist in the chart, throw an error
165: if (chart.count(point) == 0) {
166: throw ALE::Exception("Unknown point in ordering");
167: }
168: // If the point has not been ordered
169: if (idx == -1) {
170: // Recurse to its cover
171: const Obj<typename sieve_type::coneSequence>& cone = sieve->cone(point);
172: typename sieve_type::coneSequence::iterator end = cone->end();
174: for(typename sieve_type::coneSequence::iterator c_iter = cone->begin(); c_iter != end; ++c_iter) {
175: if (this->_debug > 1) {std::cout << " Recursing to " << *c_iter << std::endl;}
176: this->orderPoint(section, sieve, *c_iter, offset, bcOffset, sendOverlap);
177: }
178: const int dim = section->getFiberDimension(point);
179: const int cDim = section->getConstraintDimension(point);
180: const int fDim = dim - cDim;
182: // If the point has constrained variables
183: if (cDim) {
184: if (this->_debug > 1) {std::cout << " Ordering boundary point " << point << " at " << bcOffset << std::endl;}
185: section->setIndexBC(point, bcOffset);
186: bcOffset += cDim;
187: }
188: // If the point has free variables
189: if (fDim) {
190: bool number = true;
192: // Maybe use template specialization here
193: if (!sendOverlap.isNull() && sendOverlap->capContains(point)) {
194: const Obj<typename send_overlap_type::supportSequence>& ranks = sendOverlap->support(point);
196: for(typename send_overlap_type::supportSequence::iterator r_iter = ranks->begin(); r_iter != ranks->end(); ++r_iter) {
197: if (this->commRank() > *r_iter) {
198: number = false;
199: break;
200: }
201: }
202: }
203: if (number) {
204: if (this->_debug > 1) {std::cout << " Ordering point " << point << " at " << offset << std::endl;}
205: section->setIndex(point, offset);
206: offset += dim;
207: } else {
208: if (this->_debug > 1) {std::cout << " Ignoring ghost point " << point << std::endl;}
209: }
210: }
211: }
212: };
213: template<typename Section_>
214: void orderPoint(const Obj<Section_>& section, const Obj<sieve_type>& sieve, const typename Section_::point_type& point, value_type& offset, value_type& bcOffset, const Obj<send_overlap_type>& sendOverlap = NULL) {
215: const Obj<typename Section_::atlas_type>& atlas = section->getAtlas();
216: const Obj<typename sieve_type::coneSequence>& cone = sieve->cone(point);
217: typename sieve_type::coneSequence::iterator end = cone->end();
218: typename Section_::index_type idx = section->getAtlas()->restrictPoint(point)[0];
219: const value_type& dim = idx.prefix;
220: const typename Section_::index_type defaultIdx(0, -1);
222: if (atlas->getChart().count(point) == 0) {
223: idx = defaultIdx;
224: }
225: if (idx.index == -1) {
226: for(typename sieve_type::coneSequence::iterator c_iter = cone->begin(); c_iter != end; ++c_iter) {
227: if (this->_debug > 1) {std::cout << " Recursing to " << *c_iter << std::endl;}
228: this->orderPoint(section, sieve, *c_iter, offset, bcOffset, sendOverlap);
229: }
230: if (dim > 0) {
231: bool number = true;
233: // Maybe use template specialization here
234: if (!sendOverlap.isNull() && sendOverlap->capContains(point)) {
235: const Obj<typename send_overlap_type::supportSequence>& ranks = sendOverlap->support(point);
237: for(typename send_overlap_type::supportSequence::iterator r_iter = ranks->begin(); r_iter != ranks->end(); ++r_iter) {
238: if (this->commRank() > *r_iter) {
239: number = false;
240: break;
241: }
242: }
243: }
244: if (number) {
245: if (this->_debug > 1) {std::cout << " Ordering point " << point << " at " << offset << std::endl;}
246: idx.index = offset;
247: atlas->updatePoint(point, &idx);
248: offset += dim;
249: } else {
250: if (this->_debug > 1) {std::cout << " Ignoring ghost point " << point << std::endl;}
251: }
252: } else if (dim < 0) {
253: if (this->_debug > 1) {std::cout << " Ordering boundary point " << point << " at " << bcOffset << std::endl;}
254: idx.index = bcOffset;
255: atlas->updatePoint(point, &idx);
256: bcOffset += dim;
257: }
258: }
259: };
260: template<typename Section_>
261: void orderPatch(const Obj<Section_>& section, const Obj<sieve_type>& sieve, const Obj<send_overlap_type>& sendOverlap = NULL, const value_type offset = 0, const value_type bcOffset = -2) {
262: const typename Section_::chart_type& chart = section->getChart();
263: int off = offset;
264: int bcOff = bcOffset;
266: if (this->_debug > 1) {std::cout << "Ordering patch" << std::endl;}
267: for(typename Section_::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
268: if (this->_debug > 1) {std::cout << "Ordering closure of point " << *p_iter << std::endl;}
269: this->orderPoint(section, sieve, *p_iter, off, bcOff, sendOverlap);
270: }
271: for(typename Section_::chart_type::const_iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
272: const int& idx = section->getIndex(*p_iter);
274: if (idx < 0) {
275: if (this->_debug > 1) {std::cout << "Correcting boundary offset of point " << *p_iter << std::endl;}
276: section->setIndex(*p_iter, off - (idx + 2));
277: }
278: }
279: };
280: public: // Numbering
281: // Number all local points
282: // points in the overlap are only numbered by the owner with the lowest rank
283: template<typename Sequence_>
284: void constructLocalNumbering(const Obj<numbering_type>& numbering, const Obj<send_overlap_type>& sendOverlap, const Obj<Sequence_>& points) {
285: const int debug = sendOverlap->debug();
286: int localSize = 0;
288: if (debug) {std::cout << "["<<numbering->commRank()<<"] Constructing local numbering" << std::endl;}
289: numbering->setFiberDimension(points, 1);
290: for(typename Sequence_::iterator l_iter = points->begin(); l_iter != points->end(); ++l_iter) {
291: value_type val;
293: if (debug) {std::cout << "["<<numbering->commRank()<<"] Checking point " << *l_iter << std::endl;}
294: if (sendOverlap->capContains(*l_iter)) {
295: const Obj<typename send_overlap_type::traits::supportSequence>& sendPatches = sendOverlap->support(*l_iter);
296: int minRank = sendOverlap->commSize();
298: for(typename send_overlap_type::traits::supportSequence::iterator p_iter = sendPatches->begin(); p_iter != sendPatches->end(); ++p_iter) {
299: if (*p_iter < minRank) minRank = *p_iter;
300: }
301: if (minRank < sendOverlap->commRank()) {
302: if (debug) {std::cout << "["<<numbering->commRank()<<"] remote point, on proc " << minRank << std::endl;}
303: val = this->_unknownNumber;
304: } else {
305: if (debug) {std::cout << "["<<numbering->commRank()<<"] local point" << std::endl;}
306: val = localSize++;
307: }
308: } else {
309: if (debug) {std::cout << "["<<numbering->commRank()<<"] local point" << std::endl;}
310: val = localSize++;
311: }
312: if (debug) {std::cout << "["<<numbering->commRank()<<"] has number " << val << std::endl;}
313: numbering->updatePoint(*l_iter, &val);
314: }
315: if (debug) {std::cout << "["<<numbering->commRank()<<"] local points" << std::endl;}
316: numbering->setLocalSize(localSize);
317: };
318: // Order all local points
319: // points in the overlap are only ordered by the owner with the lowest rank
320: template<typename Sequence_, typename Section_>
321: void constructLocalOrder(const Obj<order_type>& order, const Obj<send_overlap_type>& sendOverlap, const Sequence_& points, const Obj<Section_>& section) {
322: int localSize = 0;
324: ///std::cout << "["<<order->commRank()<<"] Constructing local ordering" << std::endl;
325: for(typename Sequence_::const_iterator l_iter = points.begin(); l_iter != points.end(); ++l_iter) {
326: order->setFiberDimension(*l_iter, 1);
327: }
328: for(typename Sequence_::const_iterator l_iter = points.begin(); l_iter != points.end(); ++l_iter) {
329: oValue_type val;
331: ///std::cout << "["<<order->commRank()<<"] Checking point " << *l_iter << std::endl;
332: if (sendOverlap->capContains(*l_iter)) {
333: const Obj<typename send_overlap_type::traits::supportSequence>& sendPatches = sendOverlap->support(*l_iter);
334: int minRank = sendOverlap->commSize();
336: for(typename send_overlap_type::traits::supportSequence::iterator p_iter = sendPatches->begin(); p_iter != sendPatches->end(); ++p_iter) {
337: if (*p_iter < minRank) minRank = *p_iter;
338: }
339: if (minRank < sendOverlap->commRank()) {
340: ///std::cout << "["<<order->commRank()<<"] remote point, on proc " << minRank << std::endl;
341: val = this->_unknownOrder;
342: } else {
343: ///std::cout << "["<<order->commRank()<<"] local point" << std::endl;
344: val.prefix = localSize;
345: val.index = section->getConstrainedFiberDimension(*l_iter);
346: }
347: } else {
348: ///std::cout << "["<<order->commRank()<<"] local point" << std::endl;
349: val.prefix = localSize;
350: val.index = section->getConstrainedFiberDimension(*l_iter);
351: }
352: ///std::cout << "["<<order->commRank()<<"] has offset " << val.prefix << " and size " << val.index << std::endl;
353: localSize += val.index;
354: order->updatePoint(*l_iter, &val);
355: }
356: ///std::cout << "["<<order->commRank()<<"] local size" << std::endl;
357: order->setLocalSize(localSize);
358: };
359: // Calculate process offsets
360: template<typename Numbering>
361: void calculateOffsets(const Obj<Numbering>& numbering) {
362: int localSize = numbering->getLocalSize();
363: int *offsets = new int[numbering->commSize()+1];
365: offsets[0] = 0;
366: MPI_Allgather(&localSize, 1, MPI_INT, &(offsets[1]), 1, MPI_INT, numbering->comm());
367: for(int p = 2; p <= numbering->commSize(); p++) {
368: offsets[p] += offsets[p-1];
369: }
370: numbering->setGlobalOffsets(offsets);
371: delete [] offsets;
372: };
373: // Update local offsets based upon process offsets
374: // TODO: The sequence should be const, but LabelSifter has no proper const_iterator
375: template<typename Numbering, typename Sequence>
376: void updateOrder(const Obj<Numbering>& numbering, Sequence& points) {
377: const typename Numbering::value_type val = numbering->getGlobalOffset(numbering->commRank());
379: for(typename Sequence::const_iterator l_iter = points.begin(); l_iter != points.end(); ++l_iter) {
380: if (numbering->isLocal(*l_iter)) {
381: numbering->updateAddPoint(*l_iter, &val);
382: }
383: }
384: };
385: // Communicate numbers in the overlap
386: void completeNumbering(const Obj<numbering_type>& numbering, const Obj<send_overlap_type>& sendOverlap, const Obj<recv_overlap_type>& recvOverlap, bool allowDuplicates = false) {
387: typedef Field<send_overlap_type, int, Section<point_type, value_type, value_alloc_type> > send_section_type;
388: typedef Field<recv_overlap_type, int, Section<point_type, value_type, value_alloc_type> > recv_section_type;
389: typedef typename ALE::DiscreteSieve<point_type, alloc_type> dsieve_type;
390: typedef typename ALE::Topology<int, dsieve_type, alloc_type> dtopology_type;
391: typedef typename ALE::New::SectionCompletion<dtopology_type, int, alloc_type> completion;
392: const Obj<send_section_type> sendSection = new send_section_type(numbering->comm(), this->debug());
393: const Obj<recv_section_type> recvSection = new recv_section_type(numbering->comm(), sendSection->getTag(), this->debug());
394: const int debug = sendOverlap->debug();
396: if (debug) {std::cout << "["<<numbering->commRank()<<"] Completing numbering" << std::endl;}
397: completion::completeSection(sendOverlap, recvOverlap, numbering->getAtlas(), numbering, sendSection, recvSection);
398: #if 1
399: const Obj<typename recv_overlap_type::traits::baseSequence> rPoints = recvOverlap->base();
401: for(typename recv_overlap_type::traits::baseSequence::iterator p_iter = rPoints->begin(); p_iter != rPoints->end(); ++p_iter) {
402: const Obj<typename recv_overlap_type::coneSequence>& ranks = recvOverlap->cone(*p_iter);
403: const typename recv_overlap_type::target_type& localPoint = *p_iter;
405: for(typename recv_overlap_type::coneSequence::iterator r_iter = ranks->begin(); r_iter != ranks->end(); ++r_iter) {
406: const typename recv_overlap_type::target_type& remotePoint = r_iter.color();
407: const int rank = *r_iter;
408: const Obj<typename recv_section_type::section_type>& section = recvSection->getSection(rank);
409: const typename recv_section_type::value_type *values = section->restrictPoint(remotePoint);
411: if (section->getFiberDimension(remotePoint) == 0) continue;
412: if (debug) {std::cout << "["<<numbering->commRank()<<"] local point " << localPoint << " remote point " << remotePoint << " number " << values[0] << std::endl;}
413: if (values[0] >= 0) {
414: if (debug) {std::cout << "["<<numbering->commRank()<<"] local point " << localPoint << " dim " << numbering->getAtlas()->getFiberDimension(localPoint) << std::endl;}
415: if (numbering->isLocal(localPoint) && !allowDuplicates) {
416: ostringstream msg;
417: msg << "["<<numbering->commRank()<<"]Multiple indices for local point " << localPoint << " remote point " << remotePoint << " from " << rank << " with index " << values[0];
418: throw ALE::Exception(msg.str().c_str());
419: }
420: if (numbering->getAtlas()->getFiberDimension(localPoint) == 0) {
421: ostringstream msg;
422: msg << "["<<numbering->commRank()<<"]Unexpected local point " << localPoint << " remote point " << remotePoint << " from " << rank << " with index " << values[0];
423: throw ALE::Exception(msg.str().c_str());
424: }
425: int val = -(values[0]+1);
426: numbering->updatePoint(localPoint, &val);
427: }
428: }
429: }
430: #else
431: const typename recv_section_type::sheaf_type& patches = recvSection->getPatches();
433: for(typename recv_section_type::sheaf_type::const_iterator p_iter = patches.begin(); p_iter != patches.end(); ++p_iter) {
434: const typename recv_section_type::patch_type& rPatch = p_iter->first;
435: const Obj<typename recv_section_type::section_type>& section = recvSection->getSection(rPatch);
436: const typename recv_section_type::chart_type& points = section->getChart();
438: for(typename recv_section_type::chart_type::const_iterator r_iter = points.begin(); r_iter != points.end(); ++r_iter) {
439: const typename recv_section_type::point_type& point = *r_iter;
440: const typename recv_section_type::value_type *values = section->restrictPoint(point);
442: if (section->getFiberDimension(point) == 0) continue;
443: if (values[0] >= 0) {
444: if (numbering->isLocal(point) && !allowDuplicates) {
445: ostringstream msg;
446: msg << "["<<numbering->commRank()<<"]Multiple indices for point " << point << " from " << rPatch << " with index " << values[0];
447: throw ALE::Exception(msg.str().c_str());
448: }
449: if (numbering->getAtlas()->getFiberDimension(point) == 0) {
450: ostringstream msg;
451: msg << "["<<numbering->commRank()<<"]Unexpected point " << point << " from " << rPatch << " with index " << values[0];
452: throw ALE::Exception(msg.str().c_str());
453: }
454: int val = -(values[0]+1);
455: numbering->updatePoint(point, &val);
456: }
457: }
458: }
459: #endif
460: };
461: // Communicate (size,offset)s in the overlap
462: void completeOrder(const Obj<order_type>& order, const Obj<send_overlap_type>& sendOverlap, const Obj<recv_overlap_type>& recvOverlap, bool allowDuplicates = false) {
463: typedef Field<send_overlap_type, int, Section<point_type, oValue_type, oValue_alloc_type> > send_section_type;
464: typedef Field<recv_overlap_type, int, Section<point_type, oValue_type, oValue_alloc_type> > recv_section_type;
465: typedef ConstantSection<point_type, int, alloc_type> constant_sizer;
466: typedef typename ALE::DiscreteSieve<point_type, alloc_type> dsieve_type;
467: typedef typename ALE::Topology<int, dsieve_type, alloc_type> dtopology_type;
468: typedef typename ALE::New::SectionCompletion<dtopology_type, int, alloc_type> completion;
469: const Obj<send_section_type> sendSection = new send_section_type(order->comm(), this->debug());
470: const Obj<recv_section_type> recvSection = new recv_section_type(order->comm(), sendSection->getTag(), this->debug());
472: ///std::cout << "["<<order->commRank()<<"] Completing ordering" << std::endl;
473: completion::completeSection(sendOverlap, recvOverlap, order->getAtlas(), order, sendSection, recvSection);
474: Obj<typename recv_overlap_type::traits::baseSequence> recvPoints = recvOverlap->base();
476: for(typename recv_overlap_type::traits::baseSequence::iterator p_iter = recvPoints->begin(); p_iter != recvPoints->end(); ++p_iter) {
477: if (!order->hasPoint(*p_iter)) {
478: order->setFiberDimension(*p_iter, 1);
479: order->updatePoint(*p_iter, &this->_unknownOrder);
480: }
481: }
482: for(typename recv_overlap_type::traits::baseSequence::iterator p_iter = recvPoints->begin(); p_iter != recvPoints->end(); ++p_iter) {
483: const Obj<typename recv_overlap_type::traits::coneSequence>& ranks = recvOverlap->cone(*p_iter);
484: const typename recv_overlap_type::target_type& localPoint = *p_iter;
486: for(typename recv_overlap_type::traits::coneSequence::iterator r_iter = ranks->begin(); r_iter != ranks->end(); ++r_iter) {
487: const typename recv_overlap_type::target_type& remotePoint = r_iter.color();
488: const int rank = *r_iter;
489: const Obj<typename recv_section_type::section_type>& section = recvSection->getSection(rank);
490: const typename recv_section_type::value_type *values = section->restrictPoint(remotePoint);
492: if (section->getFiberDimension(remotePoint) == 0) continue;
493: ///std::cout << "["<<order->commRank()<<"] local point " << localPoint << " remote point " << remotePoint<<"("<<rank<<")" << " offset " << values[0].prefix << " and size " << values[0].index << std::endl;
494: if (values[0].index == 0) continue;
495: if (values[0].prefix >= 0) {
496: if (order->isLocal(localPoint)) {
497: if (!allowDuplicates) {
498: ostringstream msg;
499: msg << "["<<order->commRank()<<"]Multiple indices for local point " << localPoint << " remote point " << remotePoint << " from " << rank << " with index " << values[0];
500: throw ALE::Exception(msg.str().c_str());
501: }
502: continue;
503: }
504: const oValue_type val(-(values[0].prefix+1), values[0].index);
505: order->updatePoint(localPoint, &val);
506: } else {
507: if (order->isLocal(localPoint)) continue;
508: order->updatePoint(localPoint, values);
509: }
510: }
511: }
512: };
513: // Construct a full global numbering
514: template<typename Sequence>
515: void constructNumbering(const Obj<numbering_type>& numbering, const Obj<send_overlap_type>& sendOverlap, const Obj<recv_overlap_type>& recvOverlap, const Obj<Sequence>& points) {
516: this->constructLocalNumbering(numbering, sendOverlap, points);
517: this->calculateOffsets(numbering);
518: this->updateOrder(numbering, *points.ptr());
519: this->completeNumbering(numbering, sendOverlap, recvOverlap);
520: };
521: // Construct a full global order
522: template<typename Sequence, typename Section>
523: void constructOrder(const Obj<order_type>& order, const Obj<send_overlap_type>& sendOverlap, const Obj<recv_overlap_type>& recvOverlap, const Sequence& points, const Obj<Section>& section) {
524: this->constructLocalOrder(order, sendOverlap, points, section);
525: this->calculateOffsets(order);
526: this->updateOrder(order, points);
527: this->completeOrder(order, sendOverlap, recvOverlap);
528: };
529: template<typename Sequence, typename Section>
530: void constructOrder(const Obj<order_type>& order, const Obj<send_overlap_type>& sendOverlap, const Obj<recv_overlap_type>& recvOverlap, const Obj<Sequence>& points, const Obj<Section>& section) {
531: this->constructLocalOrder(order, sendOverlap, *points.ptr(), section);
532: this->calculateOffsets(order);
533: this->updateOrder(order, *points.ptr());
534: this->completeOrder(order, sendOverlap, recvOverlap);
535: };
536: public:
537: // Construct the inverse map from numbers to points
538: // If we really need this, then we should consider using a label
539: void constructInverseOrder(const Obj<numbering_type>& numbering) {
540: const typename numbering_type::chart_type& chart = numbering->getChart();
542: for(typename numbering_type::chart_type::iterator p_iter = chart.begin(); p_iter != chart.end(); ++p_iter) {
543: numbering->setPoint(numbering->getIndex(*p_iter), *p_iter);
544: }
545: };
546: public: // Real interface
547: template<typename ABundle_>
548: const Obj<numbering_type>& getLocalNumbering(const Obj<ABundle_>& bundle, const int depth) {
549: if ((this->_localNumberings.find(bundle.ptr()) == this->_localNumberings.end()) ||
550: (this->_localNumberings[bundle.ptr()].find("depth") == this->_localNumberings[bundle.ptr()].end()) ||
551: (this->_localNumberings[bundle.ptr()]["depth"].find(depth) == this->_localNumberings[bundle.ptr()]["depth"].end())) {
552: Obj<numbering_type> numbering = new numbering_type(bundle->comm(), bundle->debug());
553: Obj<send_overlap_type> sendOverlap = new send_overlap_type(bundle->comm(), bundle->debug());
555: this->constructLocalNumbering(numbering, sendOverlap, bundle->depthStratum(depth));
556: if (this->_debug) {std::cout << "Creating new local numbering: ptr " << bundle.ptr() << " depth " << depth << std::endl;}
557: this->_localNumberings[bundle.ptr()]["depth"][depth] = numbering;
558: } else {
559: if (this->_debug) {std::cout << "Using old local numbering: ptr " << bundle.ptr() << " depth " << depth << std::endl;}
560: }
561: return this->_localNumberings[bundle.ptr()]["depth"][depth];
562: };
563: template<typename ABundle_>
564: const Obj<numbering_type>& getNumbering(const Obj<ABundle_>& bundle, const int depth) {
565: if ((this->_numberings.find(bundle.ptr()) == this->_numberings.end()) ||
566: (this->_numberings[bundle.ptr()].find("depth") == this->_numberings[bundle.ptr()].end()) ||
567: (this->_numberings[bundle.ptr()]["depth"].find(depth) == this->_numberings[bundle.ptr()]["depth"].end())) {
568: bundle->constructOverlap();
569: Obj<numbering_type> numbering = new numbering_type(bundle->comm(), bundle->debug());
570: Obj<send_overlap_type> sendOverlap = bundle->getSendOverlap();
571: Obj<recv_overlap_type> recvOverlap = bundle->getRecvOverlap();
573: // std::cout << "["<<bundle->commRank()<<"]Creating new numbering: fixed depth value " << depth << std::endl;
574: this->constructNumbering(numbering, sendOverlap, recvOverlap, bundle->depthStratum(depth));
575: if (this->_debug) {std::cout << "Creating new numbering: depth " << depth << std::endl;}
576: this->_numberings[bundle.ptr()]["depth"][depth] = numbering;
577: // } else {
578: // std::cout << "["<<bundle->commRank()<<"]Using old numbering: fixed depth value " << depth << std::endl;
579: }
580: return this->_numberings[bundle.ptr()]["depth"][depth];
581: };
582: template<typename ABundle_>
583: const Obj<numbering_type>& getNumbering(const Obj<ABundle_>& bundle, const std::string& labelname, const int value) {
584: if ((this->_numberings.find(bundle.ptr()) == this->_numberings.end()) ||
585: (this->_numberings[bundle.ptr()].find(labelname) == this->_numberings[bundle.ptr()].end()) ||
586: (this->_numberings[bundle.ptr()][labelname].find(value) == this->_numberings[bundle.ptr()][labelname].end())) {
587: bundle->constructOverlap();
588: Obj<numbering_type> numbering = new numbering_type(bundle->comm(), bundle->debug());
589: Obj<send_overlap_type> sendOverlap = bundle->getSendOverlap();
590: Obj<recv_overlap_type> recvOverlap = bundle->getRecvOverlap();
592: if (this->_debug) {std::cout << "["<<bundle->commRank()<<"]Creating new numbering: " << labelname << " value " << value << std::endl;}
593: this->constructNumbering(numbering, sendOverlap, recvOverlap, bundle->getLabelStratum(labelname, value));
594: this->_numberings[bundle.ptr()][labelname][value] = numbering;
595: } else {
596: if (this->_debug) {std::cout << "["<<bundle->commRank()<<"]Using old numbering: " << labelname << " value " << value << std::endl;}
597: }
598: return this->_numberings[bundle.ptr()][labelname][value];
599: };
600: template<typename ABundle_, typename Section_>
601: const Obj<order_type>& getGlobalOrder(const Obj<ABundle_>& bundle, const std::string& name, const Obj<Section_>& section) {
602: if ((this->_orders.find(bundle.ptr()) == this->_orders.end()) ||
603: (this->_orders[bundle.ptr()].find(name) == this->_orders[bundle.ptr()].end())) {
604: bundle->constructOverlap();
605: Obj<order_type> order = new order_type(bundle->comm(), bundle->debug());
606: Obj<send_overlap_type> sendOverlap = bundle->getSendOverlap();
607: Obj<recv_overlap_type> recvOverlap = bundle->getRecvOverlap();
609: if (this->_debug) {std::cout << "["<<bundle->commRank()<<"]Creating new global order: " << name << std::endl;}
610: this->constructOrder(order, sendOverlap, recvOverlap, section->getChart(), section);
611: this->_orders[bundle.ptr()][name] = order;
612: } else {
613: if (this->_debug) {std::cout << "["<<bundle->commRank()<<"]Using old global order: " << name << std::endl;}
614: }
615: return this->_orders[bundle.ptr()][name];
616: };
617: template<typename ABundle_>
618: void setGlobalOrder(const Obj<ABundle_>& bundle, const std::string& name, const Obj<order_type>& order) {
619: this->_orders[bundle.ptr()][name] = order;
620: };
621: };
622: }
623: #endif