From 4cb2a0e76d288e4d5ac96c69dc46c7e6fbb62189 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 18 May 1998 08:40:46 +0000 Subject: [PATCH] Performance tweaks with assumptions on the size of passed vectors. git-svn-id: https://svn.dealii.org/trunk@305 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/deal.II/include/dofs/dof_accessor.h | 18 +++---- deal.II/deal.II/source/dofs/dof_accessor.cc | 27 ++++++---- deal.II/deal.II/source/dofs/dof_handler.cc | 57 +++++--------------- deal.II/deal.II/source/numerics/assembler.cc | 2 +- deal.II/deal.II/source/numerics/base.cc | 19 +++---- deal.II/deal.II/source/numerics/matrices.cc | 16 +++--- 6 files changed, 53 insertions(+), 86 deletions(-) diff --git a/deal.II/deal.II/include/dofs/dof_accessor.h b/deal.II/deal.II/include/dofs/dof_accessor.h index 09fa115881..1f9b25ff0d 100644 --- a/deal.II/deal.II/include/dofs/dof_accessor.h +++ b/deal.II/deal.II/include/dofs/dof_accessor.h @@ -87,6 +87,10 @@ class DoFAccessor { * Exception */ DeclException0 (ExcVectorNotEmpty); + /** + * Exception + */ + DeclException0 (ExcVectorDoesNotMatch); protected: /** @@ -202,9 +206,8 @@ class DoFLineAccessor : public DoFAccessor, public BaseClass { * on vertex 0, dofs on vertex 1, * dofs on line. * - * The dof indices are appended to the end - * of the vector. It is assumed that - * the vector be empty beforehand. + * It is assumed that the vector already + * has the right size beforehand. */ void get_dof_indices (vector &dof_indices) const; @@ -289,9 +292,8 @@ class DoFQuadAccessor : public DoFAccessor, public BaseClass { * dofs on line 0, dofs on line 1, etc, * dofs on quad 0, etc. * - * The dof indices are appended to the end - * of the vector. It is assumed that - * the vector be empty beforehand. + * It is assumed that the vector already + * has the right size beforehand. */ void get_dof_indices (vector &dof_indices) const; @@ -489,10 +491,6 @@ class DoFCellAccessor : public DoFSubstructAccessor { void get_dof_values (const dVector &values, vector &dof_values) const; - /** - * Exception - */ - DeclException0 (ExcVectorDoesNotMatch); /** * Exception */ diff --git a/deal.II/deal.II/source/dofs/dof_accessor.cc b/deal.II/deal.II/source/dofs/dof_accessor.cc index 18b5c5416f..07cbbee068 100644 --- a/deal.II/deal.II/source/dofs/dof_accessor.cc +++ b/deal.II/deal.II/source/dofs/dof_accessor.cc @@ -89,14 +89,16 @@ void DoFLineAccessor::get_dof_indices (vector &dof_indices) const { Assert (dof_handler != 0, ExcInvalidObject()); Assert (&dof_handler->get_selected_fe() != 0, ExcInvalidObject()); - Assert (dof_indices.size() == 0, ExcVectorNotEmpty()); - - dof_indices.reserve (dof_handler->get_selected_fe().total_dofs); + Assert (dof_indices.size() == (2*dof_handler->get_selected_fe().dofs_per_vertex + + dof_handler->get_selected_fe().dofs_per_line), + ExcVectorDoesNotMatch()); + + vector::iterator next = dof_indices.begin(); for (unsigned int vertex=0; vertex<2; ++vertex) for (unsigned int d=0; dget_selected_fe().dofs_per_vertex; ++d) - dof_indices.push_back (vertex_dof_index(vertex,d)); + *next++ = vertex_dof_index(vertex,d); for (unsigned int d=0; dget_selected_fe().dofs_per_line; ++d) - dof_indices.push_back (dof_index(d)); + *next++ = dof_index(d); }; @@ -200,17 +202,20 @@ void DoFQuadAccessor::get_dof_indices (vector &dof_indices) const { Assert (dof_handler != 0, ExcInvalidObject()); Assert (&dof_handler->get_selected_fe() != 0, ExcInvalidObject()); - Assert (dof_indices.size() == 0, ExcVectorNotEmpty()); - - dof_indices.reserve (dof_handler->get_selected_fe().total_dofs); + Assert (dof_indices.size() == (4*dof_handler->get_selected_fe().dofs_per_vertex + + 4*dof_handler->get_selected_fe().dofs_per_line + + dof_handler->get_selected_fe().dofs_per_quad), + ExcVectorDoesNotMatch()); + + vector::iterator next = dof_indices.begin(); for (unsigned int vertex=0; vertex<4; ++vertex) for (unsigned int d=0; dget_selected_fe().dofs_per_vertex; ++d) - dof_indices.push_back (vertex_dof_index(vertex,d)); + *next++ = vertex_dof_index(vertex,d); for (unsigned int line=0; line<4; ++line) for (unsigned int d=0; dget_selected_fe().dofs_per_line; ++d) - dof_indices.push_back (this->line(line)->dof_index(d)); + *next++ = this->line(line)->dof_index(d); for (unsigned int d=0; dget_selected_fe().dofs_per_quad; ++d) - dof_indices.push_back (dof_index(d)); + *next++ = dof_index(d); }; diff --git a/deal.II/deal.II/source/dofs/dof_handler.cc b/deal.II/deal.II/source/dofs/dof_handler.cc index c5cdcbc89c..68326925b7 100644 --- a/deal.II/deal.II/source/dofs/dof_handler.cc +++ b/deal.II/deal.II/source/dofs/dof_handler.cc @@ -1193,13 +1193,10 @@ void DoFHandler::transfer_cell (const typename DoFHandler::cell_iterat if (new_cell->active() && old_cell->active()) // both cells have no children { - vector old_dofs, new_dofs; + vector old_dofs(selected_fe->total_dofs); + vector new_dofs(selected_fe->total_dofs); old_cell->get_dof_indices (old_dofs); new_cell->get_dof_indices (new_dofs); - Assert (old_dofs.size() == selected_fe->total_dofs, - ExcInternalError ()); - Assert (new_dofs.size() == selected_fe->total_dofs, - ExcInternalError ()); // copy dofs one-by-one for (unsigned int j=0; j::transfer_cell (const typename DoFHandler::cell_iterat }; // numbers of old dofs - vector old_dof_indices; + vector old_dof_indices (selected_fe->total_dofs); old_cell->get_dof_indices (old_dof_indices); - Assert (old_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int c=0; c::children_per_cell; ++c) { // numbers of child dofs - vector child_dof_indices; + vector child_dof_indices(selected_fe->total_dofs); child[c]->get_dof_indices (child_dof_indices); - Assert (child_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int k=0; ktotal_dofs; ++k) for (unsigned int j=0; jtotal_dofs; ++j) if (selected_fe->prolongate(c)(k,j) != 0.) @@ -1250,21 +1241,15 @@ void DoFHandler::transfer_cell (const typename DoFHandler::cell_iterat }; // numbers of new dofs - vector new_dof_indices; + vector new_dof_indices(selected_fe->total_dofs); new_cell->get_dof_indices(new_dof_indices); - - Assert (new_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); for (unsigned int c=0; c::children_per_cell; ++c) { // numbers of child dofs - vector child_dof_indices; + vector child_dof_indices (selected_fe->total_dofs); child[c]->get_dof_indices (child_dof_indices); - Assert (child_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int k=0; ktotal_dofs; ++k) for (unsigned int j=0; jtotal_dofs; ++j) if (selected_fe->restrict(c)(k,j) != 0.) @@ -1289,13 +1274,10 @@ void DoFHandler::transfer_cell (const typename DoFHandler::cell_iterat if (new_cell->active() && old_cell->active()) // both cells have no children { - vector old_dofs, new_dofs; + vector old_dofs (selected_fe->total_dofs); + vector new_dofs (selected_fe->total_dofs); old_cell->get_dof_indices (old_dofs); new_cell->get_dof_indices (new_dofs); - Assert (old_dofs.size() == selected_fe->total_dofs, - ExcInternalError ()); - Assert (new_dofs.size() == selected_fe->total_dofs, - ExcInternalError ()); // copy dofs one-by-one for (unsigned int j=0; j::transfer_cell (const typename DoFHandler::cell_iterat }; // numbers of old dofs - vector old_dof_indices; + vector old_dof_indices (selected_fe->total_dofs); old_cell->get_dof_indices (old_dof_indices); - Assert (old_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int c=0; c::children_per_cell; ++c) { // numbers of child dofs - vector child_dof_indices; + vector child_dof_indices (selected_fe->total_dofs); child[c]->get_dof_indices (child_dof_indices); - Assert (child_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int k=0; ktotal_dofs; ++k) for (unsigned int j=0; jtotal_dofs; ++j) if (selected_fe->prolongate(c)(k,j) != 0.) @@ -1347,21 +1323,15 @@ void DoFHandler::transfer_cell (const typename DoFHandler::cell_iterat }; // numbers of new dofs - vector new_dof_indices; + vector new_dof_indices (selected_fe->total_dofs); new_cell->get_dof_indices(new_dof_indices); - - Assert (new_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); for (unsigned int c=0; c::children_per_cell; ++c) { // numbers of child dofs - vector child_dof_indices; + vector child_dof_indices (selected_fe->total_dofs); child[c]->get_dof_indices (child_dof_indices); - Assert (child_dof_indices.size() == selected_fe->total_dofs, - ExcInternalError ()); - for (unsigned int k=0; ktotal_dofs; ++k) for (unsigned int j=0; jtotal_dofs; ++j) if (selected_fe->restrict(c)(k,j) != 0.) @@ -1482,12 +1452,11 @@ void DoFHandler::distribute_cell_to_dof_vector (const dVector &cell_data, active_cell_iterator cell = begin_active(), endc = end(); unsigned int present_cell = 0; - vector dof_indices; const unsigned int total_dofs = selected_fe->total_dofs; + vector dof_indices (total_dofs); for (; cell!=endc; ++cell, ++present_cell) { - dof_indices.resize (0); cell->get_dof_indices (dof_indices); for (unsigned int i=0; i::assemble (const Equation &equation) { // get indices of dofs - vector dofs; + vector dofs (n_dofs); get_dof_indices (dofs); // distribute cell matrix diff --git a/deal.II/deal.II/source/numerics/base.cc b/deal.II/deal.II/source/numerics/base.cc index 6f28164669..1a0618c8e8 100644 --- a/deal.II/deal.II/source/numerics/base.cc +++ b/deal.II/deal.II/source/numerics/base.cc @@ -59,10 +59,10 @@ template void ProblemBase::clear () { tria = 0; dof_handler = 0; - system_sparsity.reinit (0,0,0); + system_sparsity.reinit (1,1,1); system_matrix.clear (); - right_hand_side.reinit (0); - solution.reinit (0); + right_hand_side.reinit (1); + solution.reinit (1); constraints.clear (); }; @@ -212,7 +212,7 @@ void ProblemBase::integrate_difference (const Function &exact_sol // and assign that to the vector // \psi. const unsigned int n_q_points = q.n_quadrature_points; - vector psi; + vector psi (n_q_points); // in praxi: first compute // exact solution vector @@ -300,7 +300,7 @@ void ProblemBase::integrate_difference (const Function &exact_sol // same procedure as above, but now // psi is a vector of gradients const unsigned int n_q_points = q.n_quadrature_points; - vector > psi; + vector > psi (n_q_points); // in praxi: first compute // exact solution vector @@ -503,12 +503,9 @@ ProblemBase::make_boundary_value_list (const FunctionMap &dirichlet_ FunctionMap::const_iterator function_ptr; // field to store the indices of dofs - // initialize once to get the size right - // for the following fields. - vector face_dofs; - face->get_dof_indices (face_dofs); + vector face_dofs (fe.dofs_per_face); vector > dof_locations (face_dofs.size(), Point()); - vector dof_values; + vector dof_values (fe.dofs_per_face); for (; face!=endf; ++face) if ((function_ptr = dirichlet_bc.find(face->boundary_indicator())) != @@ -519,8 +516,6 @@ ProblemBase::make_boundary_value_list (const FunctionMap &dirichlet_ // get indices, physical location and // boundary values of dofs on this // face - face_dofs.erase (face_dofs.begin(), face_dofs.end()); - dof_values.erase (dof_values.begin(), dof_values.end()); face->get_dof_indices (face_dofs); fe.get_face_ansatz_points (face, boundary, dof_locations); function_ptr->second->value_list (dof_locations, dof_values); diff --git a/deal.II/deal.II/source/numerics/matrices.cc b/deal.II/deal.II/source/numerics/matrices.cc index 53fc60946d..cad018acfe 100644 --- a/deal.II/deal.II/source/numerics/matrices.cc +++ b/deal.II/deal.II/source/numerics/matrices.cc @@ -163,7 +163,7 @@ void MassMatrix::assemble (dFMatrix &cell_matrix, if (coefficient != 0) { - vector coefficient_values; + vector coefficient_values (fe_values.n_quadrature_points); coefficient->value_list (fe_values.get_quadrature_points(), coefficient_values); for (unsigned int point=0; point::assemble (dFMatrix &cell_matrix, const dFMatrix &values = fe_values.get_shape_values (); const vector &weights = fe_values.get_JxW_values (); - vector rhs_values; + vector rhs_values (fe_values.n_quadrature_points); right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values); if (coefficient != 0) { - vector coefficient_values; + vector coefficient_values (fe_values.n_quadrature_points); coefficient->value_list (fe_values.get_quadrature_points(), coefficient_values); for (unsigned int point=0; point::assemble (dVector &rhs, const dFMatrix &values = fe_values.get_shape_values (); const vector &weights = fe_values.get_JxW_values (); - vector rhs_values; + vector rhs_values(fe_values.n_quadrature_points); right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values); for (unsigned int point=0; point::assemble (dFMatrix &cell_matrix, const vector > >&gradients = fe_values.get_shape_grads (); const dFMatrix &values = fe_values.get_shape_values (); - vector rhs_values; + vector rhs_values(fe_values.n_quadrature_points); const vector &weights = fe_values.get_JxW_values (); right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values); if (coefficient != 0) { - vector coefficient_values; + vector coefficient_values(fe_values.n_quadrature_points); coefficient->value_list (fe_values.get_quadrature_points(), coefficient_values); for (unsigned int point=0; point::assemble (dFMatrix &cell_matrix, if (coefficient != 0) { - vector coefficient_values; + vector coefficient_values(fe_values.n_quadrature_points); coefficient->value_list (fe_values.get_quadrature_points(), coefficient_values); for (unsigned int point=0; point::assemble (dVector &rhs, const dFMatrix &values = fe_values.get_shape_values (); const vector &weights = fe_values.get_JxW_values (); - vector rhs_values; + vector rhs_values(fe_values.n_quadrature_points); right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values); for (unsigned int point=0; point