Thyra Package Browser (Single Doxygen Collection) Version of the Day
DefaultSpmdVectorSpace_UnitTests.cpp
Go to the documentation of this file.
00001 
00002 #include "Thyra_DefaultSpmdVectorSpace.hpp"
00003 #include "Thyra_DetachedSpmdVectorView.hpp"
00004 #include "Thyra_DetachedVectorView.hpp"
00005 #include "Thyra_TestingTools.hpp"
00006 #include "Teuchos_UnitTestHarness.hpp"
00007 #include "Teuchos_DefaultComm.hpp"
00008 
00009 //#define THYRA_DEFAULT_SPMD_VECTOR_SPACE_UNIT_TESTS_DUMP
00010 
00011 #ifdef THYRA_DEFAULT_SPMD_VECTOR_SPACE_UNIT_TESTS_DUMP
00012 #  include "RTOpPack_SPMD_apply_op_decl.hpp"
00013 #  include "Thyra_SpmdVectorBase.hpp"
00014 #endif // THYRA_DEFAULT_SPMD_VECTOR_SPACE_UNIT_TESTS_DUMP
00015 
00016 
00017 namespace {
00018 
00019 
00020 //
00021 // Helper code and declarations
00022 //
00023 
00024 
00025 using Teuchos::as;
00026 using Teuchos::null;
00027 using Teuchos::RCP;
00028 using Teuchos::rcp;
00029 using Teuchos::get_extra_data;
00030 using Thyra::VectorSpaceBase;
00031 using Thyra::VectorBase;
00032 using Thyra::MultiVectorBase;
00033 using Thyra::createMember;
00034 using Thyra::createMembers;
00035 using Thyra::DefaultSpmdVectorSpace;
00036 using Thyra::defaultSpmdVectorSpace;
00037 using Thyra::ConstDetachedVectorView;
00038 using Thyra::DetachedVectorView;
00039 using Thyra::ConstDetachedSpmdVectorView;
00040 using Thyra::DetachedSpmdVectorView;
00041 typedef Thyra::Ordinal Ordinal;
00042 
00043 
00044 const int g_localDim = 4; // ToDo: Make variable!
00045 
00046 
00047 template<class Scalar>
00048 RCP<VectorSpaceBase<Scalar> > 
00049 createSpmdVectorSpace(const Teuchos_Ordinal localDim)
00050 {
00051   return defaultSpmdVectorSpace<Scalar>(
00052     Teuchos::DefaultComm<Teuchos_Ordinal>::getComm(),
00053     localDim, -1 );
00054 }
00055 
00056 
00057 //
00058 // Unit Tests
00059 //
00060 
00061 
00062 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, defaultConstruct,
00063   Scalar )
00064 {
00065 
00066   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00067     Thyra::defaultSpmdVectorSpace<Scalar>());
00068   TEST_EQUALITY(vs->getComm(), null);
00069   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(-1));
00070   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(-1));
00071   TEST_EQUALITY(vs->mapCode(), as<Ordinal>(-1));
00072   TEST_EQUALITY(vs->dim(), as<Ordinal>(-1));
00073   
00074 }
00075 
00076 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00077   defaultConstruct )
00078 
00079 
00080 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, serialConstruct,
00081   Scalar )
00082 {
00083 
00084   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00085     defaultSpmdVectorSpace<Scalar>(g_localDim));
00086   TEST_EQUALITY(vs->getComm(), null);
00087   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(0));
00088   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00089   TEST_EQUALITY(vs->mapCode(), as<Ordinal>(g_localDim));
00090   TEST_EQUALITY(vs->dim(), as<Ordinal>(g_localDim));
00091 }
00092 
00093 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00094   serialConstruct )
00095 
00096 
00097 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, serialConstructZeroSize,
00098   Scalar )
00099 {
00100 
00101   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00102     defaultSpmdVectorSpace<Scalar>(0));
00103   TEST_EQUALITY(vs->getComm(), null);
00104   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(0));
00105   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(0));
00106   TEST_EQUALITY(vs->mapCode(), as<Ordinal>(0));
00107   TEST_EQUALITY(vs->dim(), as<Ordinal>(0));
00108 
00109   ECHO(const RCP<VectorBase<Scalar> > v = createMember<Scalar>(vs));
00110   out << "v = " << *v;
00111 
00112   ECHO(const RCP<MultiVectorBase<Scalar> > mv = createMembers<Scalar>(vs, 0));
00113   out << "mv = " << *mv;
00114 
00115 }
00116 
00117 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00118   serialConstructZeroSize )
00119 
00120 
00121 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, parallelConstruct,
00122   Scalar )
00123 {
00124   ECHO(const RCP<const Teuchos::Comm<Ordinal> > comm =
00125     Teuchos::DefaultComm<Teuchos_Ordinal>::getComm());
00126   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00127     defaultSpmdVectorSpace<Scalar>(comm, g_localDim, -1));
00128   TEST_EQUALITY(vs->getComm(), comm);
00129   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(comm->getRank()*g_localDim));
00130   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00131   TEST_EQUALITY(vs->dim(), as<Ordinal>(comm->getSize()*g_localDim));
00132 }
00133 
00134 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00135   parallelConstruct )
00136 
00137 
00138 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, parallelConstructGlobalDim,
00139   Scalar )
00140 {
00141   ECHO(const RCP<const Teuchos::Comm<Ordinal> > comm =
00142     Teuchos::DefaultComm<Teuchos_Ordinal>::getComm());
00143   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00144     defaultSpmdVectorSpace<Scalar>(comm, g_localDim, g_localDim * comm->getSize()));
00145   TEST_EQUALITY(vs->getComm(), comm);
00146   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(comm->getRank()*g_localDim));
00147   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00148   TEST_EQUALITY(vs->dim(), as<Ordinal>(comm->getSize()*g_localDim));
00149 }
00150 
00151 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00152   parallelConstructGlobalDim )
00153 
00154 
00155 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, locallyReplicatedParallelConstruct,
00156   Scalar )
00157 {
00158   ECHO(const RCP<const Teuchos::Comm<Ordinal> > comm =
00159     Teuchos::DefaultComm<Teuchos_Ordinal>::getComm());
00160   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00161     defaultSpmdVectorSpace<Scalar>(comm, g_localDim, g_localDim));
00162   TEST_EQUALITY(vs->getComm(), comm);
00163   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(0));
00164   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00165   TEST_EQUALITY(vs->dim(), as<Ordinal>(g_localDim));
00166 }
00167 
00168 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00169   locallyReplicatedParallelConstruct )
00170 
00171 
00172 //TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, parallelConstructEmptyProc,
00173 //  Scalar )
00174 //{
00175 //
00176 //  ECHO(const RCP<const Teuchos::Comm<Ordinal> > comm =
00177 //    Teuchos::DefaultComm<Teuchos_Ordinal>::getComm());
00178 //  const int procRank = comm->getRank();
00179 //  ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00180 //    defaultSpmdVectorSpace<Scalar>(comm, procRank == 0 ? 0 : g_localDim, -1));
00181 //  TEST_EQUALITY(vs->getComm(), comm);
00182 //
00183 //  if (procRank == 0) {
00184 //    TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(0));
00185 //  }
00186 //  else {
00187 //    TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00188 //  }
00189 //  TEST_EQUALITY(vs->dim(), as<Ordinal>((comm->getSize()-1)*g_localDim));
00190 //
00191 //  if (vs->dim()) {
00192 //
00193 //    ECHO(const RCP<VectorBase<Scalar> > v = createMember<Scalar>(vs));
00194 //    ECHO(V_S(v.ptr(), as<Scalar>(1.0)));
00195 //    out << "v = " << *v;
00196 //    
00197 //    // ToDo: Fix MultiVector to work with empty processors
00198 //    //ECHO(const RCP<MultiVectorBase<Scalar> > mv = createMembers<Scalar>(vs, 1));
00199 //    //ECHO(assign(mv.ptr(), as<Scalar>(1.0)));
00200 //    //out << "mv = " << *mv;
00201 //
00202 //  }
00203 //
00204 //}
00205 //
00206 //TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00207 //  parallelConstructEmptyProc )
00208 
00209 
00210 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, deprecatedSerialConstruct,
00211   Scalar )
00212 {
00213 
00214   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00215     Thyra::defaultSpmdVectorSpace<Scalar>(g_localDim));
00216   TEST_EQUALITY(vs->getComm(), null);
00217   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(0));
00218   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00219   TEST_EQUALITY(vs->mapCode(), as<Ordinal>(g_localDim));
00220   TEST_EQUALITY(vs->dim(), as<Ordinal>(g_localDim));
00221   ECHO(const RCP<VectorBase<Scalar> > v = createMember<Scalar>(vs));
00222   TEST_ASSERT(vs->isCompatible(*v->space()));
00223 }
00224 
00225 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00226   deprecatedSerialConstruct )
00227 
00228 
00229 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, deprecatedParallelConstruct,
00230   Scalar )
00231 {
00232   ECHO(const RCP<const Teuchos::Comm<Ordinal> > comm =
00233     Teuchos::DefaultComm<Teuchos_Ordinal>::getComm());
00234   ECHO(RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
00235     Thyra::defaultSpmdVectorSpace<Scalar>(comm, g_localDim, -1));
00236   TEST_EQUALITY(vs->getComm(), comm);
00237   TEST_EQUALITY(vs->localOffset(), as<Ordinal>(comm->getRank()*g_localDim));
00238   TEST_EQUALITY(vs->localSubDim(), as<Ordinal>(g_localDim));
00239   TEST_EQUALITY(vs->dim(), as<Ordinal>(comm->getSize()*g_localDim));
00240   ECHO(const RCP<VectorBase<Scalar> > v = createMember<Scalar>(vs));
00241   TEST_ASSERT(vs->isCompatible(*v->space()));
00242 }
00243 
00244 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00245   deprecatedParallelConstruct )
00246 
00247 
00248 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, parallelFullExtract,
00249   Scalar )
00250 {
00251 
00252   const RCP<const VectorSpaceBase<Scalar> > vs =
00253     createSpmdVectorSpace<Scalar>(g_localDim);
00254 
00255   const RCP<VectorBase<Scalar> > v = createMember(vs);
00256 #ifdef THYRA_DEFAULT_SPMD_VECTOR_SPACE_UNIT_TESTS_DUMP
00257     RTOpPack::show_spmd_apply_op_dump = true;
00258 #endif
00259   {
00260     out << "\nSetting up v[i] = i, i=0...n-1 ...\n";
00261     const DetachedSpmdVectorView<Scalar> dv(v);
00262     const Ordinal localOffset = dv.spmdSpace()->localOffset();
00263     const Ordinal localSubDim = dv.spmdSpace()->localSubDim();
00264     for (Ordinal i = 0; i < localSubDim; ++i) {
00265       dv[i] = as<Scalar>(localOffset + i);
00266     }
00267   }
00268 
00269   out << "\nv = " << *v;
00270 
00271   {
00272 
00273     const ConstDetachedVectorView<Scalar> dv(v);
00274 
00275     TEST_EQUALITY(dv.subDim(), vs->dim());
00276     
00277     out << "\nTest that dv[i] == i, i=0...n-1 ... ";
00278     bool local_success = true;
00279     for (Ordinal i = 0; i < dv.subDim(); ++i) {
00280       TEST_ARRAY_ELE_EQUALITY( dv, i, as<Scalar>(i) );
00281     }
00282     if (local_success) out << "passed\n";
00283     else success = false;
00284 
00285   }
00286 
00287 #ifdef THYRA_DEFAULT_SPMD_VECTOR_SPACE_UNIT_TESTS_DUMP
00288     RTOpPack::show_spmd_apply_op_dump = false;
00289 #endif
00290   
00291 }
00292 
00293 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00294   parallelFullExtract)
00295 
00296 
00297 
00298 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVectorSpace, dangling_vs,
00299   Scalar )
00300 {
00301   RCP<const Thyra::VectorSpaceBase<Scalar> > vs = 
00302     defaultSpmdVectorSpace<Scalar>(g_localDim);
00303   const int globalDim = vs->dim();
00304   RCP<VectorBase<Scalar> > x1 = createMember(vs);
00305   {
00306     // x1 owns a false RCP to vs
00307     TEST_EQUALITY_CONST( x1->space().has_ownership(), true );
00308     // RCP<> for x1 owns a true RCP to vs
00309     const std::string label = "VectorSpaceBase";
00310     RCP<const VectorSpaceBase<Scalar> > extra_data_x1 = 
00311       get_extra_data<RCP<const VectorSpaceBase<Scalar> >, VectorBase<Scalar> >(x1, label);
00312     TEST_EQUALITY_CONST( extra_data_x1.has_ownership(), true );
00313   }
00314   RCP<Thyra::VectorBase<Scalar> > x0 = x1->clone_v();
00315   {
00316     // x0 owns a false RCP to vs
00317     TEST_EQUALITY_CONST( x0->space().has_ownership(), true );
00318     // RCP<> for x0 owns a true RCP to a _DIFFERENT_ VectorSpaceBase
00319     // object because the one used to clone x1 is a false RCP, so the
00320     // VectorSpaceBase was cloned and that is the one that was set on the RCP.
00321     std::string label = "VectorSpaceBase";
00322     RCP<const VectorSpaceBase<Scalar> > extra_data_x0 = 
00323       get_extra_data<RCP<const
00324       VectorSpaceBase<Scalar> >, VectorBase<Scalar> >(x0, label );
00325     TEST_EQUALITY_CONST( extra_data_x0.has_ownership(), true );
00326     TEST_EQUALITY( extra_data_x0.ptr(), vs.ptr() );
00327   }
00328   vs = null; // vs still around because x1's RCP owns it
00329   x1 = null; // vs deleted
00330   {
00331     RCP<const VectorSpaceBase<Scalar> > vs_old = x0->space();
00332     TEST_EQUALITY_CONST( vs_old->dim(), globalDim );
00333   }
00334   
00335 }
00336 
00337 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultSpmdVectorSpace,
00338   dangling_vs)
00339 
00340 
00341 
00342 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines