]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Convert ReadWriteVector to std::unique_ptr.
authorWolfgang Bangerth <bangerth@colostate.edu>
Wed, 4 Apr 2018 03:44:06 +0000 (21:44 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 4 Apr 2018 03:44:06 +0000 (21:44 -0600)
include/deal.II/lac/la_vector.templates.h
include/deal.II/lac/read_write_vector.h
include/deal.II/lac/read_write_vector.templates.h

index 764be32d5b97f8437c3980944d79bc4a36c128e4..f49f85ddb6f8948e60206e1ceccedba756df8780 100644 (file)
@@ -81,7 +81,7 @@ namespace LinearAlgebra
     if (this->size() != in_vector.size())
       this->reinit(in_vector, true);
 
-    dealii::internal::VectorOperations::Vector_copy<Number, Number> copier(in_vector.val, this->val);
+    dealii::internal::VectorOperations::Vector_copy<Number, Number> copier(in_vector.values.get(), this->values.get());
     internal::VectorOperations::parallel_for(copier, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -97,7 +97,7 @@ namespace LinearAlgebra
     if (this->size() != in_vector.size())
       ReadWriteVector<Number>::reinit(in_vector, true);
 
-    dealii::internal::VectorOperations::Vector_copy<Number, Number2> copier(in_vector.val, this->val);
+    dealii::internal::VectorOperations::Vector_copy<Number, Number2> copier(in_vector.values.get(), this->values.get());
     internal::VectorOperations::parallel_for(copier, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -111,7 +111,7 @@ namespace LinearAlgebra
     Assert(s==static_cast<Number>(0), ExcMessage("Only 0 can be assigned to a vector."));
     (void) s;
 
-    internal::VectorOperations::Vector_set<Number> setter(Number(), this->val);
+    internal::VectorOperations::Vector_set<Number> setter(Number(), this->values.get());
     internal::VectorOperations::parallel_for(setter, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -124,7 +124,7 @@ namespace LinearAlgebra
   {
     AssertIsFinite(factor);
 
-    internal::VectorOperations::Vectorization_multiply_factor<Number> vector_multiply(this->val, factor);
+    internal::VectorOperations::Vectorization_multiply_factor<Number> vector_multiply(this->values.get(), factor);
     internal::VectorOperations::parallel_for(vector_multiply, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -155,7 +155,7 @@ namespace LinearAlgebra
     Assert(down_V.size()==this->size(),
            ExcMessage("Cannot add two vectors with different numbers of elements"));
 
-    internal::VectorOperations::Vectorization_add_v<Number> vector_add(this->val, down_V.val);
+    internal::VectorOperations::Vectorization_add_v<Number> vector_add(this->values.get(), down_V.values.get());
     internal::VectorOperations::parallel_for(vector_add, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -173,7 +173,7 @@ namespace LinearAlgebra
     const Vector<Number> &down_V = dynamic_cast<const Vector<Number>&>(V);
     Assert(down_V.size()==this->size(),
            ExcMessage("Cannot subtract two vectors with different numbers of elements"));
-    internal::VectorOperations::Vectorization_subtract_v<Number> vector_subtract(this->val, down_V.val);
+    internal::VectorOperations::Vectorization_subtract_v<Number> vector_subtract(this->values.get(), down_V.values.get());
     internal::VectorOperations::parallel_for(vector_subtract, 0, this->size(), this->thread_loop_partitioner);
 
     return *this;
@@ -194,7 +194,7 @@ namespace LinearAlgebra
            ExcMessage("Cannot compute the scalar product "
                       "of two vectors with different numbers of elements"));
     Number sum;
-    internal::VectorOperations::Dot<Number, Number> dot(this->val, down_V.val);
+    internal::VectorOperations::Dot<Number, Number> dot(this->values.get(), down_V.values.get());
     internal::VectorOperations::parallel_reduce(dot, 0, this->size(), sum, this->thread_loop_partitioner);
 
     return sum;
@@ -218,7 +218,7 @@ namespace LinearAlgebra
   {
     AssertIsFinite(a);
 
-    internal::VectorOperations::Vectorization_add_factor<Number> vector_add(this->val, a);
+    internal::VectorOperations::Vectorization_add_factor<Number> vector_add(this->values.get(), a);
     internal::VectorOperations::parallel_for(vector_add, 0, this->size(), this->thread_loop_partitioner);
   }
 
@@ -237,7 +237,7 @@ namespace LinearAlgebra
     Assert(down_V.size()==this->size(),
            ExcMessage("Cannot add two vectors with different numbers of elements"));
 
-    internal::VectorOperations::Vectorization_add_av<Number> vector_add_av(this->val, down_V.val, a);
+    internal::VectorOperations::Vectorization_add_av<Number> vector_add_av(this->values.get(), down_V.values.get(), a);
     internal::VectorOperations::parallel_for(vector_add_av, 0, this->size(), this->thread_loop_partitioner);
   }
 
@@ -265,8 +265,8 @@ namespace LinearAlgebra
     Assert(down_W.size()==this->size(),
            ExcMessage("Cannot add two vectors with different numbers of elements"));
 
-    internal::VectorOperations::Vectorization_add_avpbw<Number> vector_add(this->val, down_V.val,
-        down_W.val, a, b);
+    internal::VectorOperations::Vectorization_add_avpbw<Number> vector_add(this->values.get(), down_V.values.get(),
+        down_W.values.get(), a, b);
     internal::VectorOperations::parallel_for(vector_add, 0, this->size(), this->thread_loop_partitioner);
   }
 
@@ -285,7 +285,7 @@ namespace LinearAlgebra
 
     // Downcast V. It fails, throws an exception.
     const Vector<Number> &down_V = dynamic_cast<const Vector<Number>&>(V);
-    internal::VectorOperations::Vectorization_sadd_xav<Number> vector_sadd_xav(this->val, down_V.val,
+    internal::VectorOperations::Vectorization_sadd_xav<Number> vector_sadd_xav(this->values.get(), down_V.values.get(),
         a, s);
     internal::VectorOperations::parallel_for(vector_sadd_xav, 0, this->size(), this->thread_loop_partitioner);
   }
@@ -305,7 +305,7 @@ namespace LinearAlgebra
     Assert(down_scaling_factors.size()==this->size(),
            ExcMessage("Cannot add two vectors with different numbers of elements"));
 
-    internal::VectorOperations::Vectorization_scale<Number> vector_scale(this->val, down_scaling_factors.val);
+    internal::VectorOperations::Vectorization_scale<Number> vector_scale(this->values.get(), down_scaling_factors.values.get());
     internal::VectorOperations::parallel_for(vector_scale, 0, this->size(), this->thread_loop_partitioner);
   }
 
@@ -322,7 +322,7 @@ namespace LinearAlgebra
 
     // Downcast V. If fails, throws an exception.
     const Vector<Number> &down_V = dynamic_cast<const Vector<Number>&>(V);
-    internal::VectorOperations::Vectorization_equ_au<Number> vector_equ(this->val, down_V.val, a);
+    internal::VectorOperations::Vectorization_equ_au<Number> vector_equ(this->values.get(), down_V.values.get(), a);
     internal::VectorOperations::parallel_for(vector_equ, 0, this->size(), this->thread_loop_partitioner);
   }
 
@@ -335,7 +335,7 @@ namespace LinearAlgebra
 
     const size_type size = this->size();
     for (size_type i=0; i<size; ++i)
-      if (this->val[i] != Number())
+      if (this->values[i] != Number())
         return false;
 
     return true;
@@ -350,7 +350,7 @@ namespace LinearAlgebra
 
     typedef typename VectorSpaceVector<Number>::real_type real_type;
     value_type sum;
-    internal::VectorOperations::MeanValue<Number> mean_value(this->val);
+    internal::VectorOperations::MeanValue<Number> mean_value(this->values.get());
     internal::VectorOperations::parallel_reduce(mean_value, 0, this->size(), sum, this->thread_loop_partitioner);
 
     return sum/static_cast<real_type>(this->size());
@@ -365,7 +365,7 @@ namespace LinearAlgebra
 
     typedef typename VectorSpaceVector<Number>::real_type real_type;
     real_type sum;
-    internal::VectorOperations::Norm1<Number, real_type> norm1(this->val);
+    internal::VectorOperations::Norm1<Number, real_type> norm1(this->values.get());
     internal::VectorOperations::parallel_reduce(norm1, 0, this->size(), sum, this->thread_loop_partitioner);
 
     return sum;
@@ -386,7 +386,7 @@ namespace LinearAlgebra
     // precision) using the BLAS approach with a weight, see e.g. dnrm2.f.
     typedef typename VectorSpaceVector<Number>::real_type real_type;
     real_type norm_square;
-    internal::VectorOperations::Norm2<Number, real_type> norm2(this->val);
+    internal::VectorOperations::Norm2<Number, real_type> norm2(this->values.get());
     internal::VectorOperations::parallel_reduce(norm2, 0, this->size(), norm_square,
                                                 this->thread_loop_partitioner);
     if (numbers::is_finite(norm_square) &&
@@ -399,10 +399,10 @@ namespace LinearAlgebra
         const size_type size = this->size();
         for (size_type i=0; i<size; ++i)
           {
-            if (this->val[i] != Number())
+            if (this->values[i] != Number())
               {
                 const real_type abs_x =
-                  numbers::NumberTraits<Number>::abs(this->val[i]);
+                  numbers::NumberTraits<Number>::abs(this->values[i]);
                 if (scale < abs_x)
                   {
                     sum = 1. + sum * (scale/abs_x) * (scale/abs_x);
@@ -425,7 +425,7 @@ namespace LinearAlgebra
     typename ReadWriteVector<Number>::real_type norm = 0.;
     const size_type size = this->size();
     for (size_type i=0; i<size; ++i)
-      norm = std::max(std::abs(this->val[i]),norm);
+      norm = std::max(std::abs(this->values[i]),norm);
 
     return norm;
   }
@@ -455,7 +455,7 @@ namespace LinearAlgebra
            ExcMessage("Cannot add two vectors with different numbers of elements"));
 
     Number sum;
-    internal::VectorOperations::AddAndDot<Number> adder(this->val, down_V.val, down_W.val, a);
+    internal::VectorOperations::AddAndDot<Number> adder(this->values.get(), down_V.values.get(), down_W.values.get(), a);
     internal::VectorOperations::parallel_reduce(adder, 0, this->size(), sum, this->thread_loop_partitioner);
     AssertIsFinite(sum);
 
index 5916ca070e95d7ad7959bdd077fa0d152fabae5f..b5b5359f0b8d1db75658d30534397b1c505dd970 100644 (file)
@@ -167,7 +167,7 @@ namespace LinearAlgebra
     /**
      * Destructor.
      */
-    ~ReadWriteVector ();
+    ~ReadWriteVector () = default;
 
     /**
      * Set the global size of the vector to @p size. The stored elements have
@@ -610,7 +610,7 @@ namespace LinearAlgebra
     /**
      * Pointer to the array of local elements of this vector.
      */
-    Number *val;
+    std::unique_ptr<Number[]> values;
 
     /**
      * For parallel loops with TBB, this member variable stores the affinity
@@ -668,8 +668,6 @@ namespace LinearAlgebra
   template <typename Number>
   inline
   ReadWriteVector<Number>::ReadWriteVector ()
-    :
-    val(nullptr)
   {
     // virtual functions called in constructors and destructors never use the
     // override in a derived class
@@ -683,8 +681,7 @@ namespace LinearAlgebra
   inline
   ReadWriteVector<Number>::ReadWriteVector (const ReadWriteVector<Number> &v)
     :
-    Subscriptor(),
-    val(nullptr)
+    Subscriptor()
   {
     this->operator=(v);
   }
@@ -694,8 +691,6 @@ namespace LinearAlgebra
   template <typename Number>
   inline
   ReadWriteVector<Number>::ReadWriteVector (const size_type size)
-    :
-    val(nullptr)
   {
     // virtual functions called in constructors and destructors never use the
     // override in a derived class
@@ -708,8 +703,6 @@ namespace LinearAlgebra
   template <typename Number>
   inline
   ReadWriteVector<Number>::ReadWriteVector (const IndexSet &locally_stored_indices)
-    :
-    val(nullptr)
   {
     // virtual functions called in constructors and destructors never use the
     // override in a derived class
@@ -719,20 +712,6 @@ namespace LinearAlgebra
 
 
 
-  template <typename Number>
-  inline
-  ReadWriteVector<Number>::~ReadWriteVector ()
-  {
-    try
-      {
-        resize_val(0);
-      }
-    catch (...)
-      {}
-  }
-
-
-
   template <typename Number>
   inline
   typename ReadWriteVector<Number>::size_type
@@ -768,7 +747,7 @@ namespace LinearAlgebra
   typename ReadWriteVector<Number>::iterator
   ReadWriteVector<Number>::begin ()
   {
-    return val;
+    return values.get();
   }
 
 
@@ -778,7 +757,7 @@ namespace LinearAlgebra
   typename ReadWriteVector<Number>::const_iterator
   ReadWriteVector<Number>::begin () const
   {
-    return val;
+    return values.get();
   }
 
 
@@ -788,7 +767,7 @@ namespace LinearAlgebra
   typename ReadWriteVector<Number>::iterator
   ReadWriteVector<Number>::end ()
   {
-    return val + this->n_elements();
+    return values.get() + this->n_elements();
   }
 
 
@@ -798,7 +777,7 @@ namespace LinearAlgebra
   typename ReadWriteVector<Number>::const_iterator
   ReadWriteVector<Number>::end () const
   {
-    return val + this->n_elements();
+    return values.get() + this->n_elements();
   }
 
 
@@ -808,7 +787,7 @@ namespace LinearAlgebra
   Number
   ReadWriteVector<Number>::operator() (const size_type global_index) const
   {
-    return val[global_to_local(global_index)];
+    return values[global_to_local(global_index)];
   }
 
 
@@ -818,7 +797,7 @@ namespace LinearAlgebra
   Number &
   ReadWriteVector<Number>::operator() (const size_type global_index)
   {
-    return val[global_to_local (global_index)];
+    return values[global_to_local (global_index)];
   }
 
 
@@ -847,10 +826,10 @@ namespace LinearAlgebra
   template <typename Number2>
   inline
   void ReadWriteVector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
-                                                      std::vector<Number2> &values) const
+                                                      std::vector<Number2> &extracted_values) const
   {
     for (size_type i = 0; i < indices.size(); ++i)
-      values[i] = operator()(indices[i]);
+      extracted_values[i] = operator()(indices[i]);
   }
 
 
@@ -879,7 +858,7 @@ namespace LinearAlgebra
   {
     AssertIndexRange (local_index, this->n_elements());
 
-    return val[local_index];
+    return values[local_index];
   }
 
 
@@ -891,7 +870,7 @@ namespace LinearAlgebra
   {
     AssertIndexRange (local_index, this->n_elements());
 
-    return val[local_index];
+    return values[local_index];
   }
 
 
@@ -933,13 +912,13 @@ namespace LinearAlgebra
   void
   ReadWriteVector<Number>::add (const size_type    n_indices,
                                 const size_type   *indices,
-                                const Number2     *values)
+                                const Number2     *values_to_add)
   {
     for (size_type i=0; i<n_indices; ++i)
       {
         Assert (numbers::is_finite(values[i]),
                 ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
-        this->operator()(indices[i]) += values[i];
+        this->operator()(indices[i]) += values_to_add[i];
       }
   }
 
@@ -965,7 +944,7 @@ namespace LinearAlgebra
       const size_type end)
   {
     for (size_type i=begin; i<end; ++i)
-      functor(parent.val[i]);
+      functor(parent.values[i]);
   }
 
 #endif  // ifndef DOXYGEN
index 19744e0010615240a43d1983ae2d491344d7b489..3e9a1fd711cb0dfa411ba36b9fcaa0725d2ed6ef 100644 (file)
@@ -54,17 +54,15 @@ namespace LinearAlgebra
   {
     if (new_alloc_size == 0)
       {
-        if (val != nullptr)
-          free(val);
-        val = nullptr;
+        values.reset ();
         thread_loop_partitioner = std::make_shared<parallel::internal::TBBPartitioner>();
       }
     else
       {
-        if (val != nullptr)
-          free(val);
+        Number *new_values;
+        Utilities::System::posix_memalign ((void **)&new_values, 64, sizeof(Number)*new_alloc_size);
+        values.reset (new_values);
 
-        Utilities::System::posix_memalign ((void **)&val, 64, sizeof(Number)*new_alloc_size);
         if (new_alloc_size >= 4*dealii::internal::VectorImplementation::minimum_parallel_grain_size)
           thread_loop_partitioner = std::make_shared<parallel::internal::TBBPartitioner>();
       }
@@ -154,7 +152,7 @@ namespace LinearAlgebra
     int ierr = trilinos_vec.trilinos_vector().ExtractView (&start_ptr, &leading_dimension);
     AssertThrow (ierr == 0, ExcTrilinosError(ierr));
 
-    std::copy(start_ptr, start_ptr + leading_dimension, val);
+    std::copy(start_ptr, start_ptr + leading_dimension, values.get());
 
     // reset the communication pattern
     source_stored_elements.clear();
@@ -186,7 +184,7 @@ namespace LinearAlgebra
     if (n_elements() != in_vector.n_elements())
       reinit(in_vector, true);
 
-    dealii::internal::VectorOperations::Vector_copy<Number,Number> copier(in_vector.val, val);
+    dealii::internal::VectorOperations::Vector_copy<Number,Number> copier(in_vector.values.get(), values.get());
     dealii::internal::VectorOperations::parallel_for(copier, 0, n_elements(), thread_loop_partitioner);
 
     return *this;
@@ -203,7 +201,7 @@ namespace LinearAlgebra
     if (n_elements() != in_vector.n_elements())
       reinit(in_vector, true);
 
-    dealii::internal::VectorOperations::Vector_copy<Number,Number2> copier(in_vector.val, val);
+    dealii::internal::VectorOperations::Vector_copy<Number,Number2> copier(in_vector.values.get(), values.get());
     dealii::internal::VectorOperations::parallel_for(copier, 0, n_elements(), thread_loop_partitioner);
 
     return *this;
@@ -221,7 +219,7 @@ namespace LinearAlgebra
     const size_type this_size = n_elements();
     if (this_size>0)
       {
-        dealii::internal::VectorOperations::Vector_set<Number> setter(Number(), val);
+        dealii::internal::VectorOperations::Vector_set<Number> setter(Number(), values.get());
         dealii::internal::VectorOperations::parallel_for(setter, 0, this_size, thread_loop_partitioner);
       }
 
@@ -376,12 +374,12 @@ namespace LinearAlgebra
         AssertThrow(err == 0, ExcMessage("Epetra Import() failed with error code: "
                                          + Utilities::to_string(err)));
 
-        const double *values = target_vector.Values();
+        const double *new_values = target_vector.Values();
         const int size = target_vector.MyLength();
         Assert(size==0 || values != nullptr, ExcInternalError("Import failed."));
 
         for (int i=0; i<size; ++i)
-          val[i] = values[i];
+          values[i] = new_values[i];
       }
     else if (operation==VectorOperation::add)
       {
@@ -389,12 +387,12 @@ namespace LinearAlgebra
         AssertThrow(err == 0, ExcMessage("Epetra Import() failed with error code: "
                                          + Utilities::to_string(err)));
 
-        const double *values = target_vector.Values();
+        const double *new_values = target_vector.Values();
         const int size = target_vector.MyLength();
         Assert(size==0 || values != nullptr, ExcInternalError("Import failed."));
 
         for (int i=0; i<size; ++i)
-          val[i] += values[i];
+          values[i] += new_values[i];
       }
     else
       AssertThrow(false, ExcNotImplemented());
@@ -441,7 +439,7 @@ namespace LinearAlgebra
     const unsigned int n_elements = stored_elements.n_elements();
     if (operation == VectorOperation::insert)
       {
-        cudaError_t error_code = cudaMemcpy(val, cuda_vec.get_values(),
+        cudaError_t error_code = cudaMemcpy(values.get(), cuda_vec.get_values(),
                                             n_elements*sizeof(Number),
                                             cudaMemcpyDeviceToHost);
         AssertCuda(error_code);
@@ -457,7 +455,7 @@ namespace LinearAlgebra
 
         // Add the two vectors
         for (unsigned int i=0; i<n_elements; ++i)
-          val[i] += tmp[i];
+          values[i] += tmp[i];
       }
   }
 #endif
@@ -469,7 +467,7 @@ namespace LinearAlgebra
   ReadWriteVector<Number>::swap (ReadWriteVector<Number> &v)
   {
     std::swap(stored_elements, v.stored_elements);
-    std::swap(val, v.val);
+    std::swap(values, v.values);
   }
 
 
@@ -508,7 +506,7 @@ namespace LinearAlgebra
     out << std::endl;
     unsigned int i=0;
     for (const auto &idx : this->stored_elements)
-      out << "[" << idx << "]: " << val[i++] << '\n';
+      out << "[" << idx << "]: " << values[i++] << '\n';
     out << std::flush;
 
     AssertThrow (out, ExcIO());

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.