Kokkos Node API and Local Linear Algebra Kernels Version of the Day
CrsMatrix_UnitTests.cpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #include <Teuchos_UnitTestHarness.hpp>
00043 
00044 #include "Kokkos_ConfigDefs.hpp"
00045 #include "Kokkos_DefaultNode.hpp"
00046 #include "Kokkos_DefaultKernels.hpp"
00047 #include "Kokkos_Version.hpp"
00048 
00049 namespace {
00050 
00051   using Kokkos::DefaultNode;
00052   using Teuchos::ArrayRCP;
00053   using Teuchos::RCP;
00054   using Teuchos::rcp;
00055   using Teuchos::null;
00056   using Teuchos::arcp;
00057   using Teuchos::ParameterList;
00058   using Teuchos::parameterList;
00059 
00060   typedef Kokkos::DefaultNode::DefaultNodeType Node;
00061 
00062   int N = 100;
00063 
00064   TEUCHOS_STATIC_SETUP()
00065   {
00066     Teuchos::CommandLineProcessor &clp = Teuchos::UnitTestRepository::getCLP();
00067     clp.addOutputSetupOptions(true);
00068     clp.setOption("test-size",&N,"Vector length for tests.");
00069   }
00070 
00071   //
00072   // UNIT TESTS
00073   // 
00074 
00075   // intialize using static graph
00076   TEUCHOS_UNIT_TEST( CrsGraph, RuntimeExceptions )
00077   {
00078     typedef Kokkos::DefaultKernels<double,int,Node>::SparseOps SparseOps;
00079     typedef SparseOps::graph<int,Node>::graph_type             Graph;
00080     const size_t N = 10;
00081     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00082     RCP<Graph> G = rcp(new Graph(N,node,parameterList()));
00083     {
00084       ArrayRCP<size_t>  ptrs_tooSmall(N), ptrs_tooBig(N+2);
00085       ArrayRCP<int>     inds;
00086       TEST_THROW( G->setStructure(ptrs_tooSmall, inds), std::runtime_error );
00087       TEST_THROW( G->setStructure(ptrs_tooBig,   inds), std::runtime_error );
00088     }
00089     {
00090       ArrayRCP<size_t> ptrs(N+1);
00091       for (size_t i=0; i<=N; ++i) ptrs[i] = i;
00092       ArrayRCP<int> tooFewInds(N-1);
00093       TEST_THROW( G->setStructure(ptrs, tooFewInds), std::runtime_error );
00094     }
00095   }
00096 
00097   TEUCHOS_UNIT_TEST( CrsGraph, StatusTests )
00098   {
00099     typedef Kokkos::DefaultKernels<double,int,Node>::SparseOps SparseOps;
00100     typedef SparseOps::graph<int,Node>::graph_type             Graph;
00101     const size_t N = 10;
00102     ArrayRCP<size_t> ptrs = arcp<size_t>(N+1);
00103     std::fill(ptrs.begin(), ptrs.end(), 0);
00104     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00105     RCP<Graph> G = rcp(new Graph(N,node,parameterList()));
00106     G->setStructure(ptrs,null);
00107     RCP<ParameterList> params = parameterList();
00108     SparseOps::finalizeGraph(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,params);
00109     TEST_EQUALITY_CONST( G->isEmpty(), true );
00110   }
00111 
00112   TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( CrsMatrix, StaticGraph, Scalar, Ordinal )
00113   {
00114     typedef typename Kokkos::DefaultKernels<Scalar,Ordinal,Node>::SparseOps     BaseSparseOps;
00115     typedef typename BaseSparseOps::template bind_scalar<Scalar>::other_type        SparseOps;
00116     typedef typename SparseOps::template graph<Ordinal,Node>::graph_type                Graph;
00117     typedef typename SparseOps::template matrix<Scalar,Ordinal,Node>::matrix_type      Matrix;
00118     const size_t N = 10;
00119     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00120     // build a non-empty graph, tridiagonal
00121     const size_t testNumEntries = 3*N-2;
00122     ArrayRCP<Ordinal> inds(testNumEntries);
00123     ArrayRCP<Scalar > vals(testNumEntries);
00124     ArrayRCP<size_t> ptrs(N+1);
00125     {
00126       std::fill( inds.begin(), inds.end(), 0 );
00127       std::fill( vals.begin(), vals.end(), 0 );
00128       size_t curoffset = 0;
00129       for (size_t r=0; r < N; ++r) {
00130         ptrs[r] = curoffset;
00131         if (r > 0 && r < N-1) curoffset += 3;
00132         else                  curoffset += 2;
00133       }
00134       ptrs[N] = curoffset;
00135     }
00136     RCP<Graph> G = rcp(new Graph(N,node,parameterList()));
00137     TEST_EQUALITY( G->getNumRows(), N );
00138     G->setStructure(ptrs, inds);
00139     SparseOps::finalizeGraph(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,null);
00140     ArrayRCP<const Ordinal> chkInds;
00141     ArrayRCP<const size_t> chkPtrs;
00142     chkInds = G->getIndices();
00143     chkPtrs = G->getPointers();
00144     TEST_EQUALITY( inds, chkInds );
00145     TEST_EQUALITY( ptrs, chkPtrs );
00146     TEST_EQUALITY_CONST( G->isEmpty(), false );
00147     TEST_EQUALITY( G->getNumRows(), N );
00148     Matrix M(G,null);
00149     M.setValues(vals);
00150     SparseOps::finalizeMatrix(*G,M,null);
00151   }
00152 
00153 
00154   // intialize using static graph
00155   TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( CrsMatrix, DynamicGraph, Scalar, Ordinal )
00156   {
00157     typedef typename Kokkos::DefaultKernels<Scalar,Ordinal,Node>::SparseOps     BaseSparseOps;
00158     typedef typename BaseSparseOps::template bind_scalar<Scalar>::other_type        SparseOps;
00159     typedef typename SparseOps::template graph<Ordinal,Node>::graph_type                Graph;
00160     typedef typename SparseOps::template matrix<Scalar,Ordinal,Node>::matrix_type      Matrix;
00161     const size_t N = 10;
00162     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00163     // build a non-empty graph, tridiagonal
00164     const size_t testNumEntries = 3*N-2;
00165     ArrayRCP<Ordinal> inds(testNumEntries);
00166     ArrayRCP<Scalar > vals(testNumEntries);
00167     ArrayRCP<size_t> ptrs(N+1);
00168     {
00169       std::fill( inds.begin(), inds.end(), 0 );
00170       std::fill( vals.begin(), vals.end(), 0 );
00171       size_t curoffset = 0;
00172       for (size_t r=0; r < N; ++r) {
00173         size_t numper;
00174         if (r > 0 && r < N-1) numper = 3;
00175         else numper = 2;
00176         ptrs[r] = curoffset;
00177         curoffset += numper;
00178       }
00179       ptrs[N] = curoffset;
00180     }
00181     RCP<Graph> G = rcp(new Graph(N,node,null) );
00182     Matrix M(G,null);
00183     G->setStructure(ptrs,inds);
00184     M.setValues(vals);
00185     ArrayRCP<const Ordinal> chkInds;
00186     ArrayRCP<const size_t> chkPtrs;
00187     ArrayRCP<const Scalar> chkVals;
00188     chkPtrs = G->getPointers();
00189     chkInds = G->getIndices();
00190     chkVals = M.getValues();
00191     TEST_EQUALITY( ptrs, chkPtrs );
00192     TEST_EQUALITY( inds, chkInds );
00193     TEST_EQUALITY( vals, chkVals );
00194     SparseOps::finalizeGraphAndMatrix(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,M,null);
00195     TEST_EQUALITY_CONST( G->isEmpty(),     false );
00196   }
00197 
00198   // no rows
00199   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( CrsGraph, NoRows, Ordinal )
00200   {
00201     typedef typename Kokkos::DefaultKernels<void  ,Ordinal,Node>::SparseOps     BaseSparseOps;
00202     typedef typename BaseSparseOps::template graph<Ordinal,Node>::graph_type            Graph;
00203     const size_t N = 0;
00204     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00205 
00206     // build a null-size graph
00207     Graph G(N,node,null);
00208     {
00209       ArrayRCP<size_t> ptrs;
00210       ArrayRCP<Ordinal> inds;
00211       ptrs = null;
00212       inds = null;
00213       // ptrs is null; not allowed
00214       TEST_THROW( G.setStructure(ptrs, inds), std::runtime_error );
00215       ptrs = arcp<size_t>(1);
00216       ptrs[0] = 0;
00217       G.setStructure(ptrs, inds);
00218       ptrs = null;
00219       inds = null;
00220       ArrayRCP<const size_t> getptrs;
00221       ArrayRCP<const Ordinal> getinds;
00222       getptrs = G.getPointers();
00223       getinds = G.getIndices();
00224       TEST_INEQUALITY_CONST( getptrs, null );
00225       TEST_EQUALITY_CONST( getinds, null );
00226     }
00227   }
00228 
00229  #define UNIT_TEST_GROUP_ORDINAL_SCALAR( ORDINAL, SCALAR ) \
00230        TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( CrsMatrix, StaticGraph,  SCALAR, ORDINAL ) \
00231        TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( CrsMatrix, DynamicGraph, SCALAR, ORDINAL )
00232 
00233  #define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
00234           TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsGraph, NoRows,  ORDINAL ) \
00235           UNIT_TEST_GROUP_ORDINAL_SCALAR(ORDINAL, int) \
00236           UNIT_TEST_GROUP_ORDINAL_SCALAR(ORDINAL, float)
00237       UNIT_TEST_GROUP_ORDINAL(int)
00238       typedef short int ShortInt; UNIT_TEST_GROUP_ORDINAL(ShortInt)
00239 
00240 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends