Tpetra parallel linear algebra  Version of the Day
Tpetra_MpiPlatform.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_ConfigDefs.hpp>
43 
44 // mfh 29 Jun 2014: It makes life easier for Sierra developers if
45 // Trilinos just builds all .cpp files unconditionally, rather than
46 // making the decision whether to build them dependent on CMake
47 // options. Thus, we just exclude all the content of this file if MPI
48 // is not enabled.
49 #ifdef HAVE_TPETRA_MPI
50 # include <Tpetra_MpiPlatform.hpp>
51 
52 namespace Tpetra {
53 
55  MpiPlatform () :
56  comm_ (Teuchos::createMpiComm<int> (Teuchos::opaqueWrapper<MPI_Comm> (MPI_COMM_WORLD)))
57  {}
58 
59  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
60  MpiPlatform (int* argc, char*** argv) :
61  comm_ (Teuchos::null)
62  {
63  initialize (argc, argv);
64  comm_ = getDefaultComm ();
65  }
66 
67  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
68  MpiPlatform (const Teuchos::RCP<NodeType>& /* node */) :
69  comm_ (Teuchos::createMpiComm<int> (Teuchos::opaqueWrapper<MPI_Comm> (MPI_COMM_WORLD)))
70  {
71  }
72 
73  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
74  MpiPlatform (int* argc, char*** argv, const Teuchos::RCP<NodeType>& /* node */) :
75  comm_ (Teuchos::null)
76  {
77  initialize (argc, argv);
78  comm_ = getDefaultComm ();
79  }
80 
81  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
82  MpiPlatform (const Teuchos::RCP<NodeType>& /* node */,
83  const Teuchos::RCP<const Teuchos::OpaqueWrapper<MPI_Comm> >& rawMpiComm)
84  : comm_ (Teuchos::null)
85  {
86  TEUCHOS_TEST_FOR_EXCEPTION(
87  rawMpiComm.is_null (), std::invalid_argument, "Tpetra::MpiPlatform "
88  "constructor: The input RCP<OpaqueWrapper<MPI_Comm> > is null. That "
89  "means something different than MPI_COMM_NULL. If you want to give "
90  "MPI_COMM_NULL to this constructor, please wrap MPI_COMM_NULL in a "
91  "nonnull Teuchos::OpaqueWrapper by using the "
92  "Teuchos::opaqueWrapper<MPI_Comm>() nonmember constructor.");
93  comm_ = Teuchos::createMpiComm<int> (rawMpiComm);
94  }
95 
96  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
97  MpiPlatform (int* argc,
98  char*** argv,
99  const Teuchos::RCP<NodeType>& /* node */,
100  const Teuchos::RCP<const Teuchos::OpaqueWrapper<MPI_Comm> >& rawMpiComm)
101  : comm_ (Teuchos::null)
102  {
103  TEUCHOS_TEST_FOR_EXCEPTION(
104  rawMpiComm.is_null (), std::invalid_argument, "Tpetra::MpiPlatform "
105  "constructor: The input RCP<OpaqueWrapper<MPI_Comm> > is null. That "
106  "means something different than MPI_COMM_NULL. If you want to give "
107  "MPI_COMM_NULL to this constructor, please wrap MPI_COMM_NULL in a "
108  "nonnull Teuchos::OpaqueWrapper by using the "
109  "Teuchos::opaqueWrapper<MPI_Comm>() nonmember constructor.");
110  comm_ = Teuchos::createMpiComm<int> (rawMpiComm);
111 
112  // NOTE (mfh 29 Jun 2014): The OpaqueWrapper might wrap the
113  // MPI_Comm in something that calls MPI_Comm_free. Thus, we
114  // can't just ignore it; we have to give it to the comm_ so that
115  // its destructor (which might call MPI_Comm_free) will be
116  // called at the right time. This is why we don't set comm
117  // using getDefaultComm(). This is also why we pass comm_
118  // directly to initialize(): that way there aren't two
119  // references to the raw MPI_Comm floating around, and comm_'s
120  // destructor will get to do the right thing.
121  initialize (argc, argv, comm_);
122  }
123 
124  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
125  MpiPlatform (const Teuchos::RCP<NodeType>& /* node */, MPI_Comm rawMpiComm)
126  : comm_ (Teuchos::createMpiComm<int> (Teuchos::opaqueWrapper<MPI_Comm> (rawMpiComm)))
127  {
128  }
129 
130  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
131  MpiPlatform (int* argc,
132  char*** argv,
133  const Teuchos::RCP<NodeType>& /* node */,
134  MPI_Comm rawMpiComm)
135  : comm_ (Teuchos::null)
136  {
137  initialize (argc, argv, rawMpiComm);
138  comm_ = getDefaultComm ();
139  }
140 
141  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
142  ~MpiPlatform () {}
143 
144  Teuchos::RCP<const Teuchos::Comm<int> >
145  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
146  getComm () const {
147  TEUCHOS_TEST_FOR_EXCEPTION(
148  comm_.is_null (), std::logic_error, "Tpetra::MpiPlatform::getComm: "
149  "The default communicator is null. This should never happen. "
150  "Please report this bug to the Tpetra developers.");
151  return comm_;
152  }
153 
154  Teuchos::RCP<MpiPlatform< ::Tpetra::Details::DefaultTypes::node_type>::NodeType>
155  MpiPlatform<Tpetra::Details::DefaultTypes::node_type>::
156  getNode () const
157  {
158  return Teuchos::rcp (new Tpetra::Details::DefaultTypes::node_type);
159  }
160 
161 } // namespace Tpetra
162 
163 #endif // HAVE_TPETRA_MPI
Tpetra::MpiPlatform::MpiPlatform
MpiPlatform(const Teuchos::RCP< NodeType > &node)
Constructor that accepts a Kokkos Node.
Definition: Tpetra_MpiPlatform.hpp:91
Tpetra::Details::DefaultTypes::node_type
::Kokkos::Compat::KokkosDeviceWrapperNode< execution_space > node_type
Default value of Node template parameter.
Definition: Tpetra_Details_DefaultTypes.hpp:105
Tpetra::getDefaultComm
Teuchos::RCP< const Teuchos::Comm< int > > getDefaultComm()
Get Tpetra's default communicator.
Definition: Tpetra_Core.cpp:201
Tpetra::initialize
void initialize(int *argc, char ***argv)
Initialize Tpetra.
Definition: Tpetra_Core.cpp:230
Tpetra
Namespace Tpetra contains the class and methods constituting the Tpetra library.