Commit c0201986 authored by GILLES Sebastien's avatar GILLES Sebastien

#1422 PetscVector: create a new cpp file to hold constructors and init methods.

parent c5bf26be
......@@ -1446,6 +1446,7 @@
BECB2E0D213FFBAC0093CB05 /* SnesMacro.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BECB2E0A213FFBAC0093CB05 /* SnesMacro.hpp */; };
BECDB4B922C21749009BA8E2 /* PrintTypeName.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BECDB4B722C21749009BA8E2 /* PrintTypeName.hpp */; };
BECDB4BA22C21749009BA8E2 /* PrintTypeName.hxx in Headers */ = {isa = PBXBuildFile; fileRef = BECDB4B822C21749009BA8E2 /* PrintTypeName.hxx */; };
BECEF91F22DF2A0800D0DDE7 /* VectorInitMethods.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BECEF91E22DF2A0800D0DDE7 /* VectorInitMethods.cpp */; };
BED13353206519C800A3B0AA /* Penalization.hxx in Headers */ = {isa = PBXBuildFile; fileRef = BED13351206519C700A3B0AA /* Penalization.hxx */; };
BED13354206519C800A3B0AA /* Penalization.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BED13352206519C800A3B0AA /* Penalization.hpp */; };
BED793B11D1AD75900492784 /* HyperelasticLaw.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BED793AE1D1AD75900492784 /* HyperelasticLaw.hpp */; };
......@@ -4608,6 +4609,7 @@
BECDB4B622C21749009BA8E2 /* SourceList.cmake */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SourceList.cmake; sourceTree = "<group>"; };
BECDB4B722C21749009BA8E2 /* PrintTypeName.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = PrintTypeName.hpp; sourceTree = "<group>"; };
BECDB4B822C21749009BA8E2 /* PrintTypeName.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = PrintTypeName.hxx; sourceTree = "<group>"; };
BECEF91E22DF2A0800D0DDE7 /* VectorInitMethods.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VectorInitMethods.cpp; sourceTree = "<group>"; };
BED13351206519C700A3B0AA /* Penalization.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Penalization.hxx; sourceTree = "<group>"; };
BED13352206519C800A3B0AA /* Penalization.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Penalization.hpp; sourceTree = "<group>"; };
BED748E41906734200BAB761 /* main_test_ondomatic_numbering.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main_test_ondomatic_numbering.cpp; sourceTree = "<group>"; };
......@@ -10330,6 +10332,7 @@
isa = PBXGroup;
children = (
BEEFEF7C196ECCC000C80FF1 /* Vector.cpp */,
BECEF91E22DF2A0800D0DDE7 /* VectorInitMethods.cpp */,
BEEFEF7D196ECCC000C80FF1 /* Vector.hpp */,
BEEFEF7E196ECCC000C80FF1 /* Vector.hxx */,
BEEFEF79196ECCC000C80FF1 /* AccessVectorContent.cpp */,
......@@ -12699,6 +12702,7 @@
BEDEB9251C3C073100B1C71B /* Mumps.cpp in Sources */,
BE90E1B21A24929A00CCAFDE /* AccessVectorContent.cpp in Sources */,
BE41A8D11A24AA7F004E4312 /* Petsc.cpp in Sources */,
BECEF91F22DF2A0800D0DDE7 /* VectorInitMethods.cpp in Sources */,
BE90E1AF1A24929A00CCAFDE /* Vector.cpp in Sources */,
BE110EA31E11514200D2D2C8 /* VectorHelper.cpp in Sources */,
BE90E1711A24926E00CCAFDE /* String.cpp in Sources */,
......@@ -13,6 +13,7 @@ target_sources(${MOREFEM_UTILITIES}
"${CMAKE_CURRENT_LIST_DIR}/AccessGhostContent.cpp"
"${CMAKE_CURRENT_LIST_DIR}/AccessVectorContent.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Vector.cpp"
"${CMAKE_CURRENT_LIST_DIR}/VectorInitMethods.cpp"
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/AccessGhostContent.hpp"
......
......@@ -24,7 +24,7 @@
#include "ThirdParty/Wrappers/Petsc/Vector/Internal/CheckUpdateGhostManager.hpp"
#include "ThirdParty/Wrappers/Petsc/Exceptions/Petsc.hpp"
#include "ThirdParty/Wrappers/Mpi/Mpi.hpp"
# include "ThirdParty/Wrappers/Petsc/Print.hpp"
#include "ThirdParty/Wrappers/Petsc/Print.hpp"
namespace MoReFEM
......@@ -55,193 +55,15 @@ namespace MoReFEM
#endif // MOREFEM_CHECK_UPDATE_GHOSTS_CALL_RELEVANCE
Vector::Vector()
: petsc_vector_(PETSC_NULL),
do_petsc_destroy_(false) // Nothing is assigned yet!
{ }
Vector::Vector(const Vec& petsc_vector, bool do_destroy_petsc)
: petsc_vector_(petsc_vector),
do_petsc_destroy_(do_destroy_petsc)
{
assert(petsc_vector != PETSC_NULL);
}
Vector::Vector(const Vector& rhs)
: petsc_vector_(PETSC_NULL),
do_petsc_destroy_(rhs.do_petsc_destroy_)
{
CompleteCopy(rhs, __FILE__, __LINE__);
}
void Vector::InitFromProgramWiseBinaryFile(const Mpi& mpi,
unsigned int processor_wise_size,
unsigned int program_wise_size,
const std::vector<PetscInt>& ghost_padding,
const std::string& binary_file,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL);
assert(processor_wise_size <= program_wise_size && "Detect easily invalid order in arguments...");
do_petsc_destroy_ = true;
Viewer viewer(mpi,
binary_file,
FILE_MODE_READ,
invoking_file, invoking_line);
if (mpi.Nprocessor<int>() > 1)
{
InitMpiVectorWithGhost(mpi,
processor_wise_size,
program_wise_size,
ghost_padding,
invoking_file, invoking_line);
int error_code = VecLoad(petsc_vector_, viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "VecLoad", invoking_file, invoking_line);
UpdateGhosts(invoking_file, invoking_line);
}
else
{
assert(processor_wise_size == program_wise_size);
assert(ghost_padding.empty());
InitSequentialVector(mpi,
processor_wise_size,
invoking_file, invoking_line);
int error_code = VecLoad(petsc_vector_, viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "VecLoad", invoking_file, invoking_line);
}
}
Vector::~Vector()
{
if (do_petsc_destroy_)
{
assert(petsc_vector_ != PETSC_NULL);
int error_code = VecDestroy(&petsc_vector_);
assert(!error_code && "Error in Vec destruction."); // no exception in destructors!
static_cast<void>(error_code); // to avoid arning in release compilation.
}
}
void Swap(Wrappers::Petsc::Vector& A, Wrappers::Petsc::Vector& B)
{
using std::swap;
swap(A.do_petsc_destroy_, B.do_petsc_destroy_);
swap(A.petsc_vector_, B.petsc_vector_);
}
void Vector::InitSequentialVector(const Mpi& mpi,
unsigned int size,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
int error_code = VecCreateSeq(mpi.GetCommunicator(), static_cast<PetscInt>(size), &petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateSeq", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitMpiVector(const Mpi& mpi,
unsigned int local_size,
unsigned int global_size,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(local_size <= global_size && "If not, either sequential mode or the local and global were "
"provided in the wrong order.");
int error_code = VecCreateMPI(mpi.GetCommunicator(),
static_cast<PetscInt>(local_size),
static_cast<PetscInt>(global_size),
&petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateMPI", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitMpiVectorWithGhost(const Mpi& mpi,
unsigned int local_size,
unsigned int global_size,
const std::vector<PetscInt>& ghost_padding,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(local_size <= global_size && "If not, either sequential mode or the local and global were "
"provided in the wrong order.");
const PetscInt Nghost = static_cast<PetscInt>(ghost_padding.size());
ghost_padding_ = ghost_padding;
int error_code = VecCreateGhost(mpi.GetCommunicator(),
static_cast<PetscInt>(local_size),
static_cast<PetscInt>(global_size),
Nghost,
ghost_padding.data(),
&petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateGhost", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitSequentialFromFile(const Mpi& mpi,
const std::string& file,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(mpi.Nprocessor<int>() == 1 && "This method assumes sequential case!");
std::ifstream stream;
FilesystemNS::File::Read(stream, file, invoking_file, invoking_line);
std::string line;
std::vector<double> value_list;
while (getline(stream, line))
{
// All lines are expected to be one value.
value_list.push_back(stod(line));
}
// Now Init the vector with the appropriate size.
const unsigned int Nvalue = static_cast<unsigned int>(value_list.size());
InitSequentialVector(mpi, Nvalue, invoking_file, invoking_line);
// And fill it with the values.
AccessVectorContent<Utilities::Access::read_and_write> content(*this, invoking_file, invoking_line);
for (auto i = 0u; i < Nvalue; ++i)
content[i] = value_list[static_cast<std::size_t>(i)];
}
void Vector::DuplicateLayout(const Vector& original, const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
......
/*!
//
// \file
//
//
// Created by Sebastien Gilles <sebastien.gilles@inria.fr> on the Fri, 20 Sep 2013 14:51:15 +0200
// Copyright (c) Inria. All rights reserved.
//
// \ingroup ThirdPartyGroup
// \addtogroup ThirdPartyGroup
// \{
*/
#include <cassert>
#include <cmath>
#include "Utilities/Filesystem/File.hpp"
#include "Utilities/Exceptions/PrintAndAbort.hpp"
#include "ThirdParty/Wrappers/Petsc/Viewer.hpp"
#include "ThirdParty/Wrappers/Petsc/Vector/Vector.hpp"
#include "ThirdParty/Wrappers/Petsc/Vector/AccessGhostContent.hpp"
#include "ThirdParty/Wrappers/Petsc/Exceptions/Petsc.hpp"
#include "ThirdParty/Wrappers/Mpi/Mpi.hpp"
#include "ThirdParty/Wrappers/Petsc/Print.hpp"
namespace MoReFEM
{
namespace Wrappers
{
namespace Petsc
{
Vector::Vector()
: petsc_vector_(PETSC_NULL),
do_petsc_destroy_(false) // Nothing is assigned yet!
{ }
Vector::Vector(const Vec& petsc_vector, bool do_destroy_petsc)
: petsc_vector_(petsc_vector),
do_petsc_destroy_(do_destroy_petsc)
{
assert(petsc_vector != PETSC_NULL);
}
Vector::Vector(const Vector& rhs)
: petsc_vector_(PETSC_NULL),
do_petsc_destroy_(rhs.do_petsc_destroy_)
{
CompleteCopy(rhs, __FILE__, __LINE__);
}
void Vector::InitFromProgramWiseBinaryFile(const Mpi& mpi,
unsigned int processor_wise_size,
unsigned int program_wise_size,
const std::vector<PetscInt>& ghost_padding,
const std::string& binary_file,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL);
assert(processor_wise_size <= program_wise_size && "Detect easily invalid order in arguments...");
do_petsc_destroy_ = true;
Viewer viewer(mpi,
binary_file,
FILE_MODE_READ,
invoking_file, invoking_line);
if (mpi.Nprocessor<int>() > 1)
{
InitMpiVectorWithGhost(mpi,
processor_wise_size,
program_wise_size,
ghost_padding,
invoking_file, invoking_line);
int error_code = VecLoad(petsc_vector_, viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "VecLoad", invoking_file, invoking_line);
UpdateGhosts(invoking_file, invoking_line);
}
else
{
assert(processor_wise_size == program_wise_size);
assert(ghost_padding.empty());
InitSequentialVector(mpi,
processor_wise_size,
invoking_file, invoking_line);
int error_code = VecLoad(petsc_vector_, viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "VecLoad", invoking_file, invoking_line);
}
}
Vector::~Vector()
{
if (do_petsc_destroy_)
{
assert(petsc_vector_ != PETSC_NULL);
int error_code = VecDestroy(&petsc_vector_);
assert(!error_code && "Error in Vec destruction."); // no exception in destructors!
static_cast<void>(error_code); // to avoid arning in release compilation.
}
}
void Vector::InitSequentialVector(const Mpi& mpi,
unsigned int size,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
int error_code = VecCreateSeq(mpi.GetCommunicator(), static_cast<PetscInt>(size), &petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateSeq", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitMpiVector(const Mpi& mpi,
unsigned int local_size,
unsigned int global_size,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(local_size <= global_size && "If not, either sequential mode or the local and global were "
"provided in the wrong order.");
int error_code = VecCreateMPI(mpi.GetCommunicator(),
static_cast<PetscInt>(local_size),
static_cast<PetscInt>(global_size),
&petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateMPI", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitMpiVectorWithGhost(const Mpi& mpi,
unsigned int local_size,
unsigned int global_size,
const std::vector<PetscInt>& ghost_padding,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(local_size <= global_size && "If not, either sequential mode or the local and global were "
"provided in the wrong order.");
const PetscInt Nghost = static_cast<PetscInt>(ghost_padding.size());
ghost_padding_ = ghost_padding;
int error_code = VecCreateGhost(mpi.GetCommunicator(),
static_cast<PetscInt>(local_size),
static_cast<PetscInt>(global_size),
Nghost,
ghost_padding.data(),
&petsc_vector_);
if (error_code)
throw ExceptionNS::Exception(error_code, "VecCreateGhost", invoking_file, invoking_line);
do_petsc_destroy_ = true;
}
void Vector::InitSequentialFromFile(const Mpi& mpi,
const std::string& file,
const char* invoking_file, int invoking_line)
{
assert(petsc_vector_ == PETSC_NULL && "Should not be initialized when this method is called!");
assert(mpi.Nprocessor<int>() == 1 && "This method assumes sequential case!");
std::ifstream stream;
FilesystemNS::File::Read(stream, file, invoking_file, invoking_line);
std::string line;
std::vector<double> value_list;
while (getline(stream, line))
{
// All lines are expected to be one value.
value_list.push_back(stod(line));
}
// Now Init the vector with the appropriate size.
const unsigned int Nvalue = static_cast<unsigned int>(value_list.size());
InitSequentialVector(mpi, Nvalue, invoking_file, invoking_line);
// And fill it with the values.
AccessVectorContent<Utilities::Access::read_and_write> content(*this, invoking_file, invoking_line);
for (auto i = 0u; i < Nvalue; ++i)
content[i] = value_list[static_cast<std::size_t>(i)];
}
} // namespace Petsc
} // namespace Wrappers
} // namespace MoReFEM
/// @} // addtogroup ThirdPartyGroup
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