From: wolf Date: Mon, 9 Sep 2002 23:18:33 +0000 (+0000) Subject: SparseMatrix::block_read/write X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c19b303b2fbd0e1453027117cb73eed711b23e37;p=dealii-svn.git SparseMatrix::block_read/write git-svn-id: https://svn.dealii.org/trunk@6379 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 bbea7bf757..3284318ab0 100644 --- a/deal.II/doc/news/2002/c-3-4.html +++ b/deal.II/doc/news/2002/c-3-4.html @@ -207,10 +207,11 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK
  1. - New: SparsityPattern now has functions SparsityPattern and SparseMatrix now have 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. + of these objects into a file in binary format, and later to re-read it + without much need for parsing.
    (WB 2002/09/09)

    diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index 211a3517e4..38d3b8967b 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -774,6 +774,62 @@ class SparseMatrix : public Subscriptor const char *zero_string = " ", const double denominator = 1.) 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. Note, + * however, that no checks are + * performed whether new data and + * the underlying + * @ref{SparsityPattern} object + * fit together. It is your + * responsibility to make sure + * that the sparsity pattern and + * the data to be read match. + * + * 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) @@ -849,14 +905,14 @@ class SparseMatrix : public Subscriptor number *val; /** - * Allocated size of @p{val}. This - * can be larger than the - * actually used part if the size - * of the matrix was reduced - * somewhen in the past by - * associating a sparsity pattern - * with a smaller size to this - * object somewhen, using the + * Allocated size of + * @p{val}. This can be larger + * than the actually used part if + * the size of the matrix was + * reduced somewhen in the past + * by associating a sparsity + * pattern with a smaller size to + * this object, using the * @p{reinit} function. */ unsigned int max_len; diff --git a/deal.II/lac/include/lac/sparse_matrix.templates.h b/deal.II/lac/include/lac/sparse_matrix.templates.h index eb72506f77..fcb345952c 100644 --- a/deal.II/lac/include/lac/sparse_matrix.templates.h +++ b/deal.II/lac/include/lac/sparse_matrix.templates.h @@ -1172,6 +1172,58 @@ void SparseMatrix::print_formatted (std::ostream &out, +template +void +SparseMatrix::block_write (ostream &out) const +{ + AssertThrow (out, ExcIO()); + + // first the simple objects, + // bracketed in [...] + out << '[' << max_len << "]["; + // then write out real data + out.write (reinterpret_cast(&val[0]), + reinterpret_cast(&val[max_len]) + - reinterpret_cast(&val[0])); + out << ']'; + + AssertThrow (out, ExcIO()); +}; + + + +template +void +SparseMatrix::block_read (istream &in) +{ + AssertThrow (in, ExcIO()); + + char c; + + // first read in simple data + in >> c; + AssertThrow (c == '[', ExcIO()); + in >> max_len; + + in >> c; + AssertThrow (c == ']', ExcIO()); + in >> c; + AssertThrow (c == '[', ExcIO()); + + // reallocate space + delete[] val; + val = new number[max_len]; + + // then read data + in.read (reinterpret_cast(&val[0]), + reinterpret_cast(&val[max_len]) + - reinterpret_cast(&val[0])); + in >> c; + AssertThrow (c == ']', ExcIO()); +}; + + + template unsigned int SparseMatrix::memory_consumption () const diff --git a/tests/lac/sparse_matrices.cc b/tests/lac/sparse_matrices.cc index 9cfa6f85c8..94c7f47f83 100644 --- a/tests/lac/sparse_matrices.cc +++ b/tests/lac/sparse_matrices.cc @@ -155,4 +155,21 @@ int main() if (std::fabs(A_res[i] - E_res[i]) > 1.e-14) deallog << "SparseMatrix and SparseMatrixEZ differ!!!" << std::endl; + + // dump A into a file, and re-read + // it, then check equality + std::ofstream tmp_write ("sparse_matrices.tmp"); + A.block_write (tmp_write); + tmp_write.close (); + + std::ifstream tmp_read ("sparse_matrices.tmp"); + SparseMatrix A_tmp; + A_tmp.reinit (A.get_sparsity_pattern()); + A_tmp.block_read (tmp_read); + tmp_read.close (); + + for (unsigned int i=0; i