MueLu  Version of the Day
MueLu_NullspaceFactory_kokkos_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_NULLSPACEFACTORY_KOKKOS_DEF_HPP
47 #define MUELU_NULLSPACEFACTORY_KOKKOS_DEF_HPP
48 
49 #ifdef HAVE_MUELU_KOKKOS_REFACTOR
50 
52 
53 #include <Xpetra_Matrix.hpp>
54 #include <Xpetra_MultiVectorFactory.hpp>
55 
56 #include "MueLu_Level.hpp"
57 #include "MueLu_Monitor.hpp"
58 
59 namespace MueLu {
60 
61  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class DeviceType>
62  RCP<const ParameterList> NullspaceFactory_kokkos<Scalar,LocalOrdinal,GlobalOrdinal,Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType>>::GetValidParameterList() const {
63  RCP<ParameterList> validParamList = rcp(new ParameterList());
64 
65  validParamList->set< RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the fine level matrix (only needed if default null space is generated)");
66  validParamList->set< RCP<const FactoryBase> >("Nullspace", Teuchos::null, "Generating factory of the fine level null space");
67 
68  // TODO not very elegant.
69  validParamList->set< std::string >("Fine level nullspace", "Nullspace", "Variable name which is used to store null space multi vector on the finest level (default=\"Nullspace\"");
70 
71  validParamList->set< RCP<const FactoryBase> >("Nullspace1", Teuchos::null, "Generating factory of the fine level null space");
72  validParamList->set< RCP<const FactoryBase> >("Nullspace2", Teuchos::null, "Generating factory of the fine level null space");
73 
74  return validParamList;
75  }
76 
77  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class DeviceType>
78  void NullspaceFactory_kokkos<Scalar,LocalOrdinal,GlobalOrdinal,Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType>>::DeclareInput(Level& currentLevel) const {
79  const ParameterList& pL = GetParameterList();
80  std::string nspName = pL.get<std::string>("Fine level nullspace");
81 
82  // only request "A" in DeclareInput if
83  // 1) there is not nspName (e.g. "Nullspace") is available in Level, AND
84  // 2) it is the finest level (i.e. LevelID == 0)
85  if (currentLevel.IsAvailable(nspName, NoFactory::get()) == false && currentLevel.GetLevelID() == 0)
86  Input(currentLevel, "A");
87 
88  if (currentLevel.GetLevelID() != 0) {
89  // validate nullspaceFact_
90 
91  // 1) nullspaceFact_ must not be Teuchos::null, since the default factory for "Nullspace" is
92  // a NullspaceFactory
93  // 2) nullspaceFact_ must be a TentativePFactory i.e. at least a TwoLevelFactoryBase derived object
94 
95  currentLevel.DeclareInput("Nullspace", GetFactory(nspName).get(), this); /* ! "Nullspace" and nspName mismatch possible here */
96  }
97  }
98 
99  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class DeviceType>
100  void NullspaceFactory_kokkos<Scalar,LocalOrdinal,GlobalOrdinal,Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType>>::Build(Level& currentLevel) const {
101  FactoryMonitor m(*this, "Nullspace factory", currentLevel);
102 
103  RCP<MultiVector> nullspace;
104 
105  //TEUCHOS_TEST_FOR_EXCEPTION(currentLevel.GetLevelID() != 0, Exceptions::RuntimeError, "MueLu::NullspaceFactory::Build(): NullspaceFactory can be used for finest level (LevelID == 0) only.");
106  const ParameterList& pL = GetParameterList();
107  std::string nspName = pL.get<std::string>("Fine level nullspace");
108 
109  if (currentLevel.GetLevelID() == 0) {
110  if (currentLevel.IsAvailable(nspName, NoFactory::get())) {
111  // FIXME: with the new version of Level::GetFactory(), this never happens
112 
113  // When a fine nullspace have already been defined by user using Set("Nullspace", ...), we use it
114  nullspace = currentLevel.Get< RCP<MultiVector> >(nspName, NoFactory::get());
115  GetOStream(Runtime1) << "Use user-given nullspace " << nspName << ": nullspace dimension=" << nullspace->getNumVectors() << " nullspace length=" << nullspace->getGlobalLength() << std::endl;
116 
117  } else {
118  // "Nullspace" (nspName) is not available
119  auto A = Get< RCP<Matrix> >(currentLevel, "A");
120 
121  // Determine numPDEs
122  LO numPDEs = 1;
123  if (A->IsView("stridedMaps") == true) {
124  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
125  TEUCHOS_TEST_FOR_EXCEPTION(rcp_dynamic_cast<const StridedMap>(A->getRowMap()).is_null(), Exceptions::BadCast, "MueLu::CoalesceFactory::Build: cast to strided row map failed.");
126  numPDEs = rcp_dynamic_cast<const StridedMap>(A->getRowMap())->getFixedBlockSize();
127  oldView = A->SwitchToView(oldView);
128  }
129 
130  GetOStream(Runtime1) << "Generating canonical nullspace: dimension = " << numPDEs << std::endl;
131 
132  nullspace = MultiVectorFactory::Build(A->getDomainMap(), numPDEs);
133  auto nullspaceView = nullspace->template getLocalView<DeviceType>();
134 
135  int numBlocks = nullspace->getLocalLength() / numPDEs;
136 
137  SC one = Teuchos::ScalarTraits<SC>::one();
138  for (int i = 0; i < numPDEs; i++) {
139  Kokkos::parallel_for("NullspaceF:Build:for", numBlocks, KOKKOS_LAMBDA(const int j) {
140  nullspaceView(j*numPDEs + i, i) = one;
141  });
142  }
143  }
144 
145  } else {
146  // On coarser levels always use "Nullspace" as variable name, since it is expected by
147  // tentative P factory to be "Nullspace"
148  nullspace = currentLevel.Get< RCP<MultiVector> >("Nullspace", GetFactory(nspName).get()); // NOTE: "Nullspace" and nspName mismatch possible here
149  }
150 
151  // provide "Nullspace" variable on current level (used by TentativePFactory)
152  Set(currentLevel, "Nullspace", nullspace);
153 
154  } // Build
155 
156 } //namespace MueLu
157 
158 #endif // HAVE_MUELU_KOKKOS_REFACTOR
159 #endif // MUELU_NULLSPACEFACTORY_KOKKOS_DEF_HPP
Namespace for MueLu classes and methods.
static const NoFactory * get()
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< !Impl::is_integral< ExecPolicy >::value >::type *=0)
Description of what is happening (more verbose)