Commit 703f68bf authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Refactoring of DistributedGrid_1D.

parent bd354d67
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ SET( headers BufferEntitiesHelper.h
             DistributedMesh.h
             DistributedMeshSynchronizer.h
             DistributedGrid_1D.h
             DistributedGrid_1D.hpp
             DistributedGrid_2D.h
             DistributedGrid_3D.h
             DistributedGridSynchronizer.h
+27 −175
Original line number Diff line number Diff line
@@ -30,207 +30,61 @@ class DistributedMesh<Grid< 1, RealType, Device, Index >>

      static constexpr int getMeshDimension() { return 1; };    

      DistributedMesh()
      : isSet(false ){};
      DistributedMesh();

      bool setup( const Config::ParameterContainer& parameters,
                  const String& prefix )
      {
         this->domainDecomposition.x() = parameters.getParameter< int >( "grid-domain-decomposition-x" );
         return true;
      }      
                  const String& prefix );
      
      void setDomainDecomposition( const CoordinatesType& domainDecomposition )
      {
         this->domainDecomposition = domainDecomposition;
      }
      void setDomainDecomposition( const CoordinatesType& domainDecomposition );
      
      const CoordinatesType& getDomainDecomposition()
      {
         return this->domainDecomposition;
      }
      const CoordinatesType& getDomainDecomposition() const;
      
      template<typename CommunicatorType>
      void setGlobalGrid(GridType globalGrid, CoordinatesType overlap, int *distribution=NULL)
      {
         isSet=true;

         this->overlap=overlap;

         left=-1;
         right=-1;

         Dimensions= GridType::getMeshDimension();
         spaceSteps=globalGrid.getSpaceSteps();

         distributed=false;
         if(CommunicatorType::IsInitialized())
         {
             rank=CommunicatorType::GetRank();
             this->nproc=CommunicatorType::GetSize();
             //use only if have more than one process
             if(this->nproc>1)
             {
                 distributed=true;
             }
         }

         if(!distributed)
         {
             //Without distribution

             std::cout <<"BEZ Distribuce"<<std::endl;
             rank=0;
             localOrigin=globalGrid.getOrigin();
             localSize=globalGrid.getDimensions();
             localGridSize=globalGrid.getDimensions();
             globalSize=globalGrid.getDimensions();
             globalBegin=CoordinatesType(0);

             localBegin=CoordinatesType(0);
             return;
         }
         else
         {            
             //nearnodes
             if(rank!=0)
                 left=rank-1;
             if(rank!=nproc-1)
                 right=rank+1;
      void setGlobalGrid( const GridType& globalGrid, const CoordinatesType& overlap );
       
             this->domainDecomposition[ 0 ] = rank;

             globalSize=globalGrid.getDimensions();                 

             //compute local mesh size               
             numberOfLarger=globalGrid.getDimensions().x()%nproc;

             localSize.x()=(globalGrid.getDimensions().x()/nproc);               
             if(numberOfLarger>rank)
                  localSize.x()+=1;                      

             if(numberOfLarger>rank)
             {
                 globalBegin.x()=rank*localSize.x();
                 localOrigin.x()=globalGrid.getOrigin().x()
                              +(globalBegin.x()-overlap.x())*globalGrid.getSpaceSteps().x();
             }
             else
             {
                 globalBegin.x()=numberOfLarger*(localSize.x()+1)+(rank-numberOfLarger)*localSize.x();
                 localOrigin.x()=(globalGrid.getOrigin().x()-overlap.x())
                              +globalBegin.x()*globalGrid.getSpaceSteps().x();
             }

            localBegin=overlap;

             //vlevo neni prekryv
             if(left==-1)
             {
                 localOrigin.x()+=overlap.x()*globalGrid.getSpaceSteps().x();
                 localBegin.x()=0;
             }

             localGridSize=localSize;
             //add overlaps
             if(left==-1||right==-1)
                 localGridSize.x()+=overlap.x();
             else
                 localGridSize.x()+=2*overlap.x();

         }  
      } 
       
      void setupGrid( GridType& grid)
      {
         TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by SetupGrid");
         grid.setOrigin(localOrigin);
         grid.setDimensions(localGridSize);
         //compute local proporions by sideefect
         grid.setSpaceSteps(spaceSteps);
         grid.SetDistMesh(this);
      };
      void setupGrid( GridType& grid );
       
      String printProcessCoords()
      {
         return convertToString(rank);
      };
      String printProcessCoords() const;

      String printProcessDistr()
      {
         return convertToString(nproc);
      };       
      String printProcessDistr() const;

      bool isDistributed()
      {
         return this->distributed;
      };
      bool isDistributed() const;
       
      int getLeft()
      {
         TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by getLeft");
         return this->left;
      };
      int getLeft() const;
       
      int getRight()
      {
         TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by getRight");
         return this->right;
      };
      int getRight() const;
       
      CoordinatesType getOverlap()
      {
         return this->overlap;
      };
      const CoordinatesType& getOverlap() const;

      //number of elements of local sub domain WITHOUT overlap
      CoordinatesType getLocalSize()
      {
         return this->localSize;
      }
      const CoordinatesType& getLocalSize() const;

      //number of elements of global grid
      CoordinatesType getGlobalSize()
      {
         return this->globalSize;
      }
      //dimensions of global grid
      const CoordinatesType& getGlobalSize() const;

      //coordinates of begin of local subdomain without overlaps in global grid
      CoordinatesType getGlobalBegin()
      {
         return this->globalBegin;
      }
      const CoordinatesType& getGlobalBegin() const;

      //number of elemnts of local sub domain WITH overlap
      CoordinatesType getLocalGridSize()
      {
         return this->localGridSize;
      }
      //number of elements of local sub domain WITH overlap
      const CoordinatesType& getLocalGridSize() const;
       
      //coordinates of begin of local subdomain without overlaps in local grid       
      CoordinatesType getLocalBegin()
      {
         return this->localBegin;
      }
      
      void writeProlog( Logger& logger )
      {
         logger.writeParameter( "Domain decomposition:", this->getDomainDecomposition() );
      }
      const CoordinatesType& getLocalBegin() const;
      
      void writeProlog( Logger& logger ) const;       
       
   private : 

      GridType globalGrid;
      PointType localOrigin;
      CoordinatesType localBegin;
      CoordinatesType localSize;
      CoordinatesType localGridSize;
      CoordinatesType overlap;
      CoordinatesType globalSize;
      CoordinatesType globalDimensions;
      CoordinatesType globalBegin;
      PointType spaceSteps;
        
        
      IndexType Dimensions;        
      bool distributed;
        
@@ -246,12 +100,10 @@ class DistributedMesh<Grid< 1, RealType, Device, Index >>
      int right;

      bool isSet;
        
       
};



} // namespace DistributedMeshes
} // namespace Meshes
} // namespace TNL

#include <TNL/Meshes/DistributedMeshes/DistributedGrid_1D.hpp>
 No newline at end of file
+253 −0
Original line number Diff line number Diff line
/***************************************************************************
                          DistributedGrid_1D.hpp  -  description
                             -------------------
    begin                : January 09, 2018
    copyright            : (C) 2018 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Meshes/DistributedMeshes/DistributedGrid_1D.h>

namespace TNL {
   namespace Meshes {
      namespace DistributedMeshes {

template<typename RealType, typename Device, typename Index >
DistributedMesh< Grid< 1, RealType, Device, Index > >::
DistributedMesh()
: isSet(false ){};

template< typename RealType, typename Device, typename Index >     
bool
DistributedMesh< Grid< 1, RealType, Device, Index > >::
setup( const Config::ParameterContainer& parameters,
       const String& prefix )
{
   this->domainDecomposition.x() = parameters.getParameter< int >( "grid-domain-decomposition-x" );
   return true;
}      

template< typename RealType, typename Device, typename Index >     
void
DistributedMesh< Grid< 1, RealType, Device, Index > >::
setDomainDecomposition( const CoordinatesType& domainDecomposition )
{
   this->domainDecomposition = domainDecomposition;
}

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getDomainDecomposition() const
{
   return this->domainDecomposition;
}

template< typename RealType, typename Device, typename Index >     
   template< typename CommunicatorType>
void
DistributedMesh< Grid< 1, RealType, Device, Index > >::
setGlobalGrid( const GridType& globalGrid, const CoordinatesType& overlap )
{
   this->globalGrid = globalGrid;
   this->isSet = true;
   this->overlap = overlap;
   left=-1;
   right=-1;

   Dimensions = GridType::getMeshDimension();
   spaceSteps = globalGrid.getSpaceSteps();

   distributed = false;
   if( CommunicatorType::IsInitialized() )
   {
       rank = CommunicatorType::GetRank();
       this->nproc = CommunicatorType::GetSize();
       if( this->nproc>1 )
       {
           distributed = true;
       }
   }

   if( !distributed )
   {
       rank = 0;
       localOrigin = globalGrid.getOrigin();
       localSize = globalGrid.getDimensions();
       localGridSize = globalGrid.getDimensions();
       globalDimensions = globalGrid.getDimensions();
       globalBegin = CoordinatesType(0);
       localBegin = CoordinatesType(0);
       return;
   }
   else
   {            
       //nearnodes
       if(rank!=0)
           left=rank-1;
       if(rank!=nproc-1)
           right=rank+1;

       this->domainDecomposition[ 0 ] = rank;

       globalDimensions=globalGrid.getDimensions();                 

       //compute local mesh size               
       numberOfLarger=globalGrid.getDimensions().x()%nproc;

       localSize.x()=(globalGrid.getDimensions().x()/nproc);               
       if(numberOfLarger>rank)
            localSize.x()+=1;                      

       if(numberOfLarger>rank)
       {
           globalBegin.x()=rank*localSize.x();
           localOrigin.x()=globalGrid.getOrigin().x()
                        +(globalBegin.x()-overlap.x())*globalGrid.getSpaceSteps().x();
       }
       else
       {
           globalBegin.x()=numberOfLarger*(localSize.x()+1)+(rank-numberOfLarger)*localSize.x();
           localOrigin.x()=(globalGrid.getOrigin().x()-overlap.x())
                        +globalBegin.x()*globalGrid.getSpaceSteps().x();
       }

      localBegin=overlap;

       //vlevo neni prekryv
       if(left==-1)
       {
           localOrigin.x()+=overlap.x()*globalGrid.getSpaceSteps().x();
           localBegin.x()=0;
       }

       localGridSize=localSize;
       //add overlaps
       if(left==-1||right==-1)
           localGridSize.x()+=overlap.x();
       else
           localGridSize.x()+=2*overlap.x();

   }  
} 

template< typename RealType, typename Device, typename Index >     
void
DistributedMesh< Grid< 1, RealType, Device, Index > >::
setupGrid( GridType& grid)
{
   TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by SetupGrid");
   grid.setOrigin(localOrigin);
   grid.setDimensions(localGridSize);
   //compute local proporions by sideefect
   grid.setSpaceSteps(spaceSteps);
   grid.SetDistMesh(this);
};

template< typename RealType, typename Device, typename Index >     
String
DistributedMesh< Grid< 1, RealType, Device, Index > >::
printProcessCoords() const
{
   return convertToString(rank);
};

template< typename RealType, typename Device, typename Index >     
String
DistributedMesh< Grid< 1, RealType, Device, Index > >::
printProcessDistr() const
{
   return convertToString(nproc);
};       

template< typename RealType, typename Device, typename Index >     
bool
DistributedMesh< Grid< 1, RealType, Device, Index > >::
isDistributed() const
{
   return this->distributed;
};

template< typename RealType, typename Device, typename Index >     
int
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getLeft() const
{
   TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by getLeft");
   return this->left;
};

template< typename RealType, typename Device, typename Index >     
int
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getRight() const
{
   TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by getRight");
   return this->right;
};

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getOverlap() const
{
   return this->overlap;
};

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getLocalSize() const
{
   return this->localSize;
}

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getGlobalSize() const
{
   return this->globalGrid.getDimensions();
}

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getGlobalBegin() const
{
   return this->globalBegin;
}

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getLocalGridSize() const
{
   return this->localGridSize;
}

template< typename RealType, typename Device, typename Index >     
const typename DistributedMesh< Grid< 1, RealType, Device, Index > >::CoordinatesType&
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getLocalBegin() const
{
   return this->localBegin;
}

template< typename RealType, typename Device, typename Index >
void
DistributedMesh< Grid< 1, RealType, Device, Index > >::
writeProlog( Logger& logger ) const
{
   this->globalGrid.writeProlog( logger );
   logger.writeParameter( "Domain decomposition:", this->getDomainDecomposition() );
}

      } //namespace DistributedMeshes
   } // namespace Meshes
} // namespace TNL
+2 −2
Original line number Diff line number Diff line
@@ -417,6 +417,8 @@ void
Grid< 1, Real, Device, Index >::
writeProlog( Logger& logger ) const
{
   if( this->getDistributedMesh() )
      return this->getDistributedMesh()->writeProlog( logger );
   logger.writeParameter( "Dimension:", getMeshDimension() );
   logger.writeParameter( "Domain origin:", this->origin );
   logger.writeParameter( "Domain proportions:", this->proportions );
@@ -424,8 +426,6 @@ writeProlog( Logger& logger ) const
   logger.writeParameter( "Space steps:", this->getSpaceSteps() );
   logger.writeParameter( "Number of cells:", getEntitiesCount< Cell >() );
   logger.writeParameter( "Number of vertices:", getEntitiesCount< Vertex >() );
   if( this->getDistributedMesh() )
      this->getDistributedMesh()->writeProlog( logger );
}

} // namespace Meshes
+2 −3
Original line number Diff line number Diff line
@@ -496,6 +496,8 @@ void
Grid< 2, Real, Device, Index >::
writeProlog( Logger& logger ) const
{
   if( this->getDistributedMesh() )
      return this->getDistributedMesh()->writeProlog( logger );
   logger.writeParameter( "Dimension:", getMeshDimension() );
   logger.writeParameter( "Domain origin:", this->origin );
   logger.writeParameter( "Domain proportions:", this->proportions );
@@ -504,9 +506,6 @@ writeProlog( Logger& logger ) const
   logger.writeParameter( "Number of cells:", getEntitiesCount< Cell >() );
   logger.writeParameter( "Number of faces:", getEntitiesCount< Face >() );
   logger.writeParameter( "Number of vertices:", getEntitiesCount< Vertex >() );
   if( this->getDistributedMesh() )
      this->getDistributedMesh()->writeProlog( logger );

}

} // namespace Meshes
Loading