Commit ed98de1f authored by Vít Hanousek's avatar Vít Hanousek
Browse files

1D Distributed Grid - refactoring - DistributedGrid_Base

parent 3c0bb9eb
Loading
Loading
Loading
Loading
+20 −62
Original line number Diff line number Diff line
@@ -12,39 +12,27 @@

#include <TNL/Meshes/Grid.h>
#include <TNL/Logger.h>
#include <TNL/Meshes/DistributedMeshes/DistributedGrid_Base.h>

namespace TNL {
namespace Meshes { 
namespace DistributedMeshes {

template< typename RealType, typename Device, typename Index >     
class DistributedMesh< Grid< 1, RealType, Device, Index > >
class DistributedMesh< Grid< 1, RealType, Device, Index > > : public DistributedGrid_Base<1, RealType, Device, Index >
{

    public:
    
      typedef Index IndexType;
      typedef Grid< 1, RealType, Device, IndexType > GridType;
      typedef typename GridType::PointType PointType;
      typedef Containers::StaticVector< 1, IndexType > CoordinatesType;

      static constexpr int getMeshDimension() { return 1; };    
      using typename DistributedGrid_Base<1, RealType, Device, Index >::IndexType;
      using typename DistributedGrid_Base<1, RealType, Device, Index >::GridType;
      using typename DistributedGrid_Base<1, RealType, Device, Index >::PointType;
      using typename DistributedGrid_Base<1, RealType, Device, Index >::CoordinatesType;

      DistributedMesh();

      bool setup( const Config::ParameterContainer& parameters,
                  const String& prefix );
      
      void setDomainDecomposition( const CoordinatesType& domainDecomposition );
      
      const CoordinatesType& getDomainDecomposition() const;
      
      template< int EntityDimension >
      IndexType getEntitiesCount() const;

      template< typename Entity >
      IndexType getEntitiesCount() const;      
      
      template<typename CommunicatorType>
      void setGlobalGrid( const GridType& globalGrid, const CoordinatesType& overlap );
       
@@ -54,58 +42,27 @@ class DistributedMesh< Grid< 1, RealType, Device, Index > >

      String printProcessDistr() const;    

      bool isDistributed() const;
       
      int getLeft() const;
       
      int getRight() const;
      
      const CoordinatesType& getOverlap() const;

      //number of elements of local sub domain WITHOUT overlap
      const CoordinatesType& getLocalSize() const;

      //dimensions of global grid
      const CoordinatesType& getGlobalSize() const;

      //coordinates of begin of local subdomain without overlaps in global grid
      const CoordinatesType& getGlobalBegin() const;
/*      template< int EntityDimension >
      IndexType getEntitiesCount() const;

      //number of elements of local sub domain WITH overlap
      const CoordinatesType& getLocalGridSize() const;
      template< typename Entity >
      IndexType getEntitiesCount() const;   */   
     
      //coordinates of begin of local subdomain without overlaps in local grid       
      const CoordinatesType& getLocalBegin() const;
      
      void writeProlog( Logger& logger ) const;       
      
   private : 

      GridType globalGrid;
      PointType localOrigin;
      CoordinatesType localBegin;
      CoordinatesType localSize;
      CoordinatesType localGridSize;
      CoordinatesType overlap;
      CoordinatesType globalDimensions;
      CoordinatesType globalBegin;
      PointType spaceSteps;

      IndexType Dimensions;        
      bool distributed;
      
      int rank;
      int nproc;
      
      CoordinatesType domainDecomposition;
      CoordinatesType subdomainCoordinates;      
        
      int numberOfLarger;
  private:
        
      int left;
      int right;

      bool isSet;
      
};

      } //namespace DistributedMeshes
@@ -113,3 +70,4 @@ class DistributedMesh< Grid< 1, RealType, Device, Index > >
} // namespace TNL

#include <TNL/Meshes/DistributedMeshes/DistributedGrid_1D.hpp>
+46 −116
Original line number Diff line number Diff line
@@ -10,8 +10,6 @@

#pragma once

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

namespace TNL {
   namespace Meshes {
      namespace DistributedMeshes {
@@ -19,7 +17,9 @@ namespace TNL {
template<typename RealType, typename Device, typename Index >
DistributedMesh< Grid< 1, RealType, Device, Index > >::
DistributedMesh()
: domainDecomposition( 0 ), isSet( false ) {}
{
    this->domainDecomposition=CoordinatesType( 0 );
}

template< typename RealType, typename Device, typename Index >     
bool
@@ -31,22 +31,7 @@ setup( const Config::ParameterContainer& parameters,
   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< int EntityDimension >
Index
@@ -63,7 +48,7 @@ DistributedMesh< Grid< 1, RealType, Device, Index > >::
getEntitiesCount() const
{
   return this->globalGrid. template getEntitiesCount< Entity >();
}
}*/

template< typename RealType, typename Device, typename Index >     
   template< typename CommunicatorType>
@@ -78,75 +63,75 @@ setGlobalGrid( const GridType& globalGrid,
   left=-1;
   right=-1;

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

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

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

       this->domainDecomposition[ 0 ] = nproc;
       globalDimensions=globalGrid.getDimensions();                 
       this->domainDecomposition[ 0 ] = this->nproc;
       this->globalDimensions=globalGrid.getDimensions();                 

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

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

       if(numberOfLarger>rank)
       if(numberOfLarger>this->rank)
       {
           globalBegin.x()=rank*localSize.x();
           localOrigin.x()=globalGrid.getOrigin().x()
                        +(globalBegin.x()-overlap.x())*globalGrid.getSpaceSteps().x();
           this->globalBegin.x()=this->rank*this->localSize.x();
           this->localOrigin.x()=globalGrid.getOrigin().x()
                        +(this->globalBegin.x()-this->overlap.x())*this->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();
           this->globalBegin.x()=numberOfLarger*(this->localSize.x()+1)+(this->rank-numberOfLarger)*this->localSize.x();
           this->localOrigin.x()=(this->globalGrid.getOrigin().x()-overlap.x())
                        +this->globalBegin.x()*this->globalGrid.getSpaceSteps().x();
       }

      localBegin=overlap;
      this->localBegin=overlap;

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

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

@@ -155,11 +140,11 @@ 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);
   TNL_ASSERT_TRUE(this->isSet,"DistributedGrid is not set, but used by SetupGrid");
   grid.setOrigin(this->localOrigin);
   grid.setDimensions(this->localGridSize);
   //compute local proportions by sideefect
   grid.setSpaceSteps(spaceSteps);
   grid.setSpaceSteps(this->spaceSteps);
   grid.SetDistMesh(this);
};

@@ -168,7 +153,7 @@ String
DistributedMesh< Grid< 1, RealType, Device, Index > >::
printProcessCoords() const
{
   return convertToString(rank);
   return convertToString(this->rank);
};

template< typename RealType, typename Device, typename Index >     
@@ -176,23 +161,16 @@ String
DistributedMesh< Grid< 1, RealType, Device, Index > >::
printProcessDistr() const
{
   return convertToString(nproc);
   return convertToString(this->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");
   TNL_ASSERT_TRUE(this->isSet,"DistributedGrid is not set, but used by getLeft");
   return this->left;
};

@@ -201,58 +179,10 @@ int
DistributedMesh< Grid< 1, RealType, Device, Index > >::
getRight() const
{
   TNL_ASSERT_TRUE(isSet,"DistributedGrid is not set, but used by getRight");
   TNL_ASSERT_TRUE(this->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 > >::
+86 −0
Original line number Diff line number Diff line
/***************************************************************************
                          DistributedGrid_Base.h  -  part common for all dimensions
                             -------------------
    begin                : July 07, 2018
    copyright            : (C) 2018 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/


/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Meshes/Grid.h>
#include <TNL/Logger.h>

namespace TNL {
namespace Meshes { 
namespace DistributedMeshes {

template<int dim, typename RealType, typename Device, typename Index >     
class DistributedGrid_Base
{
  public:

      typedef Index IndexType;
      typedef Grid< dim, RealType, Device, IndexType > GridType;
      typedef typename GridType::PointType PointType;
      typedef Containers::StaticVector< dim, IndexType > CoordinatesType;

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

      DistributedGrid_Base();
    
      void setDomainDecomposition( const CoordinatesType& domainDecomposition );      
      const CoordinatesType& getDomainDecomposition() const;

      bool isDistributed() const;
           
      const CoordinatesType& getOverlap() const;

      //number of elements of local sub domain WITHOUT overlap
      const CoordinatesType& getLocalSize() const;

      //dimensions of global grid
      const CoordinatesType& getGlobalSize() const;

      //coordinates of begin of local subdomain without overlaps in global grid
      const CoordinatesType& getGlobalBegin() const;

      //number of elements of local sub domain WITH overlap
      const CoordinatesType& getLocalGridSize() const;
       
      //coordinates of begin of local subdomain without overlaps in local grid       
      const CoordinatesType& getLocalBegin() const;

   public: 

      GridType globalGrid;
      PointType localOrigin;
      CoordinatesType localBegin;
      CoordinatesType localSize;
      CoordinatesType localGridSize;
      CoordinatesType overlap;
      CoordinatesType globalDimensions;
      CoordinatesType globalBegin;
      PointType spaceSteps;

      CoordinatesType domainDecomposition;
      CoordinatesType subdomainCoordinates;    

      IndexType Dimensions;        
      bool distributed;
        
      int rank;
      int nproc;

      bool isSet;

};

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

#include <TNL/Meshes/DistributedMeshes/DistributedGrid_Base.hpp>
+99 −0
Original line number Diff line number Diff line
/***************************************************************************
                          DistributedGrid_Base.hpp  -  description
                             -------------------
    begin                : July 07, 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<int dim, typename RealType, typename Device, typename Index >
DistributedGrid_Base< dim, RealType, Device, Index >::
DistributedGrid_Base()
: isSet( false ) {}

template< int dim, typename RealType, typename Device, typename Index >     
void
DistributedGrid_Base< dim, RealType, Device, Index >::
setDomainDecomposition( const CoordinatesType& domainDecomposition )
{
   this->domainDecomposition = domainDecomposition;
}

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getDomainDecomposition() const
{
   return this->domainDecomposition;
}
      
template< int dim, typename RealType, typename Device, typename Index >     
bool
DistributedGrid_Base< dim, RealType, Device, Index >::
isDistributed() const
{
   return this->distributed;
};

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getOverlap() const
{
   return this->overlap;
};

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getLocalSize() const
{
   return this->localSize;
}

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

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getGlobalBegin() const
{
   return this->globalBegin;
}

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getLocalGridSize() const
{
   return this->localGridSize;
}

template< int dim, typename RealType, typename Device, typename Index >     
const typename DistributedGrid_Base< dim, RealType, Device, Index >::CoordinatesType&
DistributedGrid_Base< dim, RealType, Device, Index >::
getLocalBegin() const
{
   return this->localBegin;
}

      } //namespace DistributedMeshes
   } // namespace Meshes
} // namespace TNL
+3 −3
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ class CutMeshFunction


#define DIM 3
#define CODIM 1
#define CODIM 2
#define DEVICE Host
#define SIZE 5

@@ -152,8 +152,8 @@ int main(int argc, char **argv)
   {
        CutMeshFunction<MeshFunction<MeshType>,CutMeshType,DofType>::Cut(
            *meshFunctionptr,*cutGrid, cutDof, 
            StaticVector<DIM-CODIM,int>(0,2),
            StaticVector<CODIM,int>(1),
            StaticVector<DIM-CODIM,int>(0),
            StaticVector<CODIM,int>(1,2),
            StaticVector<CODIM,typename CutMeshType::IndexType>(i) );

        MeshFunction<CutMeshType> cutMeshFunction;