From: wolf Date: Mon, 9 Sep 2002 22:41:08 +0000 (+0000) Subject: SparsityPattern::block_write/block_read X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b50f813d0925b64fef684035b6f544ec5f86ebd;p=dealii-svn.git SparsityPattern::block_write/block_read git-svn-id: https://svn.dealii.org/trunk@6377 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/2002/c-3-4.html b/deal.II/doc/news/2002/c-3-4.html index 7801e6758c..bbea7bf757 100644 --- a/deal.II/doc/news/2002/c-3-4.html +++ b/deal.II/doc/news/2002/c-3-4.html @@ -207,7 +207,16 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK
  1. - New:Vector has a function SparsityPattern now has functions block_write/block_read, allowing to dump the data + of this object in to a file in binary format, and later to re-read it + without much need for much parsing. +
    + (WB 2002/09/09) +

    + +
  2. + New: Vector has a function lp_norm, computing the lp-norm of a vector for arbitrary p.
    diff --git a/deal.II/lac/include/lac/sparsity_pattern.h b/deal.II/lac/include/lac/sparsity_pattern.h index f32db1fa57..e5b3d33a94 100644 --- a/deal.II/lac/include/lac/sparsity_pattern.h +++ b/deal.II/lac/include/lac/sparsity_pattern.h @@ -19,6 +19,7 @@ #include #include +#include template class FullMatrix; @@ -741,6 +742,54 @@ class SparsityPattern : public Subscriptor */ const unsigned int * get_column_numbers () const; + /** + * Write the data of this object + * en bloc to a file. This is + * done in a binary mode, so the + * output is neither readable by + * humans nor (probably) by other + * computers using a different + * operating system of number + * format. + * + * The purpose of this function + * is that you can swap out + * matrices and sparsity pattern + * if you are short of memory, + * want to communicate between + * different programs, or allow + * objects to be persistent + * across different runs of the + * program. + */ + void block_write (ostream &out) const; + + /** + * Read data that has previously + * been written by + * @p{block_write} en block from + * a file. This is done using the + * inverse operations to the + * above function, so it is + * reasonably fast because the + * bitstream is not interpreted + * except for a few numbers up + * front. + * + * The object is resized on this + * operation, and all previous + * contents are lost. + * + * A primitive form of error + * checking is performed which + * will recognize the bluntest + * attempts to interpret some + * data as a vector stored + * bitwise to a file, but not + * more. + */ + void block_read (std::istream &in); + /** * Determine an estimate for the * memory consumption (in bytes) diff --git a/deal.II/lac/source/sparsity_pattern.cc b/deal.II/lac/source/sparsity_pattern.cc index 37911fbbe6..fb401a7684 100644 --- a/deal.II/lac/source/sparsity_pattern.cc +++ b/deal.II/lac/source/sparsity_pattern.cc @@ -775,6 +775,74 @@ SparsityPattern::bandwidth () const +void +SparsityPattern::block_write (ostream &out) const +{ + AssertThrow (out, ExcIO()); + + // first the simple objects, + // bracketed in [...] + out << '[' << max_dim << ' ' + << rows << ' ' + << max_vec_len << ' ' + << max_row_length << ' ' + << compressed << "]["; + // then write out real data + out.write (reinterpret_cast(&rowstart[0]), + reinterpret_cast(&rowstart[max_dim]) + - reinterpret_cast(&rowstart[0])); + out << "]["; + out.write (reinterpret_cast(&colnums[0]), + reinterpret_cast(&colnums[max_vec_len]) + - reinterpret_cast(&colnums[0])); + out << ']'; + + AssertThrow (out, ExcIO()); +}; + + + +void +SparsityPattern::block_read (istream &in) +{ + AssertThrow (in, ExcIO()); + + char c; + + // first read in simple data + in >> c; + AssertThrow (c == '[', ExcIO()); + in >> max_dim >> rows >> max_vec_len >> max_row_length >> compressed; + + in >> c; + AssertThrow (c == ']', ExcIO()); + in >> c; + AssertThrow (c == '[', ExcIO()); + + // reallocate space + delete[] rowstart; + delete[] colnums; + + rowstart = new unsigned int[max_dim]; + colnums = new unsigned int[max_vec_len]; + + // then read data + in.read (reinterpret_cast(&rowstart[0]), + reinterpret_cast(&rowstart[max_dim]) + - reinterpret_cast(&rowstart[0])); + in >> c; + AssertThrow (c == ']', ExcIO()); + in >> c; + AssertThrow (c == '[', ExcIO()); + in.read (reinterpret_cast(&colnums[0]), + reinterpret_cast(&colnums[max_vec_len]) + - reinterpret_cast(&colnums[0])); + in >> c; + AssertThrow (c == ']', ExcIO()); +}; + + + unsigned int SparsityPattern::memory_consumption () const { diff --git a/tests/lac/sparsity_pattern.cc b/tests/lac/sparsity_pattern.cc index bc688c6083..567ec62038 100644 --- a/tests/lac/sparsity_pattern.cc +++ b/tests/lac/sparsity_pattern.cc @@ -19,6 +19,7 @@ #include #include #include +#include int @@ -91,9 +92,13 @@ main () // now check for equivalence of sp3 and sp4 for (unsigned int row=0; row