TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SpmdLocalDataAccess,
  getNonconstLocalSubMultiVectorView_empty_p0, Scalar )
{
  typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
  const RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
    createZeroEleProcVS<Scalar>(g_localDim);
  const RCP<const Teuchos::Comm<Ordinal> > comm = vs->getComm();
  const int procRank = comm->getRank();
  PRINT_VAR(procRank);
  const int numProcs = comm->getSize();
  PRINT_VAR(numProcs);
  const RCP<MultiVectorBase<Scalar> > mv = createMembers<Scalar>(vs, g_numCols);
  const Scalar val = as<Scalar>(1.5);
  PRINT_VAR(val);
  assign<Scalar>(mv.ptr(), val);
  {
    out << "*** Test that we get and change the nonconst view correctly ...\n";
    RTOpPack::SubMultiVectorView<Scalar> lsmv = 
      getNonconstLocalSubMultiVectorView<Scalar>(mv);
    if (procRank == 0) {
      TEST_EQUALITY_CONST(lsmv.globalOffset(), 0);
      TEST_EQUALITY_CONST(lsmv.subDim(), 0);
      TEST_EQUALITY_CONST(lsmv.values(), null);
    }
    else {
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank-1)*g_localDim));
      TEST_EQUALITY(lsmv.subDim(), g_localDim);
    }
    TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
    TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
    TEST_EQUALITY(lsmv.numSubCols(), g_numCols);
    for (int i = 0; i < lsmv.subDim(); ++i) {
      for (int j = 0; j < lsmv.numSubCols(); ++j) {
        lsmv(i,j) = lsmv.globalOffset() + i + 0.1 * j;
      }
    }
  }
  {
    out << "*** Test that we get the same values when we grab const view ...\n";
    RTOpPack::ConstSubMultiVectorView<Scalar> lsmv = 
      getLocalSubMultiVectorView<Scalar>(mv);
    for (int i = 0; i < lsmv.subDim(); ++i) {
      for (int j = 0; j < lsmv.numSubCols(); ++j) {
        TEST_EQUALITY(lsmv(i,j), as<Scalar>(lsmv.globalOffset() + i + 0.1 * j));
      }
    }
  }
}
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SpmdLocalDataAccess,
  getNonconstLocalSubMultiVectorView_procRankLocalDim, Scalar )
{
  typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
  const RCP<const DefaultSpmdVectorSpace<Scalar> > vs =
    createProcRankLocalDimVS<Scalar>();
  const RCP<const Teuchos::Comm<Ordinal> > comm = vs->getComm();
  const int procRank = comm->getRank();
  PRINT_VAR(procRank);
  const int numProcs = comm->getSize();
  PRINT_VAR(numProcs);

  out << "*** A) Test getting nonconst MV view directly from SPMD MultiVector  ...\n";
  {
    const RCP<MultiVectorBase<Scalar> > mv = createMembers<Scalar>(vs, g_numCols);
    const Scalar val = as<Scalar>(1.5);
    PRINT_VAR(val);
    assign<Scalar>(mv.ptr(), val);
    {
      out << "*** A.1) Get and change the nonconst view ...\n";
      RTOpPack::SubMultiVectorView<Scalar> lsmv = 
        getNonconstLocalSubMultiVectorView<Scalar>(mv);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY(lsmv.numSubCols(), g_numCols);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        for (int j = 0; j < lsmv.numSubCols(); ++j) {
          lsmv(i,j) = lsmv.globalOffset() + i + 0.1 * j;
        }
      }
    }
    {
      out << "*** A.2) Get the same values when we grab const view ...\n";
      RTOpPack::ConstSubMultiVectorView<Scalar> lsmv = 
        getLocalSubMultiVectorView<Scalar>(mv);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        for (int j = 0; j < lsmv.numSubCols(); ++j) {
          TEST_EQUALITY(lsmv(i,j), as<Scalar>(lsmv.globalOffset() + i + 0.1 * j));
        }
      }
    }
  }

  out << "*** B) Test getting nonconst MV view indirectly from one-block"
      << " Product MultiVector  ...\n";
  {
    const RCP<const VectorSpaceBase<Scalar> > pvs = 
      productVectorSpace<Scalar>(tuple<RCP<const VectorSpaceBase<Scalar> > >(vs)());
    const RCP<MultiVectorBase<Scalar> > pmv = createMembers<Scalar>(pvs, g_numCols);
    const Scalar val = as<Scalar>(1.8);
    PRINT_VAR(val);
    assign<Scalar>(pmv.ptr(), val);
    {
      out << "*** B.1) Get and change the nonconst view ...\n";
      RTOpPack::SubMultiVectorView<Scalar> lsmv = 
        getNonconstLocalSubMultiVectorView<Scalar>(pmv);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY(lsmv.numSubCols(), g_numCols);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        for (int j = 0; j < lsmv.numSubCols(); ++j) {
          lsmv(i,j) = lsmv.globalOffset() + i + 0.1 * j;
        }
      }
    }
    {
      out << "*** B.2) Get the same values when we grab const view ...\n";
      RTOpPack::ConstSubMultiVectorView<Scalar> lsmv = 
        getLocalSubMultiVectorView<Scalar>(pmv);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY(lsmv.numSubCols(), g_numCols);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        for (int j = 0; j < lsmv.numSubCols(); ++j) {
          TEST_EQUALITY(lsmv(i,j), as<Scalar>(lsmv.globalOffset() + i + 0.1 * j));
        }
      }
    }
  }

  out << "*** C) Test getting nonconst MV view directly from SPMD Vector  ...\n";
  if (1) {
    const RCP<VectorBase<Scalar> > v = createMember<Scalar>(vs);
    const Scalar val = as<Scalar>(2.1);
    PRINT_VAR(val);
    assign<Scalar>(v.ptr(), val);
    {
      out << "*** C.1) Get and change the nonconst MV view ...\n";
      RTOpPack::SubMultiVectorView<Scalar> lsmv = 
        getNonconstLocalSubMultiVectorView<Scalar>(v);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY_CONST(lsmv.numSubCols(), 1);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        lsmv(i,0) = lsmv.globalOffset() + i;
      }
    }
    {
      out << "*** C.2) Get the same values when we grab const MV view ...\n";
      RTOpPack::ConstSubMultiVectorView<Scalar> lsmv = 
        getLocalSubMultiVectorView<Scalar>(v);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY_CONST(lsmv.numSubCols(), 1);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        TEST_EQUALITY(lsmv(i,0), as<Scalar>(lsmv.globalOffset() + i));
      }
    }
  }

  out << "*** D) Test getting nonconst MV view indirectly from one-block"
      << " Product MultiVector  ...\n";
  {
    const RCP<const VectorSpaceBase<Scalar> > pvs = 
      productVectorSpace<Scalar>(tuple<RCP<const VectorSpaceBase<Scalar> > >(vs)());
    const RCP<VectorBase<Scalar> > pv = createMember<Scalar>(pvs);
    const Scalar val = as<Scalar>(1.8);
    PRINT_VAR(val);
    assign<Scalar>(pv.ptr(), val);
    {
      out << "*** D.1) Get and change the nonconst view ...\n";
      RTOpPack::SubMultiVectorView<Scalar> lsmv = 
        getNonconstLocalSubMultiVectorView<Scalar>(pv);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY(lsmv.numSubCols(), 1);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        lsmv(i,0) = lsmv.globalOffset() + i;
      }
    }
    {
      out << "*** D.2) Get the same values when we grab const view ...\n";
      RTOpPack::ConstSubMultiVectorView<Scalar> lsmv = 
        getLocalSubMultiVectorView<Scalar>(pv);
      TEST_EQUALITY(lsmv.globalOffset(), as<Ordinal>((procRank*(procRank+1))/2));
      TEST_EQUALITY(lsmv.subDim(), procRank+1);
      TEST_EQUALITY(lsmv.leadingDim(), lsmv.subDim());
      TEST_EQUALITY_CONST(lsmv.colOffset(), 0);
      TEST_EQUALITY(lsmv.numSubCols(), 1);
      for (int i = 0; i < lsmv.subDim(); ++i) {
        TEST_EQUALITY(lsmv(i,0), as<Scalar>(lsmv.globalOffset() + i));
      }
    }
  }

}