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

Working Heat equation example with Communicator in Problem

parent 4abeee6c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ endif()

# set Debug/Release options
set( CMAKE_CXX_FLAGS "-std=c++11 -pthread -Wall -Wno-unused-local-typedefs -Wno-unused-variable" )
set( CMAKE_CXX_FLAGS_DEBUG "-g -rdynamic" )
set( CMAKE_CXX_FLAGS_DEBUG "-g -rdynamic -ftemplate-backtrace-limit=0" )
set( CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -mtune=native -DNDEBUG" )
#set( CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -mtune=native -DNDEBUG -ftree-vectorizer-verbose=1 -ftree-vectorize -fopt-info-vec-missed -funroll-loops" )
# pass -rdynamic only in Debug mode
+6 −9
Original line number Diff line number Diff line
@@ -11,10 +11,6 @@
#ifndef TNL_HEAT_EQUATION_H_
#define TNL_HEAT_EQUATION_H_

#ifdef HAVE_MPI
    #define USE_MPI
#endif

#include <TNL/Solvers/Solver.h>
#include <TNL/Solvers/FastBuildConfigTag.h>
#include <TNL/Solvers/BuildConfigTags.h>
@@ -61,7 +57,8 @@ template< typename Real,
          typename Index,
          typename MeshType,
          typename MeshConfig,
          typename SolverStarter >
          typename SolverStarter,
          typename CommunicatorType >
class heatEquationSetter
{
   public:
@@ -83,12 +80,12 @@ class heatEquationSetter
         if( boundaryConditionsType == "dirichlet" )
         {
            typedef Operators::DirichletBoundaryConditions< MeshType, Constant > BoundaryConditions;
            typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide, ApproximateOperator > Problem;
            typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide,CommunicatorType, ApproximateOperator > Problem;
            SolverStarter solverStarter;
            return solverStarter.template run< Problem >( parameters );
         }
         typedef Operators::NeumannBoundaryConditions< MeshType, Constant, Real, Index > BoundaryConditions;
         typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide, ApproximateOperator > Problem;
         typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide, CommunicatorType, ApproximateOperator > Problem;
         SolverStarter solverStarter;
         return solverStarter.template run< Problem >( parameters );
      }
@@ -96,12 +93,12 @@ class heatEquationSetter
      if( boundaryConditionsType == "dirichlet" )
      {
         typedef Operators::DirichletBoundaryConditions< MeshType, MeshFunction > BoundaryConditions;
         typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide, ApproximateOperator > Problem;
         typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide,CommunicatorType, ApproximateOperator > Problem;
         SolverStarter solverStarter;
         return solverStarter.template run< Problem >( parameters );
      }
      typedef Operators::NeumannBoundaryConditions< MeshType, MeshFunction, Real, Index > BoundaryConditions;
      typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide, ApproximateOperator > Problem;
      typedef HeatEquationProblem< MeshType, BoundaryConditions, RightHandSide,CommunicatorType, ApproximateOperator > Problem;
      SolverStarter solverStarter;
      return solverStarter.template run< Problem >( parameters );
   };
+52 −2
Original line number Diff line number Diff line
@@ -10,9 +10,14 @@

#pragma once



#ifdef HAVE_MPI

#include <mpi.h>
#include <TNL/String.h>
#include <iostream>
#include <fstream>

namespace TNL {
namespace Communicators {
@@ -35,17 +40,58 @@ namespace Communicators {
        
        public:

        //can be call before init
        static bool isAvailable()
        {
            return true;
        }

        //can be called only after init 
        static bool isDistributed()
        {
            return GetSize()>1;
        };

        typedef MPI::Request Request;
        static MPI::Request NullRequest;
        static std::streambuf *psbuf;
        static std::streambuf *backup;
        static std::ofstream filestr;

        static void Init(int argc, char **argv)
        static void Init(int argc, char **argv, bool redirect)
        {
            MPI::Init(argc,argv);
            NullRequest=MPI::REQUEST_NULL;

            if(isDistributed() && redirect)
            {
                //redirect all stdout to files, only 0 take to go to console
                backup=std::cout.rdbuf();

                //redirect output to files...
                if(MPI::COMM_WORLD.Get_rank()!=0)
                {
                    std::cout<< GetRank() <<": Redirecting std::out to file" <<std::endl;
                    String stdoutfile;
                    stdoutfile=String( "./stdout-")+convertToString(MPI::COMM_WORLD.Get_rank())+String(".txt");
                    filestr.open (stdoutfile.getString()); 
                    psbuf = filestr.rdbuf(); 
                    std::cout.rdbuf(psbuf);
                }
            }

        };

        static void Finalize()
        {
            if(isDistributed())
            {
                if(MPI::COMM_WORLD.Get_rank()!=0)
                { 
                    std::cout.rdbuf(backup);
                    filestr.close(); 
                }
            }
            MPI::Finalize();
        };

@@ -121,8 +167,12 @@ namespace Communicators {
    };

    MPI::Request MpiCommunicator::NullRequest;
    std::streambuf *MpiCommunicator::psbuf;
    std::streambuf *MpiCommunicator::backup;
    std::ofstream MpiCommunicator::filestr;

}//namespace Communicators
} // namespace TNL

#endif

+11 −1
Original line number Diff line number Diff line
@@ -22,7 +22,12 @@ namespace Communicators {
        typedef int Request;
        static Request NullRequest;

        static void Init(int argc, char **argv)
        static bool isAvailable()
        {
            return true;
        }

        static void Init(int argc, char **argv, bool redirect)
        {
            NullRequest=-1;
        };
@@ -36,6 +41,11 @@ namespace Communicators {
            return true;
        };

        static bool isDistributed()
        {
            return false;
        };

        static int GetRank()
        {
            return 0;
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ namespace Problems {
template< typename Mesh,
          typename BoundaryCondition,
          typename RightHandSide,
          typename CommType,
          typename DifferentialOperator = Operators::LinearDiffusion< Mesh,
                                                              typename BoundaryCondition::RealType > >
class HeatEquationProblem : public PDEProblem< Mesh,
@@ -58,6 +59,8 @@ class HeatEquationProblem : public PDEProblem< Mesh,
      using typename BaseType::MeshDependentDataType;
      using typename BaseType::MeshDependentDataPointer;

      typedef CommType CommunicatorType;

      static String getTypeStatic();

      String getPrologHeader() const;
Loading