44 #include <Teuchos_GlobalMPISession.hpp>
45 #include <Teuchos_oblackholestream.hpp>
46 #include <Teuchos_CommandLineProcessor.hpp>
47 #include <Teuchos_TimeMonitor.hpp>
49 #include <Kokkos_DefaultNode.hpp>
51 #include <Tpetra_Version.hpp>
52 #include <Tpetra_DefaultPlatform.hpp>
53 #include <Tpetra_MultiVector.hpp>
54 #include <Tpetra_RTI.hpp>
65 namespace TpetraExamples {
80 template <
class Arg1,
class Arg2,
class MultPrec>
81 class mprec_mult :
public std::binary_function<Arg1,Arg2,MultPrec> {
85 operator() (
const Arg1& x,
const Arg2& y)
const {
86 return (MultPrec)(x) * (MultPrec)(y);
105 template <
class T1,
class T2,
class Op>
107 public std::binary_function<std::pair<T1,T2>, std::pair<T1,T2>, std::pair<T1,T2>> {
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));
120 template <
class T1,
class T2,
class Op>
132 template <
class T1,
class T2>
133 class ZeroOp<std::pair<T1,T2>> {
135 static inline std::pair<T1,T2> identity() {
136 return std::make_pair( Teuchos::ScalarTraits<T1>::zero(),
137 Teuchos::ScalarTraits<T2>::zero() );
143 int main(
int argc,
char *argv[])
148 Teuchos::oblackholestream blackhole;
149 Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
151 using Teuchos::TimeMonitor;
155 using std::make_pair;
162 auto comm = platform.getComm();
163 auto node = platform.getNode();
164 const int myImageID = comm->getRank();
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) {
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;
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);
217 std::multiplies<float>(),
221 std::cout << std::left <<
"dot( ones, twos ) result == " << fresult
222 <<
", expected value is " << numGlobalRows*2.0f << std::endl;
231 Teuchos::ArrayRCP<float> xdata = x->get1dViewNonConst();
232 for (
size_t i=1; i < numLocalRows; ++i) {
233 xdata[i] = 1.0f / 4096.0f;
240 auto timePF = TimeMonitor::getNewTimer(
"Pure float dot()");
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>()) );
247 std::cout << std::left << std::endl << std::setw(25) <<
"pure float result" <<
" == " << std::setprecision(12) << std::scientific << fresult << std::endl;
255 auto timeAD = TimeMonitor::getNewTimer(
"Double acc. dot()");
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>()) );
262 std::cout << std::left << std::setw(25) <<
"double acc. result" <<
" == " << std::setprecision(12) << std::scientific << fresult << std::endl;
266 double dresult = 0.0;
267 auto timeMAD = TimeMonitor::getNewTimer(
"Double mult./acc. dot()");
269 TimeMonitor lcltimer(*timeMAD);
270 for (
int l=0; l != numTimeTrials; ++l)
272 mprec_mult<float,float,double>(),
273 std::plus<double>()) );
276 std::cout << std::left << std::setw(25) <<
"double mult/add result" <<
" == " << std::setprecision(12) << std::scientific << dresult << std::endl;
280 auto timePD = TimeMonitor::getNewTimer(
"Pure double dot()");
285 TimeMonitor lcltimer(*timePD);
286 for (
int l=0; l != numTimeTrials; ++l)
288 std::multiplies<double>(),
289 std::plus<double>()) );
292 std::cout << std::left << std::setw(25) <<
"pure double result" <<
" == " << std::setprecision(12) << std::scientific << dresult << std::endl
307 auto timeTwoDotInd = TimeMonitor::getNewTimer(
"dot(z,w) and dot(z,v) independently");
309 TimeMonitor lcltimer(*timeTwoDotInd);
310 for (
int l=0; l != numTimeTrials; ++l)
313 std::multiplies<double>(),
314 std::plus<double>()) );
316 std::multiplies<double>(),
317 std::plus<double>()) );
320 auto timeTwoDotSim = TimeMonitor::getNewTimer(
"dot(z,w) and dot(z,v) simultaneously");
322 TimeMonitor lcltimer(*timeTwoDotSim);
323 pair<double,double> tdres;
324 for (
int l=0; l != numTimeTrials; ++l)
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>()))
338 TimeMonitor::summarize( std::cout );
342 std::cout <<
"\nEnd Result: TEST PASSED" << std::endl;
pair_op< T1, T2, Op > make_pair_op(Op op)
Non-member constructor to create a pair_op.
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.
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.
Reduction function object that combines pairs of T1, T2.
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.
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...
Function object multiplying two different types in a third type's precision.
A static identity functor, providing a static method identity() that returns zero.
void unary_transform(Vector< S, LO, GO, Node > &vec_inout, OP op)
Transform values of vec_inout using via operator op.