Zoltan 2 Version 0.5
BasicVectorInput.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::BasicVectorInput 
00047 
00048 #include <Zoltan2_BasicVectorInput.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 
00056 using Teuchos::RCP;
00057 using Teuchos::Comm;
00058 using Teuchos::DefaultComm;
00059 
00060 typedef Zoltan2::BasicUserTypes<scalar_t, gno_t, lno_t, gno_t> userTypes_t;
00061 
00062 int checkBasicVector(
00063   Zoltan2::BasicVectorInput<userTypes_t> *ia, int len, int glen,
00064   gno_t *ids, int mvdim, const scalar_t **values, int *valueStrides,
00065   int wdim, const scalar_t **weights, int *weightStrides)
00066 {
00067   int fail = 0;
00068   bool strideOne = false;
00069 
00070   if (valueStrides == NULL) strideOne = true;
00071 
00072   if (ia->getNumberOfVectors() != mvdim)
00073     fail = 100;
00074 
00075   if (!fail && ia->getNumberOfWeights() != wdim)
00076     fail = 101;
00077 
00078   if (!fail && ia->getLocalLength() != size_t(len))
00079     fail = 102;
00080 
00081   if (!fail && ia->getGlobalLength() != size_t(glen))
00082     fail = 103;
00083 
00084   for (int v=0; !fail && v < mvdim; v++){
00085     const gno_t *idList;
00086     const scalar_t *vals;
00087     int correctStride = (strideOne ? 1 : valueStrides[v]);
00088     int stride;
00089 
00090     size_t nvals = ia->getVector(v, idList, vals, stride);
00091 
00092     if (nvals != size_t(len*stride))
00093       fail = 104;
00094 
00095     if (!fail && stride != correctStride)
00096       fail = 105;
00097 
00098     for (int i=0; !fail && i < len; i++){
00099 // TODO fix values check
00100 //      if (vals[stride*i] != values[v][correctStride*i])
00101 //        fail = 106;
00102 
00103       if (!fail && idList[i] != ids[i])
00104         fail = 107;
00105     }
00106   }
00107 
00108   for (int w=0; !fail && w < wdim; w++){
00109     const scalar_t *wgts;
00110     int stride;
00111 
00112     size_t nvals = ia->getVectorWeights(w, wgts, stride);
00113 
00114     if (nvals != size_t(len*stride))
00115       fail = 108;
00116 
00117     if (!fail && stride != weightStrides[w])
00118       fail = 109;
00119 
00120     for (int i=0; !fail && i < len; i++){
00121       if (wgts[stride*i] != weights[w][weightStrides[w]*i])
00122         fail = 110;
00123     }
00124   }
00125 
00126   return fail;
00127 }
00128   
00129 
00130 int main(int argc, char *argv[])
00131 {
00132   Teuchos::GlobalMPISession session(&argc, &argv);
00133   RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
00134   int rank = comm->getRank();
00135   int nprocs = comm->getSize();
00136   int fail = 0;
00137 
00138   // Create a single vector and a strided multi-vector with 
00139   // strided multi-weights.
00140 
00141   lno_t numLocalIds = 10;
00142   gno_t *myIds = new gno_t [numLocalIds];
00143   gno_t base = rank * numLocalIds;
00144   
00145   int wdim = 2;
00146   scalar_t *weights = new scalar_t [numLocalIds*wdim];
00147   int *weightStrides = new int [wdim];
00148   const scalar_t **weightPtrs = new const scalar_t * [wdim];
00149 
00150   int mvdim = 3;
00151   scalar_t *v_values= new scalar_t [numLocalIds];
00152   scalar_t *mv_values= new scalar_t [mvdim * numLocalIds];
00153   int *valueStrides = new int [mvdim];
00154   const scalar_t **valuePtrs = new const scalar_t * [mvdim];
00155 
00156   for (lno_t i=0; i < numLocalIds; i++){
00157     myIds[i] = base+i;
00158 
00159     for (int w=0; w < wdim; w++)
00160       weights[w*numLocalIds + i] = w + 1 + nprocs - rank;
00161 
00162     v_values[i] = numLocalIds-i;
00163 
00164     for (int v=0; v < mvdim; v++)
00165       mv_values[i*mvdim + v] = (v+1) * (nprocs-rank) / (i+1);
00166   }
00167 
00168   for (int w=0; w < wdim; w++){
00169     weightStrides[w] = 1;
00170     weightPtrs[w] = weights + numLocalIds*w;
00171   }
00172 
00173   for (int v=0; v < mvdim; v++){
00174     valueStrides[v] = mvdim;
00175     valuePtrs[v] = mv_values + v;
00176   }
00177 
00178   Zoltan2::BasicVectorInput<userTypes_t> *ia;
00179 
00180   {
00181     // A Zoltan2::BasicVectorInput object with one vector and no weights
00182 
00183     std::vector<const scalar_t *> weightValues;
00184     std::vector<int> strides;
00185   
00186     try{
00187      ia = new Zoltan2::BasicVectorInput<userTypes_t>(numLocalIds, myIds,
00188        v_values, 1, weightValues, strides);
00189     }
00190     catch (std::exception &e){
00191       fail = 1;
00192     }
00193   
00194     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 1", fail);
00195   
00196     fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
00197       myIds, 1, valuePtrs, NULL, 0, NULL, NULL);
00198   
00199     delete ia;
00200 
00201     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 1", fail);
00202   }
00203 
00204   {
00205     // A Zoltan2::BasicVectorInput object with one vector and weights
00206 
00207     std::vector<const scalar_t *> weightValues;
00208     std::vector<int> strides;
00209 
00210     weightValues.push_back(weightPtrs[0]);
00211     weightValues.push_back(weightPtrs[1]);
00212     strides.push_back(1);
00213     strides.push_back(1);
00214   
00215     try{
00216      ia = new Zoltan2::BasicVectorInput<userTypes_t>(numLocalIds, myIds,
00217        v_values, 1, weightValues, strides);
00218     }
00219     catch (std::exception &e){
00220       fail = 1;
00221     }
00222   
00223     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 2", fail);
00224   
00225     fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
00226       myIds, 1, valuePtrs, NULL, wdim, weightPtrs, weightStrides);
00227   
00228     delete ia;
00229 
00230     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 2", fail);
00231   }
00232 
00233   {
00234     // A Zoltan2::BasicVectorInput object with a multivector and no weights
00235 
00236     std::vector<const scalar_t *> weightValues, values;
00237     std::vector<int> wstrides, vstrides;
00238 
00239     for (int dim=0; dim < mvdim; dim++){
00240       values.push_back(valuePtrs[dim]);
00241       vstrides.push_back(mvdim);
00242     }
00243   
00244   
00245     try{
00246       ia = new Zoltan2::BasicVectorInput<userTypes_t>(
00247         numLocalIds, myIds, values, vstrides, weightValues, wstrides);
00248     }
00249     catch (std::exception &e){
00250       fail = 1;
00251     }
00252   
00253     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 3", fail);
00254   
00255     fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
00256       myIds, mvdim, valuePtrs, valueStrides, 0, NULL, NULL);
00257   
00258     delete ia;
00259 
00260     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 3", fail);
00261   }
00262 
00263   {
00264     // A Zoltan2::BasicVectorInput object with a multivector with weights
00265 
00266     std::vector<const scalar_t *> weightValues, values;
00267     std::vector<int> wstrides, vstrides;
00268 
00269     for (int dim=0; dim < wdim; dim++){
00270       weightValues.push_back(weightPtrs[dim]);
00271       wstrides.push_back(1);
00272     }
00273 
00274     for (int dim=0; dim < mvdim; dim++){
00275       values.push_back(valuePtrs[dim]);
00276       vstrides.push_back(mvdim);
00277     }
00278   
00279     try{
00280      ia = new Zoltan2::BasicVectorInput<userTypes_t>(
00281         numLocalIds, myIds, values, vstrides, weightValues, wstrides);
00282       
00283     }
00284     catch (std::exception &e){
00285       fail = 1;
00286     }
00287   
00288     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
00289   
00290     fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
00291       myIds, mvdim, valuePtrs, valueStrides,
00292       wdim, weightPtrs, weightStrides);
00293   
00294     delete ia;
00295 
00296     TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 4", fail);
00297   }
00298 
00299   if (rank == 0)
00300     std::cout << "PASS" << std::endl;
00301 
00302   delete [] myIds;
00303   delete [] weights;
00304   delete [] weightStrides;
00305   delete [] weightPtrs;
00306   delete [] v_values;
00307   delete [] mv_values;
00308   delete [] valueStrides;
00309   delete [] valuePtrs;
00310 
00311   return fail;
00312 }
00313