Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
RTITutorialSimple.cpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) 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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 */
43 
44 #include <Teuchos_GlobalMPISession.hpp>
45 #include <Teuchos_XMLParameterListReader.hpp>
46 #include <Teuchos_XMLParameterListHelpers.hpp>
47 #include <Teuchos_DefaultMpiComm.hpp>
48 
49 #include <Tpetra_ConfigDefs.hpp>
50 
51 #undef HAVE_KOKKOSCLASSIC_THRUST
52 
53 #include <Tpetra_HybridPlatform.hpp>
54 #include <Tpetra_Map.hpp>
55 #include <Tpetra_Vector.hpp>
56 #include <Tpetra_RTI.hpp>
57 
58 #include <functional>
59 
60 // Function object that takes two arguments and returns the second
61 template <class S>
62 class AssignSecond {
63  public:
64  AssignSecond() {}
65  S operator()(const S &s1, const S &s2) {return s2;}
66 };
67 
68 // A few examples of RTI capability
69 template <class Node>
70 void simple_rti_examples(const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
71 {
72  using Teuchos::RCP;
73  using Tpetra::RTI::reduce;
74  using Tpetra::RTI::ZeroOp;
78  const Tpetra::global_size_t INVALID = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
79  // Create a simple map with 1000 local entries per node
80  const size_t numLocal = 1000*comm->getSize();
81  auto map = Tpetra::createContigMapWithNode<int,int>(INVALID,numLocal,comm,node);
82  auto dx = Tpetra::createVector<double>(map),
83  dy = Tpetra::createVector<double>(map);
84 
85  // Set dx to random
86  dx->randomize();
87 
88  // Assign dy = dx, multiple ways!
89  // Via functor
90  binary_transform( *dy, *dx, AssignSecond<double>() );
91  // Via C++11 lambda expression
92  binary_transform( *dy, *dx, [](double, double xx){return xx;} );
93  // Via convenient macro
94  TPETRA_BINARY_TRANSFORM( dy, dx, dx );
95 
96  // Perform multi-precision inner product...
97  // floating point inner product with double precision accumulator
98  float fresult; double dresult;
99  auto fx = Tpetra::createVector<float>(map),
100  fy = Tpetra::createVector<float>(map);
101  TPETRA_BINARY_TRANSFORM( fx, dx, (float)dx );
102  TPETRA_BINARY_TRANSFORM( fy, dy, (float)dy );
103  // ... using a composite adaptor and standard functors
104  fresult = reduce(*fx, *fy, reductionGlob<ZeroOp<double>>(std::multiplies<float>(), std::plus<double>()) );
105  // ... using a convenience macro to generate all of that
106  fresult = TPETRA_REDUCE2( fx, fy, fx*fy, ZeroOp<double>, std::plus<double>() );
107 
108  // compare against double precision approach
109  dresult = TPETRA_REDUCE2( dx, dy, dx*dy, ZeroOp<double>, std::plus<double>() );
110  if (comm->getRank() == 0) {
111  std::cout << " Float product/double accumulator: " << fresult << std::endl;
112  std::cout << "Double product/double accumulator: " << dresult << std::endl;
113  }
114 }
115 
116 // User-specified run object; think of this as main(), but templated on the node type
117 template <class Node>
118 class runTest {
119  public:
120  // HybridNode requires a method with this signature
121  static void run(Teuchos::ParameterList &myMachPL, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
122  {
123  std::cout << "Running test with Node==" << Teuchos::typeName(*node) << " on rank " << comm->getRank() << "/" << comm->getSize() << std::endl;
124  simple_rti_examples<Node>(comm,node);
125  }
126 };
127 
128 // Actual main(): configure the HybridPlatform
129 int main(int argc, char **argv) {
130  Teuchos::GlobalMPISession mpisess(&argc,&argv,&std::cout);
131  Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::createMpiComm<int>(Teuchos::opaqueWrapper<MPI_Comm>(MPI_COMM_WORLD));
132 
133  //
134  // read machine file and initialize platform
135  //
136  std::string machine_list(
137  "<ParameterList> "
138  " <ParameterList name='%1=0'> "
139  " <Parameter name='NodeType' type='string' value='Kokkos::SerialNode'/> "
140  " </ParameterList> "
141  " <ParameterList name='=-1'> "
142  " <Parameter name='NodeType' type='string' value='Kokkos::OpenMPNode'/> "
143  " <Parameter name='Verbose' type='int' value='0'/> "
144  " <Parameter name='Num Threads' type='int' value='-1'/> "
145  " </ParameterList> "
146  " <ParameterList name='=-2'> "
147  " <Parameter name='NodeType' type='string' value='Kokkos::TBBNode'/> "
148  " <Parameter name='Verbose' type='int' value='0'/> "
149  " <Parameter name='Num Threads' type='int' value='-1'/> "
150  " </ParameterList> "
151  " <ParameterList name='=-3'> "
152  " <Parameter name='NodeType' type='string' value='Kokkos::TPINode'/> "
153  " <Parameter name='Verbose' type='int' value='0'/> "
154  " <Parameter name='Num Threads' type='int' value='0'/> "
155  " </ParameterList> "
156  " <ParameterList name='=-4'> "
157  " <Parameter name='NodeType' type='string' value='Kokkos::ThrustGPUNode'/> "
158  " <Parameter name='Verbose' type='int' value='0'/> "
159  " <Parameter name='Device Number' type='int' value='0'/> "
160  " </ParameterList> "
161  "</ParameterList> "
162  );
163 
164  // Load the machine ParameterList from the string above; this will typically
165  // be loaded from an XML file
166  Teuchos::ParameterList machPL;
167  Teuchos::updateParametersFromXmlString(machine_list, inOutArg(machPL));
168  Tpetra::HybridPlatform platform(comm,machPL);
169  platform.runUserCode<runTest>();
170 
171  if (comm->getRank() == 0) std::cout << "End Result: TEST PASSED" << std::endl;
172  return 0;
173 }
void binary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
Transform values of vec_inout using vec_inout, vec_in2 and operator op.
Definition: Tpetra_RTI.hpp:167
size_t global_size_t
Global size_t object.
Glob::RedOP::result_type reduce(const Vector< S, LO, GO, Node > &vec_in, Glob glob)
Reduce values of vec_in using the operators instantiated in glob.
Definition: Tpetra_RTI.hpp:201
ReductionGlob< GOP, ROP, IOP > reductionGlob(GOP gop, ROP rop)
Non-member constructor to instantiate a type glob of a static identity functor and generation and red...
Definition: Tpetra_RTI.hpp:137
A static identity functor, providing a static method identity() that returns zero.
Definition: Tpetra_RTI.hpp:67
void unary_transform(Vector< S, LO, GO, Node > &vec_inout, OP op)
Transform values of vec_inout using via operator op.
Definition: Tpetra_RTI.hpp:155
A platform class for hybrid nodes.