From a36ede1748289a1d2a3613ef2124fc54afb25ccd Mon Sep 17 00:00:00 2001
From: Markus Blatt <mblatt@dune-project.org>
Date: Thu, 25 Nov 2010 20:16:45 +0000
Subject: [PATCH] Make Generic_MPI_Datatype deprecated in favor of
 MPI_Datatype.

[[Imported from SVN: r6269]]
---
 dune/common/mpicollectivecommunication.hh | 16 ++++++++-------
 dune/common/mpitraits.hh                  | 24 +++++++++++++++++++++--
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/dune/common/mpicollectivecommunication.hh b/dune/common/mpicollectivecommunication.hh
index 4bd7cc9f8..4bdbb1ec8 100644
--- a/dune/common/mpicollectivecommunication.hh
+++ b/dune/common/mpicollectivecommunication.hh
@@ -13,10 +13,12 @@
 #include <algorithm>
 #include <functional>
 
+#include "deprecated.hh"
 #include "exceptions.hh"
 #include "collectivecommunication.hh"
 #include "binaryfunctions.hh"
 #include "shared_ptr.hh"
+#include "mpitraits.hh"
 
 #if HAVE_MPI
 // MPI header
@@ -24,6 +26,7 @@
 
 namespace Dune
 {
+
   //=======================================================
   // use singleton pattern and template specialization to
   // generate MPI data types
@@ -35,7 +38,7 @@ namespace Dune
   class Generic_MPI_Datatype
   {
   public:
-    static MPI_Datatype get ()
+    static MPI_Datatype get()  DUNE_DEPRECATED
     {
       if (!type)
       {
@@ -59,7 +62,7 @@ namespace Dune
   template<> \
   class Generic_MPI_Datatype<p>{ \
   public: \
-    static inline MPI_Datatype get(){ \
+    static inline MPI_Datatype get() DUNE_DEPRECATED { \
       return m; \
     } \
   }
@@ -79,7 +82,6 @@ namespace Dune
 
 #undef ComposeMPITraits
 
-
   //=======================================================
   // use singleton pattern and template specialization to
   // generate MPI operations
@@ -295,15 +297,15 @@ namespace Dune
     template<typename T>
     int broadcast (T* inout, int len, int root) const
     {
-      return MPI_Bcast(inout,len,Generic_MPI_Datatype<T>::get(),root,communicator);
+      return MPI_Bcast(inout,len,MPITraits<T>::getType(),root,communicator);
     }
 
     //! @copydoc CollectiveCommunication::gather()
     template<typename T>
     int gather (T* in, T* out, int len, int root) const     // note out must have space for P*len elements
     {
-      return MPI_Gather(in,len,Generic_MPI_Datatype<T>::get(),
-                        out,len,Generic_MPI_Datatype<T>::get(),
+      return MPI_Gather(in,len,MPITraits<T>::getType(),
+                        out,len,MPITraits<T>::getType(),
                         root,communicator);
     }
 
@@ -325,7 +327,7 @@ namespace Dune
     template<typename BinaryFunction, typename Type>
     int allreduce(Type* in, Type* out, int len) const
     {
-      return MPI_Allreduce(in, out, len, Generic_MPI_Datatype<Type>::get(),
+      return MPI_Allreduce(in, out, len, MPITraits<Type>::getType(),
                            Generic_MPI_Op<Type, BinaryFunction>::get(),communicator);
     }
 
diff --git a/dune/common/mpitraits.hh b/dune/common/mpitraits.hh
index 062771c27..e10e34e5d 100644
--- a/dune/common/mpitraits.hh
+++ b/dune/common/mpitraits.hh
@@ -19,6 +19,8 @@ namespace Dune
    * @brief Traits classes for mapping types onto MPI_Datatype.
    * @author Markus Blatt
    */
+
+#if HAVE_MPI
   /**
    * @brief A traits class describing the mapping of types onto MPI_Datatypes.
    *
@@ -30,7 +32,25 @@ namespace Dune
    */
   template<typename T>
   struct MPITraits
-  {};
+  {
+  private:
+    MPITraits(){}
+    MPITraits(const MPITraits&){}
+    static MPI_Datatype datatype;
+    static MPI_Datatype vectortype;
+  public:
+    static inline MPI_Datatype getType()
+    {
+      if(datatype==MPI_DATATYPE_NULL) {
+        MPI_Type_contiguous(sizeof(T),MPI_BYTE,&datatype);
+        MPI_Type_commit(&datatype);
+      }
+      return datatype;
+    }
+
+  };
+  template<class T>
+  MPI_Datatype MPITraits<T>::datatype = MPI_DATATYPE_NULL;
 
 #ifndef DOXYGEN
 #if HAVE_MPI
@@ -169,7 +189,7 @@ namespace Dune
   MPI_Datatype MPITraits<std::pair<T1,T2> >::type=MPI_DATATYPE_NULL;
 #endif
 #endif
-
+#endif
   /** @} */
 }
 
-- 
GitLab