#include <Teuchos_GlobalMPISession.hpp>
#include <Teuchos_oblackholestream.hpp>
#include <Teuchos_CommandLineProcessor.hpp>
#include <Teuchos_TimeMonitor.hpp>
#include <Kokkos_DefaultNode.hpp>
#include <Tpetra_Version.hpp>
#include <Tpetra_DefaultPlatform.hpp>
#include <Tpetra_MultiVector.hpp>
#include <Tpetra_RTI.hpp>
#include <iostream>
#include <iomanip>
#include <functional>
#include <utility>
namespace TpetraExamples {
template <class Arg1, class Arg2, class MultPrec>
class mprec_mult : public std::binary_function<Arg1,Arg2,MultPrec> {
public:
mprec_mult() {}
inline MultPrec
operator() (const Arg1& x, const Arg2& y) const {
return (MultPrec)(x) * (MultPrec)(y);
}
};
template <class T1, class T2, class Op>
class pair_op :
public std::binary_function<std::pair<T1,T2>, std::pair<T1,T2>, std::pair<T1,T2>> {
private:
Op op_;
public:
pair_op(Op op) : op_(op) {}
inline std::pair<T1,T2>
operator() (const std::pair<T1,T2>& a, const std::pair<T1,T2>& b) const {
return std::make_pair (op_ (a.first, b.first), op_ (a.second, b.second));
}
};
template <class T1, class T2, class Op>
return pair_op<T1,T2,Op>(op);
}
}
namespace Tpetra {
namespace RTI {
template <class T1, class T2>
class ZeroOp<std::pair<T1,T2>> {
public:
static inline std::pair<T1,T2> identity() {
return std::make_pair( Teuchos::ScalarTraits<T1>::zero(),
Teuchos::ScalarTraits<T2>::zero() );
}
};
}
}
int main(int argc, char *argv[])
{
Teuchos::oblackholestream blackhole;
Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
using Teuchos::TimeMonitor;
using std::make_pair;
using std::pair;
auto comm = platform.getComm();
auto node = platform.getNode();
const int myImageID = comm->getRank();
bool verbose = (myImageID==0);
int numGlobal_user = 100*comm->getSize();
int numTimeTrials = 3;
Teuchos::CommandLineProcessor cmdp(false,true);
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
cmdp.setOption("global-size",&numGlobal_user,"Global test size.");
cmdp.setOption("num-time-trials",&numTimeTrials,"Number of trials in timing loops.");
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
return -1;
}
if (verbose) {
std::cout << "\n" << Tpetra::version() << std::endl;
std::cout << "Comm info: " << *comm;
std::cout << "Node type: " << Teuchos::typeName(*node) << std::endl;
std::cout << std::endl;
}
auto map = Tpetra::createUniformContigMapWithNode<int,int>(numGlobalRows, comm, node);
const size_t numLocalRows = map->getNodeNumElements();
auto x = Tpetra::createVector<float>(map),
y = Tpetra::createVector<float>(map);
auto z = Tpetra::createVector<double>(map),
w = Tpetra::createVector<double>(map),
v = Tpetra::createVector<double>(map);
reductionGlob<
ZeroOp<float>>(
std::multiplies<float>(),
std::plus<float>()
) );
if (verbose) {
std::cout << std::left << "dot( ones, twos ) result == " << fresult
<< ", expected value is " << numGlobalRows*2.0f << std::endl;
}
{
Teuchos::ArrayRCP<float> xdata = x->get1dViewNonConst();
for (size_t i=1; i < numLocalRows; ++i) {
xdata[i] = 1.0f / 4096.0f;
}
}
auto timePF = TimeMonitor::getNewTimer("Pure float dot()");
{
TimeMonitor lcltimer(*timePF);
for (int l=0; l != numTimeTrials; ++l)
fresult =
Tpetra::RTI::reduce(*x, *y, reductionGlob<ZeroOp<float>>(std::multiplies<float>(), std::plus<float>()) );
}
if (verbose) {
std::cout << std::left << std::endl << std::setw(25) << "pure float result" << " == " << std::setprecision(12) << std::scientific << fresult << std::endl;
}
auto timeAD = TimeMonitor::getNewTimer("Double acc. dot()");
{
TimeMonitor lcltimer(*timeAD);
for (int l=0; l != numTimeTrials; ++l)
fresult =
Tpetra::RTI::reduce(*x, *y, reductionGlob<ZeroOp<double>>(std::multiplies<float>(), std::plus<double>()) );
}
if (verbose) {
std::cout << std::left << std::setw(25) << "double acc. result" << " == " << std::setprecision(12) << std::scientific << fresult << std::endl;
}
double dresult = 0.0;
auto timeMAD = TimeMonitor::getNewTimer("Double mult./acc. dot()");
{
TimeMonitor lcltimer(*timeMAD);
for (int l=0; l != numTimeTrials; ++l)
mprec_mult<float,float,double>(),
std::plus<double>()) );
}
if (verbose) {
std::cout << std::left << std::setw(25) << "double mult/add result" << " == " << std::setprecision(12) << std::scientific << dresult << std::endl;
}
auto timePD = TimeMonitor::getNewTimer("Pure double dot()");
{
TimeMonitor lcltimer(*timePD);
for (int l=0; l != numTimeTrials; ++l)
std::multiplies<double>(),
std::plus<double>()) );
}
if (verbose) {
std::cout << std::left << std::setw(25) << "pure double result" << " == " << std::setprecision(12) << std::scientific << dresult << std::endl
<< std::endl;
}
auto timeTwoDotInd = TimeMonitor::getNewTimer("dot(z,w) and dot(z,v) independently");
{
TimeMonitor lcltimer(*timeTwoDotInd);
for (int l=0; l != numTimeTrials; ++l)
{
std::multiplies<double>(),
std::plus<double>()) );
std::multiplies<double>(),
std::plus<double>()) );
}
}
auto timeTwoDotSim = TimeMonitor::getNewTimer("dot(z,w) and dot(z,v) simultaneously");
{
TimeMonitor lcltimer(*timeTwoDotSim);
pair<double,double> tdres;
for (int l=0; l != numTimeTrials; ++l)
{
reductionGlob<ZeroOp<pair<double,double>>>(
[](double zi, double wi, double vi) { return make_pair(zi*wi, zi*vi); },
TpetraExamples::make_pair_op<double,double>(std::plus<double>()))
);
}
}
if (verbose) {
TimeMonitor::summarize( std::cout );
}
if (verbose) {
std::cout << "\nEnd Result: TEST PASSED" << std::endl;
}
return 0;
}