Tpetra Matrix/Vector Services  Version of the Day
Tpetra_HybridPlatform.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include <Tpetra_HybridPlatform.hpp>
43 #include <cstdio> // for std::sscanf
44 
45 // This macro is only for use by Tpetra developers.
46 // It should only be invoked in the Tpetra namespace.
47 #define TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(N) \
48  template <> bool HybridPlatform::isNodeSupported<N>() {return true;}
49 
50 namespace Tpetra {
51  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(Kokkos::SerialNode)
52 #ifdef HAVE_KOKKOSCLASSIC_TBB
53  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(Kokkos::TBBNode)
54 #endif
55 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
56  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(Kokkos::OpenMPNode)
57 #endif
58 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
59  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(Kokkos::TPINode)
60 #endif
61 #ifdef HAVE_KOKKOSCLASSIC_THRUST
62  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DEF(Kokkos::ThrustGPUNode)
63 #endif
64 
68  : comm_ (comm)
69  , nodeCreated_ (false)
70  , nodeType_ (SERIALNODE)
71  {
72  // ParameterList format:
73  //
74  // Node designation sublists have a name beginning with one of the
75  // following characters: % = [ and satisfying the following
76  // format:
77 
78  // %M=N is satisfied if mod(myrank,M) == N
79  // =N is satisfied if myrank == N
80  // [M,N] is satisfied if myrank \in [M,N]
81  //
82  // A node designation sublist must have a parameter entry of type
83  // std::string named "NodeType". The value indicates the type of
84  // the Node. The activated node designation sublist will be
85  // passed to the Node constructor.
86  //
87  // For example:
88  // "%2=0" ->
89  // NodeType = "Kokkos::ThrustGPUNode"
90  // DeviceNumber = 0
91  // Verbose = 1
92  // "%2=1" ->
93  // NodeType = "Kokkos::TPINode"
94  // NumThreads = 8
95  //
96  // In this scenario, nodes that are equivalent to zero module 2,
97  // that is, even nodes, will be selected to use ThrustGPUNode
98  // objects and initialized with the parameter list containing
99  // NodeType = "Kokkos::ThrustGPUNode"
100  // DeviceNumber = 0
101  // Verbose = 1
102  //
103  // Nodes that are equivalent to one modulo 2, i.e., odd nodes,
104  // will be selected to use TPINode objects and initialized with
105  // the parameter list containing
106  // NodeType = "Kokkos::TPINode"
107  // NumThreads = 8
108  //
109  // If multiple node designation sublists match the process rank,
110  // then the first encountered node designation will be used. I
111  // don't know if ParameterList respects any ordering, therefore,
112  // multiple matching designations are to be avoided.
113 
114  const int myrank = comm_->getRank();
115  std::string desigNode("");
116  bool matchFound = false;
117  for (Teuchos::ParameterList::ConstIterator it = pl.begin(); it != pl.end(); ++it) {
118  if (it->second.isList()) {
119  int parsedLen, M, N;
120  const std::string &name = it->first;
121  const Teuchos::ParameterList &sublist = Teuchos::getValue<Teuchos::ParameterList>(it->second);
122  // select and assign instList_;
123  parsedLen = 0;
124  if (std::sscanf(name.c_str(),"%%%d=%d%n",&M,&N,&parsedLen) == 2 && (size_t)parsedLen == name.length()) {
125  if ((myrank % M) == N) {
126  matchFound = true;
127  }
128  }
129  parsedLen = 0;
130  if (std::sscanf(name.c_str(),"=%d%n",&N,&parsedLen) == 1 && (size_t)parsedLen == name.length()) {
131  if (myrank == N) {
132  matchFound = true;
133  }
134  }
135  parsedLen = 0;
136  if (std::sscanf(name.c_str(),"[%d,%d]%n",&M,&N,&parsedLen) == 2 && (size_t)parsedLen == name.length()) {
137  if (M <= myrank && myrank <= N) {
138  matchFound = true;
139  }
140  }
141  if (name == "default") {
142  matchFound = true;
143  }
144  if (matchFound) {
145  try {
146  desigNode = sublist.get<std::string>("NodeType");
147  }
149  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(true, std::runtime_error,
150  std::endl << Teuchos::typeName(*this) << ": Invalid machine file." << std::endl
151  << "Missing parameter \"NodeType\" on Node " << myrank << " for Node designator " << "\"" << name << "\":" << std::endl
152  << sublist << std::endl);
153  }
154  if (desigNode == "Kokkos::SerialNode") {
155  nodeType_ = SERIALNODE;
156  }
157 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
158  else if (desigNode == "Kokkos::TPINode") {
159  nodeType_ = TPINODE;
160  }
161 #endif
162 #ifdef HAVE_KOKKOSCLASSIC_TBB
163  else if (desigNode == "Kokkos::TBBNode") {
164  nodeType_ = TBBNODE;
165  }
166 #endif
167 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
168  else if (desigNode == "Kokkos::OpenMPNode") {
169  nodeType_ = OMPNODE;
170  }
171 #endif
172 #ifdef HAVE_KOKKOSCLASSIC_THRUST
173  else if (desigNode == "Kokkos::ThrustGPUNode") {
174  nodeType_ = THRUSTGPUNODE;
175  }
176 #endif
177  else {
178  matchFound = false;
179  }
180  if (matchFound) {
181  instList_ = sublist;
182  break;
183  }
184  }
185  }
186  }
187  if (!matchFound) {
188  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(true, std::runtime_error,
189  Teuchos::typeName(*this) << ": No matching node type on rank " << myrank);
190  }
191  }
192 
194  {}
195 
197  {
198  RCP<ParameterList> list = Teuchos::parameterList();
199  {
200  ParameterList subpl;
201  subpl.set("NodeType","Kokkos::SerialNode");
202  subpl.setParameters( Kokkos::SerialNode::getDefaultParameters() );
203  list->set("=-1",subpl);
204  }
205 #ifdef HAVE_KOKKOSCLASSIC_TBB
206  {
207  ParameterList subpl;
208  subpl.set("NodeType","Kokkos::TBBNode");
209  subpl.setParameters( Kokkos::TBBNode::getDefaultParameters() );
210  list->set("=-2",subpl);
211  }
212 #endif
213 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
214  {
215  ParameterList subpl;
216  subpl.set("NodeType","Kokkos::OpenMPNode");
217  subpl.setParameters( Kokkos::OpenMPNode::getDefaultParameters() );
218  list->set("=-3",subpl);
219  }
220 #endif
221 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
222  {
223  ParameterList subpl;
224  subpl.set("NodeType","Kokkos::TPINode");
225  subpl.setParameters( Kokkos::TPINode::getDefaultParameters() );
226  list->set("=-4",subpl);
227  }
228 #endif
229 #ifdef HAVE_KOKKOSCLASSIC_THRUST
230  {
231  ParameterList subpl;
232  subpl.set("NodeType","Kokkos::ThrustGPUNode");
233  subpl.setParameters( Kokkos::ThrustGPUNode::getDefaultParameters() );
234  list->set("=-5",subpl);
235  }
236 #endif
237  return list;
238  }
239 
242  return comm_;
243  }
244 
245  void HybridPlatform::createNode () {
246  using Teuchos::rcp;
247 
248  if (nodeCreated_) {
249  return;
250  }
251  switch (nodeType_) {
252  case SERIALNODE:
253  serialNode_ = rcp (new Kokkos::SerialNode (instList_));
254  break;
255 #ifdef HAVE_KOKKOSCLASSIC_TBB
256  case TBBNODE:
257  tbbNode_ = rcp (new Kokkos::TBBNode (instList_));
258  break;
259 #endif
260 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
261  case OMPNODE:
262  ompNode_ = rcp (new Kokkos::OpenMPNode (instList_));
263  break;
264 #endif
265 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
266  case TPINODE:
267  tpiNode_ = rcp (new Kokkos::TPINode (instList_));
268  break;
269 #endif
270 #ifdef HAVE_KOKKOSCLASSIC_THRUST
271  case THRUSTGPUNODE:
272  thrustNode_ = rcp (new Kokkos::ThrustGPUNode (instList_));
273  break;
274 #endif
275  default:
276  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
277  Teuchos::typeName(*this) << "::runUserCode(): Invalid Node type."
278  << std::endl);
279  } // end of switch
280  nodeCreated_ = true;
281  }
282 } // namespace Tpetra
283 
284 
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Comm Instance.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
ConstIterator end() const
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
params_t::ConstIterator ConstIterator
ConstIterator begin() const
ParameterList & setParameters(const ParameterList &source)
HybridPlatform(const Teuchos::RCP< const Teuchos::Comm< int > > &comm, Teuchos::ParameterList &pl)
Constructor.
static Teuchos::RCP< Teuchos::ParameterList > listSupportedNodes()
List of supported nodes and their valid parameters.
std::string typeName(const T &t)
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)