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