Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_UniqueGlobalIndexer.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef __Panzer_UniqueGlobalIndexer_hpp__
44 #define __Panzer_UniqueGlobalIndexer_hpp__
45 
46 #include <vector>
47 #include <string>
48 
49 #include <unordered_map> // a hash table for buildLocalIds()
50 
51 #include "Teuchos_RCP.hpp"
52 #include "Teuchos_Comm.hpp"
53 
54 #include "Phalanx_KokkosDeviceTypes.hpp"
55 
56 namespace panzer {
57 
58 // Forward declaration.
59 template <typename LocalOrdinalT> class ConnManagerBase;
60 
62 public:
64  virtual ~UniqueGlobalIndexerBase() = 0;
65 
68  virtual Teuchos::RCP<Teuchos::Comm<int> > getComm() const = 0;
69 
72  virtual int getNumFields() const = 0;
73 
88  virtual int getFieldNum(const std::string & str) const = 0;
89 
92  virtual void getFieldOrder(std::vector<std::string> & fieldOrder) const = 0;
93 
103  virtual const std::string & getFieldString(int num) const = 0;
104 
107  virtual void getElementBlockIds(std::vector<std::string> & elementBlockIds) const = 0;
108 
111  virtual bool fieldInBlock(const std::string & field, const std::string & block) const = 0;
112 
115  virtual const std::vector<int> & getBlockFieldNumbers(const std::string & blockId) const = 0;
116 
120  virtual const std::vector<int> & getGIDFieldOffsets(const std::string & blockId,int fieldNum) const = 0;
121 
134  virtual const std::pair<std::vector<int>,std::vector<int> > &
135  getGIDFieldOffsets_closure(const std::string & blockId, int fieldNum,
136  int subcellDim,int subcellId) const = 0;
137 
143  virtual int getElementBlockGIDCount(const std::string & blockId) const = 0;
144 
150  virtual int getElementBlockGIDCount(const std::size_t & blockIndex) const = 0;
151 };
152 
153 template <typename LocalOrdinalT,typename GlobalOrdinalT>
155 public:
157  virtual ~UniqueGlobalIndexer() = 0;
158 
161  virtual Teuchos::RCP<Teuchos::Comm<int> > getComm() const = 0;
162 
165  virtual int getNumFields() const = 0;
166 
181  virtual int getFieldNum(const std::string & str) const = 0;
182 
185  virtual void getFieldOrder(std::vector<std::string> & fieldOrder) const = 0;
186 
189  virtual void getElementBlockIds(std::vector<std::string> & elementBlockIds) const = 0;
190 
193  virtual bool fieldInBlock(const std::string & field, const std::string & block) const = 0;
194 
197  virtual const std::vector<int> & getBlockFieldNumbers(const std::string & blockId) const = 0;
198 
202  virtual const std::vector<int> & getGIDFieldOffsets(const std::string & blockId,int fieldNum) const = 0;
203 
216  virtual const std::pair<std::vector<int>,std::vector<int> > &
217  getGIDFieldOffsets_closure(const std::string & blockId, int fieldNum,
218  int subcellDim,int subcellId) const = 0;
219 
220  // Methods requiring Local or Global OrdinalT
222 
225  virtual void getElementOrientation(LocalOrdinalT localElmtId,std::vector<double> & gidsOrientation) const = 0;
226 
234  virtual const std::vector<LocalOrdinalT> & getElementBlock(const std::string & blockId) const = 0;
235 
239  virtual void getElementGIDs(LocalOrdinalT localElmtId,std::vector<GlobalOrdinalT> & gids,const std::string & blockIdHint="") const = 0;
240 
247  virtual void
249  std::vector<GlobalOrdinalT>& indices) const = 0;
250 
257  virtual void
259  std::vector<GlobalOrdinalT>& indices) const = 0;
260 
267  virtual void
269  std::vector<GlobalOrdinalT>& indices) const = 0;
270 
276  virtual int
277  getNumOwned() const = 0;
278 
284  virtual int
285  getNumGhosted() const = 0;
286 
292  virtual int
293  getNumOwnedAndGhosted() const = 0;
294 
297  virtual void ownedIndices(const std::vector<GlobalOrdinalT> & indices,std::vector<bool> & isOwned) const = 0;
298 
302  const Kokkos::View<const LocalOrdinalT*,Kokkos::LayoutRight,PHX::Device> getElementLIDs(LocalOrdinalT localElmtId) const
303  { return Kokkos::subview(localIDs_k_, localElmtId, Kokkos::ALL() ); }
304 
307  const Kokkos::View<const LocalOrdinalT**,Kokkos::LayoutRight,PHX::Device> getLIDs() const
308  {return localIDs_k_;}
309 
313  void getElementLIDs(Kokkos::View<const int*,PHX::Device> cellIds,
314  Kokkos::View<LocalOrdinalT**,PHX::Device> lids) const
315  {
316  CopyCellLIDsFunctor functor;
317  functor.cellIds = cellIds;
318  functor.global_lids = localIDs_k_;
319  functor.local_lids = lids; // we assume this array is sized correctly!
320 
321  Kokkos::parallel_for(cellIds.extent(0),functor);
322  }
323 
329  virtual int getElementBlockGIDCount(const std::string & blockId) const = 0;
330 
336  virtual int getElementBlockGIDCount(const std::size_t & blockIndex) const = 0;
337 
341 
343  public:
344  typedef typename PHX::Device execution_space;
345 
346  Kokkos::View<const int*,PHX::Device> cellIds;
347  Kokkos::View<const LocalOrdinalT**,Kokkos::LayoutRight,PHX::Device> global_lids;
348  Kokkos::View<LocalOrdinalT**,PHX::Device> local_lids;
349 
350  KOKKOS_INLINE_FUNCTION
351  void operator()(const int cell) const
352  {
353  for(int i=0;i<static_cast<int>(local_lids.extent(1));i++)
354  local_lids(cell,i) = global_lids(cellIds(cell),i);
355  }
356 
357  };
358 
359 protected:
360 
365  {
366  // this method is implmented as two steps to ensure
367  // that setLocalIds works, it would be better to simply
368  // call:
369  // buildLocalIdsFromOwnedElements(localIDs_);
370 
371  std::vector<std::vector<LocalOrdinalT> > localIDs;
373  setLocalIds(localIDs);
374  }
375 
379  void buildLocalIdsFromOwnedElements(std::vector<std::vector<LocalOrdinalT> > & localIDs) const ;
380 
385  void setLocalIds(const std::vector<std::vector<LocalOrdinalT> > & localIDs)
386  {
387  // determine the maximium second dimension of the local IDs
388  std::size_t max = 0;
389  for(std::size_t i=0;i<localIDs.size();i++)
390  max = localIDs[i].size() > max ? localIDs[i].size() : max;
391 
392  // allocate for the kokkos size
393  Kokkos::View<LocalOrdinalT**,Kokkos::LayoutRight,PHX::Device> localIDs_k
394  = Kokkos::View<LocalOrdinalT**,Kokkos::LayoutRight,PHX::Device>("ugi:localIDs_",localIDs.size(),max);
395  for(std::size_t i=0;i<localIDs.size();i++) {
396  for(std::size_t j=0;j<localIDs[i].size();j++)
397  localIDs_k(i,j) = localIDs[i][j];
398  }
399 
400  // store in Kokkos type
401  localIDs_k_ = localIDs_k;
402  }
403 
410  {
411  localIDs_k_ = src.localIDs_k_;
412  }
413 
414 private:
415  Kokkos::View<const LocalOrdinalT**,Kokkos::LayoutRight,PHX::Device> localIDs_k_;
416 };
417 
418 // prevents a warning because a destructor does not exist
420 
421 // prevents a warning because a destructor does not exist
422 template <typename LocalOrdinalT,typename GlobalOrdinalT>
424 
425 template <typename LocalOrdinalT,typename GlobalOrdinalT>
427 buildLocalIdsFromOwnedElements(std::vector<std::vector<LocalOrdinalT> > & localIDs) const
428 {
429  std::vector<GlobalOrdinalT> ownedAndGhosted;
430  this->getOwnedAndGhostedIndices(ownedAndGhosted);
431 
432  // build global to local hash map (temporary and used only once)
433  std::unordered_map<GlobalOrdinalT,LocalOrdinalT> hashMap;
434  for(std::size_t i=0;i<ownedAndGhosted.size();i++)
435  hashMap[ownedAndGhosted[i]] = i;
436 
437  std::vector<std::string> elementBlocks;
438  this->getElementBlockIds(elementBlocks);
439 
440  // compute total number of elements
441  std::size_t numElmts = 0;
442  for(std::size_t eb=0;eb<elementBlocks.size();eb++)
443  numElmts += this->getElementBlock(elementBlocks[eb]).size();
444  localIDs.resize(numElmts); // allocate local ids
445 
446  // perform computation of local ids
447  for(std::size_t eb=0;eb<elementBlocks.size();eb++) {
448  std::vector<GlobalOrdinalT> gids;
449  const std::vector<LocalOrdinalT> & elmts = this->getElementBlock(elementBlocks[eb]);
450 
451  for(std::size_t e=0;e<elmts.size();e++) {
452  this->getElementGIDs(elmts[e],gids,elementBlocks[eb]);
453  std::vector<LocalOrdinalT> & lids = localIDs[elmts[e]];
454  lids.resize(gids.size());
455 
456  for(std::size_t g=0;g<gids.size();g++)
457  lids[g] = hashMap[gids[g]];
458  }
459  }
460 }
461 
462 }
463 
464 #endif
const Kokkos::View< const LocalOrdinalT **, Kokkos::LayoutRight, PHX::Device > getLIDs() const
virtual int getNumFields() const =0
virtual const std::vector< int > & getGIDFieldOffsets(const std::string &blockId, int fieldNum) const =0
Use the field pattern so that you can find a particular field in the GIDs array.
KOKKOS_INLINE_FUNCTION void operator()(const int cell) const
virtual bool fieldInBlock(const std::string &field, const std::string &block) const =0
const Kokkos::View< const LocalOrdinalT *, Kokkos::LayoutRight, PHX::Device > getElementLIDs(LocalOrdinalT localElmtId) const
virtual void getElementGIDs(LocalOrdinalT localElmtId, std::vector< GlobalOrdinalT > &gids, const std::string &blockIdHint="") const =0
Get the global IDs for a particular element. This function overwrites the gids variable.
virtual int getElementBlockGIDCount(const std::string &blockId) const =0
How any GIDs are associate with a particular element block.
virtual int getNumFields() const =0
virtual void getOwnedAndGhostedIndices(std::vector< GlobalOrdinalT > &indices) const =0
Get the set of owned and ghosted indices for this processor.
virtual void getElementBlockIds(std::vector< std::string > &elementBlockIds) const =0
virtual ~UniqueGlobalIndexer()=0
Pure virtual destructor: prevents warnings with inline empty implementation.
virtual int getNumOwned() const =0
Get the number of indices owned by this processor.
Kokkos::View< LocalOrdinalT **, PHX::Device > local_lids
void setLocalIds(const std::vector< std::vector< LocalOrdinalT > > &localIDs)
virtual ~UniqueGlobalIndexerBase()=0
Pure virtual destructor: prevents warnings with inline empty implementation.
virtual const std::pair< std::vector< int >, std::vector< int > > & getGIDFieldOffsets_closure(const std::string &blockId, int fieldNum, int subcellDim, int subcellId) const =0
Use the field pattern so that you can find a particular field in the GIDs array. This version lets yo...
virtual Teuchos::RCP< Teuchos::Comm< int > > getComm() const =0
virtual void getElementOrientation(LocalOrdinalT localElmtId, std::vector< double > &gidsOrientation) const =0
Get a vector containg the orientation of the GIDs relative to the neighbors.
virtual const std::vector< int > & getGIDFieldOffsets(const std::string &blockId, int fieldNum) const =0
Use the field pattern so that you can find a particular field in the GIDs array.
virtual void getFieldOrder(std::vector< std::string > &fieldOrder) const =0
virtual Teuchos::RCP< Teuchos::Comm< int > > getComm() const =0
Kokkos::View< const LO **, PHX::Device > lids
virtual bool fieldInBlock(const std::string &field, const std::string &block) const =0
virtual Teuchos::RCP< const ConnManagerBase< LocalOrdinalT > > getConnManagerBase() const =0
Returns the connection manager currently being used.
void getElementLIDs(Kokkos::View< const int *, PHX::Device > cellIds, Kokkos::View< LocalOrdinalT **, PHX::Device > lids) const
virtual const std::pair< std::vector< int >, std::vector< int > > & getGIDFieldOffsets_closure(const std::string &blockId, int fieldNum, int subcellDim, int subcellId) const =0
Use the field pattern so that you can find a particular field in the GIDs array. This version lets yo...
virtual void getElementBlockIds(std::vector< std::string > &elementBlockIds) const =0
virtual const std::vector< int > & getBlockFieldNumbers(const std::string &blockId) const =0
virtual int getNumOwnedAndGhosted() const =0
Get the number of owned and ghosted indices for this processor.
virtual void getGhostedIndices(std::vector< GlobalOrdinalT > &indices) const =0
Get the set of indices ghosted for this processor.
virtual int getElementBlockGIDCount(const std::string &blockId) const =0
How many GIDs are associate with a particular element block.
virtual int getNumGhosted() const =0
Get the number of indices ghosted for this processor.
void shareLocalIDs(const UniqueGlobalIndexer< LocalOrdinalT, GlobalOrdinalT > &src)
virtual const std::string & getFieldString(int num) const =0
Reverse lookup of the field string from a field number.
Kokkos::View< const LocalOrdinalT **, Kokkos::LayoutRight, PHX::Device > localIDs_k_
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
Kokkos::View< const LocalOrdinalT **, Kokkos::LayoutRight, PHX::Device > global_lids
virtual int getFieldNum(const std::string &str) const =0
Get the number used for access to this field.
virtual void ownedIndices(const std::vector< GlobalOrdinalT > &indices, std::vector< bool > &isOwned) const =0
void buildLocalIdsFromOwnedElements(std::vector< std::vector< LocalOrdinalT > > &localIDs) const
virtual const std::vector< int > & getBlockFieldNumbers(const std::string &blockId) const =0
virtual void getFieldOrder(std::vector< std::string > &fieldOrder) const =0
virtual const std::vector< LocalOrdinalT > & getElementBlock(const std::string &blockId) const =0
virtual int getFieldNum(const std::string &str) const =0
Get the number used for access to this field.
virtual void getOwnedIndices(std::vector< GlobalOrdinalT > &indices) const =0
Get the set of indices owned by this processor.