1*29bbc08cSBarry Smith /*$Id: axpy.c,v 1.47 2000/05/28 15:21:54 bsmith Exp bsmith $*/ 26f79c3a4SBarry Smith 3e090d566SSatish Balay #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 46f79c3a4SBarry Smith 55615d1e5SSatish Balay #undef __FUNC__ 6b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatAXPY" 706be10caSBarry Smith /*@ 821c89e3eSBarry Smith MatAXPY - Computes Y = a*X + Y. 96f79c3a4SBarry Smith 10fee21e36SBarry Smith Collective on Mat 11fee21e36SBarry Smith 1298a79cdbSBarry Smith Input Parameters: 1398a79cdbSBarry Smith + X, Y - the matrices 1498a79cdbSBarry Smith - a - the scalar multiplier 1598a79cdbSBarry Smith 16e182c471SBarry Smith Contributed by: Matthew Knepley 17d4bb536fSBarry Smith 182860a424SLois Curfman McInnes Notes: 192860a424SLois Curfman McInnes Since the current implementation of MatAXPY() uses MatGetRow() to access 202860a424SLois Curfman McInnes matrix data, efficiency is somewhat limited. 212860a424SLois Curfman McInnes 222860a424SLois Curfman McInnes Level: intermediate 232860a424SLois Curfman McInnes 249cf4f1e8SLois Curfman McInnes .keywords: matrix, add 25d4bb536fSBarry Smith 262860a424SLois Curfman McInnes .seealso: MatAYPX() 2706be10caSBarry Smith @*/ 2806be10caSBarry Smith int MatAXPY(Scalar *a,Mat X,Mat Y) 296f79c3a4SBarry Smith { 3006be10caSBarry Smith int m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr; 3106be10caSBarry Smith Scalar *val,*vals; 326f79c3a4SBarry Smith 333a40ed3dSBarry Smith PetscFunctionBegin; 34d4bb536fSBarry Smith PetscValidHeaderSpecific(X,MAT_COOKIE); 35d4bb536fSBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 3690f02eecSBarry Smith PetscValidScalarPointer(a); 3790f02eecSBarry Smith 38d4bb536fSBarry Smith MatGetSize(X,&m1,&n1); MatGetSize(Y,&m2,&n2); 39*29bbc08cSBarry Smith if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %d %d %d %d",m1,m2,n1,n2); 401987afe7SBarry Smith 41f830108cSBarry Smith if (X->ops->axpy) { 42f830108cSBarry Smith ierr = (*X->ops->axpy)(a,X,Y);CHKERRQ(ierr); 43d4bb536fSBarry Smith } else { 4490f02eecSBarry Smith ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr); 45d4bb536fSBarry Smith if (*a == 1.0) { 46d4bb536fSBarry Smith for (i = start; i < end; i++) { 47d4bb536fSBarry Smith ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 48d4bb536fSBarry Smith ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 49d4bb536fSBarry Smith ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 50d4bb536fSBarry Smith } 51d4bb536fSBarry Smith } else { 52d4bb536fSBarry Smith vals = (Scalar*)PetscMalloc((n1+1)*sizeof(Scalar));CHKPTRQ(vals); 5306be10caSBarry Smith for (i=start; i<end; i++) { 5490f02eecSBarry Smith ierr = MatGetRow(X,i,&ncols,&row,&val);CHKERRQ(ierr); 5506be10caSBarry Smith for (j=0; j<ncols; j++) { 5606be10caSBarry Smith vals[j] = (*a)*val[j]; 576f79c3a4SBarry Smith } 58dbb450caSBarry Smith ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 5990f02eecSBarry Smith ierr = MatRestoreRow(X,i,&ncols,&row,&val);CHKERRQ(ierr); 606f79c3a4SBarry Smith } 61606d414cSSatish Balay ierr = PetscFree(vals);CHKERRQ(ierr); 62d4bb536fSBarry Smith } 636d4a8577SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 646d4a8577SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 651987afe7SBarry Smith } 663a40ed3dSBarry Smith PetscFunctionReturn(0); 676f79c3a4SBarry Smith } 68052efed2SBarry Smith 695615d1e5SSatish Balay #undef __FUNC__ 70b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatShift" 71052efed2SBarry Smith /*@ 722860a424SLois Curfman McInnes MatShift - Computes Y = Y + a I, where a is a scalar and I is the identity matrix. 73052efed2SBarry Smith 74fee21e36SBarry Smith Collective on Mat 75fee21e36SBarry Smith 7698a79cdbSBarry Smith Input Parameters: 7798a79cdbSBarry Smith + Y - the matrices 7898a79cdbSBarry Smith - a - the scalar 7998a79cdbSBarry Smith 802860a424SLois Curfman McInnes Level: intermediate 812860a424SLois Curfman McInnes 82052efed2SBarry Smith .keywords: matrix, add, shift 836b9ee512SLois Curfman McInnes 84f56f2b3fSBarry Smith .seealso: MatDiagonalSet() 85052efed2SBarry Smith @*/ 86052efed2SBarry Smith int MatShift(Scalar *a,Mat Y) 87052efed2SBarry Smith { 88052efed2SBarry Smith int i,start,end,ierr; 89052efed2SBarry Smith 903a40ed3dSBarry Smith PetscFunctionBegin; 9177c4ece6SBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 9290f02eecSBarry Smith PetscValidScalarPointer(a); 93f830108cSBarry Smith if (Y->ops->shift) { 94f830108cSBarry Smith ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr); 9562d58ce1SBarry Smith } else { 96d4bb536fSBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 97052efed2SBarry Smith for (i=start; i<end; i++) { 98052efed2SBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES);CHKERRQ(ierr); 99052efed2SBarry Smith } 1006d4a8577SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1016d4a8577SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 102052efed2SBarry Smith } 1033a40ed3dSBarry Smith PetscFunctionReturn(0); 104052efed2SBarry Smith } 1056d84be18SBarry Smith 1065615d1e5SSatish Balay #undef __FUNC__ 107f56f2b3fSBarry Smith #define __FUNC__ /*<a name="MatDiagonalSet"></a>*/"MatDiagonalSet" 1086d84be18SBarry Smith /*@ 109f56f2b3fSBarry Smith MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix 110f56f2b3fSBarry Smith that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is 111f56f2b3fSBarry Smith INSERT_VALUES. 1126d84be18SBarry Smith 1136d84be18SBarry Smith Input Parameters: 11498a79cdbSBarry Smith + Y - the input matrix 115f56f2b3fSBarry Smith . D - the diagonal matrix, represented as a vector 116f56f2b3fSBarry Smith - i - INSERT_VALUES or ADD_VALUES 1176d84be18SBarry Smith 118fee21e36SBarry Smith Collective on Mat and Vec 119fee21e36SBarry Smith 1202860a424SLois Curfman McInnes Level: intermediate 1212860a424SLois Curfman McInnes 1226b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal 1236b9ee512SLois Curfman McInnes 1246b9ee512SLois Curfman McInnes .seealso: MatShift() 1256d84be18SBarry Smith @*/ 126f56f2b3fSBarry Smith int MatDiagonalSet(Mat Y,Vec D,InsertMode is) 1276d84be18SBarry Smith { 1286d84be18SBarry Smith int i,start,end,ierr; 1296d84be18SBarry Smith 1303a40ed3dSBarry Smith PetscFunctionBegin; 13177c4ece6SBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 13290f02eecSBarry Smith PetscValidHeaderSpecific(D,VEC_COOKIE); 133f56f2b3fSBarry Smith if (Y->ops->diagonalset) { 134f56f2b3fSBarry Smith ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr); 13594d884c6SBarry Smith } else { 1366d84be18SBarry Smith int vstart,vend; 1376d84be18SBarry Smith Scalar *v; 1386d84be18SBarry Smith ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr); 1396d84be18SBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 140d4bb536fSBarry Smith if (vstart != start || vend != end) { 141*29bbc08cSBarry Smith SETERRQ4(PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %d %d vec %d %d mat",vstart,vend,start,end); 142d4bb536fSBarry Smith } 1436d84be18SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 1446d84be18SBarry Smith for (i=start; i<end; i++) { 145f56f2b3fSBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr); 1466d84be18SBarry Smith } 1472e8a6d31SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 1486d4a8577SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1496d4a8577SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1506d84be18SBarry Smith } 1513a40ed3dSBarry Smith PetscFunctionReturn(0); 1526d84be18SBarry Smith } 153d4bb536fSBarry Smith 154d4bb536fSBarry Smith #undef __FUNC__ 155b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatAYPX" 156d4bb536fSBarry Smith /*@ 157d4bb536fSBarry Smith MatAYPX - Computes Y = X + a*Y. 158d4bb536fSBarry Smith 159fee21e36SBarry Smith Collective on Mat 160fee21e36SBarry Smith 16198a79cdbSBarry Smith Input Parameters: 16298a79cdbSBarry Smith + X,Y - the matrices 16398a79cdbSBarry Smith - a - the scalar multiplier 16498a79cdbSBarry Smith 165e182c471SBarry Smith Contributed by: Matthew Knepley 166d4bb536fSBarry Smith 1672860a424SLois Curfman McInnes Notes: 1682860a424SLois Curfman McInnes This routine currently uses the MatAXPY() implementation. 1692860a424SLois Curfman McInnes 1702860a424SLois Curfman McInnes Level: intermediate 1712860a424SLois Curfman McInnes 172d4bb536fSBarry Smith .keywords: matrix, add 173d4bb536fSBarry Smith 1742860a424SLois Curfman McInnes .seealso: MatAXPY() 175d4bb536fSBarry Smith @*/ 176d4bb536fSBarry Smith int MatAYPX(Scalar *a,Mat X,Mat Y) 177d4bb536fSBarry Smith { 178d4bb536fSBarry Smith Scalar one = 1.0; 179d4bb536fSBarry Smith int mX,mY,nX,nY,ierr; 180d4bb536fSBarry Smith 1813a40ed3dSBarry Smith PetscFunctionBegin; 182d4bb536fSBarry Smith PetscValidHeaderSpecific(X,MAT_COOKIE); 183d4bb536fSBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 184d4bb536fSBarry Smith PetscValidScalarPointer(a); 185d4bb536fSBarry Smith 186329f5518SBarry Smith ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr); 187329f5518SBarry Smith ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr); 188*29bbc08cSBarry Smith if (mX != mY || nX != nY) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrices: %d %d first %d %d second",mX,mY,nX,nY); 189d4bb536fSBarry Smith 190d4bb536fSBarry Smith ierr = MatScale(a,Y);CHKERRQ(ierr); 191d4bb536fSBarry Smith ierr = MatAXPY(&one,X,Y);CHKERRQ(ierr); 1923a40ed3dSBarry Smith PetscFunctionReturn(0); 193d4bb536fSBarry Smith } 194