]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix uninitialized memory in ghost range of distributed vector
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 12 Apr 2017 11:23:49 +0000 (13:23 +0200)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 12 Apr 2017 11:26:54 +0000 (13:26 +0200)
include/deal.II/lac/la_parallel_vector.templates.h
tests/mpi/parallel_vector_21.cc [new file with mode: 0644]
tests/mpi/parallel_vector_21.mpirun=3.output [new file with mode: 0644]

index b886ca165822ec6a241ce2990d9f8fc13b211d56..fedc2724672304d2cae958095ee71ce1535ab1c2 100644 (file)
@@ -104,8 +104,8 @@ namespace LinearAlgebra
       // set entries to zero if so requested
       if (omit_zeroing_entries == false)
         this->operator = (Number());
-
-      vector_is_ghosted = false;
+      else
+        zero_out_ghosts();
     }
 
 
@@ -133,6 +133,8 @@ namespace LinearAlgebra
 
       if (omit_zeroing_entries == false)
         this->operator= (Number());
+      else
+        zero_out_ghosts();
 
       if (import_data != nullptr)
         {
@@ -145,8 +147,6 @@ namespace LinearAlgebra
           import_data = nullptr;
         }
 
-      vector_is_ghosted = false;
-
       thread_loop_partitioner = v.thread_loop_partitioner;
     }
 
@@ -240,8 +240,6 @@ namespace LinearAlgebra
       dealii::internal::VectorOperations::Vector_copy<Number,Number> copier(v.val, val);
       internal::VectorOperations::parallel_for(copier, 0, partitioner->local_size(),
                                                thread_loop_partitioner);
-
-      zero_out_ghosts();
     }
 
 
diff --git a/tests/mpi/parallel_vector_21.cc b/tests/mpi/parallel_vector_21.cc
new file mode 100644 (file)
index 0000000..ae8d638
--- /dev/null
@@ -0,0 +1,72 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+// Check that the data range representing ghosts is really initialized to zero
+// when doing reinit() from another vector and manually setting the local
+// range
+
+#include "../tests.h"
+#include <deal.II/base/utilities.h>
+#include <deal.II/base/index_set.h>
+#include <deal.II/lac/la_parallel_vector.h>
+#include <deal.II/lac/read_write_vector.h>
+#include <fstream>
+#include <iostream>
+#include <vector>
+
+void test()
+{
+  unsigned int my_id = Utilities::MPI::this_mpi_process (MPI_COMM_WORLD);
+  unsigned int n_procs = Utilities::MPI::n_mpi_processes (MPI_COMM_WORLD);
+
+  IndexSet locally_owned(n_procs*2);
+  locally_owned.add_range(my_id*2, my_id*2+2);
+  IndexSet ghost_set(n_procs*2);
+  ghost_set.add_index(0);
+  ghost_set.add_index(2);
+
+  LinearAlgebra::distributed::Vector<double> v(locally_owned, ghost_set, MPI_COMM_WORLD);
+
+  // create vector without actually setting the entries since they will be
+  // overwritten soon anyway
+  LinearAlgebra::distributed::Vector<double> v2;
+  v2.reinit(v, true);
+
+  // set locally owned range of v2 manually
+  for (unsigned int i=0; i<v2.local_size(); ++i)
+    v2.local_element(i) = 1.;
+
+  // add entries to ghost values
+  v2(0) += 1.;
+  v2(2) += 1.;
+  v2.compress(VectorOperation::add);
+
+  // now we should have the correct data, not some uninitialized trash that
+  // resided in the ghost range
+  v2.print(deallog.get_file_stream());
+
+  v2.update_ghost_values();
+  v2.print(deallog.get_file_stream());
+}
+
+
+
+int main (int argc, char **argv)
+{
+  Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads());
+
+  MPILogInitAll log;
+  test();
+}
diff --git a/tests/mpi/parallel_vector_21.mpirun=3.output b/tests/mpi/parallel_vector_21.mpirun=3.output
new file mode 100644 (file)
index 0000000..3b5b3b8
--- /dev/null
@@ -0,0 +1,35 @@
+
+Process #0
+Local range: [0, 2), global size: 6
+Vector data:
+4.000e+00 1.000e+00 
+Process #0
+Local range: [0, 2), global size: 6
+Vector data:
+4.000e+00 1.000e+00 
+Ghost entries (global index / value):
+(2/4.000e+00) 
+
+Process #1
+Local range: [2, 4), global size: 6
+Vector data:
+4.000e+00 1.000e+00 
+Process #1
+Local range: [2, 4), global size: 6
+Vector data:
+4.000e+00 1.000e+00 
+Ghost entries (global index / value):
+(0/4.000e+00) 
+
+
+Process #2
+Local range: [4, 6), global size: 6
+Vector data:
+1.000e+00 1.000e+00 
+Process #2
+Local range: [4, 6), global size: 6
+Vector data:
+1.000e+00 1.000e+00 
+Ghost entries (global index / value):
+(0/4.000e+00) (2/4.000e+00) 
+

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.