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 int N = 10;
00081     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00082     RCP<Graph> G = rcp(new Graph(N,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 (int 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 int 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,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 Ordinal N = 10;
00119     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00120     // build a non-empty graph, tridiagonal
00121     const Ordinal 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       Ordinal curoffset = 0;
00129       for (Ordinal 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,N,node,parameterList()));
00137     TEST_EQUALITY( G->getNumRows(), N );
00138     TEST_EQUALITY( G->getNumCols(), N );
00139     G->setStructure(ptrs, inds);
00140     SparseOps::finalizeGraph(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,null);
00141     ArrayRCP<const Ordinal> chkInds;
00142     chkInds = G->getIndices();
00143     TEST_EQUALITY( inds, chkInds );
00144     TEST_EQUALITY_CONST( G->isEmpty(), false );
00145     TEST_EQUALITY( G->getNumRows(), N );
00146     TEST_EQUALITY( G->getNumCols(), N );
00147     Matrix M(G,null);
00148     M.setValues(vals);
00149     SparseOps::finalizeMatrix(*G,M,null);
00150   }
00151 
00152 
00153   // intialize using static graph
00154   TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( CrsMatrix, DynamicGraph, Scalar, Ordinal )
00155   {
00156     typedef typename Kokkos::DefaultKernels<Scalar,Ordinal,Node>::SparseOps     BaseSparseOps;
00157     typedef typename BaseSparseOps::template bind_scalar<Scalar>::other_type        SparseOps;
00158     typedef typename SparseOps::template graph<Ordinal,Node>::graph_type                Graph;
00159     typedef typename SparseOps::template matrix<Scalar,Ordinal,Node>::matrix_type      Matrix;
00160     const Ordinal N = 10;
00161     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00162     // build a non-empty graph, tridiagonal
00163     const Ordinal testNumEntries = 3*N-2;
00164     ArrayRCP<Ordinal> inds(testNumEntries);
00165     ArrayRCP<Scalar > vals(testNumEntries);
00166     ArrayRCP<size_t> ptrs(N+1);
00167     {
00168       std::fill( inds.begin(), inds.end(), 0 );
00169       std::fill( vals.begin(), vals.end(), 0 );
00170       Ordinal curoffset = 0;
00171       for (Ordinal r=0; r < N; ++r) {
00172         Ordinal numper;
00173         if (r > 0 && r < N-1) numper = 3;
00174         else numper = 2;
00175         ptrs[r] = curoffset;
00176         curoffset += numper;
00177       }
00178       ptrs[N] = curoffset;
00179     }
00180     RCP<Graph> G = rcp(new Graph(N,N,node,null) );
00181     Matrix M(G,null);
00182     G->setStructure(ptrs,inds);
00183     M.setValues(vals);
00184     ArrayRCP<const Ordinal> chkInds;
00185     ArrayRCP<const Scalar> chkVals;
00186     chkInds = G->getIndices();
00187     chkVals = M.getValues();
00188     TEST_EQUALITY( inds, chkInds );
00189     TEST_EQUALITY( vals, chkVals );
00190     SparseOps::finalizeGraphAndMatrix(Teuchos::UNDEF_TRI,Teuchos::NON_UNIT_DIAG,*G,M,null);
00191     TEST_EQUALITY_CONST( G->isEmpty(),     false );
00192   }
00193 
00194   // no rows
00195   TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( CrsGraph, NoRows, Ordinal )
00196   {
00197     typedef typename Kokkos::DefaultKernels<void  ,Ordinal,Node>::SparseOps     BaseSparseOps;
00198     typedef typename BaseSparseOps::template graph<Ordinal,Node>::graph_type            Graph;
00199     const Ordinal N = 0;
00200     RCP<Node> node = Kokkos::DefaultNode::getDefaultNode();
00201 
00202     // build a null-size graph
00203     Graph G(N,N,node,null);
00204     {
00205       ArrayRCP<size_t>  ptrs;
00206       ArrayRCP<Ordinal> inds;
00207       ptrs = null;
00208       inds = null;
00209       // ptrs is null; not allowed
00210       TEST_THROW( G.setStructure(ptrs, inds), std::runtime_error );
00211       ptrs = arcp<size_t>(1);
00212       ptrs[0] = 0;
00213       G.setStructure(ptrs, inds);
00214       ptrs = null;
00215       inds = null;
00216       ArrayRCP<const size_t>  getptrs;
00217       ArrayRCP<const Ordinal> getinds;
00218       getptrs = G.getPointers();
00219       getinds = G.getIndices();
00220       TEST_INEQUALITY_CONST( getptrs, null );
00221       TEST_EQUALITY_CONST( getinds, null );
00222     }
00223   }
00224 
00225  #define UNIT_TEST_GROUP_ORDINAL_SCALAR( ORDINAL, SCALAR ) \
00226        TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( CrsMatrix, StaticGraph,  SCALAR, ORDINAL ) \
00227        TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( CrsMatrix, DynamicGraph, SCALAR, ORDINAL )
00228 
00229  #define UNIT_TEST_GROUP_ORDINAL( ORDINAL ) \
00230           TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CrsGraph, NoRows,  ORDINAL ) \
00231           UNIT_TEST_GROUP_ORDINAL_SCALAR(ORDINAL, int) \
00232           UNIT_TEST_GROUP_ORDINAL_SCALAR(ORDINAL, float)
00233       UNIT_TEST_GROUP_ORDINAL(int)
00234       typedef short int ShortInt; UNIT_TEST_GROUP_ORDINAL(ShortInt)
00235 
00236 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends