Commit c2711bda authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

#936 Petsc: add MatLoad functionality (and thus also the possibility to dump...

#936 Petsc: add MatLoad functionality (and thus also the possibility to dump matrices in binary format, as only those might be loaded).
parent b46fcd2d
......@@ -11,6 +11,7 @@
#include "Utilities/Exceptions/PrintAndAbort.hpp"
#include "ThirdParty/IncludeWithoutWarning/Seldon/Seldon.hpp"
#include "ThirdParty/Wrappers/Petsc/Matrix/Matrix.hpp"
#include "Core/InitHappyHeart.hpp"
......@@ -48,9 +49,16 @@ int main(int argc, char ** argv)
const auto& input_parameter_data = happy_heart.GetInputParameterList();
const auto& mpi = happy_heart.GetMpi();
// Path to the matrix to convert, hardcoded at the moment.
const std::string matrix_path = "/Users/sebastien/Desktop/mass_matrix.binary.hhdata";
try
{
Wrappers::Petsc::Matrix matrix(mpi,
matrix_path,
__FILE__, __LINE__);
std::cout << matrix.GetProcessorWiseSize(__FILE__, __LINE__).first << std::endl;
}
......
......@@ -32,7 +32,6 @@ namespace HappyHeart
{
Matrix::Matrix()
: parent(),
petsc_matrix_(PETSC_NULL),
......@@ -40,6 +39,30 @@ namespace HappyHeart
{ }
Matrix::Matrix(const Mpi& mpi,
const std::string& binary_file,
const char* invoking_file, int invoking_line)
: parent(),
petsc_matrix_(PETSC_NULL),
do_petsc_destroy_(true)
{
const auto communicator = mpi.GetCommunicator();
Viewer viewer(mpi,
binary_file,
FILE_MODE_READ,
invoking_file, invoking_line);
int error_code = MatCreate(communicator, &petsc_matrix_);
if (error_code)
throw ExceptionNS::Exception(error_code, "MatCreate", invoking_file, invoking_line);
error_code = MatLoad(petsc_matrix_, viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "MatLoad", invoking_file, invoking_line);
}
Matrix::Matrix(const Matrix& rhs)
: parent(rhs),
petsc_matrix_(PETSC_NULL),
......@@ -384,17 +407,31 @@ namespace HappyHeart
}
void Matrix::View(const Mpi& mpi, const std::string& output_file, const char* invoking_file, int invoking_line,
void Matrix::View(const Mpi& mpi,
const std::string& output_file,
const char* invoking_file, int invoking_line,
PetscViewerFormat format) const
{
Viewer viewer(mpi, output_file, format, invoking_file, invoking_line);
int error_code = MatView(Internal(), viewer.GetNonCstViewer());
int error_code = MatView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "MatView", invoking_file, invoking_line);
}
void Matrix::ViewBinary(const Mpi& mpi,
const std::string& output_file,
const char* invoking_file, int invoking_line) const
{
Viewer viewer(mpi, output_file, FILE_MODE_WRITE, invoking_file, invoking_line);
int error_code = MatView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "MatView", invoking_file, invoking_line);
}
void Matrix::GetRow(PetscInt row_index,
std::vector<PetscInt>& row_content_position_list,
......
......@@ -103,6 +103,16 @@ namespace HappyHeart
//! Constructor.
explicit Matrix();
/*!
* \brief Constructor from a file: load a matrix dumped with View() method.
*
* \param[in] binary_file File from which the matrix must be loaded. This file must be in binary format.
* \copydoc doxygen_hide_invoking_file_and_line
*/
explicit Matrix(const Mpi& mpi,
const std::string& binary_file,
const char* invoking_file, int invoking_line);
//! Destructor.
~Matrix();
......@@ -464,6 +474,19 @@ namespace HappyHeart
void View(const Mpi& mpi, const std::string& output_file, const char* invoking_file, int invoking_line,
PetscViewerFormat format = PETSC_VIEWER_DEFAULT) const;
/*!
* \brief Wrapper over MatView in the case the viewer is a binary file.
*
* \param[in] output_file File into which the vector content will be written.
* \param[in] invoking_file File that invoked the function or class; usually __FILE__.
* \param[in] invoking_line File that invoked the function or class; usually __LINE__.
*/
void ViewBinary(const Mpi& mpi,
const std::string& output_file,
const char* invoking_file, int invoking_line) const;
/*!
* \brief Wrapper over MatNorm.
*
......
......@@ -460,7 +460,7 @@ namespace HappyHeart
{
Viewer viewer(mpi, output_file, format, invoking_file, invoking_line);
int error_code = VecView(Internal(), viewer.GetNonCstViewer());
int error_code = VecView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "VecView", invoking_file, invoking_line);
}
......
......@@ -21,7 +21,8 @@ namespace HappyHeart
Viewer::Viewer(const Mpi& mpi, const std::string& output_file,
Viewer::Viewer(const Mpi& mpi,
const std::string& ascii_file,
PetscViewerFormat format,
const char* invoking_file, int invoking_line)
: viewer_(nullptr)
......@@ -31,12 +32,12 @@ namespace HappyHeart
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerCreate", invoking_file, invoking_line);
if (format == PETSC_VIEWER_ASCII_MATLAB && !Utilities::String::EndsWith(output_file, ".m"))
throw ExceptionNS::WrongMatlabExtension(output_file, invoking_file, invoking_line);
if (format == PETSC_VIEWER_ASCII_MATLAB && !Utilities::String::EndsWith(ascii_file, ".m"))
throw ExceptionNS::WrongMatlabExtension(ascii_file, invoking_file, invoking_line);
error_code = PetscViewerASCIIOpen(mpi.GetCommunicator(),
output_file.c_str(),
&viewer_);
ascii_file.c_str(),
&viewer_);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerASCIIOpen", invoking_file, invoking_line);
......@@ -48,6 +49,27 @@ namespace HappyHeart
Viewer::Viewer(const Mpi& mpi,
const std::string& binary_file,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line)
: viewer_(nullptr)
{
int error_code = PetscViewerCreate(mpi.GetCommunicator(), &viewer_);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerCreate", invoking_file, invoking_line);
error_code = PetscViewerBinaryOpen(mpi.GetCommunicator(),
binary_file.c_str(),
file_mode,
&viewer_);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerBinaryOpen", invoking_file, invoking_line);
}
Viewer::~Viewer()
{
......@@ -58,7 +80,7 @@ namespace HappyHeart
}
PetscViewer& Viewer::GetNonCstViewer()
PetscViewer& Viewer::GetUnderlyingPetscObject()
{
assert(!(!viewer_));
return viewer_;
......
......@@ -43,10 +43,10 @@ namespace HappyHeart
///@{
/*!
* \brief Constructor for a file.
* \brief Constructor for an ascii file.
*
* \copydetails doxygen_hide_mpi_param
* \param[in] output_file File to which the Petsc object will be written.
* \param[in] ascii_file File to which the Petsc object will be associated.
* \param[in] invoking_file The name of the file in which the funciton is called.
* Usually __FILE__, although it might be the another given by the function that called
* Viewer (for instance if the Viewer is for MatView it might be better to obtain the place
......@@ -56,7 +56,36 @@ namespace HappyHeart
* formats available; relevant ones so far are PETSC_VIEWER_DEFAULT and PETSC_VIEWER_ASCII_MATLAB.
*
*/
Viewer(const Mpi& mpi, const std::string& output_file, PetscViewerFormat format,
Viewer(const Mpi& mpi,
const std::string& ascii_file,
PetscViewerFormat format,
const char* invoking_file, int line);
/*!
* \class doxygen_hide_petsc_file_mode
*
* \param[in] file_mode From http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Viewer/PetscViewerBinaryOpen.html#PetscViewerBinaryOpen :
* - FILE_MODE_WRITE - create new file for binary output.
* - FILE_MODE_READ - open existing file for binary input.
* - FILE_MODE_APPEND - open existing file for binary output.
*/
/*!
* \brief Constructor for a binary file.
*
* \copydetails doxygen_hide_mpi_param
* \param[in] binary_file File to which the Petsc object will be associated.
* \param[in] invoking_file The name of the file in which the funciton is called.
* Usually __FILE__, although it might be the another given by the function that called
* Viewer (for instance if the Viewer is for MatView it might be better to obtain the place
* MatView was invoked rather than the internal line of this function where Viewer is created).
* \param[in] line Invoking line, usually __LINE__ (but see discussion above).
* \copydoc doxygen_hide_petsc_file_mode
*/
Viewer(const Mpi& mpi,
const std::string& binary_file,
PetscFileMode file_mode,
const char* invoking_file, int line);
......@@ -80,7 +109,7 @@ namespace HappyHeart
//! Access to the underlying viewer.
PetscViewer& GetNonCstViewer();
PetscViewer& GetUnderlyingPetscObject();
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment