// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_DefaultMpiComm.hpp" #include "ROL_PinTVector.hpp" #include "ROL_PinTVectorCommunication_Tpetra.hpp" #include "Tpetra_Core.hpp" typedef double RealT; typedef double ElementT; typedef Tpetra::Map<>::local_ordinal_type LO; typedef Tpetra::Map<>::global_ordinal_type GO; typedef Tpetra::Map<>::node_type Node; typedef Tpetra::Map Map; typedef Tpetra::MultiVector MV; int main(int argc, char* argv[]) { typedef ROL::Ptr MVP; typedef ROL::Ptr> PtrVector; Teuchos::GlobalMPISession mpiSession(&argc, &argv); ROL::Ptr > comm = Tpetra::getDefaultComm(); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing if (iprint > 0) outStream = ROL::makePtrFromRef(std::cout); else outStream = ROL::makePtrFromRef(bhs); int errorFlag = 0; RealT errtol = ROL::ROL_THRESHOLD(); int numRanks = -1; int myRank = -1; MPI_Comm_size(MPI_COMM_WORLD, &numRanks); MPI_Comm_rank(MPI_COMM_WORLD, &myRank); std::string procStr = std::to_string(myRank) + "/" + std::to_string(numRanks) + ": "; *outStream << "Proc " << myRank << "/" << numRanks << std::endl; try { int spatialProcs = 1; ROL::Ptr pintComm = ROL::makePtr(MPI_COMM_WORLD,spatialProcs); ROL::Ptr> vectorComm = ROL::makePtr>(); ROL::Ptr> mpiSpaceComm = ROL::makePtr>(pintComm->getSpaceCommunicator()); int numEntries = 20; ROL::Ptr map = ROL::makePtr(numEntries,0,mpiSpaceComm); *outStream << "Testing checkVector" << std::endl; { // allocate state vector MVP x_data = ROL::makePtr(map,1,true); MVP y_data = ROL::makePtr(map,1,true); MVP z_data = ROL::makePtr(map,1,true); x_data->randomize(); y_data->randomize(); z_data->randomize(); PtrVector x_vec = ROL::makePtr>(x_data); PtrVector y_vec = ROL::makePtr>(y_data); PtrVector z_vec = ROL::makePtr>(z_data); std::vector stencil = {-1,0}; ROL::Ptr> x_pint = ROL::makePtr>(pintComm,vectorComm,x_vec,3*numRanks,stencil); ROL::Ptr> y_pint = ROL::makePtr>(pintComm,vectorComm,y_vec,3*numRanks,stencil); ROL::Ptr> z_pint = ROL::makePtr>(pintComm,vectorComm,z_vec,3*numRanks,stencil); /* *outStream << "X = " << std::endl; x_pint->print(*outStream); *outStream << "\n\nY = " << std::endl; y_pint->print(*outStream); *outStream << "\n\nZ = " << std::endl; z_pint->print(*outStream); */ if(x_pint->numOwnedVectors()!=4) { std::stringstream ss; ss << procStr << "Number owned vectors is " << x_pint->numOwnedVectors() << " is not 4!"; throw std::logic_error("Rank " + ss.str()); } if(x_pint->numOwnedSteps()!=3) { std::stringstream ss; ss << procStr << "Number owned steps is " << x_pint->numOwnedSteps() << " is not 3!"; throw std::logic_error("Rank " + ss.str()); } std::vector consistency = x_pint->checkVector(*y_pint,*z_pint,(myRank==0 ? true : false),*outStream); ROL::StdVector checkvec( ROL::makePtrFromRef(consistency) ); if (checkvec.norm() > std::sqrt(errtol)) { errorFlag++; std::stringstream ss; ss << procStr << "Failed check vector!"; throw std::logic_error("Rank " + ss.str()); } } // test boundary exchange (insert into buffer from vector) ///////////////////////////////////////////////////////////////////////////////////////////// *outStream << "Testing boundary exchange (left stencil)" << std::endl; { std::vector stencil = {-1,0}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // backwards time is owned for this stencil TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) ); p_pint->getVectorPtr(-1)->scale((myRank+1)*100-1); // backwards time is owned for this stencil p_pint->getVectorPtr( 0)->scale((myRank+1)*100+0); p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1); p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2); p_pint->boundaryExchange(); if(myRank!=0) { // no left boundary exchange to check dynamic_cast&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->getLocalView(); TEUCHOS_ASSERT(p_array.rank==2); TEUCHOS_ASSERT(p_array.dimension(0)==numEntries); TEUCHOS_ASSERT(p_array.dimension(1)==1); for(int i=0;i stencil = {1,0}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(2)) ); p_pint->getVectorPtr( 0)->scale((myRank+1)*100+-2); p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1); p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2); p_pint->getVectorPtr( 3)->scale((myRank+1)*100+3); p_pint->boundaryExchange(); if(myRank!=2) { // no right boundary exchange to check dynamic_cast&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->getLocalView(); TEUCHOS_ASSERT(p_array.rank==2); TEUCHOS_ASSERT(p_array.dimension(0)==numEntries); TEUCHOS_ASSERT(p_array.dimension(1)==1); for(int i=0;i stencil = {-1,0,1}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(2)) ); p_pint->getVectorPtr(-1)->scale((myRank+1)*100-3); p_pint->getVectorPtr( 0)->scale((myRank+1)*100-2); // this vector is actually sent (because of the stencil) p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1); p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2); // this vector is actually sent (because of the stencil) p_pint->getVectorPtr( 3)->scale((myRank+1)*100+3); p_pint->boundaryExchange(); if(myRank!=0) { // no left boundary exchange to check dynamic_cast&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->getLocalView(); TEUCHOS_ASSERT(p_array.rank==2); TEUCHOS_ASSERT(p_array.dimension(0)==numEntries); TEUCHOS_ASSERT(p_array.dimension(1)==1); for(int i=0;i&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->getLocalView(); for(int i=0;i stencil = {-1,0}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // backwards time is owned for this stencil TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) ); TEUCHOS_ASSERT( ROL::is_nullPtr(p_pint->getVectorPtr( 3)) ); p_pint->getRemoteBufferPtr(-1)->scale((myRank)*100-5); p_pint->getVectorPtr(-1)->scale((myRank)*100-1); p_pint->getVectorPtr( 0)->scale((myRank)*100+0); p_pint->getVectorPtr( 1)->scale((myRank)*100+1); p_pint->getVectorPtr( 2)->scale((myRank)*100+2); p_pint->boundaryExchangeSumInto(); if(myRank!=2) { dynamic_cast&>(*p_pint->getVectorPtr(2)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getVectorPtr(2)).getVector()->getLocalView(); TEUCHOS_ASSERT(p_array.rank==2); TEUCHOS_ASSERT(p_array.dimension(0)==numEntries); TEUCHOS_ASSERT(p_array.dimension(1)==1); for(int i=0;i stencil = {1,0}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 3)) ); p_pint->getRemoteBufferPtr(3)->scale((myRank)*100+5); p_pint->getVectorPtr( 0)->scale((myRank)*100+-2); p_pint->getVectorPtr( 1)->scale((myRank)*100+1); p_pint->getVectorPtr( 2)->scale((myRank)*100+2); p_pint->getVectorPtr( 3)->scale((myRank)*100+3); p_pint->boundaryExchangeSumInto(); if(myRank!=0) { dynamic_cast&>(*p_pint->getVectorPtr(0)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getVectorPtr(0)).getVector()->getLocalView(); for(int i=0;i stencil = {-1,0,1}; MVP p_data = ROL::makePtr(map,1,true); p_data->putScalar(1.0); PtrVector p_vec = ROL::makePtr>(p_data); ROL::Ptr> p_pint = ROL::makePtr>(pintComm,vectorComm,p_vec,3*numRanks,stencil); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // this comes from the stencil TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) ); TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 3)) ); // this comes from the stencil p_pint->getRemoteBufferPtr(-1)->scale((myRank)*100-5); // this vector is being sent to left p_pint->getRemoteBufferPtr( 3)->scale((myRank)*100+5); // this vector is being sent to right p_pint->getVectorPtr(-1)->scale((myRank)*100-3); p_pint->getVectorPtr( 0)->scale((myRank)*100-2); // this vector is recv (from left) p_pint->getVectorPtr( 1)->scale((myRank)*100+1); p_pint->getVectorPtr( 2)->scale((myRank)*100+2); // this vector is recv (from right) p_pint->getVectorPtr( 3)->scale((myRank)*100+3); p_pint->boundaryExchangeSumInto(); if(myRank!=2) { dynamic_cast&>(*p_pint->getVectorPtr(2)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getVectorPtr(2)).getVector()->getLocalView(); for(int i=0;i&>(*p_pint->getVectorPtr(0)).getVector()->sync(); auto p_array = dynamic_cast&>(*p_pint->getVectorPtr(0)).getVector()->getLocalView(); for(int i=0;i