Zoltan 2 Version 0.5
IdentifierModel.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 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 Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 //
00046 // Testing of IdentifierModel
00047 //
00048 //   TODO  test with BasicIdentifierInput with weights
00049 
00050 #include <Zoltan2_IdentifierModel.hpp>
00051 #include <Zoltan2_XpetraCrsMatrixInput.hpp>
00052 #include <Zoltan2_BasicIdentifierInput.hpp>
00053 #include <Zoltan2_TestHelpers.hpp>
00054 
00055 #include <set>
00056 #include <bitset>
00057 
00058 #include <Teuchos_Comm.hpp>
00059 #include <Teuchos_DefaultComm.hpp>
00060 #include <Teuchos_ArrayView.hpp>
00061 #include <Teuchos_OrdinalTraits.hpp>
00062 
00063 #include <Tpetra_CrsMatrix.hpp>
00064 
00065 using namespace std;
00066 using Teuchos::RCP;
00067 using Teuchos::rcp;
00068 using Teuchos::Comm;
00069 using Teuchos::DefaultComm;
00070 
00071 void testIdentifierModel(std::string fname, gno_t xdim, gno_t ydim, gno_t zdim,
00072   const RCP<const Comm<int> > &comm, bool consecutiveIds)
00073 {
00074   int rank = comm->getRank();
00075   int fail = 0, gfail = 0;
00076 
00077   std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags = 0;
00078   if (consecutiveIds)
00079     modelFlags.set(Zoltan2::IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
00080 
00081   RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
00082 
00084   // Use an Tpetra::CrsMatrix for the user data.
00086   typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t> tcrsMatrix_t;
00087   
00088   UserInputForTests *input;
00089   if (fname.size() > 0)
00090     input = new UserInputForTests(testDataFilePath, fname, comm, true);
00091   else
00092     input = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true);
00093 
00094   RCP<tcrsMatrix_t > M = input->getTpetraCrsMatrix();
00095   lno_t nLocalIds = M->getNodeNumRows();
00096   gno_t nGlobalIds =  M->getGlobalNumRows();
00097 
00098   ArrayView<const gno_t> idList = M->getRowMap()->getNodeElementList();
00099   std::set<gno_t> idSet(idList.begin(), idList.end());
00100 
00102   // Create an IdentifierModel with this input
00104 
00105   typedef Zoltan2::XpetraCrsMatrixInput<tcrsMatrix_t> adapter_t;
00106   typedef Zoltan2::MatrixInput<tcrsMatrix_t> base_adapter_t;
00107   typedef Zoltan2::StridedData<lno_t, scalar_t> input_t;
00108 
00109   RCP<const adapter_t> ia = Teuchos::rcp(new adapter_t(M));
00110   
00111   Zoltan2::IdentifierModel<base_adapter_t> *model = NULL;
00112   const base_adapter_t *base_ia = ia.get();
00113 
00114   try{
00115     model = new Zoltan2::IdentifierModel<base_adapter_t>(
00116       base_ia, env, comm, modelFlags);
00117   }
00118   catch (std::exception &e){
00119     std::cerr << rank << ") " << e.what() << std::endl;
00120     fail = 1;
00121   }
00122 
00123   gfail = globalFail(comm, fail);
00124 
00125   if (gfail)
00126     printFailureCode(comm, fail);
00127   
00128   // Test the IdentifierModel interface
00129 
00130   if (model->getLocalNumIdentifiers() != size_t(nLocalIds))
00131     fail = 2;
00132 
00133   if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds))
00134     fail = 3;
00135 
00136   // For now, MatrixInput does not implement weights
00137   if (!fail && model->getIdentifierWeightDim() !=  0)
00138     fail = 4;
00139 
00140   gfail = globalFail(comm, fail);
00141 
00142   if (gfail)
00143     printFailureCode(comm, fail);
00144   
00145   ArrayView<const gno_t> gids;
00146   ArrayView<input_t> wgts;
00147   
00148   model->getIdentifierList(gids, wgts);
00149 
00150   if (!fail && gids.size() != nLocalIds)
00151     fail = 5;
00152 
00153   if (!fail && wgts.size() != 0)
00154     fail = 6;
00155 
00156   for (lno_t i=0; !fail && i < nLocalIds; i++){
00157     std::set<gno_t>::iterator next = idSet.find(gids[i]);
00158     if (next == idSet.end())
00159       fail = 7;
00160   }
00161 
00162   if (!fail && consecutiveIds){
00163     bool inARow = Zoltan2::IdentifierTraits<gno_t>::areConsecutive(
00164       gids.getRawPtr(), nLocalIds);
00165 
00166     if (!inARow)
00167       fail = 8;
00168   }
00169 
00170   gfail = globalFail(comm, fail);
00171 
00172   if (gfail)
00173     printFailureCode(comm, fail);
00174 
00175   delete model;
00176   delete input;
00177 }
00178 
00179 int main(int argc, char *argv[])
00180 {
00181   Teuchos::GlobalMPISession session(&argc, &argv);
00182   Teuchos::RCP<const Teuchos::Comm<int> > comm =
00183     Teuchos::DefaultComm<int>::getComm();
00184 
00185   int rank = comm->getRank();
00186 
00187   string fname("simple");
00188 
00189   bool wishConsecutiveIds = true;
00190 
00191   if (rank == 0){
00192     std::cout << fname;
00193     std::cout << ", consecutive IDs not requested" << std::endl;
00194   }
00195   testIdentifierModel(fname, 0,0,0,comm, !wishConsecutiveIds);
00196 
00197   if (rank == 0){
00198     std::cout << fname;
00199     std::cout << ", consecutive IDs are requested" << std::endl;
00200   }
00201   testIdentifierModel(fname, 0,0,0,comm,  wishConsecutiveIds);
00202 
00203   if (rank==0) std::cout << "PASS" << std::endl;
00204 
00205   return 0;
00206 }