Kokkos Node API and Local Linear Algebra Kernels Version of the Day
CrsMatrix_CrsTimingTests.hpp
Go to the documentation of this file.
00001 #include <Teuchos_TimeMonitor.hpp>
00002 
00007   namespace Test {
00008     int numRows = 1000;
00009     int numIters = 1000;
00010     int numThreads = 0;
00011   }
00012 
00013   template <class Node>
00014   RCP<Node> getNode() {
00015     assert(false);
00016   }
00017 
00018   TEUCHOS_STATIC_SETUP()
00019   {
00020     Teuchos::CommandLineProcessor &clp = Teuchos::UnitTestRepository::getCLP();
00021     clp.addOutputSetupOptions(true);
00022     clp.setOption("test-size",&Test::numRows,"Vector length for tests.");
00023     clp.setOption("test-iters",&Test::numIters,"Number of mat-vecs to time.");
00024     clp.setOption("num-threads",&Test::numThreads,"Number of threads.");
00025   }
00026 
00027 
00028   TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( CrsTiming, PowerTriDiag, GRPH, MAT )
00029   {
00030     typedef typename MAT::ScalarType        Scalar;
00031     typedef typename MAT::OrdinalType       Ordinal;
00032     typedef typename MAT::NodeType          Node;
00033     typedef typename MAT::LocalMatOpsType   SparseOps;
00034     typedef MultiVector<Scalar,Node>                                 MV;
00035     typedef Teuchos::ScalarTraits<Scalar>                            ST;
00036     
00037     using Teuchos::arcpClone;
00038     using Teuchos::tuple;
00039 
00040     out << "Testing " << Teuchos::TypeNameTraits<GRPH>::name() << std::endl 
00041         << "    and " << Teuchos::TypeNameTraits<MAT>::name()  << std::endl;
00042 
00043     RCP<Node> node = getNode<Node>();
00044 
00045     GRPH G(Test::numRows,node);
00046     MAT  A(G);
00047     // 
00048     // allocate buffers and fill the graph and matrix
00049     // must allocate 2D and finalize(true) to get first-touch reallocation
00050     // 
00051     {
00052       ArrayRCP<size_t> numEntries(Test::numRows);
00053       ArrayRCP<ArrayRCP<Ordinal> >   inds(Test::numRows);
00054       ArrayRCP<ArrayRCP<Scalar > >   vals(Test::numRows);
00055       numEntries[0] = 2;
00056       inds[0] = arcpClone<Ordinal>( tuple<Ordinal>(0,1) );
00057       vals[0] = arcpClone<Scalar >( tuple<Scalar >(2,-1) );
00058       for (int i=1; i != Test::numRows-1; ++i) {
00059         numEntries[i] = 3;
00060         inds[i] = arcpClone<Ordinal>( tuple<Ordinal>(i-1,i,i+1) );
00061         vals[i] = arcpClone<Scalar >( tuple<Scalar >( -1,3, -1) );
00062       }
00063       numEntries[Test::numRows-1] = 2;
00064       inds[Test::numRows-1] = arcpClone<Ordinal>( tuple<Ordinal>(Test::numRows-2,Test::numRows-1) );
00065       vals[Test::numRows-1] = arcpClone<Scalar> ( tuple<Scalar >(-1,2) );
00066       G.set2DStructure(inds, numEntries);
00067       inds    = Teuchos::null;
00068       A.set2DValues(vals);
00069       vals    = Teuchos::null;
00070       const bool PleaseOptimizeStorage = true;
00071       A.finalize(PleaseOptimizeStorage);
00072     }
00073     // 
00074     // fill the matvec
00075     // 
00076     typename SparseOps::template rebind<Scalar>::other matvec(node);
00077     matvec.initializeStructure(G);
00078     matvec.initializeValues(A);
00079     // 
00080     // time the matvec
00081     // 
00082     MV X(node), Y(node);
00083     X.initializeValues( Test::numRows,1, node->template allocBuffer<Scalar>(Test::numRows), Test::numRows);
00084     Y.initializeValues( Test::numRows,1, node->template allocBuffer<Scalar>(Test::numRows), Test::numRows);
00085     DefaultArithmetic<MV>::Init( X, ST::one() );
00086     DefaultArithmetic<MV>::Init( Y, ST::one() );
00087     Teuchos::RCP<Teuchos::Time> matvectime = Teuchos::TimeMonitor::getNewTimer("LocalTimer");
00088     {
00089       Teuchos::TimeMonitor lcltimer(*matvectime);
00090       for (int i=0; i<Test::numIters; ++i) {
00091         if ( (i%2) == 0 ) {
00092           matvec.multiply(Teuchos::NO_TRANS,ST::one(),X,Y);
00093         }
00094         else {
00095           matvec.multiply(Teuchos::NO_TRANS,ST::one(),Y,X);
00096         }
00097       }
00098     }
00099     out << "Time is " << matvectime->totalElapsedTime() << std::endl;
00100   }
00101 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends