From 6dbe4935eee4b31e40cdcde16c7ec72267521d03 Mon Sep 17 00:00:00 2001 From: wolf Date: Thu, 18 Nov 1999 10:00:00 +0000 Subject: [PATCH] Free the memory no more needed for the colnums array upon compress(). git-svn-id: https://svn.dealii.org/trunk@1888 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/Todo | 7 ++-- deal.II/lac/include/lac/sparse_matrix.h | 3 ++ deal.II/lac/source/sparse_matrix.cc | 53 +++++++++++++++++++------ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/deal.II/lac/Todo b/deal.II/lac/Todo index 253c5e218a..9859d5f085 100644 --- a/deal.II/lac/Todo +++ b/deal.II/lac/Todo @@ -10,18 +10,17 @@ Use unsigned integers for the colnums array in dSMatrixStruct. This eliminating the need for conversions. -Let dSMatrixStruct::compress free the memory of colnums which is - no longer needed. - - Make ILU decomposition faster by following the comment in the innermost loop (about the better use of the index j). + Change in-class defined SolverSelector functions out-of-class to conform to general style guide. (Ralf?) + Why not replace Vector::scale by Vector::operator *= ? + Use the commented-out version in PreconditionBlock::invert_diagblocks using the try-catch clauses diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index 561f767654..d23b50f6ef 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -209,6 +209,9 @@ class SparseMatrixStruct : public Subscriptor * entry of square matrices, which is * always the first entry of each row. * + * The memory which is no more + * needed is released. + * * #SparseMatrix# objects require the * #SparseMatrixStruct# objects they are * initialized with to be compressed, to diff --git a/deal.II/lac/source/sparse_matrix.cc b/deal.II/lac/source/sparse_matrix.cc index 5c3264d0d5..89710fcf78 100644 --- a/deal.II/lac/source/sparse_matrix.cc +++ b/deal.II/lac/source/sparse_matrix.cc @@ -309,6 +309,18 @@ SparseMatrixStruct::compress () next_row_start = 0, row_length = 0; + // first find out how many non-zero + // elements there are, in order to + // allocate the right amount of + // memory + const unsigned int + nonzero_elements = count_if (&colnums[rowstart[0]], + &colnums[rowstart[rows]], + bind2nd(not_equal_to(), -1)); + // now allocate the respective memory + int *new_colnums = new int[nonzero_elements]; + + // reserve temporary storage to // store the entries of one row vector tmp_entries (max_row_length); @@ -337,10 +349,10 @@ SparseMatrixStruct::compress () sort ((rows==cols) ? &tmp_entries[1] : &tmp_entries[0], &tmp_entries[row_length]); - // Re-insert column numbers - // into the field + // insert column numbers + // into the new field for (unsigned int j=0; j(line)), + (new_colnums[rowstart[line]] == static_cast(line)), ExcInternalError()); // assert that the first entry // does not show up in @@ -358,18 +370,35 @@ SparseMatrixStruct::compress () // among themselves (this handles // both cases, quadratic and // rectangular matrices) - Assert (find (&colnums[rowstart[line]+1], - &colnums[next_row_start], - colnums[rowstart[line]]) == - &colnums[next_row_start], + Assert (find (&new_colnums[rowstart[line]+1], + &new_colnums[next_row_start], + new_colnums[rowstart[line]]) == + &new_colnums[next_row_start], ExcInternalError()); - Assert (adjacent_find(&colnums[rowstart[line]+1], - &colnums[next_row_start]) == - &colnums[next_row_start], + Assert (adjacent_find(&new_colnums[rowstart[line]+1], + &new_colnums[next_row_start]) == + &new_colnums[next_row_start], ExcInternalError()); }; - + + // assert that we have used all + // allocated space, no more and no + // less + Assert (next_free_entry == nonzero_elements, + ExcInternalError()); + + // set iterator-past-the-end rowstart[rows] = next_row_start; + + // set colnums to the newly + // allocated array and delete the + // old one + delete[] colnums; + colnums = new_colnums; + + // store the size + max_vec_len = nonzero_elements; + compressed = true; }; -- 2.39.5