From 7f0b0e3850ec263f10ca4362718568dca8750dac Mon Sep 17 00:00:00 2001 From: bangerth Date: Tue, 12 Feb 2013 15:14:35 +0000 Subject: [PATCH] Follow the documented advice of not looping over all elements of a matrix but do so row by row. git-svn-id: https://svn.dealii.org/trunk@28323 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/source/lac/chunk_sparsity_pattern.cc | 68 +++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/deal.II/source/lac/chunk_sparsity_pattern.cc b/deal.II/source/lac/chunk_sparsity_pattern.cc index 4fdc3389c0..49fd1bc3a5 100644 --- a/deal.II/source/lac/chunk_sparsity_pattern.cc +++ b/deal.II/source/lac/chunk_sparsity_pattern.cc @@ -480,37 +480,43 @@ ChunkSparsityPattern::n_nonzero_elements () const else { - // if columns don't align, then - // just iterate over all chunks and - // see what this leads to - SparsityPattern::const_iterator p = sparsity_pattern.begin(); - unsigned int n = 0; - for (; p!=sparsity_pattern.end(); ++p) - if ((p->row() != sparsity_pattern.n_rows() - 1) - && - (p->column() != sparsity_pattern.n_cols() - 1)) - n += chunk_size * chunk_size; - else if ((p->row() == sparsity_pattern.n_rows() - 1) - && - (p->column() != sparsity_pattern.n_cols() - 1)) - // last chunk row, but not - // last chunk column. only a - // smaller number (n_rows % - // chunk_size) of rows - // actually exist - n += (n_rows() % chunk_size) * chunk_size; - else if ((p->row() != sparsity_pattern.n_rows() - 1) - && - (p->column() == sparsity_pattern.n_cols() - 1)) - // last chunk column, but - // not row - n += (n_cols() % chunk_size) * chunk_size; - else - // bottom right chunk - n += (n_cols() % chunk_size) * - (n_rows() % chunk_size); - - return n; + // if columns don't align, then + // just iterate over all chunks and + // see what this leads to. follow the advice in the documentation of + // the sparsity pattern iterators to do the loop over individual rows, + // rather than all elements + unsigned int n = 0; + + for (int row = 0; row < n_rows(); ++row) + { + SparsityPattern::const_iterator p = sparsity_pattern.begin(row); + for (; p!=sparsity_pattern.end(row); ++p) + if ((row != sparsity_pattern.n_rows() - 1) + && + (p->column() != sparsity_pattern.n_cols() - 1)) + n += chunk_size * chunk_size; + else if ((row == sparsity_pattern.n_rows() - 1) + && + (p->column() != sparsity_pattern.n_cols() - 1)) + // last chunk row, but not + // last chunk column. only a + // smaller number (n_rows % + // chunk_size) of rows + // actually exist + n += (n_rows() % chunk_size) * chunk_size; + else if ((row != sparsity_pattern.n_rows() - 1) + && + (p->column() == sparsity_pattern.n_cols() - 1)) + // last chunk column, but + // not row + n += (n_cols() % chunk_size) * chunk_size; + else + // bottom right chunk + n += (n_cols() % chunk_size) * + (n_rows() % chunk_size); + } + + return n; } } } -- 2.39.5