From eb46ca37b69fd44f5d2d3b60d7a00a898b794ab8 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 3 Apr 2018 21:44:06 -0600 Subject: [PATCH] Convert ReadWriteVector to std::unique_ptr. --- include/deal.II/lac/la_vector.templates.h | 44 +++++++-------- include/deal.II/lac/read_write_vector.h | 53 ++++++------------- .../deal.II/lac/read_write_vector.templates.h | 34 ++++++------ 3 files changed, 54 insertions(+), 77 deletions(-) diff --git a/include/deal.II/lac/la_vector.templates.h b/include/deal.II/lac/la_vector.templates.h index 764be32d5b..f49f85ddb6 100644 --- a/include/deal.II/lac/la_vector.templates.h +++ b/include/deal.II/lac/la_vector.templates.h @@ -81,7 +81,7 @@ namespace LinearAlgebra if (this->size() != in_vector.size()) this->reinit(in_vector, true); - dealii::internal::VectorOperations::Vector_copy copier(in_vector.val, this->val); + dealii::internal::VectorOperations::Vector_copy 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::reinit(in_vector, true); - dealii::internal::VectorOperations::Vector_copy copier(in_vector.val, this->val); + dealii::internal::VectorOperations::Vector_copy 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(0), ExcMessage("Only 0 can be assigned to a vector.")); (void) s; - internal::VectorOperations::Vector_set setter(Number(), this->val); + internal::VectorOperations::Vector_set 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 vector_multiply(this->val, factor); + internal::VectorOperations::Vectorization_multiply_factor 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 vector_add(this->val, down_V.val); + internal::VectorOperations::Vectorization_add_v 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 &down_V = dynamic_cast&>(V); Assert(down_V.size()==this->size(), ExcMessage("Cannot subtract two vectors with different numbers of elements")); - internal::VectorOperations::Vectorization_subtract_v vector_subtract(this->val, down_V.val); + internal::VectorOperations::Vectorization_subtract_v 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 dot(this->val, down_V.val); + internal::VectorOperations::Dot 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 vector_add(this->val, a); + internal::VectorOperations::Vectorization_add_factor 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 vector_add_av(this->val, down_V.val, a); + internal::VectorOperations::Vectorization_add_av 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 vector_add(this->val, down_V.val, - down_W.val, a, b); + internal::VectorOperations::Vectorization_add_avpbw 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 &down_V = dynamic_cast&>(V); - internal::VectorOperations::Vectorization_sadd_xav vector_sadd_xav(this->val, down_V.val, + internal::VectorOperations::Vectorization_sadd_xav 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 vector_scale(this->val, down_scaling_factors.val); + internal::VectorOperations::Vectorization_scale 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 &down_V = dynamic_cast&>(V); - internal::VectorOperations::Vectorization_equ_au vector_equ(this->val, down_V.val, a); + internal::VectorOperations::Vectorization_equ_au 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; ival[i] != Number()) + if (this->values[i] != Number()) return false; return true; @@ -350,7 +350,7 @@ namespace LinearAlgebra typedef typename VectorSpaceVector::real_type real_type; value_type sum; - internal::VectorOperations::MeanValue mean_value(this->val); + internal::VectorOperations::MeanValue mean_value(this->values.get()); internal::VectorOperations::parallel_reduce(mean_value, 0, this->size(), sum, this->thread_loop_partitioner); return sum/static_cast(this->size()); @@ -365,7 +365,7 @@ namespace LinearAlgebra typedef typename VectorSpaceVector::real_type real_type; real_type sum; - internal::VectorOperations::Norm1 norm1(this->val); + internal::VectorOperations::Norm1 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::real_type real_type; real_type norm_square; - internal::VectorOperations::Norm2 norm2(this->val); + internal::VectorOperations::Norm2 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; ival[i] != Number()) + if (this->values[i] != Number()) { const real_type abs_x = - numbers::NumberTraits::abs(this->val[i]); + numbers::NumberTraits::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::real_type norm = 0.; const size_type size = this->size(); for (size_type i=0; ival[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 adder(this->val, down_V.val, down_W.val, a); + internal::VectorOperations::AddAndDot 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); diff --git a/include/deal.II/lac/read_write_vector.h b/include/deal.II/lac/read_write_vector.h index 5916ca070e..b5b5359f0b 100644 --- a/include/deal.II/lac/read_write_vector.h +++ b/include/deal.II/lac/read_write_vector.h @@ -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 values; /** * For parallel loops with TBB, this member variable stores the affinity @@ -668,8 +668,6 @@ namespace LinearAlgebra template inline ReadWriteVector::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::ReadWriteVector (const ReadWriteVector &v) : - Subscriptor(), - val(nullptr) + Subscriptor() { this->operator=(v); } @@ -694,8 +691,6 @@ namespace LinearAlgebra template inline ReadWriteVector::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 inline ReadWriteVector::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 - inline - ReadWriteVector::~ReadWriteVector () - { - try - { - resize_val(0); - } - catch (...) - {} - } - - - template inline typename ReadWriteVector::size_type @@ -768,7 +747,7 @@ namespace LinearAlgebra typename ReadWriteVector::iterator ReadWriteVector::begin () { - return val; + return values.get(); } @@ -778,7 +757,7 @@ namespace LinearAlgebra typename ReadWriteVector::const_iterator ReadWriteVector::begin () const { - return val; + return values.get(); } @@ -788,7 +767,7 @@ namespace LinearAlgebra typename ReadWriteVector::iterator ReadWriteVector::end () { - return val + this->n_elements(); + return values.get() + this->n_elements(); } @@ -798,7 +777,7 @@ namespace LinearAlgebra typename ReadWriteVector::const_iterator ReadWriteVector::end () const { - return val + this->n_elements(); + return values.get() + this->n_elements(); } @@ -808,7 +787,7 @@ namespace LinearAlgebra Number ReadWriteVector::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::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 inline void ReadWriteVector::extract_subvector_to (const std::vector &indices, - std::vector &values) const + std::vector &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::add (const size_type n_indices, const size_type *indices, - const Number2 *values) + const Number2 *values_to_add) { for (size_type i=0; ioperator()(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(); } 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(); } @@ -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 copier(in_vector.val, val); + dealii::internal::VectorOperations::Vector_copy 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 copier(in_vector.val, val); + dealii::internal::VectorOperations::Vector_copy 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 setter(Number(), val); + dealii::internal::VectorOperations::Vector_set 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::swap (ReadWriteVector &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()); -- 2.39.5