From 72dce620c467f6d4eb3f545faebd2b6742991391 Mon Sep 17 00:00:00 2001
From: Markus Blatt <mblatt@dune-project.org>
Date: Sun, 16 Jul 2006 17:51:32 +0000
Subject: [PATCH] A preliminary version of an MPI helper for discussion

[[Imported from SVN: r4598]]
---
 common/geometrytype.hh       |   8 +-
 common/mpihelper.hh          | 185 +++++++++++++++++++++++++++++++++++
 common/stdstreams.hh         |   2 +-
 common/test/.gitignore       |   3 +-
 common/test/Makefile.am      |   6 +-
 common/test/mpihelpertest.cc |  23 +++++
 6 files changed, 221 insertions(+), 6 deletions(-)
 create mode 100644 common/mpihelper.hh
 create mode 100644 common/test/mpihelpertest.cc

diff --git a/common/geometrytype.hh b/common/geometrytype.hh
index 11a3bcab2..7ac491ad4 100644
--- a/common/geometrytype.hh
+++ b/common/geometrytype.hh
@@ -27,7 +27,8 @@ namespace Dune {
       simplex,               //!< Simplicial element in any nonnegative dimension
       cube,                  //!< Cube element in any nonnegative dimension
       pyramid,               //!< Four sided pyramid in three dimensions
-      prism                  //!< Prism element in three dimensions
+      prism,                  //!< Prism element in three dimensions
+      undefined
     };
 
   private:
@@ -36,11 +37,12 @@ namespace Dune {
     BasicType basicType_ : 16;
 
     /** \brief Dimension of the element */
-    short dim_;
+    unsigned int dim_;
 
   public:
     /** \brief Default constructor, not initializing anything */
-    GeometryType () {}
+    GeometryType ()
+    {}
 
     /** \brief Constructor */
     GeometryType(BasicType basicType, unsigned int dim)
diff --git a/common/mpihelper.hh b/common/mpihelper.hh
new file mode 100644
index 000000000..d3984d058
--- /dev/null
+++ b/common/mpihelper.hh
@@ -0,0 +1,185 @@
+// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// vi: set et ts=4 sw=2 sts=2:
+// $Id: $
+#ifndef DUNE_MPIHELPER
+#define DUNE_MPIHELPER
+#if HAVE_MPI
+#include "mpi.h"
+#endif
+
+namespace Dune
+{
+
+  class FakeMPIHelper
+  {
+  public:
+    /**
+     * @brief A simple smart pointer responsible for creation
+     * and deletion of the instance.
+     */
+    class InstancePointer
+    {
+    public:
+      /** @brief Construct a null pointer. */
+      InstancePointer() : pointer_(0)
+      {}
+      /** @brief Delete the instance we point to. */
+      ~InstancePointer()
+      {
+        if(pointer_ != 0)
+          delete pointer_;
+      }
+      /**
+       * @brief Get a pointer to the instance.
+       * @return The instance we store.
+       */
+      FakeMPIHelper* get()
+      {
+        return pointer_;
+      }
+      /**
+       * @brief Set the pointer.
+       * @paramter pointer A pointer to the instance.
+       */
+      void set(FakeMPIHelper* pointer)
+      {
+        if(pointer != 0) {
+          delete pointer_;
+          pointer_ = pointer;
+        }
+      }
+    private:
+      FakeMPIHelper* pointer_;
+    };
+  public:
+    enum {
+      /**
+       * @brief Are we fake (i. e. pretend to have MPI support but are compiled
+       * without.
+       */
+      isFake = true
+    };
+
+
+    /**
+     * @brief The type of the mpi communicator.
+     */
+    typedef int MPICommunicator;
+
+    static MPICommunicator getCommunicator()
+    {
+      return -1;
+    }
+
+    static FakeMPIHelper& instance(int argc, char** argv)
+    {
+      if(instance_.get() == 0)
+        instance_.set(new FakeMPIHelper());
+      return *instance_.get();
+    }
+
+  private:
+    FakeMPIHelper()
+    {}
+    FakeMPIHelper(const FakeMPIHelper&);
+    FakeMPIHelper& operator=(const FakeMPIHelper);
+
+    static InstancePointer instance_;
+  };
+
+  FakeMPIHelper::InstancePointer FakeMPIHelper::instance_ = FakeMPIHelper::InstancePointer();
+
+#ifdef HAVE_MPI
+
+  class MPIHelper
+  {
+  public:
+    /**
+     * @brief A simple smart pointer responsible for creation
+     * and deletion of the instance.
+     */
+    class InstancePointer
+    {
+    public:
+      /** @brief Construct a null pointer. */
+      InstancePointer() : pointer_(0)
+      {}
+      /** @brief Delete the instance we point to. */
+      ~InstancePointer()
+      {
+        if(pointer_ != 0)
+          delete pointer_;
+      }
+      /**
+       * @brief Get a pointer to the instance.
+       * @return The instance we store.
+       */
+      MPIHelper* get()
+      {
+        return pointer_;
+      }
+      /**
+       * @brief Set the pointer.
+       * @paramter pointer A pointer to the instance.
+       */
+      void set(MPIHelper* pointer)
+      {
+        if(pointer != 0) {
+          delete pointer_;
+          pointer_ = pointer;
+        }
+      }
+    private:
+      MPIHelper* pointer_;
+    };
+  public:
+    enum {
+      /**
+       * @brief Are we fake (i. e. pretend to have MPI support but are compiled
+       * without.
+       */
+      isFake = false
+    };
+
+    /**
+     * @brief The type of the mpi communicator.
+     */
+    typedef MPI_Comm MPICommunicator;
+
+    static MPICommunicator getCommunicator(){
+      return MPI_COMM_WORLD;
+    }
+
+    static MPIHelper& instance(int argc, char** argv)
+    {
+      if(instance_.get() == 0)
+        instance_.set(new MPIHelper(argc, argv));
+      return *instance_.get();
+    }
+
+  private:
+    MPIHelper(int argc, char** argv)
+    {
+      MPI_Init(&argc, &argv);
+    }
+    ~MPIHelper()
+    {
+      MPI_Finalize();
+    }
+    MPIHelper(const MPIHelper&);
+    MPIHelper& operator=(const MPIHelper);
+
+    static InstancePointer instance_;
+  };
+
+  MPIHelper::InstancePointer MPIHelper::instance_ = MPIHelper::InstancePointer();
+
+#else
+  // We do not have MPI therefore FakeMPIHelper
+  // is the MPIHelper
+#define MPIHelper FakeMPIHelper
+
+#endif
+
+} // end namespace Dune
+#endif
diff --git a/common/stdstreams.hh b/common/stdstreams.hh
index bc228e87f..6ce9adb4f 100644
--- a/common/stdstreams.hh
+++ b/common/stdstreams.hh
@@ -61,7 +61,7 @@ namespace Dune {
    * If the  level of a stream is bigger than this value
    * it will be activated.
    */
-  static const DebugLevel MINIMAL_DEBUG_LEVEL = 4;
+  static const DebugLevel MINIMAL_DEBUG_LEVEL = 3;
 
   /**
    * @brief The level of the very verbose debug stream.
diff --git a/common/test/.gitignore b/common/test/.gitignore
index 00a0dfc68..2658c4983 100644
--- a/common/test/.gitignore
+++ b/common/test/.gitignore
@@ -21,4 +21,5 @@ streamtest
 exprtmpl
 timing_xpr
 timing_old
-timing_flt
\ No newline at end of file
+timing_flt
+mpihelpertest
diff --git a/common/test/Makefile.am b/common/test/Makefile.am
index b679265fd..529ec98fc 100644
--- a/common/test/Makefile.am
+++ b/common/test/Makefile.am
@@ -3,7 +3,7 @@
 TESTPROGS = parsetest test-stack arraylisttest smartpointertest \
 	sllisttest iteratorfacadetest tuplestest fmatrixtest \
 	poolallocatortest settest gcdlcdtest streamtest \
-	bigunsignedinttest
+	bigunsignedinttest mpihelpertest
 # exprtmpl
 
 # which tests to run
@@ -74,4 +74,8 @@ timing_flt_SOURCES = timing.cc
 timing_flt_CXXFLAGS = -DDUNE_EXPRESSIONTEMPLATES -DDUNE_FLATIT -g
 timing_flt_DEPENDENCIES = $(LOCAL_LIBS)
 
+mpihelpertest_SOURCES = mpihelpertest.cc
+mpihelpertest_CXXFLAGS = $(MPI_CPPFLAGS)
+mpihelpertest_LDFLAGS = $(MPI_LDFLAGS) $(MPI_LIBS)
+
 include $(top_srcdir)/am/global-rules
diff --git a/common/test/mpihelpertest.cc b/common/test/mpihelpertest.cc
new file mode 100644
index 000000000..0eecbeb3e
--- /dev/null
+++ b/common/test/mpihelpertest.cc
@@ -0,0 +1,23 @@
+// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// vi: set et ts=4 sw=2 sts=2:
+#include "config.h"
+#include <dune/common/mpihelper.hh>
+#include <iostream>
+int main(int argc, char** argv)
+{
+  typedef Dune::MPIHelper Helper;
+
+  {
+    Helper& mpi = Helper::instance(argc, argv);
+
+    Helper::MPICommunicator comm = mpi.getCommunicator();
+  }
+
+  {
+    Helper& mpi = Helper::instance(argc, argv);
+
+    Helper::MPICommunicator comm= mpi.getCommunicator();
+  }
+  std::cout << "We are at the end!"<<std::endl;
+
+}
-- 
GitLab