Zoltan2
BasicCoordinateInput.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 // Test for Zoltan2::BasicVectorAdapter for coordinate-based problems
00047 
00048 #include <Zoltan2_BasicVectorAdapter.hpp>
00049 #include <Zoltan2_TestHelpers.hpp>
00050 
00051 #include <Teuchos_GlobalMPISession.hpp>
00052 #include <Teuchos_DefaultComm.hpp>
00053 #include <Teuchos_RCP.hpp>
00054 #include <Teuchos_CommHelpers.hpp>
00055 #include <Teuchos_SerialDenseVector.hpp>
00056 
00057 using Teuchos::RCP;
00058 using Teuchos::Comm;
00059 using Teuchos::DefaultComm;
00060 using Teuchos::Array;
00061 
00062 typedef Teuchos::SerialDenseVector<zlno_t, zscalar_t> tvec_t;
00063 
00064 typedef Zoltan2::BasicUserTypes<zscalar_t, zzgid_t, zlno_t, zgno_t> userTypes_t;
00065 
00066 int checkBasicCoordinate(
00067   Zoltan2::BasicVectorAdapter<userTypes_t> *ia, 
00068   int len, int glen, zzgid_t *ids,
00069   zscalar_t *xyz,
00070   zscalar_t *weights,
00071   int nCoords, int nWeights)
00072 {
00073   int fail = 0;
00074 
00075   if (ia->getNumEntriesPerID() != nCoords)
00076     fail = 100;
00077 
00078   if (!fail && ia->getNumWeightsPerID() != nWeights)
00079     fail = 101;
00080 
00081   if (!fail && ia->getLocalNumIDs() != size_t(len))
00082     fail = 102;
00083 
00084   for (int x=0; !fail && x < nCoords; x++){
00085     const zzgid_t *idList;
00086     const zscalar_t *vals;
00087     int stride;
00088 
00089     ia->getIDsView(idList);
00090     ia->getEntriesView(vals, stride, x);
00091 
00092     zscalar_t *coordVal = xyz + x;
00093     for (int i=0; !fail && i < len; i++, coordVal += 3){
00094 
00095       if (idList[i] != ids[i])
00096         fail = 105;
00097 
00098       if (!fail && vals[stride*i] != *coordVal)
00099         fail = 106;
00100     }
00101   }
00102 
00103   for (int w=0; !fail && w < nWeights; w++){
00104     const zscalar_t *wgts;
00105     int stride;
00106 
00107     ia->getWeightsView(wgts, stride, w);
00108 
00109     zscalar_t *weightVal = weights + len*w;
00110     for (int i=0; !fail && i < len; i++, weightVal++){
00111       if (wgts[stride*i] != *weightVal)
00112         fail = 110;
00113     }
00114   }
00115 
00116   return fail;
00117 }
00118   
00119 
00120 int main(int argc, char *argv[])
00121 {
00122   Teuchos::GlobalMPISession session(&argc, &argv);
00123   RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
00124   int rank = comm->getRank();
00125   int fail = 0;
00126 
00127   // Get some coordinates
00128 
00129   typedef Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t> mv_t;
00130   RCP<UserInputForTests> uinput;
00131   std::string fname("simple");
00132 
00133   try{
00134     uinput = rcp(new UserInputForTests(testDataFilePath, fname, comm, true));
00135   }
00136   catch(std::exception &e){
00137     fail=1;
00138   }
00139 
00140   TEST_FAIL_AND_EXIT(*comm, !fail, "input constructor", 1);
00141 
00142   RCP<mv_t> coords;
00143 
00144   try{
00145     coords = uinput->getUICoordinates();
00146   }
00147   catch(std::exception &e){
00148     fail=1;
00149   }
00150 
00151   TEST_FAIL_AND_EXIT(*comm, !fail, "getting coordinates", 1);
00152 
00153   int numLocalIds = coords->getLocalLength();
00154   int numGlobalIds = coords->getGlobalLength();
00155   int coordDim = coords->getNumVectors();
00156   ArrayView<const zgno_t> idList = coords->getMap()->getNodeElementList();
00157 
00158   // Create global Ids, x-, y- and z-coordinates, and also arrays of weights.
00159 
00160   Array<zzgid_t> myIds(numLocalIds);
00161   zgno_t base = rank * numLocalIds;
00162   
00163   int wdim = 2;
00164   Array<zscalar_t> weights(numLocalIds*wdim);
00165   for (int i = 0; i < numLocalIds*wdim; i++) weights[i] = zscalar_t(i);
00166 
00167   zscalar_t *x_values= coords->getDataNonConst(0).getRawPtr();
00168   zscalar_t *y_values= x_values;  // fake 3 dimensions if needed
00169   zscalar_t *z_values= x_values;
00170 
00171   if (coordDim > 1){
00172     y_values= coords->getDataNonConst(1).getRawPtr();
00173     if (coordDim > 2)
00174       z_values= coords->getDataNonConst(2).getRawPtr();
00175   }
00176 
00177   Array<zscalar_t> xyz_values(3*numLocalIds);
00178 
00179   for (zlno_t i=0; i < numLocalIds; i++)   // global Ids
00180     myIds[i] = base+i;
00181 
00182   zscalar_t *x = xyz_values.getRawPtr();   // a stride-3 coordinate array
00183   zscalar_t *y = x+1;
00184   zscalar_t *z = y+1;
00185 
00186   for (int i=0, ii=0; i < numLocalIds; i++, ii += 3){
00187     x[ii] = x_values[i];
00188     y[ii] = y_values[i];
00189     z[ii] = z_values[i];
00190   }
00191 
00192   RCP<Zoltan2::BasicVectorAdapter<userTypes_t> > ia;
00193 
00194   {
00196     // 3-dimensional coordinates with stride one and no weights,
00197     //   using simpler constructor
00198   
00199     int ncoords = 3;
00200     int nweights = 0;
00201   
00202     try{
00203      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00204        numLocalIds, myIds.getRawPtr(), x_values, y_values, z_values));
00205     }
00206     catch (std::exception &e){
00207       fail = 1;
00208     }
00209   
00210     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 0", fail);
00211   
00212     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00213       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00214       weights.getRawPtr(), ncoords, nweights);
00215   
00216     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 0", fail);
00217   }
00218 
00219   {
00221     // 3-dimensional coordinates with stride one and one weight
00222     //   using simpler constructor
00223   
00224     int ncoords = 3;
00225     int nweights = 1;
00226   
00227     try{
00228      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00229        numLocalIds, myIds.getRawPtr(),
00230        x_values, y_values, z_values, 1, 1, 1,
00231        true, weights.getRawPtr(), 1));
00232     }
00233     catch (std::exception &e){
00234       fail = 1;
00235     }
00236   
00237     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 0a", fail);
00238   
00239     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00240       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00241       weights.getRawPtr(), ncoords, nweights);
00242   
00243     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 0a", fail);
00244   }
00245 
00246   {
00248     // 3-dimensional coordinates with stride one and no weights
00249   
00250     int ncoords = 3;
00251     int nweights = 0;
00252 
00253     std::vector<const zscalar_t *> values, weightValues;
00254     std::vector<int> valueStrides, weightStrides;
00255   
00256     values.push_back(x_values);
00257     values.push_back(y_values);
00258     values.push_back(z_values);
00259     valueStrides.push_back(1);
00260     valueStrides.push_back(1);
00261     valueStrides.push_back(1);
00262   
00263     try{
00264      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00265        numLocalIds, myIds.getRawPtr(), values, valueStrides, 
00266        weightValues, weightStrides));
00267     }
00268     catch (std::exception &e){
00269       fail = 1;
00270     }
00271   
00272     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 1", fail);
00273   
00274     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00275       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00276       weights.getRawPtr(), ncoords, nweights);
00277   
00278     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 1", fail);
00279   
00280     // Try using the default: no strides supplied means strides are one.
00281 
00282     std::vector<int> emptyStrides;
00283   
00284     try{
00285      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00286        numLocalIds, myIds.getRawPtr(), values, emptyStrides, 
00287        weightValues, emptyStrides));
00288     }
00289     catch (std::exception &e){
00290       fail = 1;
00291     }
00292   
00293     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 2", fail);
00294   
00295     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00296       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00297       weights.getRawPtr(), ncoords, nweights);
00298   
00299     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 2", fail);
00300   }
00301 
00302   {
00304     // 2-dimensional coordinates with stride three and two weights
00305   
00306     int ncoords = 2;
00307     int nweights = 2;
00308 
00309     std::vector<const zscalar_t *> values, weightValues;
00310     std::vector<int> valueStrides, weightStrides;
00311   
00312     values.push_back(xyz_values.getRawPtr());
00313     values.push_back(xyz_values.getRawPtr() + 1);
00314     valueStrides.push_back(3);
00315     valueStrides.push_back(3);
00316   
00317     weightValues.push_back(weights.getRawPtr());
00318     weightValues.push_back(weights.getRawPtr() + numLocalIds);
00319     weightStrides.push_back(1);
00320     weightStrides.push_back(1);
00321   
00322     try{
00323      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00324        numLocalIds, myIds.getRawPtr(), values, valueStrides, 
00325        weightValues, weightStrides));
00326     }
00327     catch (std::exception &e){
00328       fail = 1;
00329     }
00330   
00331     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 3", fail);
00332   
00333     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00334       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00335       weights.getRawPtr(), ncoords, nweights);
00336   
00337     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 3", fail);
00338   
00339     // Try using default weight strides
00340 
00341     std::vector<int> emptyStrides;
00342   
00343     try{
00344      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00345        numLocalIds, myIds.getRawPtr(), values, valueStrides, 
00346        weightValues, emptyStrides));
00347     }
00348     catch (std::exception &e){
00349       fail = 1;
00350     }
00351   
00352     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
00353   
00354     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00355       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00356       weights.getRawPtr(), ncoords, nweights);
00357   
00358     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 4", fail);
00359   }
00360 
00361   {
00363     // 1-dimensional coordinates with stride one and two weights
00364 
00365     int ncoords = 1;
00366     int nweights = 2;
00367 
00368     std::vector<const zscalar_t *> values, weightValues;
00369     std::vector<int> valueStrides, weightStrides;
00370   
00371     values.push_back(x_values);
00372     valueStrides.push_back(1);
00373   
00374     weightValues.push_back(weights.getRawPtr());
00375     weightValues.push_back(weights.getRawPtr() + numLocalIds);
00376     weightStrides.push_back(1);
00377     weightStrides.push_back(1);
00378   
00379     try{
00380      ia = rcp(new Zoltan2::BasicVectorAdapter<userTypes_t>(
00381        numLocalIds, myIds.getRawPtr(), values, valueStrides, 
00382        weightValues, weightStrides));
00383     }
00384     catch (std::exception &e){
00385       fail = 1;
00386     }
00387   
00388     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
00389   
00390     fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
00391       myIds.getRawPtr(), xyz_values.getRawPtr(), 
00392       weights.getRawPtr(), ncoords, nweights);
00393   
00394     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 4", fail);
00395   }
00396 
00397   if (rank == 0)
00398     std::cout << "PASS" << std::endl;
00399 
00400   return fail;
00401 }
00402