Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Tpetra_HybridPlatform.hpp
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 #ifndef TPETRA_HYBRIDPLATFORM_HPP
43 #define TPETRA_HYBRIDPLATFORM_HPP
44 
45 #include <Tpetra_ConfigDefs.hpp>
46 #include <Teuchos_Describable.hpp>
47 #include <Teuchos_ParameterList.hpp>
48 
49 #include <Kokkos_SerialNode.hpp>
50 #ifdef HAVE_KOKKOSCLASSIC_TBB
51 #include <Kokkos_TBBNode.hpp>
52 #endif
53 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
54 #include <Kokkos_TPINode.hpp>
55 #endif
56 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
57 #include <Kokkos_OpenMPNode.hpp>
58 #endif
59 #ifdef HAVE_KOKKOSCLASSIC_THRUST
60 #include <Kokkos_ThrustGPUNode.hpp>
61 #endif
62 
63 // This macro is only for use by Tpetra developers.
64 // It should only be invoked in the Tpetra namespace,
65 // outside of the HybridPlatform class declaration.
66 #define TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(N) \
67  template <> bool HybridPlatform::isNodeSupported<N> ();
68 
69 namespace Tpetra {
70 
72  class HybridPlatform : public Teuchos::Describable {
73  public:
75 
76 
78  HybridPlatform (const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
79  Teuchos::ParameterList& pl);
80 
82  ~HybridPlatform ();
83 
85 
87 
89  Teuchos::RCP<const Teuchos::Comm<int> > getComm () const;
90 
92  static Teuchos::RCP<Teuchos::ParameterList> listSupportedNodes ();
93 
95  template <class Node>
96  static bool isNodeSupported ();
97 
116  template <template <class Node> class UserCode>
117  void runUserCode ();
118 
134  template <class UserCode>
135  void runUserCode (UserCode &code);
136 
138 
139  protected:
140  void createNode ();
141 
142  private:
143  HybridPlatform(const HybridPlatform &platform); // not supported
144  const Teuchos::RCP<const Teuchos::Comm<int> > comm_;
145  Teuchos::ParameterList instList_;
146  Teuchos::RCP<Kokkos::SerialNode> serialNode_;
147  bool nodeCreated_;
148 #ifdef HAVE_KOKKOSCLASSIC_TBB
149  Teuchos::RCP<Kokkos::TBBNode> tbbNode_;
150 #endif
151 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
152  Teuchos::RCP<Kokkos::TPINode> tpiNode_;
153 #endif
154 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
155  Teuchos::RCP<Kokkos::OpenMPNode> ompNode_;
156 #endif
157 #ifdef HAVE_KOKKOSCLASSIC_THRUST
158  Teuchos::RCP<Kokkos::ThrustGPUNode> thrustNode_;
159 #endif
160 
161  enum NodeType {
162  SERIALNODE
163 #ifdef HAVE_KOKKOSCLASSIC_TBB
164  , TBBNODE
165 #endif
166 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
167  , TPINODE
168 #endif
169 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
170  , OMPNODE
171 #endif
172 #ifdef HAVE_KOKKOSCLASSIC_THRUST
173  , THRUSTGPUNODE
174 #endif
175  } nodeType_;
176  };
177 
178  template <class Node>
180  {
181  return false;
182  }
183 
184  template <class UserCode>
185  void HybridPlatform::runUserCode (UserCode& codeobj) {
186  createNode();
187  switch (nodeType_) {
188  case SERIALNODE:
189  codeobj.template run<Kokkos::SerialNode>(instList_,comm_, serialNode_);
190  break;
191 #ifdef HAVE_KOKKOSCLASSIC_TBB
192  case TBBNODE:
193  codeobj.template run<Kokkos::TBBNode>(instList_,comm_, tbbNode_);
194  break;
195 #endif
196 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
197  case OMPNODE:
198  codeobj.template run<Kokkos::OpenMPNode>(instList_,comm_, ompNode_);
199  break;
200 #endif
201 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
202  case TPINODE:
203  codeobj.template run<Kokkos::TPINode>(instList_,comm_, tpiNode_);
204  break;
205 #endif
206 #ifdef HAVE_KOKKOSCLASSIC_THRUST
207  case THRUSTGPUNODE:
208  codeobj.template run<Kokkos::ThrustGPUNode>(instList_,comm_, thrustNode_);
209  break;
210 #endif
211  default:
212  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
213  Teuchos::typeName(*this) << "::runUserCode(): Invalid node type." << std::endl);
214  } // end of switch
215  }
216 
217  template <template<class Node> class UserCode>
219  createNode();
220  switch (nodeType_) {
221  case SERIALNODE:
222  UserCode<Kokkos::SerialNode>::run(instList_,comm_, serialNode_);
223  break;
224 #ifdef HAVE_KOKKOSCLASSIC_TBB
225  case TBBNODE:
226  UserCode<Kokkos::TBBNode>::run(instList_,comm_, tbbNode_);
227  break;
228 #endif
229 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
230  case OMPNODE:
231  UserCode<Kokkos::OpenMPNode>::run(instList_,comm_, ompNode_);
232  break;
233 #endif
234 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
235  case TPINODE:
236  UserCode<Kokkos::TPINode>::run(instList_,comm_, tpiNode_);
237  break;
238 #endif
239 #ifdef HAVE_KOKKOSCLASSIC_THRUST
240  case THRUSTGPUNODE:
241  UserCode<Kokkos::ThrustGPUNode>::run(instList_,comm_, thrustNode_);
242  break;
243 #endif
244  default:
245  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
246  Teuchos::typeName(*this) << "::runUserCode(): Invalid node type." << std::endl);
247  } // end of switch
248  }
249 
250  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(Kokkos::SerialNode)
251 #ifdef HAVE_KOKKOSCLASSIC_TBB
252  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(Kokkos::TBBNode)
253 #endif
254 #ifdef HAVE_KOKKOSCLASSIC_OPENMP
255  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(Kokkos::OpenMPNode)
256 #endif
257 #ifdef HAVE_KOKKOSCLASSIC_THREADPOOL
258  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(Kokkos::TPINode)
259 #endif
260 #ifdef HAVE_KOKKOSCLASSIC_THRUST
261  TPETRA_HYBRIDPLATFORM_ADD_NODE_SUPPORT_DECL(Kokkos::ThrustGPUNode)
262 #endif
263 
264 } // namespace Tpetra
265 
266 #endif // TPETRA_HYBRIDPLATFORM_HPP
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Comm Instance.
void runUserCode()
Run user code with the runtime-selected Node type.
HybridPlatform(const Teuchos::RCP< const Teuchos::Comm< int > > &comm, Teuchos::ParameterList &pl)
Constructor.
static bool isNodeSupported()
Whether HybridPlatform supports the given Node type.
static Teuchos::RCP< Teuchos::ParameterList > listSupportedNodes()
List of supported nodes and their valid parameters.
A platform class for hybrid nodes.