Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
RTIExample.cpp
Go to the documentation of this file.
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_oblackholestream.hpp>
46 #include <Teuchos_CommandLineProcessor.hpp>
47 #include <Teuchos_TimeMonitor.hpp>
48 
49 #include <Kokkos_DefaultNode.hpp>
50 
51 #include <Tpetra_Version.hpp>
52 #include <Tpetra_DefaultPlatform.hpp>
53 #include <Tpetra_MultiVector.hpp>
54 #include <Tpetra_RTI.hpp>
55 
56 #include <iostream>
57 #include <iomanip>
58 #include <functional>
59 #include <utility>
60 
65 namespace TpetraExamples {
66 
80  template <class Arg1, class Arg2, class MultPrec>
81  class mprec_mult : public std::binary_function<Arg1,Arg2,MultPrec> {
82  public:
83  mprec_mult() {}
84  inline MultPrec
85  operator() (const Arg1& x, const Arg2& y) const {
86  return (MultPrec)(x) * (MultPrec)(y);
87  }
88  };
89 
105  template <class T1, class T2, class Op>
106  class pair_op :
107  public std::binary_function<std::pair<T1,T2>, std::pair<T1,T2>, std::pair<T1,T2>> {
108  private:
109  Op op_;
110  public:
111  pair_op(Op op) : op_(op) {}
112  inline std::pair<T1,T2>
113  operator() (const std::pair<T1,T2>& a, const std::pair<T1,T2>& b) const {
114  return std::make_pair (op_ (a.first, b.first), op_ (a.second, b.second));
115  }
116  };
117 
120  template <class T1, class T2, class Op>
122  return pair_op<T1,T2,Op>(op);
123  }
124 
125 }
126 
127 namespace Tpetra {
128  namespace RTI {
129  //
130  // Specialization of ZeroOp for a pair of two different types.
131  //
132  template <class T1, class T2>
133  class ZeroOp<std::pair<T1,T2>> {
134  public:
135  static inline std::pair<T1,T2> identity() {
136  return std::make_pair( Teuchos::ScalarTraits<T1>::zero(),
137  Teuchos::ScalarTraits<T2>::zero() );
138  }
139  };
140  }
141 }
142 
143 int main(int argc, char *argv[])
144 {
145  //
146  // Set up MPI, if applicable.
147  //
148  Teuchos::oblackholestream blackhole;
149  Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
150 
151  using Teuchos::TimeMonitor;
152  using Tpetra::RTI::ZeroOp;
155  using std::make_pair;
156  using std::pair;
157 
158  //
159  // Get the default communicator and node
160  //
162  auto comm = platform.getComm();
163  auto node = platform.getNode();
164  const int myImageID = comm->getRank();
165 
166  //
167  // Get example parameters from command-line processor
168  //
169  bool verbose = (myImageID==0);
170  int numGlobal_user = 100*comm->getSize();
171  int numTimeTrials = 3;
172  Teuchos::CommandLineProcessor cmdp(false,true);
173  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
174  cmdp.setOption("global-size",&numGlobal_user,"Global test size.");
175  cmdp.setOption("num-time-trials",&numTimeTrials,"Number of trials in timing loops.");
176  if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
177  return -1;
178  }
179 
180  //
181  // Say hello, print some communicator info
182  //
183  if (verbose) {
184  std::cout << "\n" << Tpetra::version() << std::endl;
185  std::cout << "Comm info: " << *comm;
186  std::cout << "Node type: " << Teuchos::typeName(*node) << std::endl;
187  std::cout << std::endl;
188  }
189 
190 
191  //
192  // Create a simple map with 5 local entries per node
193  //
194  Tpetra::global_size_t numGlobalRows = numGlobal_user;
195  auto map = Tpetra::createUniformContigMapWithNode<int,int>(numGlobalRows, comm, node);
196  const size_t numLocalRows = map->getNodeNumElements();
197  auto x = Tpetra::createVector<float>(map),
198  y = Tpetra::createVector<float>(map);
199  auto z = Tpetra::createVector<double>(map),
200  w = Tpetra::createVector<double>(map),
201  v = Tpetra::createVector<double>(map);
202 
203  //
204  // Initialization and simple reduction
205  //
206 
207  // sets x[i] = 1.0f
208  Tpetra::RTI::unary_transform( *x, [](float xi){return 1.0f;} );
209  // sets y[i] = x[i]
210  Tpetra::RTI::binary_transform( *y, *x, [](float /*yi*/, float xi) {return xi;} );
211  // sets y[i] = plus(y[i], x[i]) = y[i] + x[i]
212  Tpetra::RTI::binary_transform( *y, *x, std::plus<float>() );
213  // compute dot(x,y) in single precision, sum all x[i]*y[i]
214  float fresult = Tpetra::RTI::reduce( *x, *y,
215  reductionGlob<
216  ZeroOp<float>>(
217  std::multiplies<float>(),
218  std::plus<float>()
219  ) );
220  if (verbose) {
221  std::cout << std::left << "dot( ones, twos ) result == " << fresult
222  << ", expected value is " << numGlobalRows*2.0f << std::endl;
223  }
224 
225  //
226  // Single precision testing
227  //
228 
229  // set x = [1, 1e-4, ..., 1e-4]
230  {
231  Teuchos::ArrayRCP<float> xdata = x->get1dViewNonConst();
232  for (size_t i=1; i < numLocalRows; ++i) {
233  xdata[i] = 1.0f / 4096.0f;
234  }
235  }
236  // set y[i] = x while computing square of norm2(x) (using y): multiply x[i] * y[i] in float, sum them in float, 0.0f is the additive identity
237  fresult = Tpetra::RTI::binary_pre_transform_reduce(*y, *x, reductionGlob<ZeroOp<float>>( [](float /*yi*/, float xi){return xi;}, std::multiplies<float>(), std::plus<float>()) );
238 
239  // compute pure float inner product alone, timing it for comparison
240  auto timePF = TimeMonitor::getNewTimer("Pure float dot()");
241  {
242  TimeMonitor lcltimer(*timePF);
243  for (int l=0; l != numTimeTrials; ++l)
244  fresult = Tpetra::RTI::reduce(*x, *y, reductionGlob<ZeroOp<float>>(std::multiplies<float>(), std::plus<float>()) );
245  }
246  if (verbose) {
247  std::cout << std::left << std::endl << std::setw(25) << "pure float result" << " == " << std::setprecision(12) << std::scientific << fresult << std::endl;
248  }
249 
250  //
251  // Mixed precision testing
252  //
253 
254  // compute dot(x,y) with double accumulator: multiply x[i] * y[i] in float, sum them in double, 0.0 is the additive identity
255  auto timeAD = TimeMonitor::getNewTimer("Double acc. dot()");
256  {
257  TimeMonitor lcltimer(*timeAD);
258  for (int l=0; l != numTimeTrials; ++l)
259  fresult = Tpetra::RTI::reduce(*x, *y, reductionGlob<ZeroOp<double>>(std::multiplies<float>(), std::plus<double>()) );
260  }
261  if (verbose) {
262  std::cout << std::left << std::setw(25) << "double acc. result" << " == " << std::setprecision(12) << std::scientific << fresult << std::endl;
263  }
264 
265  // compute dot(x,y) in full double precision: multiply x[i] * y[i] in double, sum them in double, 0.0 is the additive identity
266  double dresult = 0.0;
267  auto timeMAD = TimeMonitor::getNewTimer("Double mult./acc. dot()");
268  {
269  TimeMonitor lcltimer(*timeMAD);
270  for (int l=0; l != numTimeTrials; ++l)
271  dresult = Tpetra::RTI::reduce(*x, *y, reductionGlob< ZeroOp<double>>(
272  mprec_mult<float,float,double>(),
273  std::plus<double>()) );
274  }
275  if (verbose) {
276  std::cout << std::left << std::setw(25) << "double mult/add result" << " == " << std::setprecision(12) << std::scientific << dresult << std::endl;
277  }
278 
279  // compute dot(x,y) in full double precision: multiply x[i] * y[i] in double, sum them in double, 0.0 is the additive identity
280  auto timePD = TimeMonitor::getNewTimer("Pure double dot()");
281  // set [w,z] = x
282  Tpetra::RTI::binary_transform( *w, *x, [](double /*wi*/, float xi) -> double {return xi;} );
283  Tpetra::RTI::binary_transform( *z, *x, [](double /*zi*/, float xi) -> double {return xi;} );
284  {
285  TimeMonitor lcltimer(*timePD);
286  for (int l=0; l != numTimeTrials; ++l)
287  dresult = Tpetra::RTI::reduce(*w, *z, reductionGlob< ZeroOp<double>>(
288  std::multiplies<double>(),
289  std::plus<double>()) );
290  }
291  if (verbose) {
292  std::cout << std::left << std::setw(25) << "pure double result" << " == " << std::setprecision(12) << std::scientific << dresult << std::endl
293  << std::endl;
294  }
295 
296  //
297  // Compute z = alpha*x, where z is double precision and x is single precision
298  //
299  Tpetra::RTI::binary_transform( *z, *x, [](double /*zi*/, float xi) -> double {return 2.0*xi;} );
300 
301  //
302  // Two simultaneous dot products: z'*w and z'*v at the same time, saves a pass through z
303  //
304  Tpetra::RTI::unary_transform( *z, [](double xi){return 1.0f;} );
305  Tpetra::RTI::unary_transform( *w, [](double xi){return 2.0f;} );
306  Tpetra::RTI::unary_transform( *v, [](double xi){return 3.0f;} );
307  auto timeTwoDotInd = TimeMonitor::getNewTimer("dot(z,w) and dot(z,v) independently");
308  {
309  TimeMonitor lcltimer(*timeTwoDotInd);
310  for (int l=0; l != numTimeTrials; ++l)
311  {
312  dresult = Tpetra::RTI::reduce(*z, *w, reductionGlob< ZeroOp<double>>(
313  std::multiplies<double>(),
314  std::plus<double>()) );
315  dresult = Tpetra::RTI::reduce(*z, *v, reductionGlob< ZeroOp<double>>(
316  std::multiplies<double>(),
317  std::plus<double>()) );
318  }
319  }
320  auto timeTwoDotSim = TimeMonitor::getNewTimer("dot(z,w) and dot(z,v) simultaneously");
321  {
322  TimeMonitor lcltimer(*timeTwoDotSim);
323  pair<double,double> tdres;
324  for (int l=0; l != numTimeTrials; ++l)
325  {
326  tdres = Tpetra::RTI::reduce(*z, *w, *v,
327  reductionGlob<ZeroOp<pair<double,double>>>(
328  [](double zi, double wi, double vi) { return make_pair(zi*wi, zi*vi); },
329  TpetraExamples::make_pair_op<double,double>(std::plus<double>()))
330  );
331  }
332  }
333 
334  //
335  // Print timings
336  //
337  if (verbose) {
338  TimeMonitor::summarize( std::cout );
339  }
340 
341  if (verbose) {
342  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
343  }
344  return 0;
345 }
346 
pair_op< T1, T2, Op > make_pair_op(Op op)
Non-member constructor to create a pair_op.
Definition: RTIExample.cpp:121
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
Glob::RedOP::result_type binary_pre_transform_reduce(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, Glob glob)
Transforms values of vec_inout while simultaneously performing a parallel reduction.
Definition: Tpetra_RTI.hpp:253
Reduction function object that combines pairs of T1, T2.
Definition: RTIExample.cpp:106
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
Function object multiplying two different types in a third type&#39;s precision.
Definition: RTIExample.cpp:81
static DefaultPlatformType & getDefaultPlatform()
Return the default platform.
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