Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block matrix contribution #191

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Analysis/TPZAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#include "TPZSimpleTimer.h"
#include "pzelementgroup.h"
#include "TPZJacobiPrecond.h"
#include "pzmetis.h"

#ifdef WIN32
#include "pzsloan.h" // for TPZSloan
#endif
Expand Down Expand Up @@ -91,6 +93,7 @@ static TPZLogger loggerPrecond("pz.analysis.precondgraph");

#endif


using namespace std;

void TPZAnalysis::SetStructuralMatrix(TPZStructMatrix &strmatrix){
Expand Down
8 changes: 8 additions & 0 deletions External/pzmetis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ TPZMetis::TPZMetis() : TPZRenumbering()
#ifndef PZ_USING_METIS
PZError<<"TPZMetis depends on the Metis library\n";
PZError<<"Please reconfigure NeoPZ library using:\n";
<<<<<<< HEAD
PZError<<"USING_METIS=ON"<<std::endl;
// DebugStop();
||||||| 37a509e76
PZError<<"USING_METIS=ON"<<std::endl;
DebugStop();
=======
PZError<<"PZ_USING_METIS=ON"<<std::endl;
DebugStop();
#endif
>>>>>>> develop
}

void TPZMetis::Print(std::ostream &out,char * title) {
Expand Down
20 changes: 20 additions & 0 deletions Material/TPZMatCombinedSpaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ int TPZMatCombinedSpacesT<TVar>::IntegrationRuleOrder(const TPZVec<int>& elPMaxO
return intOrder;
}

template<class TVar>
int TPZMatCombinedSpacesT<TVar>::IntegrationRuleOrderBC(const TPZVec<int>& elPMaxOrder) const
{
return IntegrationRuleOrder(elPMaxOrder);
}

template<class TVar>
int TPZMatCombinedSpacesT<TVar>::ClassId() const{
return Hash("TPZMatCombinedSpacesT") ^
Expand Down Expand Up @@ -108,6 +114,20 @@ void TPZMatCombinedSpacesBC<TVar>::Contribute(const TPZVec<TPZMaterialDataT<TVar
fMatCombinedSpaces->ContributeBC(datavec,weight,ef,*tmp);
}

template<class TVar>
int TPZMatCombinedSpacesBC<TVar>::IntegrationRuleOrder(const TPZVec<int> &elPMaxOrder) const
{
return fMatCombinedSpaces->IntegrationRuleOrderBC(elPMaxOrder);
}

template<class TVar>
int TPZMatCombinedSpacesBC<TVar>::IntegrationRuleOrderBC(const TPZVec<int>& elPMaxOrder) const
{
PZError<<__PRETTY_FUNCTION__;
PZError<< "should not be called! Aborting...\n";
DebugStop();
}

/**
* @brief Fill material data parameter with necessary requirements for the
* Contribute method. Here, in base class, all requirements are considered
Expand Down
8 changes: 8 additions & 0 deletions Material/TPZMatCombinedSpaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class TPZMatCombinedSpaces : public virtual TPZSavable{
@param[in] elPMaxOrder maximum element order for each space
@return adjusted integration rule order*/
[[nodiscard]]virtual int IntegrationRuleOrder(const TPZVec<int> &elPMaxOrder) const = 0;

[[nodiscard]]virtual int IntegrationRuleOrderBC(const TPZVec<int> &elPMaxOrder) const = 0;

[[nodiscard]] int ClassId() const override;
};
Expand Down Expand Up @@ -147,6 +149,8 @@ class TPZMatCombinedSpacesT : public TPZMatCombinedSpaces{
int var, TPZVec<TVar> &sol) = 0;

[[nodiscard]] int IntegrationRuleOrder(const TPZVec<int> &elPMaxOrder) const override;

[[nodiscard]] int IntegrationRuleOrderBC(const TPZVec<int>& elPMaxOrder) const override;

[[nodiscard]] int ClassId() const override;

Expand All @@ -173,6 +177,10 @@ class TPZMatCombinedSpacesBC : public TPZMatCombinedSpacesT<TVar>{
void ContributeBC(const TPZVec<TPZMaterialDataT<TVar>> &datavec, REAL weight,
TPZFMatrix<TVar> &ek, TPZFMatrix<TVar> &ef,
TPZBndCondT<TVar> &bc) override;

[[nodiscard]] int IntegrationRuleOrder(const TPZVec<int> &elPMaxOrder) const override;

[[nodiscard]] int IntegrationRuleOrderBC(const TPZVec<int>& elPMaxOrder) const override;

/** @brief This method passes the call to the associated material.*/
virtual void Solution(const TPZVec<TPZMaterialDataT<TVar>> &datavec, int var,
Expand Down
111 changes: 109 additions & 2 deletions Matrix/pzfmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ void TPZFMatrix<std::complex<double> >::MultAdd(const TPZFMatrix<std::complex<do
&alpha, this->fElem, this->Rows(), x.fElem, x.Rows(), &beta, z.fElem, z.Rows());
}
}

#endif // USING_LAPACK

/**
Expand Down Expand Up @@ -907,7 +906,115 @@ void TPZFMatrix<TVar>::MultAdd(const TPZFMatrix<TVar> &x,const TPZFMatrix<TVar>
}
}
}

}

template <class TVar>
void TPZFMatrix<TVar>::AddContribution(int64_t i, int64_t j, const TPZFMatrix<TVar> & A, bool transpA, const TPZFMatrix<TVar>& B,
bool transpB, const TVar alpha)
{
if constexpr (!std::is_floating_point_v<RTVar>)
Error( "AddContribution must be used with a floating point type variable TVar!\n");

#ifdef USING_LAPACK
lapack_int nrows = A.Rows();
lapack_int ncols = B.Cols();
lapack_int seconddimA = A.Cols();
lapack_int seconddimB = B.Rows();
CBLAS_TRANSPOSE transposeA = CblasNoTrans;
CBLAS_TRANSPOSE transposeB = CblasNoTrans;

if (transpA)
{
nrows = A.Cols();
seconddimA = A.Rows();
transposeA = CblasTrans;
}
if (transpB)
{
ncols = B.Rows();
seconddimB = B.Cols();
transposeB = CblasTrans;
}

#else
int64_t nrows = A.Rows();
int64_t ncols = B.Cols();
int64_t seconddimA = A.Cols();
int64_t seconddimB = B.Rows();

if (transpA)
{
nrows = A.Cols();
seconddimA = A.Rows();
}
if (transpB)
{
ncols = B.Rows();
seconddimB = B.Cols();
}
#endif

#ifdef PZDEBUG
if (seconddimA != seconddimB)
{
std::stringstream out;
out << "AddContribution Matrices A and B have incompatible dimensions. ";
out << "Second dimension of A: " << seconddimA << ". Second dimension of B: " << seconddimB;
Error( out.str().c_str() );
return;
}
else if ((i+nrows > this->Rows()) || (j+ncols > this->Cols()))
{
std::stringstream out;
out << "AddContribution trying to add a submatrix out of the full matrix bounds. ";
out << "Submatrix bigger row: " << i+nrows << ". Full matrix size: " << this->Rows();
out << "Submatrix bigger column: " << j+ncols << ". Full matrix size: " << this->Cols();
Error( "AddContribution trying to add a submatrix out of the original matrix bounds" );
return;
}
#endif

#ifdef USING_LAPACK
TVar* ptr = &(*this)(i,j);
const TVar beta = 1.0;
if constexpr(std::is_same_v<TVar,double>){
cblas_dgemm(CblasColMajor, transposeA, transposeB, nrows, ncols, seconddimA, alpha, A.fElem, A.Rows(), B.fElem, B.Rows(), beta, ptr, this->Rows());
return;
}
else if constexpr(std::is_same_v<TVar,float>){
cblas_sgemm(CblasColMajor, transposeA, transposeB, nrows, ncols, seconddimA, alpha, A.fElem, A.Rows(), B.fElem, B.Rows(), beta, ptr, this->Rows());
return;
}
else if constexpr(std::is_same_v<TVar,std::complex<double>>){
cblas_zgemm(CblasColMajor, transposeA, transposeB, nrows, ncols, seconddimA, &alpha, A.fElem, A.Rows(), B.fElem, B.Rows(), &beta, ptr, this->Rows());
return;
}
else if constexpr(std::is_same_v<TVar,std::complex<float>>){
cblas_cgemm(CblasColMajor, transposeA, transposeB, nrows, ncols, seconddimA, &alpha, A.fElem, A.Rows(), B.fElem, B.Rows(), &beta, ptr, this->Rows());
return;
}

#endif //USING LAPACK

TPZFMatrix<TVar> tempA = A;
tempA *= alpha;
TPZFMatrix<TVar> tempB = B;
if (transpA)
tempA.Transpose();
if (transpB)
tempB.Transpose();

for (int64_t row = 0; row < nrows; row++)
{
for (int64_t col = 0; col < ncols; col++)
{
for (int64_t k = 0; k < seconddimA; k++)
{
(*this)(i+row,j+col) += tempA(row,k) * tempB(k,col);
}
}
}

}

/********************************/
Expand Down
13 changes: 13 additions & 0 deletions Matrix/pzfmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ class TPZFMatrix: public TPZMatrix<TVar> {
static void MultAdd(const TVar *ptr, int64_t rows, int64_t cols, const TPZFMatrix<TVar> &x,const TPZFMatrix<TVar> &y, TPZFMatrix<TVar> &z,
const TVar alpha=1.,const TVar beta = 0.,const int opt = 0);

/**
* @brief It computes this += alpha*(A * B), where A or B can be transposed.
* @param i Is the row of (this) where the first element of the matrices product should be added
* @param j Is the column of (this) where the first element of the matrices product should be added
* @param A Is A on the above operation
* @param transpA Indicates if A is Transpose or not
* @param B Is B on the above operation
* @param transpB Indicates if B is Transpose or not
* @param alpha Is alpha on the above operation
*/
virtual void AddContribution(int64_t i, int64_t j, const TPZFMatrix<TVar> & A, bool transpA, const TPZFMatrix<TVar>& B,
bool transpB, const TVar alpha = 1.0) override;

/**
* @name Generic operator with TVar type
* @{
Expand Down
7 changes: 7 additions & 0 deletions Matrix/pzmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ void TPZMatrix<TVar>::MultAdd(const TPZFMatrix<TVar> &x,const TPZFMatrix<TVar> &
}
}

template <class TVar>
void TPZMatrix<TVar>::AddContribution(int64_t i, int64_t j, const TPZFMatrix<TVar> & A, bool transpA, const TPZFMatrix<TVar>& B,
bool transpB, const TVar alpha)
{
Error( "Not implemented for this type of matrix\n" );
}

template<class TVar>
TVar TPZMatrix<TVar>::RowTimesVector(const int row, const TPZFMatrix<TVar> &v) const
{
Expand Down
6 changes: 4 additions & 2 deletions Matrix/pzmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "pzbasematrix.h"
#include "pzreal.h"


template<class TVar>
class TPZVec;
template<class TVar>
Expand Down Expand Up @@ -215,7 +214,10 @@ class TPZMatrix: public TPZBaseMatrix
virtual void MultAdd(const TPZFMatrix<TVar> & x,const TPZFMatrix<TVar>& y, TPZFMatrix<TVar>& z,
const TVar alpha=1., const TVar beta = 0., const int opt = 0) const;

virtual TVar RowTimesVector(const int row, const TPZFMatrix<TVar> &v) const;
virtual void AddContribution(int64_t i, int64_t j, const TPZFMatrix<TVar> & A, bool transpA, const TPZFMatrix<TVar>& B,
bool transpB, const TVar alpha = 1.0);

virtual TVar RowTimesVector(const int row, const TPZFMatrix<TVar> &v) const;

/** @brief Computes res = rhs - this * x */
virtual void Residual(const TPZFMatrix<TVar>& x,const TPZFMatrix<TVar>& rhs, TPZFMatrix<TVar>& res ) ;
Expand Down
Loading
Loading