Zoltan2
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 BasicIdentifierAdapter with weights
00049 
00050 #include <Zoltan2_IdentifierModel.hpp>
00051 #include <Zoltan2_XpetraCrsMatrixAdapter.hpp>
00052 #include <Zoltan2_BasicIdentifierAdapter.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, zgno_t xdim, zgno_t ydim, zgno_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<zscalar_t, zlno_t, zgno_t> tcrsMatrix_t;
00087   
00088   UserInputForTests *uinput;
00089   if (fname.size() > 0)
00090     uinput = new UserInputForTests(testDataFilePath, fname, comm, true);
00091   else
00092     uinput = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true, true);
00093 
00094   RCP<tcrsMatrix_t > M = uinput->getUITpetraCrsMatrix();
00095   zlno_t nLocalIds = M->getNodeNumRows();
00096   zgno_t nGlobalIds =  M->getGlobalNumRows();
00097 
00098   ArrayView<const zgno_t> idList = M->getRowMap()->getNodeElementList();
00099   std::set<zgno_t> idSet(idList.begin(), idList.end());
00100 
00102   // Create an IdentifierModel with this input
00104 
00105   typedef Zoltan2::XpetraCrsMatrixAdapter<tcrsMatrix_t> adapter_t;
00106   typedef Zoltan2::MatrixAdapter<tcrsMatrix_t> base_adapter_t;
00107   typedef Zoltan2::StridedData<zlno_t, zscalar_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     std::cerr << rank << ") getLocalNumIdentifiers "
00132               << model->getLocalNumIdentifiers() << " "
00133               << nLocalIds << std::endl;
00134     fail = 2;
00135   }
00136 
00137   if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds)) {
00138     std::cerr << rank << ") getGlobalNumIdentifiers "
00139               << model->getGlobalNumIdentifiers() << " "
00140               << nGlobalIds << std::endl;
00141     fail = 3;
00142   }
00143 
00144   gfail = globalFail(comm, fail);
00145 
00146   if (gfail)
00147     printFailureCode(comm, fail);
00148   
00149   ArrayView<const zgno_t> gids;
00150   ArrayView<input_t> wgts;
00151   
00152   model->getIdentifierList(gids, wgts);
00153 
00154   if (!fail && gids.size() != nLocalIds) {
00155     std::cerr << rank << ") getIdentifierList IDs "
00156               << gids.size() << " "
00157               << nLocalIds << std::endl;
00158     fail = 5;
00159   }
00160 
00161   if (!fail && wgts.size() != 0) {
00162     std::cerr << rank << ") getIdentifierList Weights "
00163               << wgts.size() << " "
00164               << 0 << std::endl;
00165     fail = 6;
00166   }
00167 
00168   for (zlno_t i=0; !fail && i < nLocalIds; i++){
00169     std::set<zgno_t>::iterator next = idSet.find(gids[i]);
00170     if (next == idSet.end()) {
00171       std::cerr << rank << ") getIdentifierList gid not found "
00172               << gids[i] << std::endl;
00173       fail = 7;
00174     }
00175   }
00176 
00177   if (!fail && consecutiveIds){
00178     bool inARow = Zoltan2::IdentifierTraits<zgno_t>::areConsecutive(
00179       gids.getRawPtr(), nLocalIds);
00180 
00181     if (!inARow) {
00182       std::cerr << rank << ") getIdentifierList not consecutive " << std::endl;
00183       fail = 8;
00184     }
00185   }
00186 
00187   gfail = globalFail(comm, fail);
00188 
00189   if (gfail)
00190     printFailureCode(comm, fail);
00191 
00192   delete model;
00193   delete uinput;
00194 }
00195 
00196 int main(int argc, char *argv[])
00197 {
00198   Teuchos::GlobalMPISession session(&argc, &argv);
00199   Teuchos::RCP<const Teuchos::Comm<int> > comm =
00200     Teuchos::DefaultComm<int>::getComm();
00201 
00202   int rank = comm->getRank();
00203 
00204   string fname("simple");
00205 
00206   bool wishConsecutiveIds = true;
00207 
00208   if (rank == 0){
00209     std::cout << fname;
00210     std::cout << ", consecutive IDs not requested" << std::endl;
00211   }
00212   testIdentifierModel(fname, 0,0,0,comm, !wishConsecutiveIds);
00213 
00214   if (rank == 0){
00215     std::cout << fname;
00216     std::cout << ", consecutive IDs are requested" << std::endl;
00217   }
00218   testIdentifierModel(fname, 0,0,0,comm,  wishConsecutiveIds);
00219 
00220   if (rank==0) std::cout << "PASS" << std::endl;
00221 
00222   return 0;
00223 }