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

Cherrypick of redirection of std::out for MPI

parent 50f55b20
Loading
Loading
Loading
Loading
+38 −1
Original line number Diff line number Diff line
@@ -13,6 +13,9 @@
#ifdef HAVE_MPI

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

namespace TNL {
namespace Communicators {
@@ -37,11 +40,30 @@ namespace Communicators {

        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=false)
        {
            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()
@@ -54,6 +76,18 @@ namespace Communicators {
            return MPI::Is_initialized();
        };

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

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

        static int GetRank()
        {
            return MPI::COMM_WORLD.Get_rank();
@@ -121,6 +155,9 @@ namespace Communicators {
    };

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

}//namespace Communicators
} // namespace TNL
+14 −4
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=false)
        {
            NullRequest=-1;
        };
@@ -36,6 +41,11 @@ namespace Communicators {
            return true;
        };

        static bool isDistributed()
        {
            return false;
        };

        static int GetRank()
        {
            return 0;
@@ -62,13 +72,13 @@ namespace Communicators {
        static Request ISend( const T *data, int count, int dest)
        {
            return 1;
        }    
        };    

        template <typename T>
        static Request IRecv( const T *data, int count, int src)
        {
            return 1;
        }
        };

        static void WaitAll(Request *reqs, int length)
        {
@@ -77,7 +87,7 @@ namespace Communicators {
        template< typename T > 
        static void Bcast(  T& data, int count, int root)
        {
        }
        };

       /* template< typename T >
        static void Allreduce( T& data,