From 8a755cf30c4fbb6d478286ae61572225e2eaed5a Mon Sep 17 00:00:00 2001 From: kronbichler Date: Wed, 8 May 2013 08:03:31 +0000 Subject: [PATCH] Improve advance() operation and comparison operation a bit. git-svn-id: https://svn.dealii.org/trunk@29480 0785d39b-7218-0410-832d-ea1e28bc413d --- .../include/deal.II/lac/chunk_sparse_matrix.h | 4 +- .../deal.II/lac/chunk_sparsity_pattern.h | 109 +++++++----------- .../include/deal.II/lac/sparsity_pattern.h | 1 + 3 files changed, 44 insertions(+), 70 deletions(-) diff --git a/deal.II/include/deal.II/lac/chunk_sparse_matrix.h b/deal.II/include/deal.II/lac/chunk_sparse_matrix.h index 3d7a5c2fc3..8ceb359cfa 100644 --- a/deal.II/include/deal.II/lac/chunk_sparse_matrix.h +++ b/deal.II/include/deal.II/lac/chunk_sparse_matrix.h @@ -1920,7 +1920,7 @@ namespace ChunkSparseMatrixIterators if (*this < other) { Iterator copy = *this; - while (copy < other) + while (copy != other) { ++copy; --difference; @@ -1929,7 +1929,7 @@ namespace ChunkSparseMatrixIterators else { Iterator copy = other; - while (copy < *this) + while (copy != *this) { ++copy; ++difference; diff --git a/deal.II/include/deal.II/lac/chunk_sparsity_pattern.h b/deal.II/include/deal.II/lac/chunk_sparsity_pattern.h index 655c0ba406..200c80303d 100644 --- a/deal.II/include/deal.II/lac/chunk_sparsity_pattern.h +++ b/deal.II/include/deal.II/lac/chunk_sparsity_pattern.h @@ -75,12 +75,6 @@ namespace ChunkSparsityPatternIterators */ unsigned int row () const; - /** - * Index in row of the element represented by this object. This function - * can only be called for entries for which is_valid_entry() is true. - */ - unsigned int index () const; - /** * Returns the global index from the reduced sparsity pattern. */ @@ -131,12 +125,12 @@ namespace ChunkSparsityPatternIterators SparsityPatternIterators::Accessor reduced_accessor; /** - * Current row number. + * Current chunk row number. */ unsigned int chunk_row; /** - * Current index in row. + * Current chunk col number. */ unsigned int chunk_col; @@ -964,7 +958,8 @@ namespace ChunkSparsityPatternIterators { Assert (is_valid_entry() == true, ExcInvalidIterator()); - return sparsity_pattern->get_chunk_size()*reduced_accessor.row()+chunk_row; + return sparsity_pattern->get_chunk_size()*reduced_accessor.row() + + chunk_row; } @@ -981,18 +976,6 @@ namespace ChunkSparsityPatternIterators - inline - unsigned int - Accessor::index() const - { - Assert (is_valid_entry() == true, ExcInvalidIterator()); - - return sparsity_pattern->get_chunk_size()*reduced_accessor.index() + - chunk_col; - } - - - inline std::size_t Accessor::reduced_index() const @@ -1026,17 +1009,7 @@ namespace ChunkSparsityPatternIterators Assert (sparsity_pattern == other.sparsity_pattern, ExcInternalError()); - // comparison is a bit messy because of the way ChunkSparsityPattern - // stores entry: chunk rows run faster than the indices of the reduced - // sparsity pattern, but the accessors should of course compare less based - // on the actual row, not the reduced one. - if (chunk_row == other.chunk_row) - return (reduced_accessor.index_within_sparsity < - other.reduced_accessor.index_within_sparsity || - (reduced_accessor.index_within_sparsity == - other.reduced_accessor.index_within_sparsity && - chunk_col < other.chunk_col)); - else + if (chunk_row != other.chunk_row) { if (reduced_accessor.index_within_sparsity == reduced_accessor.sparsity_pattern->n_nonzero_elements()) @@ -1044,24 +1017,23 @@ namespace ChunkSparsityPatternIterators if (other.reduced_accessor.index_within_sparsity == reduced_accessor.sparsity_pattern->n_nonzero_elements()) return true; - const unsigned int row = reduced_accessor.row(), - other_row = other.reduced_accessor.row(); - return (row < other_row - || - (row == other_row - && - (chunk_row < other.chunk_row - || - (chunk_row == other.chunk_row - && - (reduced_accessor.index_within_sparsity < - other.reduced_accessor.index_within_sparsity - || - (reduced_accessor.index_within_sparsity == - other.reduced_accessor.index_within_sparsity - && - chunk_col < other.chunk_col)))))); + + const unsigned int + global_row = sparsity_pattern->get_chunk_size()*reduced_accessor.row() + +chunk_row, + other_global_row = sparsity_pattern->get_chunk_size()* + other.reduced_accessor.row()+other.chunk_row; + if (global_row < other_global_row) + return true; + else if (global_row > other_global_row) + return false; } + + return (reduced_accessor.index_within_sparsity < + other.reduced_accessor.index_within_sparsity || + (reduced_accessor.index_within_sparsity == + other.reduced_accessor.index_within_sparsity && + chunk_col < other.chunk_col)); } @@ -1078,6 +1050,11 @@ namespace ChunkSparsityPatternIterators reduced_accessor.column() * chunk_size + chunk_col < sparsity_pattern->n_cols(), ExcIteratorPastEnd()); + if (chunk_size == 1) + { + reduced_accessor.advance(); + return; + } ++chunk_col; @@ -1087,34 +1064,30 @@ namespace ChunkSparsityPatternIterators reduced_accessor.column() * chunk_size + chunk_col == sparsity_pattern->n_cols()) { + const unsigned int reduced_row = reduced_accessor.row(); // end of row - if (reduced_accessor.index() + 1 == - reduced_accessor.sparsity_pattern->row_length(reduced_accessor.row())) + if (reduced_accessor.index_within_sparsity + 1 == + reduced_accessor.sparsity_pattern->rowstart[reduced_row+1]) { ++chunk_row; + chunk_col = 0; - const unsigned int old_reduced_row = reduced_accessor.row(); - // end of matrix - if (old_reduced_row * chunk_size + chunk_row == - sparsity_pattern->n_rows()) - { - chunk_row = 0; - reduced_accessor = - SparsityPatternIterators::Accessor(&sparsity_pattern->sparsity_pattern, - sparsity_pattern->sparsity_pattern.n_nonzero_elements()); - } - // end of chunk rows - else if (chunk_row == chunk_size) + // end of chunk rows or end of matrix + if (chunk_row == chunk_size || + (reduced_row * chunk_size + chunk_row == + sparsity_pattern->n_rows())) { - reduced_accessor = - *sparsity_pattern->sparsity_pattern.begin(old_reduced_row+1); chunk_row = 0; + reduced_accessor.advance(); } + // go back to the beginning of the same reduced row but with + // chunk_row increased by one else - reduced_accessor = - *sparsity_pattern->sparsity_pattern.begin(old_reduced_row); + reduced_accessor.index_within_sparsity = + reduced_accessor.sparsity_pattern->rowstart[reduced_row]; } + // advance within chunk else { reduced_accessor.advance(); @@ -1185,7 +1158,7 @@ namespace ChunkSparsityPatternIterators bool Iterator::operator != (const Iterator &other) const { - return ! (*this == other); + return ! (accessor == other.accessor); } diff --git a/deal.II/include/deal.II/lac/sparsity_pattern.h b/deal.II/include/deal.II/lac/sparsity_pattern.h index 17c5599b26..212038578d 100644 --- a/deal.II/include/deal.II/lac/sparsity_pattern.h +++ b/deal.II/include/deal.II/lac/sparsity_pattern.h @@ -1419,6 +1419,7 @@ private: */ friend class SparsityPatternIterators::Iterator; friend class SparsityPatternIterators::Accessor; + friend class ChunkSparsityPatternIterators::Accessor; }; -- 2.39.5