From c10a309181a85fefd87315abc8311a636127f6ce Mon Sep 17 00:00:00 2001 From: kronbichler Date: Sun, 28 Dec 2008 14:04:26 +0000 Subject: [PATCH] Use a cache of column indices to improve the speed of the Trilinos sparsity pattern generation. git-svn-id: https://svn.dealii.org/trunk@18036 0785d39b-7218-0410-832d-ea1e28bc413d --- .../include/lac/trilinos_sparsity_pattern.h | 53 ++++++++++++++++- .../lac/source/trilinos_sparsity_pattern.cc | 58 ++++++++++++++++--- 2 files changed, 99 insertions(+), 12 deletions(-) diff --git a/deal.II/lac/include/lac/trilinos_sparsity_pattern.h b/deal.II/lac/include/lac/trilinos_sparsity_pattern.h index 11707a96db..71a9508bf1 100755 --- a/deal.II/lac/include/lac/trilinos_sparsity_pattern.h +++ b/deal.II/lac/include/lac/trilinos_sparsity_pattern.h @@ -957,6 +957,28 @@ namespace TrilinosWrappers */ std::auto_ptr graph; + /** + * Scratch array that holds several + * indices that should be written + * into the same row of the sparsity + * pattern. This is to increase the + * speed of this function. + */ + std::vector cached_row_indices; + + /** + * A number that tells how many + * indices currently are active in + * the cache. + */ + unsigned int n_cached_elements; + + /** + * The row that is currently in the + * cache. + */ + unsigned int row_in_cache; + friend class SparseMatrix; friend class SparsityPatternIterators::const_iterator; }; @@ -1205,10 +1227,32 @@ namespace TrilinosWrappers inline void - SparsityPattern::add (const unsigned int i, - const unsigned int j) + SparsityPattern::add (const unsigned int i, + const unsigned int j) { - add (i, 1, &j); + // if we want to write an element to the + // row the cache is currently pointed to, + // we just append the data to the cache + if (i == row_in_cache) + { + // if the size is too small, extend the + // cache by 10 elements + if (n_cached_elements > cached_row_indices.size()) + cached_row_indices.resize(cached_row_indices.size() + 10); + + cached_row_indices[n_cached_elements] = j; + ++n_cached_elements; + return; + } + + // if we are to write another row data, + // we write the cache data into the + // sparsity pattern, and then call this + // function again + add (row_in_cache, n_cached_elements, &cached_row_indices[0]); + row_in_cache = i; + n_cached_elements = 0; + add (i,j); } @@ -1219,6 +1263,9 @@ namespace TrilinosWrappers const unsigned int n_cols, const unsigned int *col_indices) { + if (n_cols == 0) + return; + int * col_index_ptr = (int*)col_indices; compressed = false; diff --git a/deal.II/lac/source/trilinos_sparsity_pattern.cc b/deal.II/lac/source/trilinos_sparsity_pattern.cc index 5de4418625..49bdefb7be 100755 --- a/deal.II/lac/source/trilinos_sparsity_pattern.cc +++ b/deal.II/lac/source/trilinos_sparsity_pattern.cc @@ -90,7 +90,10 @@ namespace TrilinosWrappers col_map (row_map), compressed (true), graph (std::auto_ptr - (new Epetra_FECrsGraph(View, row_map, 0))) + (new Epetra_FECrsGraph(View, row_map, 0))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) { graph->FillComplete(); } @@ -103,7 +106,10 @@ namespace TrilinosWrappers compressed (false), graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, - int(n_entries_per_row), false))) + int(n_entries_per_row), false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} SparsityPattern::SparsityPattern (const Epetra_Map &InputMap, @@ -115,7 +121,10 @@ namespace TrilinosWrappers graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, (int*)const_cast(&(n_entries_per_row[0])), - false))) + false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} SparsityPattern::SparsityPattern (const Epetra_Map &InputRowMap, @@ -127,7 +136,10 @@ namespace TrilinosWrappers compressed (false), graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, - int(n_entries_per_row), false))) + int(n_entries_per_row), false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} SparsityPattern::SparsityPattern (const Epetra_Map &InputRowMap, @@ -140,7 +152,10 @@ namespace TrilinosWrappers graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, (int*)const_cast(&(n_entries_per_row[0])), - false))) + false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} SparsityPattern::SparsityPattern (const unsigned int m, @@ -157,7 +172,10 @@ namespace TrilinosWrappers compressed (false), graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, - int(n_entries_per_row), false))) + int(n_entries_per_row), false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} SparsityPattern::SparsityPattern (const unsigned int m, @@ -175,7 +193,10 @@ namespace TrilinosWrappers graph (std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, (int*)const_cast(&(n_entries_per_row[0])), - false))) + false))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} // Copy function is currently not working @@ -190,7 +211,10 @@ namespace TrilinosWrappers col_map (InputSP.col_map), compressed (false), graph (std::auto_ptr - (new Epetra_FECrsGraph(*InputSP.graph))) + (new Epetra_FECrsGraph(*InputSP.graph))), + cached_row_indices (1), + n_cached_elements (0), + row_in_cache (0) {} */ @@ -239,6 +263,9 @@ namespace TrilinosWrappers graph = std::auto_ptr (new Epetra_FECrsGraph(Copy, row_map, n_entries_per_row, false)); + + n_cached_elements = 0; + row_in_cache = 0; } @@ -289,6 +316,9 @@ namespace TrilinosWrappers (new Epetra_FECrsGraph(Copy, row_map, n_entries_per_row[input_row_map.MinMyGID()], false)); + + n_cached_elements = 0; + row_in_cache = 0; } @@ -473,6 +503,9 @@ namespace TrilinosWrappers graph->FillComplete(); + n_cached_elements = 0; + row_in_cache = 0; + compressed = true; } @@ -482,6 +515,13 @@ namespace TrilinosWrappers SparsityPattern::compress () { // flush buffers + if (n_cached_elements > 0) + { + add (row_in_cache, n_cached_elements, &cached_row_indices[0]); + n_cached_elements = 0; + row_in_cache = 0; + } + int ierr; ierr = graph->GlobalAssemble (col_map, row_map, true); @@ -630,7 +670,7 @@ namespace TrilinosWrappers unsigned int SparsityPattern::max_entries_per_row () const { - int nnz = graph->MaxRowDim(); + int nnz = graph->MaxNumIndices(); return static_cast(nnz); } -- 2.39.5