From 4dfd084e5e76b0ea342d139d4b2ca5d9eb6b1488 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 9 Jan 2019 10:19:43 +0100 Subject: [PATCH] Use ranged-based for loop in source/multigrid --- source/multigrid/mg_level_global_transfer.cc | 9 ++- source/multigrid/mg_transfer_block.cc | 14 ++--- source/multigrid/mg_transfer_component.cc | 18 +++--- source/multigrid/mg_transfer_internal.cc | 65 ++++++++------------ source/multigrid/mg_transfer_matrix_free.cc | 4 +- 5 files changed, 46 insertions(+), 64 deletions(-) diff --git a/source/multigrid/mg_level_global_transfer.cc b/source/multigrid/mg_level_global_transfer.cc index 30052c40d3..fb6916cdc0 100644 --- a/source/multigrid/mg_level_global_transfer.cc +++ b/source/multigrid/mg_level_global_transfer.cc @@ -214,12 +214,11 @@ namespace for (unsigned int l = 0; l < mg_dof.get_triangulation().n_global_levels(); ++l) { - for (unsigned int i = 0; i < my_copy_indices_level_mine[l].size(); ++i) - accessed_indices.push_back(my_copy_indices_level_mine[l][i].first); + for (const auto &indices : my_copy_indices_level_mine[l]) + accessed_indices.push_back(indices.first); std::vector accessed_level_indices; - for (unsigned int i = 0; i < my_copy_indices_global_mine[l].size(); ++i) - accessed_level_indices.push_back( - my_copy_indices_global_mine[l][i].second); + for (const auto &indices : my_copy_indices_global_mine[l]) + accessed_level_indices.push_back(indices.second); std::sort(accessed_level_indices.begin(), accessed_level_indices.end()); level_index_set[l].set_size(mg_dof.locally_owned_mg_dofs(l).size()); level_index_set[l].add_indices(accessed_level_indices.begin(), diff --git a/source/multigrid/mg_transfer_block.cc b/source/multigrid/mg_transfer_block.cc index 6f02384712..d5741f1428 100644 --- a/source/multigrid/mg_transfer_block.cc +++ b/source/multigrid/mg_transfer_block.cc @@ -236,13 +236,13 @@ MGTransferBlockBase::build_matrices(const DoFHandler &, // for later use. mg_block_start = sizes; // Compute start indices from sizes - for (unsigned int l = 0; l < mg_block_start.size(); ++l) + for (auto &level_blocks : mg_block_start) { types::global_dof_index k = 0; - for (unsigned int i = 0; i < mg_block_start[l].size(); ++i) + for (types::global_dof_index &level_block_start : level_blocks) { - const types::global_dof_index t = mg_block_start[l][i]; - mg_block_start[l][i] = k; + const types::global_dof_index t = level_block_start; + level_block_start = k; k += t; } } @@ -252,10 +252,10 @@ MGTransferBlockBase::build_matrices(const DoFHandler &, static_cast &>(mg_dof), block_start); types::global_dof_index k = 0; - for (unsigned int i = 0; i < block_start.size(); ++i) + for (types::global_dof_index &first_index : block_start) { - const types::global_dof_index t = block_start[i]; - block_start[i] = k; + const types::global_dof_index t = first_index; + first_index = k; k += t; } // Build index vectors for diff --git a/source/multigrid/mg_transfer_component.cc b/source/multigrid/mg_transfer_component.cc index 3ae0594598..af94fcb9d7 100644 --- a/source/multigrid/mg_transfer_component.cc +++ b/source/multigrid/mg_transfer_component.cc @@ -113,8 +113,8 @@ namespace { selected.resize(target_component.size()); std::fill_n(selected.begin(), ncomp, false); - for (unsigned int i = 0; i < target_component.size(); ++i) - selected[target_component[i]] = true; + for (const unsigned int component : target_component) + selected[component] = true; } Assert(selected.size() == target_component.size(), @@ -329,13 +329,13 @@ MGTransferComponentBase::build_matrices(const DoFHandler &, // for later use. mg_component_start = sizes; // Compute start indices from sizes - for (unsigned int l = 0; l < mg_component_start.size(); ++l) + for (auto &level_components : mg_component_start) { types::global_dof_index k = 0; - for (unsigned int i = 0; i < mg_component_start[l].size(); ++i) + for (types::global_dof_index &level_component_start : level_components) { - const types::global_dof_index t = mg_component_start[l][i]; - mg_component_start[l][i] = k; + const types::global_dof_index t = level_component_start; + level_component_start = k; k += t; } } @@ -345,10 +345,10 @@ MGTransferComponentBase::build_matrices(const DoFHandler &, DoFTools::count_dofs_per_block(mg_dof, component_start, target_component); types::global_dof_index k = 0; - for (unsigned int i = 0; i < component_start.size(); ++i) + for (types::global_dof_index &first_index : component_start) { - const types::global_dof_index t = component_start[i]; - component_start[i] = k; + const types::global_dof_index t = first_index; + first_index = k; k += t; } diff --git a/source/multigrid/mg_transfer_internal.cc b/source/multigrid/mg_transfer_internal.cc index b884e92f71..f833459ac7 100644 --- a/source/multigrid/mg_transfer_internal.cc +++ b/source/multigrid/mg_transfer_internal.cc @@ -193,20 +193,16 @@ namespace internal // * find owners of the level dofs and insert into send_data // accordingly - for (typename std::vector::iterator dofpair = - send_data_temp.begin(); - dofpair != send_data_temp.end(); - ++dofpair) + for (const auto &dofpair : send_data_temp) { std::set::iterator it; for (it = neighbors.begin(); it != neighbors.end(); ++it) { if (mg_dof - .locally_owned_mg_dofs_per_processor( - dofpair->level)[*it] - .is_element(dofpair->level_dof_index)) + .locally_owned_mg_dofs_per_processor(dofpair.level)[*it] + .is_element(dofpair.level_dof_index)) { - send_data[*it].push_back(*dofpair); + send_data[*it].push_back(dofpair); break; } } @@ -219,12 +215,9 @@ namespace internal // * send std::vector requests; { - for (std::set::iterator it = neighbors.begin(); - it != neighbors.end(); - ++it) + for (const auto dest : neighbors) { requests.push_back(MPI_Request()); - unsigned int dest = *it; std::vector &data = send_data[dest]; // If there is nothing to send, we still need to send a message, // because the receiving end will be waitng. In that case we @@ -299,11 +292,10 @@ namespace internal &status); AssertThrowMPI(ierr); - for (unsigned int i = 0; i < receive_buffer.size(); ++i) + for (const auto &dof_pair : receive_buffer) { - copy_indices_level_mine[receive_buffer[i].level] - .emplace_back(receive_buffer[i].global_dof_index, - receive_buffer[i].level_dof_index); + copy_indices_level_mine[dof_pair.level].emplace_back( + dof_pair.global_dof_index, dof_pair.level_dof_index); } } } @@ -332,20 +324,12 @@ namespace internal // mode. std::less> compare; - for (unsigned int level = 0; level < copy_indices.size(); ++level) - std::sort(copy_indices[level].begin(), - copy_indices[level].end(), - compare); - for (unsigned int level = 0; level < copy_indices_level_mine.size(); - ++level) - std::sort(copy_indices_level_mine[level].begin(), - copy_indices_level_mine[level].end(), - compare); - for (unsigned int level = 0; level < copy_indices_global_mine.size(); - ++level) - std::sort(copy_indices_global_mine[level].begin(), - copy_indices_global_mine[level].end(), - compare); + for (auto &level_indices : copy_indices) + std::sort(level_indices.begin(), level_indices.end(), compare); + for (auto &level_indices : copy_indices_level_mine) + std::sort(level_indices.begin(), level_indices.end(), compare); + for (auto &level_indices : copy_indices_global_mine) + std::sort(level_indices.begin(), level_indices.end(), compare); } @@ -376,11 +360,10 @@ namespace internal // partitioner that we are going to use for the vector const auto &part = ghosted_level_vector.get_partitioner(); ghosted_dofs.add_indices(part->ghost_indices()); - for (unsigned int i = 0; i < copy_indices_global_mine.size(); ++i) - copy_indices_global_mine[i].second = - locally_owned.n_elements() + - ghosted_dofs.index_within_set( - part->local_to_global(copy_indices_global_mine[i].second)); + for (auto &indices : copy_indices_global_mine) + indices.second = locally_owned.n_elements() + + ghosted_dofs.index_within_set( + part->local_to_global(indices.second)); } ghosted_level_vector.reinit(locally_owned, ghosted_dofs, communicator); } @@ -701,9 +684,9 @@ namespace internal const IndexSet &owned_level_dofs = mg_dof.locally_owned_mg_dofs(level); - for (unsigned int i = 0; i < local_dof_indices.size(); ++i) - if (!owned_level_dofs.is_element(local_dof_indices[i])) - ghosted_level_dofs.push_back(local_dof_indices[i]); + for (const auto local_dof_index : local_dof_indices) + if (!owned_level_dofs.is_element(local_dof_index)) + ghosted_level_dofs.push_back(local_dof_index); add_child_indices(c, fe->dofs_per_cell - @@ -772,9 +755,9 @@ namespace internal const IndexSet &owned_level_dofs_l0 = mg_dof.locally_owned_mg_dofs(0); - for (unsigned int i = 0; i < local_dof_indices.size(); ++i) - if (!owned_level_dofs_l0.is_element(local_dof_indices[i])) - ghosted_level_dofs_l0.push_back(local_dof_indices[i]); + for (const auto local_dof_index : local_dof_indices) + if (!owned_level_dofs_l0.is_element(local_dof_index)) + ghosted_level_dofs_l0.push_back(local_dof_index); const std::size_t start_index = global_level_dof_indices_l0.size(); diff --git a/source/multigrid/mg_transfer_matrix_free.cc b/source/multigrid/mg_transfer_matrix_free.cc index 906fdda090..6bd39c8cc1 100644 --- a/source/multigrid/mg_transfer_matrix_free.cc +++ b/source/multigrid/mg_transfer_matrix_free.cc @@ -612,8 +612,8 @@ MGTransferBlockMatrixFree::MGTransferBlockMatrixFree( const std::vector &mg_c) : same_for_all(false) { - for (unsigned int i = 0; i < mg_c.size(); ++i) - matrix_free_transfer_vector.emplace_back(mg_c[i]); + for (const auto &constrained_block_dofs : mg_c) + matrix_free_transfer_vector.emplace_back(constrained_block_dofs); } -- 2.39.5