45 #include "Epetra_MultiVector.h" 46 #include "Epetra_Comm.h" 47 #include "Epetra_LAPACK.h" 48 #include "Epetra_Distributor.h" 57 : Epetra_DistObject(Map,
"EpetraExt::BlockDiagMatrix"),
72 if(DataMap_)
delete DataMap_;
73 if(Pivots_)
delete [] Pivots_;
74 if(Values_)
delete [] Values_;
81 : Epetra_DistObject(Source),
87 DataMap_=
new Epetra_BlockMap(*Source.DataMap_);
89 Values_=
new double[DataMap_->NumMyPoints()];
95 void EpetraExt_BlockDiagMatrix::Allocate(){
99 int *ElementSize=
new int[NumBlocks];
101 for(
int i=0;i<NumBlocks;i++) {
103 DataSize+=ElementSize[i];
106 DataMap_=
new Epetra_BlockMap(-1,Map().NumMyElements(),Map().MyGlobalElements(),ElementSize,0,Map().
Comm());
108 delete [] ElementSize;
118 string dummy(
"multiply");
119 string ApplyMode=List_.get(
"apply mode",dummy);
120 if(ApplyMode==
string(
"multiply")) ApplyMode_=
AM_MULTIPLY;
121 else if(ApplyMode==
string(
"invert")) ApplyMode_=
AM_INVERT;
122 else if(ApplyMode==
string(
"factor")) ApplyMode_=
AM_FACTOR;
123 else EPETRA_CHK_ERR(-1);
132 for (
int i=0;i<MaxData;i++) Values_[i]=value;
144 if(!Map().SameAs(Source.Map()) || !DataMap_->SameAs(*Source.DataMap_))
145 throw ReportError(
"Maps of DistBlockMatrices incompatible in assignment",-1);
149 for(
int i=0;i<MaxData;i++) Values_[i]=Source.Values_[i];
150 for(
int i=0;i<Source.
NumMyUnknowns();i++) Pivots_[i]=Source.Pivots_[i];
153 ApplyMode_=Source.ApplyMode_;
154 HasComputed_=Source.HasComputed_;
171 for(
int i=0;i<NumBlocks;i++){
175 Values_[DataMap_->FirstPointInElement(i)]=1.0/Values_[DataMap_->FirstPointInElement(i)];
179 double * v=&Values_[DataMap_->FirstPointInElement(i)];
180 double d=1/(v[0]*v[3]-v[1]*v[2]);
189 LAPACK.GETRF(Nb,Nb,&Values_[DataMap_->FirstPointInElement(i)],Nb,&Pivots_[Map().FirstPointInElement(i)],&info);
190 if(info) EPETRA_CHK_ERR(-2);
196 int lwork=3*DataMap_->MaxMyElementSize();
197 std::vector<double> work(lwork);
198 for(
int i=0;i<NumBlocks;i++){
206 LAPACK.GETRI(Nb,&Values_[DataMap_->FirstPointInElement(i)],Nb,&Pivots_[Map().FirstPointInElement(i)],&work[0],&lwork,&info);
207 if(info) EPETRA_CHK_ERR(-3);
221 int NumVectors=X.NumVectors();
222 if(NumVectors!=Y.NumVectors())
230 const int *vlist=DataMap_->FirstPointInElementList();
231 const int *xlist=Map().FirstPointInElementList();
232 const int *blocksize=Map().ElementSizeList();
237 for(
int i=0;i<NumBlocks;i++){
241 for(
int j=0;j<NumVectors;j++){
244 Y[j][xidx0]=Values_[vidx0]*X[j][xidx0];
248 Y[j][xidx0 ]=Values_[vidx0 ]*X[j][xidx0] + Values_[vidx0+2]*X[j][xidx0+1];
249 Y[j][xidx0+1]=Values_[vidx0+1]*X[j][xidx0] + Values_[vidx0+3]*X[j][xidx0+1];
254 GEMV(
'N',Nb,Nb,1.0,&Values_[vidx0],Nb,&X[j][xidx0],0.0,&Y[j][xidx0]);
262 for(
int i=0;i<NumBlocks;i++){
266 for(
int j=0;j<NumVectors;j++){
269 Y[j][xidx0]=Values_[vidx0]*X[j][xidx0];
273 Y[j][xidx0 ]=Values_[vidx0 ]*X[j][xidx0] + Values_[vidx0+2]*X[j][xidx0+1];
274 Y[j][xidx0+1]=Values_[vidx0+1]*X[j][xidx0] + Values_[vidx0+3]*X[j][xidx0+1];
279 for(
int k=0;k<Nb;k++) Y[j][xidx0+k]=X[j][xidx0+k];
280 LAPACK.GETRS(
'N',Nb,1,&Values_[vidx0],Nb,&Pivots_[xidx0],&Y[j][xidx0],Nb,&info);
281 if(info) EPETRA_CHK_ERR(info);
295 int MyPID = DataMap_->Comm().MyPID();
296 int NumProc = DataMap_->Comm().NumProc();
298 for (
int iproc=0; iproc < NumProc; iproc++) {
300 int NumMyElements1 =DataMap_->NumMyElements();
301 int MaxElementSize1 = DataMap_->MaxElementSize();
302 int * MyGlobalElements1 = DataMap_->MyGlobalElements();
303 int * FirstPointInElementList1;
304 if (MaxElementSize1!=1) FirstPointInElementList1 = DataMap_->FirstPointInElementList();
308 os <<
" MyPID"; os <<
" ";
310 if (MaxElementSize1==1)
318 for (
int i=0; i < NumMyElements1; i++) {
319 for (
int ii=0; ii< DataMap_->ElementSize(i); ii++) {
322 os << MyPID; os <<
" ";
324 if (MaxElementSize1==1) {
325 os << MyGlobalElements1[i] <<
" ";
329 os << MyGlobalElements1[i]<<
"/" << ii <<
" ";
330 iii = FirstPointInElementList1[i]+ii;
341 Map().Comm().Barrier();
342 Map().Comm().Barrier();
343 Map().Comm().Barrier();
351 int EpetraExt_BlockDiagMatrix::CheckSizes(
const Epetra_SrcDistObject& Source){
352 return &Map() == &Source.Map();
358 int EpetraExt_BlockDiagMatrix::CopyAndPermute(
const Epetra_SrcDistObject& Source,
362 int * PermuteFromLIDs,
363 const Epetra_OffsetIndex * Indexor){
369 double *To = Values_;
371 int * ToFirstPointInElementList = 0;
372 int * FromFirstPointInElementList = 0;
373 int * FromElementSizeList = 0;
374 int MaxElementSize =
DataMap().MaxElementSize();
375 bool ConstantElementSize =
DataMap().ConstantElementSize();
377 if (!ConstantElementSize) {
378 ToFirstPointInElementList =
DataMap().FirstPointInElementList();
379 FromFirstPointInElementList = A.
DataMap().FirstPointInElementList();
380 FromElementSizeList = A.
DataMap().ElementSizeList();
390 if (MaxElementSize==1) {
392 NumSameEntries = NumSameIDs;
394 else if (ConstantElementSize) {
396 NumSameEntries = NumSameIDs * MaxElementSize;
400 NumSameEntries = FromFirstPointInElementList[NumSameIDs];
404 if (To==From) NumSameEntries = 0;
409 for (j=0; j<NumSameEntries; j++)
413 if (NumPermuteIDs>0) {
418 for (j=0; j<NumPermuteIDs; j++)
419 To[PermuteToLIDs[j]] = From[PermuteFromLIDs[j]];
424 for (j=0; j<NumPermuteIDs; j++) {
425 jj = MaxElementSize*PermuteToLIDs[j];
426 jjj = MaxElementSize*PermuteFromLIDs[j];
427 for (k=0; k<MaxElementSize; k++)
428 To[jj+k] = From[jjj+k];
435 for (j=0; j<NumPermuteIDs; j++) {
436 jj = ToFirstPointInElementList[PermuteToLIDs[j]];
437 jjj = FromFirstPointInElementList[PermuteFromLIDs[j]];
438 int ElementSize = FromElementSizeList[PermuteFromLIDs[j]];
439 for (k=0; k<ElementSize; k++)
440 To[jj+k] = From[jjj+k];
449 int EpetraExt_BlockDiagMatrix::PackAndPrepare(
const Epetra_SrcDistObject& Source,
457 Epetra_Distributor& Distor){
466 int MaxElementSize =
DataMap().MaxElementSize();
467 bool ConstantElementSize =
DataMap().ConstantElementSize();
469 int * FromFirstPointInElementList = 0;
470 int * FromElementSizeList = 0;
472 if (!ConstantElementSize) {
473 FromFirstPointInElementList = A.
DataMap().FirstPointInElementList();
474 FromElementSizeList = A.
DataMap().ElementSizeList();
477 SizeOfPacket = MaxElementSize * (int)
sizeof(
double);
479 if(NumExportIDs*SizeOfPacket>LenExports) {
480 if (LenExports>0)
delete [] Exports;
481 LenExports = NumExportIDs*SizeOfPacket;
482 Exports =
new char[LenExports];
487 if (NumExportIDs>0) {
488 ptr = (
double *) Exports;
491 if (MaxElementSize==1)
for (j=0; j<NumExportIDs; j++) *ptr++ = From[ExportLIDs[j]];
494 else if (ConstantElementSize) {
495 for (j=0; j<NumExportIDs; j++) {
496 jj = MaxElementSize*ExportLIDs[j];
497 for (k=0; k<MaxElementSize; k++)
504 SizeOfPacket = MaxElementSize;
505 for (j=0; j<NumExportIDs; j++) {
506 ptr = (
double *) Exports + j*SizeOfPacket;
507 jj = FromFirstPointInElementList[ExportLIDs[j]];
508 int ElementSize = FromElementSizeList[ExportLIDs[j]];
509 for (k=0; k<ElementSize; k++)
521 int EpetraExt_BlockDiagMatrix::UnpackAndCombine(
const Epetra_SrcDistObject& Source,
527 Epetra_Distributor& Distor,
528 Epetra_CombineMode CombineMode,
529 const Epetra_OffsetIndex * Indexor){
536 if( CombineMode != Add
537 && CombineMode != Zero
538 && CombineMode != Insert
539 && CombineMode != Average
540 && CombineMode != AbsMax )
543 if (NumImportIDs<=0)
return(0);
545 double * To = Values_;
546 int MaxElementSize =
DataMap().MaxElementSize();
547 bool ConstantElementSize =
DataMap().ConstantElementSize();
549 int * ToFirstPointInElementList = 0;
550 int * ToElementSizeList = 0;
552 if (!ConstantElementSize) {
553 ToFirstPointInElementList =
DataMap().FirstPointInElementList();
554 ToElementSizeList =
DataMap().ElementSizeList();
560 ptr = (
double *) Imports;
563 if (MaxElementSize==1) {
565 if (CombineMode==Add)
566 for (j=0; j<NumImportIDs; j++) To[ImportLIDs[j]] += *ptr++;
567 else if(CombineMode==Insert)
568 for (j=0; j<NumImportIDs; j++) To[ImportLIDs[j]] = *ptr++;
569 else if(CombineMode==AbsMax)
570 for (j=0; j<NumImportIDs; j++) {
571 To[ImportLIDs[j]] = EPETRA_MAX( To[ImportLIDs[j]],std::abs(*ptr));
576 else if(CombineMode==Average)
577 for (j=0; j<NumImportIDs; j++) {To[ImportLIDs[j]] += *ptr++; To[ImportLIDs[j]] /= 2;}
582 else if (ConstantElementSize) {
584 if (CombineMode==Add) {
585 for (j=0; j<NumImportIDs; j++) {
586 jj = MaxElementSize*ImportLIDs[j];
587 for (k=0; k<MaxElementSize; k++)
591 else if(CombineMode==Insert) {
592 for (j=0; j<NumImportIDs; j++) {
593 jj = MaxElementSize*ImportLIDs[j];
594 for (k=0; k<MaxElementSize; k++)
598 else if(CombineMode==AbsMax) {
599 for (j=0; j<NumImportIDs; j++) {
600 jj = MaxElementSize*ImportLIDs[j];
601 for (k=0; k<MaxElementSize; k++) {
602 To[jj+k] = EPETRA_MAX( To[jj+k], std::abs(*ptr));
609 else if(CombineMode==Average) {
610 for (j=0; j<NumImportIDs; j++) {
611 jj = MaxElementSize*ImportLIDs[j];
612 for (k=0; k<MaxElementSize; k++)
613 { To[jj+k] += *ptr++; To[jj+k] /= 2;}
622 SizeOfPacket = MaxElementSize;
624 if (CombineMode==Add) {
625 for (j=0; j<NumImportIDs; j++) {
626 ptr = (
double *) Imports + j*SizeOfPacket;
627 jj = ToFirstPointInElementList[ImportLIDs[j]];
628 int ElementSize = ToElementSizeList[ImportLIDs[j]];
629 for (k=0; k<ElementSize; k++)
633 else if(CombineMode==Insert){
634 for (j=0; j<NumImportIDs; j++) {
635 ptr = (
double *) Imports + j*SizeOfPacket;
636 jj = ToFirstPointInElementList[ImportLIDs[j]];
637 int ElementSize = ToElementSizeList[ImportLIDs[j]];
638 for (k=0; k<ElementSize; k++)
642 else if(CombineMode==AbsMax){
643 for (j=0; j<NumImportIDs; j++) {
644 ptr = (
double *) Imports + j*SizeOfPacket;
645 jj = ToFirstPointInElementList[ImportLIDs[j]];
646 int ElementSize = ToElementSizeList[ImportLIDs[j]];
647 for (k=0; k<ElementSize; k++) {
648 To[jj+k] = EPETRA_MAX( To[jj+k], std::abs(*ptr));
655 else if(CombineMode==Average) {
656 for (j=0; j<NumImportIDs; j++) {
657 ptr = (
double *) Imports + j*SizeOfPacket;
658 jj = ToFirstPointInElementList[ImportLIDs[j]];
659 int ElementSize = ToElementSizeList[ImportLIDs[j]];
660 for (k=0; k<ElementSize; k++)
661 { To[jj+k] += *ptr++; To[jj+k] /= 2;}
virtual const Epetra_BlockMap & DataMap() const
Returns the Epetra_BlockMap object with the distribution of underlying values.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y...
int NumMyBlocks() const
Returns the number of local blocks.
double * Values() const
Returns a pointer to the array containing the blocks.
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
int NumMyUnknowns() const
Returns the number of local unknowns.
EpetraExt_BlockDiagMatrix & operator=(const EpetraExt_BlockDiagMatrix &Source)
= Operator.
virtual ~EpetraExt_BlockDiagMatrix()
Destructor.
EpetraExt_BlockDiagMatrix: A class for storing distributed block matrices.
virtual void Print(ostream &os) const
Print method.
int BlockSize(int LID) const
Returns the size of the given block.
int NumData() const
Returns the size of the total Data block.
EpetraExt_BlockDiagMatrix(const Epetra_BlockMap &Map, bool zero_out=true)
Constructor - This map is the map of the vector this can be applied to.
virtual int Compute()
Computes the inverse / factorization if such is set on the list.
virtual int SetParameters(Teuchos::ParameterList &List)
SetParameters.
int DataSize(int LID) const
Returns the size of the data in the given block.
void PutScalar(double value)
PutScalar function.