MueLu  Version of the Day
MueLu_CoordinatesTransferFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_COORDINATESTRANSFER_FACTORY_DEF_HPP
47 #define MUELU_COORDINATESTRANSFER_FACTORY_DEF_HPP
48 
49 #include "Xpetra_ImportFactory.hpp"
50 #include "Xpetra_MultiVectorFactory.hpp"
51 #include "Xpetra_MapFactory.hpp"
52 #include "Xpetra_IO.hpp"
53 
54 #include "MueLu_CoarseMapFactory.hpp"
55 #include "MueLu_Aggregates.hpp"
57 //#include "MueLu_Utilities.hpp"
58 
59 #include "MueLu_Level.hpp"
60 #include "MueLu_Monitor.hpp"
61 
62 namespace MueLu {
63 
64  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
66  RCP<ParameterList> validParamList = rcp(new ParameterList());
67 
68  validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null, "Factory for coordinates generation");
69  validParamList->set<RCP<const FactoryBase> >("Aggregates", Teuchos::null, "Factory for coordinates generation");
70  validParamList->set<RCP<const FactoryBase> >("CoarseMap", Teuchos::null, "Generating factory of the coarse map");
71  validParamList->set<bool> ("Geometric", false, "Flag specifying that the coordinates are transfered for GeneralGeometricPFactory");
72  validParamList->set<RCP<const FactoryBase> >("coarseCoordinates", Teuchos::null, "Factory for coarse coordinates generation");
73  validParamList->set<RCP<const FactoryBase> >("gCoarseNodesPerDim", Teuchos::null, "Factory providing the global number of nodes per spatial dimensions of the mesh");
74  validParamList->set<RCP<const FactoryBase> >("lCoarseNodesPerDim", Teuchos::null, "Factory providing the local number of nodes per spatial dimensions of the mesh");
75  validParamList->set<int> ("write start", -1, "first level at which coordinates should be written to file");
76  validParamList->set<int> ("write end", -1, "last level at which coordinates should be written to file");
77 
78  return validParamList;
79  }
80 
81  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83  static bool isAvailableCoords = false;
84 
85  const ParameterList& pL = GetParameterList();
86  if(pL.get<bool>("Geometric") == true) {
87  Input(coarseLevel, "coarseCoordinates");
88  Input(coarseLevel, "gCoarseNodesPerDim");
89  Input(coarseLevel, "lCoarseNodesPerDim");
90  } else {
91  if (coarseLevel.GetRequestMode() == Level::REQUEST)
92  isAvailableCoords = coarseLevel.IsAvailable("Coordinates", this);
93 
94  if (isAvailableCoords == false) {
95  Input(fineLevel, "Coordinates");
96  Input(fineLevel, "Aggregates");
97  Input(fineLevel, "CoarseMap");
98  }
99  }
100  }
101 
102  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
104  FactoryMonitor m(*this, "Build", coarseLevel);
105 
106  using xdMV = Xpetra::MultiVector<double,LO,GO,NO>;
107 
108  GetOStream(Runtime0) << "Transferring coordinates" << std::endl;
109 
110  RCP<xdMV> coarseCoords;
111  RCP<xdMV> fineCoords;
112  Array<GO> gCoarseNodesPerDim;
113  Array<LO> lCoarseNodesPerDim;
114 
115  const ParameterList& pL = GetParameterList();
116  if(pL.get<bool>("Geometric") == true) {
117  coarseCoords = Get<RCP<xdMV> >(coarseLevel, "coarseCoordinates");
118  gCoarseNodesPerDim = Get<Array<GO> >(coarseLevel, "gCoarseNodesPerDim");
119  lCoarseNodesPerDim = Get<Array<LO> >(coarseLevel, "lCoarseNodesPerDim");
120  Set<Array<GO> >(coarseLevel, "gNodesPerDim", gCoarseNodesPerDim);
121  Set<Array<LO> >(coarseLevel, "lNodesPerDim", lCoarseNodesPerDim);
122  ;
123 
124  } else {
125 
126  if (coarseLevel.IsAvailable("Coordinates", this)) {
127  GetOStream(Runtime0) << "Reusing coordinates" << std::endl;
128  return;
129  }
130 
131  RCP<Aggregates> aggregates = Get< RCP<Aggregates> > (fineLevel, "Aggregates");
132  fineCoords = Get< RCP<xdMV> >(fineLevel, "Coordinates");
133  RCP<const Map> coarseMap = Get< RCP<const Map> > (fineLevel, "CoarseMap");
134 
135  // coarseMap is being used to set up the domain map of tentative P, and therefore, the row map of Ac
136  // Therefore, if we amalgamate coarseMap, logical nodes in the coordinates vector would correspond to
137  // logical blocks in the matrix
138 
139  ArrayView<const GO> elementAList = coarseMap->getNodeElementList();
140  LO blkSize = 1;
141  if (rcp_dynamic_cast<const StridedMap>(coarseMap) != Teuchos::null)
142  blkSize = rcp_dynamic_cast<const StridedMap>(coarseMap)->getFixedBlockSize();
143 
144  GO indexBase = coarseMap->getIndexBase();
145  size_t numElements = elementAList.size() / blkSize;
146  Array<GO> elementList(numElements);
147 
148  // Amalgamate the map
149  for (LO i = 0; i < Teuchos::as<LO>(numElements); i++)
150  elementList[i] = (elementAList[i*blkSize]-indexBase)/blkSize + indexBase;
151 
152  RCP<const Map> uniqueMap = fineCoords->getMap();
153  RCP<const Map> coarseCoordMap = MapFactory ::Build(coarseMap->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), elementList, indexBase, coarseMap->getComm());
154  coarseCoords = Xpetra::MultiVectorFactory<double,LO,GO,NO>::Build(coarseCoordMap, fineCoords->getNumVectors());
155 
156  // Create overlapped fine coordinates to reduce global communication
157  RCP<xdMV> ghostedCoords = fineCoords;
158  if (aggregates->AggregatesCrossProcessors()) {
159  RCP<const Map> nonUniqueMap = aggregates->GetMap();
160  RCP<const Import> importer = ImportFactory::Build(uniqueMap, nonUniqueMap);
161 
162  ghostedCoords = Xpetra::MultiVectorFactory<double,LO,GO,NO>::Build(nonUniqueMap, fineCoords->getNumVectors());
163  ghostedCoords->doImport(*fineCoords, *importer, Xpetra::INSERT);
164  }
165 
166  // Get some info about aggregates
167  int myPID = uniqueMap->getComm()->getRank();
168  LO numAggs = aggregates->GetNumAggregates();
169  ArrayRCP<LO> aggSizes = aggregates->ComputeAggregateSizes(true,true);
170  const ArrayRCP<const LO> vertex2AggID = aggregates->GetVertex2AggId()->getData(0);
171  const ArrayRCP<const LO> procWinner = aggregates->GetProcWinner()->getData(0);
172 
173  // Fill in coarse coordinates
174  for (size_t j = 0; j < fineCoords->getNumVectors(); j++) {
175  ArrayRCP<const double> fineCoordsData = ghostedCoords->getData(j);
176  ArrayRCP<double> coarseCoordsData = coarseCoords->getDataNonConst(j);
177 
178  for (LO lnode = 0; lnode < vertex2AggID.size(); lnode++)
179  if (procWinner[lnode] == myPID)
180  coarseCoordsData[vertex2AggID[lnode]] += fineCoordsData[lnode];
181 
182  for (LO agg = 0; agg < numAggs; agg++)
183  coarseCoordsData[agg] /= aggSizes[agg];
184  }
185  } // if pL.get<bool>("Geometric") == true
186 
187  Set<RCP<xdMV> >(coarseLevel, "Coordinates", coarseCoords);
188 
189  int writeStart = pL.get<int>("write start"), writeEnd = pL.get<int>("write end");
190  if (writeStart == 0 && fineLevel.GetLevelID() == 0 && writeStart <= writeEnd) {
191  std::ostringstream buf;
192  buf << fineLevel.GetLevelID();
193  std::string fileName = "coordinates_before_rebalance_level_" + buf.str() + ".m";
194  Xpetra::IO<double,LO,GO,NO>::Write(fileName,*fineCoords);
195  }
196  if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd) {
197  std::ostringstream buf;
198  buf << coarseLevel.GetLevelID();
199  std::string fileName = "coordinates_before_rebalance_level_" + buf.str() + ".m";
200  Xpetra::IO<double,LO,GO,NO>::Write(fileName,*coarseCoords);
201  }
202  }
203 
204 } // namespace MueLu
205 
206 #endif // MUELU_COORDINATESTRANSFER_FACTORY_DEF_HPP
RequestMode GetRequestMode() const
Timer to be used in factories. Similar to Monitor but with additional timers.
One-liner description of what is happening.
Namespace for MueLu classes and methods.
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
void DeclareInput(Level &finelevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.