From 770c72c1c322da36918312465807bab9034e0dcb Mon Sep 17 00:00:00 2001 From: turcksin Date: Thu, 5 Jun 2014 14:03:37 +0000 Subject: [PATCH] Add functions for Paralutionwrappers::SparseMatrix. git-svn-id: https://svn.dealii.org/branches/branch_paralution@33021 0785d39b-7218-0410-832d-ea1e28bc413d --- .../deal.II/lac/paralution_sparse_matrix.h | 157 ++++++++++++++++-- .../include/deal.II/lac/paralution_vector.h | 3 +- .../source/lac/paralution_sparse_matrix.cc | 61 ++++++- 3 files changed, 200 insertions(+), 21 deletions(-) diff --git a/deal.II/include/deal.II/lac/paralution_sparse_matrix.h b/deal.II/include/deal.II/lac/paralution_sparse_matrix.h index 20861a3028..c98e9d84f1 100644 --- a/deal.II/include/deal.II/lac/paralution_sparse_matrix.h +++ b/deal.II/include/deal.II/lac/paralution_sparse_matrix.h @@ -1,7 +1,7 @@ // --------------------------------------------------------------------- // $Id: paralution_sparse_matrix.h 30040 2013-07-18 17:06:48Z maier $ // -// Copyright (C) 2013 by the deal.II authors +// Copyright (C) 2013, 2014 by the deal.II authors // // This file is part of the deal.II library. // @@ -36,6 +36,14 @@ DEAL_II_NAMESPACE_OPEN */ namespace ParalutionWrappers { + /** + * Enum on format of Paralusion Matrix: DENSE, Compressed Sparse Row, + * Modified Compressed Sparse Row, Block Compressed Sparse Row, COOrdinate + * format, DIAgonal format, ELLpack format, HYBrid format (mixed between ELL + * and COO). + */ + enum matrix_format{DENSE, CSR, MCSR, BCSR, COO, DIA, ELL, HYB}; + /** * This class implements a wrapper to use the Paralution sparse matrix class * LocalMatrix. This class is designed for use in either serial @@ -47,7 +55,7 @@ namespace ParalutionWrappers * * @ingroup ParalutionWrappers * @ingroup Matrix - * @author Bruno Turcksin, 2013 + * @author Bruno Turcksin, 2013, 2014 */ template class SparseMatrix : public Subscriptor @@ -76,18 +84,14 @@ namespace ParalutionWrappers SparseMatrix(); /** - * Copy constructor. This constructor is only allowed to be called if - * the matrix to be copied is empty. This is for the same reason as for - * the SparsityPattern, see there fore the details. - * - * If you really want to copy a whole matrix, you can do so by using the - * copy_from() function. + * Copy constructor. If @p copy_backend is set to false, the copied sparse + * matrix stays on the host/device where it is created. Otherwise the copied + * sparse matrix is moved to the host/device of the given vector. */ - //TODO - //SparseMarix(const SparseMatrix &sparse_matrix); + SparseMatrix(const SparseMatrix &sparse_matrix, bool copy_backend = false); /** - * Constructo. Takes the given matrix sparsity structure to represent + * Constructor. Takes the given matrix sparsity structure to represent * the sparsity pattern of this matrix. You can change the sparsity * pattern later on by calling the reinit(const SparsityPattern&) * function. @@ -122,7 +126,7 @@ namespace ParalutionWrappers /** * This function converts the underlying SparseMatrix to - * Paralution::LocalMatrix. This function frees the SparseMatrix. + * Paralution::LocalMatrix. This function frees the dealii::SparseMatrix. */ void convert_to_paralution_csr(); //@} @@ -142,6 +146,17 @@ namespace ParalutionWrappers * of dimension $m \times n$. */ size_type n() const; + + /** + * This function returns true if the underlying matrix is a + * paralution::LocalMatrix. + */ + bool is_paralution_matrix() const; + + /** + * Return the format of the Paralution::LocalMatrix. + */ + matrix_format get_matrix_format() const; //@} /** * @name 3: Modifying entries @@ -254,7 +269,7 @@ namespace ParalutionWrappers /** * Same function as before, but now including the possibility to use - * rectaangular full_matrices and different local-to-global indexing on + * rectangular full_matrices and different local-to-global indexing on * rows and columns, respectively. * * This function only works on the host. @@ -301,9 +316,27 @@ namespace ParalutionWrappers const bool elide_zero_values = true, const bool col_indices_are_sorted = false); + /** + * Transpose the matrix. This function can only be called if the underlying + * is a Paralution::LocalMatrix. + */ + void transpose(); + + /** + * Copy @p sparse_matrix. + */ + void copy_from(const SparseMatrix &sparse_matrix); + + /** + * Copy @p sparse_matrix. If the underlying matrix is a + * Paralution::LocalMatrix, the functions returns immediately and performs + * the asynchronous transder in the background. + */ + void copy_from_async(const SparseMatrix &sparse_matrix); + //@} /** - * @name 4: Access to underlying Paralution data and move data to + * @name 4: Access to underlying data and move data to * accelerator/host */ /** @@ -319,21 +352,53 @@ namespace ParalutionWrappers void move_to_host(); /** - * Return a constant reference to the underlying Paralution LocalMatrix + * Move the SparseMatrix to the accelerator. The function returns + * immediately and performs the asynchronous transfer in the background. + * This function should only be called after convert_to_paralution_csr. + */ + void move_to_accelerator_async(); + + /** + * Move the SparseMatrix to the host. The function returns immediately and + * performs the asynchronous transfer in the background. This function + * should only be called after convert_to_paralution_csr. + */ + void move_to_host_async(); + + /** + * Synchronize the code when move_to_host_async or move_to_accelerator_async + * is used. + */ + void sync(); + + /** + * Return a constant reference to the underlying dealii::SparseMatrix. + */ + ::dealii::SparseMatrix const &dealii_matrix() const; + + /** + * Return a reference to the underlying dealii::SparseMatrix. + */ + ::dealii::SparseMatrix& dealii_matrix(); + + /** + * Return a constant reference to the underlying Paralution::LocalMatrix * data. */ paralution::LocalMatrix const ¶lution_matrix() const; /** - * Return a reference to the underlying Paralution LocalMatrix data. + * Return a reference to the underlying Paralution::LocalMatrix data. */ paralution::LocalMatrix& paralution_matrix(); //@} private : /** - * This flag is true if local_matrix is used. It becomes true after - * convert_to_paralution_csr has been called. + * This flag is true if @p local_matrix is used. It becomes true after + * convert_to_paralution_csr has been called, i.e. after the @p + * sparse_matrix as be freed and the paralution::LocalMatrix has been + * created. */ bool is_local_matrix; @@ -411,6 +476,22 @@ namespace ParalutionWrappers + template + inline bool SparseMatrix::is_paralution_matrix() const + { + return is_local_matrix; + } + + + + template + inline matrix_format SparseMatrix::get_matrix_format() const + { + return local_matrix.get_format(); + } + + + template inline void SparseMatrix::set(const size_type i, const size_type j, @@ -518,6 +599,46 @@ namespace ParalutionWrappers + template + inline void SparseMatrix::move_to_accelerator_async() + { + local_matrix.MoveToAcceleratorAsync(); + } + + + + template + inline void SparseMatrix::move_to_host_async() + { + local_matrix.MoveToHostAsync(); + } + + + + template + inline void SparseMatrix::sync() + { + local_matrix.Sync(); + } + + + + template + inline ::dealii::SparseMatrix const &SparseMatrix::dealii_matrix() const + { + return sparse_matrix; + } + + + + template + inline ::dealii::SparseMatrix& SparseMatrix::dealii_matrix() + { + return sparse_matrix; + } + + + template inline paralution::LocalMatrix const &SparseMatrix::paralution_matrix() const { diff --git a/deal.II/include/deal.II/lac/paralution_vector.h b/deal.II/include/deal.II/lac/paralution_vector.h index 16f553bf96..c7b0b72926 100644 --- a/deal.II/include/deal.II/lac/paralution_vector.h +++ b/deal.II/include/deal.II/lac/paralution_vector.h @@ -54,7 +54,6 @@ namespace ParalutionWrappers * @ingroup Vectors * @author Bruno Turcksin, 2013, 2014 */ - //TODO: lots of functions are missing template class Vector : public Subscriptor { @@ -93,7 +92,7 @@ namespace ParalutionWrappers /** * Copy constructor. Set the dimension to that of the given vector, and - * copies all the elements. If copy_backend is set to false, the copied + * copies all the elements. If @p copy_backend is set to false, the copied * vector stays on the host/device where it is created. Otherwise the * copied vector is moved to the host/device of the given vector. */ diff --git a/deal.II/source/lac/paralution_sparse_matrix.cc b/deal.II/source/lac/paralution_sparse_matrix.cc index 38d924f3d2..7f20e32150 100644 --- a/deal.II/source/lac/paralution_sparse_matrix.cc +++ b/deal.II/source/lac/paralution_sparse_matrix.cc @@ -1,7 +1,7 @@ // --------------------------------------------------------------------- // $Id: paralution_sparse_matrix.cc 31567 2013-11-06 18:01:36Z turcksin $ // -// Copyright (C) 2013 by the deal.II authors +// Copyright (C) 2013, 2014 by the deal.II authors // // This file is part of the deal.II library. // @@ -22,6 +22,25 @@ DEAL_II_NAMESPACE_OPEN namespace ParalutionWrappers { + template + SparseMatrix::SparseMatrix(const SparseMatrix &sparse_matrix, bool copy_backend) + { + is_local_matrix = sparse_matrix.is_paralution_matrix(); + // This is costly but copy_from is used to have a consistent behavior between + // sparse_matrix and local_matrix. + if (is_local_matrix==false) + sparse_matrix.copy_from(sparse_matrix.sparse_matrix()); + else + { + if (copy_backend==false) + local_matrix.CopyFrom(sparse_matrix.paralution_matrix()); + else + local_matrix.CloneFrom(sparse_matrix.paralution_matrix()); + } + } + + + template void SparseMatrix::convert_to_paralution_csr() { @@ -78,6 +97,46 @@ namespace ParalutionWrappers sparse_matrix.clear(); is_local_matrix = true; } + + + + template + void SparseMatrix::transpose() + { + if (is_local_matrix==false) + { + AssertThrow(false, + ExcMessage("Transpose cannot be used if the underlying matrix is a dealii::SparseMatrix.")); + } + else + { + local_matrix.Transpose(); + } + } + + + + template + void SparseMatrix::copy_from(const SparseMatrix &sparse_matrix) + { + is_local_matrix = sparse_matrix.is_paralution_matrix(); + if (is_local_matrix==false) + sparse_matrix.copy_from(sparse_matrix.sparse_matrix()); + else + local_matrix.CopyFrom(sparse_matrix.paralution_matrix()); + } + + + + template + void SparseMatrix::copy_from_async(const SparseMatrix &sparse_matrix) + { + is_local_matrix = sparse_matrix.is_paralution_matrix(); + if (is_local_matrix==false) + sparse_matrix.copy_from(sparse_matrix.sparse_matrix()); + else + local_matrix.CopyFromAsync(sparse_matrix.paralution_matrix()); + } } // Explicit instantiations -- 2.39.5