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 #include "Kokkos_FirstTouchSparseOps.hpp"
00059 
00060 namespace NodeTest {
00061   extern int N;
00062   extern int numIters;
00063   extern int numThreads;
00064   extern int verbose;
00065 }
00066 
00067 namespace {
00068 
00069   using Teuchos::Array;
00070   using Teuchos::ArrayRCP;
00071   using Teuchos::Time;
00072   using Teuchos::RCP;
00073   using Teuchos::rcp;
00074   using Teuchos::null;
00075   using Teuchos::TimeMonitor;
00076   using Teuchos::ParameterList;
00077   using Kokkos::MultiVector;
00078   using Kokkos::DefaultArithmetic;
00079 
00080   template <class NODE>
00081   RCP<NODE> getNode() {
00082     TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Node type not defined.");
00083   }
00084 
00085   template <class NODE>
00086   void initNode() {
00087     TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Node type not defined.");
00088   }
00089 
00090   //
00091   // UNIT TESTS
00092   // 
00093 
00094   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( AAAAA_Is_First, InitNode, NODE )
00095   {
00096     out << "Initializing " << Teuchos::TypeNameTraits<NODE>::name() << std::endl;
00097     initNode<NODE>();
00098     TEST_EQUALITY(0,0);
00099   }
00100 
00103   //  tests
00104   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( CrsMatrix, TimingTest, BaseOPS )
00105   {
00106     typedef double                             Scalar;
00107     typedef typename BaseOPS::ordinal_type     Ordinal;
00108     typedef typename BaseOPS::node_type        Node;
00109     typedef typename BaseOPS::template bind_scalar<Scalar>::other_type       OPS;
00110     typedef typename OPS::template matrix<Scalar,Ordinal,Node>::matrix_type  MAT;
00111     typedef typename OPS::template graph<Ordinal,Node>::graph_type          GRPH;
00112     typedef MultiVector<Scalar,Node>                                          MV;
00113     typedef Teuchos::ScalarTraits<Scalar>                                     ST;
00114     const Scalar ONE = ST::one(),
00115                 ZERO = ST::zero();
00116     RCP<Node> node = getNode<Node>();
00117     // generate tridiagonal matrix:
00118     // [ 2 -1                   ]
00119     // [-1  3  -1               ]
00120     // [   -1   3  -1           ]
00121     // [                        ]
00122     // [                -1  3 -1]
00123     // [                   -1  2]
00124     const int N = NodeTest::N;
00125     if (N<2) return;
00126     RCP<GRPH> G = rcp(new GRPH (N,node,null) );
00127     RCP<MAT>  A = rcp(new MAT  (G,null) );
00128     // allocate data for ptrs, indices and values
00129     const size_t totalNNZ = 3*N - 2;
00130     ArrayRCP<size_t> ptrs(N+1);
00131     ArrayRCP<Ordinal>   inds(totalNNZ);
00132     ArrayRCP<Scalar>    vals(totalNNZ);
00133     // fill the data
00134     {
00135       size_t NNZsofar = 0;
00136       ptrs[0] = NNZsofar;
00137       inds[NNZsofar] = 0; inds[NNZsofar+1] =  1;
00138       vals[NNZsofar] = 2; vals[NNZsofar+1] = -1;
00139       NNZsofar += 2;
00140       for (int i=1; i != N-1; ++i) {
00141         ptrs[i] = NNZsofar;
00142         inds[NNZsofar] = i-1; inds[NNZsofar+1] = i; inds[NNZsofar+2] = i+1;
00143         vals[NNZsofar] =  -1; vals[NNZsofar+1] = 3; vals[NNZsofar+2] =  -1;
00144         NNZsofar += 3;
00145       }
00146       ptrs[N-1] = NNZsofar;
00147       inds[NNZsofar] = N-2; inds[NNZsofar+1] = N-1;
00148       vals[NNZsofar] =  -1; vals[NNZsofar+1] = 2;
00149       NNZsofar += 2;
00150       ptrs[N]   = NNZsofar;
00151       TEUCHOS_TEST_FOR_EXCEPT(NNZsofar != totalNNZ);
00152     }
00153     G->setStructure(ptrs, inds);
00154     ptrs = Teuchos::null;
00155     inds = Teuchos::null;
00156     A->setValues(vals);
00157     vals = Teuchos::null;
00158     OPS::finalizeGraphAndMatrix(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,*A,null);
00159     Teuchos::EDiag diag;
00160     Teuchos::EUplo uplo;
00161     G->getMatDesc(uplo,diag);
00162     TEST_EQUALITY_CONST( uplo, Teuchos::UNDEF_TRI );
00163     TEST_EQUALITY_CONST( diag, Teuchos::NON_UNIT_DIAG );
00164     OPS dsm(node);
00165     out << "Testing with sparse ops: " << Teuchos::typeName(dsm) << std::endl;
00166     dsm.setGraphAndMatrix(G,A);
00167 
00168     ArrayRCP<Scalar> xdat, axdat;
00169     xdat  = node->template allocBuffer<Scalar>(N);
00170     axdat = node->template allocBuffer<Scalar>(N);
00171     MV X(node), Y(node);
00172     X.initializeValues( N,1, xdat,N);
00173     Y.initializeValues(N,1,axdat,N);
00174     DefaultArithmetic<MV>::Init( X,1);
00175     Teuchos::RCP<Teuchos::Time> matvectime = Teuchos::TimeMonitor::getNewTimer("LocalTimer");
00176     {
00177       Teuchos::TimeMonitor lcltimer(*matvectime);
00178       for (int i=0; i<NodeTest::numIters; ++i) {
00179         // Y = A*X
00180         dsm.multiply(Teuchos::NO_TRANS,ONE,X,Y);
00181       }
00182     }
00183     out << "Time is " << matvectime->totalElapsedTime() << std::endl;
00184   }
00185 
00186 
00187   // 
00188   // INSTANTIATIONS
00189   //
00190 
00191   #define TEST_NODE( NODE ) \
00192     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( AAAAA_Is_First, InitNode, NODE ) \
00193     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsMatrix, TimingTest, DefaultHostSparseOps<void,int,NODE> ) \
00194     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsMatrix, TimingTest, FirstTouchSparseOps<void,int,NODE> )
00195 
00196 }
00197 
00198 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends