]> https://gitweb.dealii.org/ - dealii.git/commitdiff
free allocated MPI resources before we call MPI_Finalize
authorMatthias Maier <tamiko@43-1.org>
Mon, 19 Apr 2021 20:43:20 +0000 (15:43 -0500)
committerMatthias Maier <tamiko@43-1.org>
Mon, 19 Apr 2021 22:22:49 +0000 (17:22 -0500)
source/base/mpi.cc

index 2a71e5c98857eff60894b394dfc0eb1655a481e4..f72b60715c2ff96707e49e40eae95eab0356b4c0 100644 (file)
@@ -623,19 +623,10 @@ namespace Utilities
         }
 
       /*
-       * Create a custom MPI datatype handle describing the memory layout
-       * of the MinMaxAvg struct and a custom MPI op handle for our
-       * max_reduce function.
-       *
-       * This setup will leak some minor allocated memory (in the range of
-       * a few bytes) at the end of the program run because we never call
-       * MPI_Type_free(&type) and MPI_Op_free(&op) to explicitly free up
-       * memory. This is a cosmetic issue but some address sanitizers will
-       * complain about this (such as clang's -fsanitize=address).
+       * A custom MPI datatype handle describing the memory layout of the
+       * MinMaxAvg struct. Initialized on first pass control reaches the
+       * static variable. So hopefully not initialized too early.
        */
-
-      /* Initialized on first pass control reaches the static variable. So
-       * hopefully not initialized too early. */
       static MPI_Datatype type = []() {
         MPI_Datatype type;
 
@@ -654,11 +645,21 @@ namespace Utilities
         ierr = MPI_Type_commit(&type);
         AssertThrowMPI(ierr);
 
+        /* Ensure that we free the allocated datatype again at the end of
+         * the program run just before we call MPI_Finalize():*/
+        MPI_InitFinalize::signals.at_mpi_finalize.connect([type]() mutable {
+          int ierr = MPI_Type_free(&type);
+          AssertThrowMPI(ierr);
+        });
+
         return type;
       }();
 
-      /* Initialized on first pass control reaches the static variable. So
-       * hopefully not initialized too early. */
+      /*
+       * A custom MPI op handle for our max_reduce function.
+       * Initialized on first pass control reaches the static variable. So
+       * hopefully not initialized too early.
+       */
       static MPI_Op op = []() {
         MPI_Op op;
 
@@ -668,6 +669,13 @@ namespace Utilities
                         &op);
         AssertThrowMPI(ierr);
 
+        /* Ensure that we free the allocated op again at the end of the
+         * program run just before we call MPI_Finalize():*/
+        MPI_InitFinalize::signals.at_mpi_finalize.connect([op]() mutable {
+          int ierr = MPI_Op_free(&op);
+          AssertThrowMPI(ierr);
+        });
+
         return op;
       }();
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.