From 03b271912c1435615c80b1a8d29b5a32a47cebab Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@dune-project.org>
Date: Fri, 22 Oct 2004 08:58:44 +0000
Subject: [PATCH] New constructor with control over the UG environment heap
 size

[[Imported from SVN: r962]]
---
 grid/uggrid.hh        | 22 +++++++++++++++++++---
 grid/uggrid/uggrid.cc | 38 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/grid/uggrid.hh b/grid/uggrid.hh
index 84736a513..3a61e3448 100644
--- a/grid/uggrid.hh
+++ b/grid/uggrid.hh
@@ -26,7 +26,7 @@
 
 namespace Dune
 {
-  /** @defgroup UGGrid UGGridImp
+  /** @defgroup UGGrid UGGrid
       \ingroup GridCommon
 
      This is the implementation of the grid interface
@@ -157,11 +157,19 @@ namespace Dune {
 
     /** \brief Constructor with control over UG's memory requirements
      *
-     * \param heap The size of UG's internal memory in megabytes.  UG allocates
+     * \param heapSize The size of UG's internal memory in megabytes.  UG allocates
      * memory only once.  I don't know what happens if you create UGGrids with
      * differing heap sizes.
+     * \param envHeapSize The size of UG's environment heap.
      */
-    UGGrid(unsigned int heap=500);
+    UGGrid(unsigned int heapSize, unsigned int envHeapSize);
+
+    /** \brief Constructor with default memory settings
+     *
+     * The default values are 500MB for the general heap and 10MB for
+     * the environment heap.
+     */
+    UGGrid();
 
     //! Desctructor
     ~UGGrid();
@@ -215,6 +223,9 @@ namespace Dune {
     void* extra_boundary_data_;
 
   private:
+
+    void init(unsigned int heapSize, unsigned int envHeapSize);
+
     // Each UGGrid object has a unique name to identify it in the
     // UG environment structure
     std::string name_;
@@ -228,6 +239,11 @@ namespace Dune {
     // number of entitys of each level an codim
     Array<int> size_;
 
+    //! Marks whether the UG environment heap size is taken from
+    //! an existing defaults file or whether the values from
+    //! the UGGrid constructor are taken
+    bool useExistingDefaultsFile;
+
   protected:
     /** \brief Number of UGGrids currently in use.
      *
diff --git a/grid/uggrid/uggrid.cc b/grid/uggrid/uggrid.cc
index 9e1da7e96..96815154f 100644
--- a/grid/uggrid/uggrid.cc
+++ b/grid/uggrid/uggrid.cc
@@ -30,10 +30,41 @@ namespace Dune
   template<> int UGGrid < 3, 3 >::numOfUGGrids = 0;
 
   template < int dim, int dimworld >
-  inline UGGrid < dim, dimworld >::UGGrid(unsigned int heap) : heapsize(heap)
+  inline UGGrid < dim, dimworld >::UGGrid()
   {
+    init(500, 10);
+  }
+
+  template < int dim, int dimworld >
+  inline UGGrid < dim, dimworld >::UGGrid(unsigned int heapSize, unsigned envHeapSize)
+  {
+    init(heapSize, envHeapSize);
+  }
+
+  template < int dim, int dimworld >
+  inline void UGGrid < dim, dimworld >::init(unsigned int heapSize, unsigned envHeapSize)
+  {
+    heapsize = heapSize;
+
     if (numOfUGGrids==0) {
 
+      useExistingDefaultsFile = false;
+
+      if (access("defaults", F_OK) == 0) {
+
+        std::cout << "Using existing UG defaults file" << std::endl;
+        useExistingDefaultsFile = true;
+
+      } else {
+
+        // Pass the explicitly given environment heap size
+        // This is only possible by passing a pseudo 'defaults'-file
+        FILE* fp = fopen("defaults", "w");
+        fprintf(fp, "envmemory  %d000000\n", envHeapSize);
+        fclose(fp);
+
+      }
+
       // Init the UG system
       int argc = 1;
       char* arg = {"dune.exe"};
@@ -58,7 +89,6 @@ namespace Dune
                                                       1,coeffs,1,upp) == NULL)
         assert(false);
 
-
       // A Dummy new format
       // We need to pass the parameters in this complicated way, because
       // UG writes into one of the strings, and code compiled by some
@@ -116,6 +146,10 @@ namespace Dune
       for (int i=0; i<4; i++)
         free(newformatArgs[i]);
 
+      // remove defaults file, if we wrote one on startup
+      if (!useExistingDefaultsFile)
+        system("rm defaults");
+
     }
 
   };
-- 
GitLab