Tpetra parallel linear algebra  Version of the Day
Tpetra_SerialPlatform.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_SERIALPLATFORM_HPP
43 #define TPETRA_SERIALPLATFORM_HPP
44 
45 #include <Tpetra_ConfigDefs.hpp>
46 #include <Tpetra_Core.hpp>
47 #include <Kokkos_DefaultNode.hpp>
48 #include <Teuchos_DefaultSerialComm.hpp>
49 #include <Teuchos_Describable.hpp>
50 
51 namespace Tpetra {
52 
81  template <class Node>
82  class SerialPlatform : public Teuchos::Describable {
83  public:
85 
86 
88  typedef Node NodeType;
89 
91 
93 
99  explicit SerialPlatform (int* argc, char*** argv) :
100  comm_ (Teuchos::null),
101  node_ (Teuchos::null)
102  {
103  initialize (argc, argv);
104  comm_ = getDefaultComm ();
105 
106  // mfh 29 Jun 2014: Don't initialize the Node yet. This ensures
107  // that (new) Kokkos won't get initialized with the wrong
108  // command-line arguments, at least not until getNode() is
109  // called. Initializing Kokkos with the wrong command-line
110  // arguments may result in poor performance due to the wrong
111  // assignment of software threads to hardware execution units.
112  //
113  // if (node_.is_null ()) {
114  // node_ = KokkosClassic::Details::getNode<NodeType> ();
115  // }
116  }
117 
123  explicit SerialPlatform (const Teuchos::RCP<NodeType>& node) :
124  comm_ (Teuchos::rcp (new Teuchos::SerialComm<int> ())),
125  node_ (node)
126  {
127  // mfh 29 Jun 2014: Don't initialize the Node yet. See above note.
128  }
129 
138  explicit SerialPlatform (int* argc, char*** argv,
139  const Teuchos::RCP<NodeType>& node) :
140  comm_ (Teuchos::null),
141  node_ (node)
142  {
143  initialize (argc, argv);
144  comm_ = getDefaultComm ();
145  // mfh 29 Jun 2014: Don't initialize the Node yet. See above note.
146  }
147 
149  virtual ~SerialPlatform () {}
150 
152 
154 
156  Teuchos::RCP<const Teuchos::Comm<int> > getComm () const {
157  return comm_;
158  }
159 
161  Teuchos::RCP<Node> getNode () const {
162  typedef SerialPlatform<NodeType> this_type;
163  if (node_.is_null ()) {
164  // NOTE (mfh 29 Jun 2014): Creating an instance of one of the
165  // new Kokkos wrapper Nodes _must_ call Kokkos::initialize.
166  // If Kokkos has not been initialized yet, this may result in
167  // Kokkos being initialized correctly, since we have no way to
168  // pass it the command-line arguments at this point. This is
169  // why we should prefer the *Platform constructors that take
170  // argc and argv, since they can (and do) call
171  // Kokkos::initialize (by calling Tpetra::initialize).
172  //
173  // mfh 29 Jun 2014: We're only keeping the *Platform classes
174  // for backwards compatibility anyway, so I don't feel bad
175  // about the const_cast here.
176  const_cast<this_type*> (this)->node_ =
177  KokkosClassic::Details::getNode<NodeType> ();
178  TEUCHOS_TEST_FOR_EXCEPTION(
179  node_.is_null (), std::logic_error, "Tpetra::MpiPlatform::getNode: "
180  "KokkosClassic::Details::getNode<NodeType>() returned null. "
181  "This should never happen. "
182  "Please report this bug to the Tpetra developers.");
183  }
184  return node_;
185  }
186 
188  protected:
190  Teuchos::RCP<const Teuchos::Comm<int> > comm_;
192  Teuchos::RCP<NodeType> node_;
193 
194  private:
196  SerialPlatform (const SerialPlatform<NodeType>& platform);
198  SerialPlatform& operator= (const SerialPlatform<NodeType>& platform);
199  };
200 
215  template <>
217  public Teuchos::Describable {
218  public:
220 
221 
224 
226 
228 
236  comm_ (Teuchos::rcp (new Teuchos::SerialComm<int> ())),
237  node_ (Teuchos::null)
238  {
239  // mfh 29 Jun 2014: Don't initialize the Node yet. This ensures
240  // that (new) Kokkos won't get initialized with the wrong
241  // command-line arguments, at least not until getNode() is
242  // called. Initializing Kokkos with the wrong command-line
243  // arguments may result in poor performance due to the wrong
244  // assignment of software threads to hardware execution units.
245  }
246 
252  explicit SerialPlatform (int* argc, char*** argv) :
253  comm_ (Teuchos::null),
254  node_ (Teuchos::null)
255  {
256  initialize (argc, argv);
257  comm_ = getDefaultComm ();
258  // mfh 29 Jun 2014: Don't initialize the Node yet. See above note.
259  }
260 
273  explicit SerialPlatform (const Teuchos::RCP<NodeType>& node) :
274  comm_ (Teuchos::rcp (new Teuchos::SerialComm<int> ())),
275  node_ (node)
276  {
277  // mfh 29 Jun 2014: Don't initialize the Node yet. See above note.
278  }
279 
288  explicit SerialPlatform (int* argc, char*** argv,
289  const Teuchos::RCP<NodeType>& node) :
290  comm_ (Teuchos::null),
291  node_ (node)
292  {
293  initialize (argc, argv);
294  comm_ = getDefaultComm ();
295  // mfh 29 Jun 2014: Don't initialize the Node yet. See above note.
296  }
297 
299  virtual ~SerialPlatform () {}
300 
302 
304 
306  Teuchos::RCP<const Teuchos::Comm<int> > getComm() const {
307  return comm_;
308  }
309 
311  Teuchos::RCP<Tpetra::Details::DefaultTypes::node_type> getNode () const {
312  typedef SerialPlatform<NodeType> this_type;
313  if (node_.is_null ()) {
314  // NOTE (mfh 29 Jun 2014): Creating an instance of one of the
315  // new Kokkos wrapper Nodes _must_ call Kokkos::initialize.
316  // If Kokkos has not been initialized yet, this may result in
317  // Kokkos being initialized correctly, since we have no way to
318  // pass it the command-line arguments at this point. This is
319  // why we should prefer the *Platform constructors that take
320  // argc and argv, since they can (and do) call
321  // Kokkos::initialize (by calling Tpetra::initialize).
322  //
323  // mfh 29 Jun 2014: We're only keeping the *Platform classes
324  // for backwards compatibility anyway, so I don't feel bad
325  // about the const_cast here.
326  const_cast<this_type*> (this)->node_ =
327  KokkosClassic::Details::getNode<NodeType> ();
328  TEUCHOS_TEST_FOR_EXCEPTION(
329  node_.is_null (), std::logic_error, "Tpetra::MpiPlatform::getNode: "
330  "KokkosClassic::Details::getNode<NodeType>() returned null. "
331  "This should never happen. "
332  "Please report this bug to the Tpetra developers.");
333  }
334  return node_;
335  }
336 
338  private:
340  SerialPlatform (const SerialPlatform<NodeType>& platform);
341 
343  SerialPlatform& operator= (const SerialPlatform<NodeType>& platform);
344 
345  protected:
347  Teuchos::RCP<const Teuchos::Comm<int> > comm_;
348 
350  Teuchos::RCP<NodeType> node_;
351  };
352 
353 } // namespace Tpetra
354 
355 #endif // TPETRA_SERIALPLATFORM_HPP
void initialize(int *argc, char ***argv)
Initialize Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
KokkosClassic::DefaultNode::DefaultNodeType node_type
Default value of Node template parameter.
Tpetra::Details::DefaultTypes::node_type NodeType
Kokkos Node type; the template parameter of this class.
Implementation of the Platform concept for non-MPI platforms.
SerialPlatform()
Default constructor: uses Kokkos default node.
SerialPlatform(int *argc, char ***argv, const Teuchos::RCP< NodeType > &node)
Constructor that accepts the same arguments as Tpetra::initialize(), plus a Kokkos Node...
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The Teuchos::Comm instance with which this object was created.
virtual ~SerialPlatform()
Destructor (virtual for memory safety of derived classes).
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The Teuchos::Comm instance with which this object was created.
Teuchos::RCP< NodeType > node_
Node object instantiated for the platform.
Node NodeType
Kokkos Node type; the template parameter of this class.
SerialPlatform(const Teuchos::RCP< NodeType > &node)
Functions for initializing and finalizing Tpetra.
SerialPlatform(int *argc, char ***argv, const Teuchos::RCP< NodeType > &node)
Constructor that accepts the same arguments as Tpetra::initialize(), plus a Kokkos Node...
Teuchos::RCP< NodeType > node_
Kokkos Node object instantiated for the platform.
SerialPlatform(const Teuchos::RCP< NodeType > &node)
Constructor that accepts a Kokkos Node.
Teuchos::RCP< const Teuchos::Comm< int > > getDefaultComm()
Get Tpetra&#39;s default communicator.
Definition: Tpetra_Core.cpp:97
SerialPlatform(int *argc, char ***argv)
Constructor that accepts the same arguments as Tpetra::initialize().
Teuchos::RCP< Tpetra::Details::DefaultTypes::node_type > getNode() const
The Kokkos Node instance with which this object was created.
Teuchos::RCP< Node > getNode() const
The Kokkos Node instance with which this object was created.
virtual ~SerialPlatform()
Destructor (virtual for memory safety of derived classes).
SerialPlatform(int *argc, char ***argv)
Constructor that accepts the same arguments as Tpetra::initialize().
Teuchos::RCP< const Teuchos::Comm< int > > comm_
Teuchos::Comm object instantiated for the platform.
Teuchos::RCP< const Teuchos::Comm< int > > comm_
Teuchos::Comm object instantiated for the platform.