Kokkos Node API and Local Linear Algebra Kernels Version of the Day
CrsTimingTest.hpp
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 // 
00005 //          Kokkos: Node API and Parallel Node Kernels
00006 //              Copyright (2008) Sandia Corporation
00007 // 
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00039 // 
00040 // ************************************************************************
00041 //@HEADER
00042 */
00043 
00044 #ifndef NODE_TEST_HPP_
00045 #define NODE_TEST_HPP_ 
00046 
00047 #include <Teuchos_UnitTestHarness.hpp>
00048 #include <Teuchos_TimeMonitor.hpp>
00049 #include <Teuchos_Time.hpp>
00050 #include <Teuchos_Tuple.hpp>
00051 #include <Teuchos_ParameterList.hpp>
00052 
00053 #include <functional>
00054 #include <algorithm>
00055 
00056 #include "Kokkos_ConfigDefs.hpp"
00057 #include "Kokkos_DefaultSparseOps.hpp"
00058 
00059 namespace NodeTest {
00060   extern int N;
00061   extern int numIters;
00062   extern int numThreads;
00063   extern int verbose;
00064 }
00065 
00066 namespace {
00067 
00068   using Teuchos::Array;
00069   using Teuchos::ArrayRCP;
00070   using Teuchos::Time;
00071   using Teuchos::RCP;
00072   using Teuchos::rcp;
00073   using Teuchos::null;
00074   using Teuchos::TimeMonitor;
00075   using Teuchos::ParameterList;
00076   using Kokkos::MultiVector;
00077   using Kokkos::DefaultArithmetic;
00078 
00079   template <class NODE>
00080   RCP<NODE> getNode() {
00081     TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Node type not defined.");
00082   }
00083 
00084   template <class NODE>
00085   void initNode() {
00086     TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Node type not defined.");
00087   }
00088 
00089   //
00090   // UNIT TESTS
00091   // 
00092 
00093   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( AAAAA_Is_First, InitNode, NODE )
00094   {
00095     out << "Initializing " << Teuchos::TypeNameTraits<NODE>::name() << std::endl;
00096     initNode<NODE>();
00097     TEST_EQUALITY(0,0);
00098   }
00099 
00102   //  tests
00103   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( CrsMatrix, TimingTest, Allocator )
00104   {
00105     typedef double                             Scalar;
00106     typedef typename BaseOPS::ordinal_type     Ordinal;
00107     typedef typename BaseOPS::node_type        Node;
00108     typedef typename BaseOPS::template bind_scalar<Scalar>::other_type       OPS;
00109     typedef typename OPS::template matrix<Scalar,Ordinal,Node>::matrix_type  MAT;
00110     typedef typename OPS::template graph<Ordinal,Node>::graph_type          GRPH;
00111     typedef MultiVector<Scalar,Node>                                          MV;
00112     typedef Teuchos::ScalarTraits<Scalar>                                     ST;
00113     const Scalar ONE = ST::one(),
00114                 ZERO = ST::zero();
00115     RCP<Node> node = getNode<Node>();
00116     // generate tridiagonal matrix:
00117     // [ 2 -1                   ]
00118     // [-1  3  -1               ]
00119     // [   -1   3  -1           ]
00120     // [                        ]
00121     // [                -1  3 -1]
00122     // [                   -1  2]
00123     const int N = NodeTest::N;
00124     if (N<2) return;
00125     RCP<GRPH> G = rcp(new GRPH (N,node,null) );
00126     RCP<MAT>  A = rcp(new MAT  (G,null) );
00127     // allocate data for ptrs, indices and values
00128     const size_t totalNNZ = 3*N - 2;
00129     ArrayRCP<size_t> ptrs(N+1);
00130     ArrayRCP<Ordinal>   inds(totalNNZ);
00131     ArrayRCP<Scalar>    vals(totalNNZ);
00132     // fill the data
00133     {
00134       size_t NNZsofar = 0;
00135       ptrs[0] = NNZsofar;
00136       inds[NNZsofar] = 0; inds[NNZsofar+1] =  1;
00137       vals[NNZsofar] = 2; vals[NNZsofar+1] = -1;
00138       NNZsofar += 2;
00139       for (int i=1; i != N-1; ++i) {
00140         ptrs[i] = NNZsofar;
00141         inds[NNZsofar] = i-1; inds[NNZsofar+1] = i; inds[NNZsofar+2] = i+1;
00142         vals[NNZsofar] =  -1; vals[NNZsofar+1] = 3; vals[NNZsofar+2] =  -1;
00143         NNZsofar += 3;
00144       }
00145       ptrs[N-1] = NNZsofar;
00146       inds[NNZsofar] = N-2; inds[NNZsofar+1] = N-1;
00147       vals[NNZsofar] =  -1; vals[NNZsofar+1] = 2;
00148       NNZsofar += 2;
00149       ptrs[N]   = NNZsofar;
00150       TEUCHOS_TEST_FOR_EXCEPT(NNZsofar != totalNNZ);
00151     }
00152     G->setStructure(ptrs, inds);
00153     ptrs = Teuchos::null;
00154     inds = Teuchos::null;
00155     A->setValues(vals);
00156     vals = Teuchos::null;
00157     OPS::finalizeGraphAndMatrix(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,*A,null);
00158     Teuchos::EDiag diag;
00159     Teuchos::EUplo uplo;
00160     G->getMatDesc(uplo,diag);
00161     TEST_EQUALITY_CONST( uplo, Teuchos::UNDEF_TRI );
00162     TEST_EQUALITY_CONST( diag, Teuchos::NON_UNIT_DIAG );
00163     OPS dsm(node);
00164     out << "Testing with sparse ops: " << Teuchos::typeName(dsm) << std::endl;
00165     dsm.setGraphAndMatrix(G,A);
00166 
00167     ArrayRCP<Scalar> xdat, axdat;
00168     xdat  = node->template allocBuffer<Scalar>(N);
00169     axdat = node->template allocBuffer<Scalar>(N);
00170     MV X(node), Y(node);
00171     X.initializeValues( N,1, xdat,N);
00172     Y.initializeValues(N,1,axdat,N);
00173     DefaultArithmetic<MV>::Init( X,1);
00174     Teuchos::RCP<Teuchos::Time> matvectime = Teuchos::TimeMonitor::getNewTimer("LocalTimer");
00175     {
00176       Teuchos::TimeMonitor lcltimer(*matvectime);
00177       for (int i=0; i<NodeTest::numIters; ++i) {
00178         // Y = A*X
00179         dsm.multiply(Teuchos::NO_TRANS,ONE,X,Y);
00180       }
00181     }
00182     out << "Time is " << matvectime->totalElapsedTime() << std::endl;
00183   }
00184 
00185 
00186   // 
00187   // INSTANTIATIONS
00188   //
00189 
00190   #define TEST_NODE( NODE ) \
00191     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( AAAAA_Is_First, InitNode, NODE ) \
00192     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsMatrix, TimingTest, DefaultHostSparseOps<void,int,NODE,DefaultCRSAllocator<NODE> > ) \
00193     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsMatrix, TimingTest, DefaultHostSparseOps<void,int,NODE,FirstTouchCRSAllocator<NODE> > )
00194 
00195 }
00196 
00197 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends