1 #define PETSCMAT_DLL 2 3 #include "private/matimpl.h" /*I "petscmat.h" I*/ 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "MatGetColumnVector" 7 /*@ 8 MatGetColumnVector - Gets the values from a given column of a matrix. 9 10 Not Collective 11 12 Input Parameters: 13 + A - the matrix 14 . yy - the vector 15 - c - the column requested (in global numbering) 16 17 Level: advanced 18 19 Notes: 20 Each processor for which this is called gets the values for its rows. 21 22 Since PETSc matrices are usually stored in compressed row format, this routine 23 will generally be slow. 24 25 The vector must have the same parallel row layout as the matrix. 26 27 Contributed by: Denis Vanderstraeten 28 29 .keywords: matrix, column, get 30 31 .seealso: MatGetRow(), MatGetDiagonal() 32 33 @*/ 34 PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnVector(Mat A,Vec yy,PetscInt col) 35 { 36 PetscScalar *y; 37 const PetscScalar *v; 38 PetscErrorCode ierr; 39 PetscInt i,j,nz,N,Rs,Re,rs,re; 40 const PetscInt *idx; 41 42 PetscFunctionBegin; 43 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 44 PetscValidHeaderSpecific(yy,VEC_COOKIE,2); 45 if (col < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col); 46 ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr); 47 if (col >= N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N); 48 ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr); 49 ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr); 50 if (Rs != rs || Re != re) SETERRQ4(PETSC_ERR_ARG_INCOMP,"Matrix %D %D does not have same ownership range (size) as vector %D %D",Rs,Re,rs,re); 51 52 if (A->ops->getcolumnvector) { 53 ierr = (*A->ops->getcolumnvector)(A,yy,col);CHKERRQ(ierr); 54 } else { 55 ierr = VecSet(yy,0.0);CHKERRQ(ierr); 56 ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 57 58 for (i=Rs; i<Re; i++) { 59 ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr); 60 if (nz && idx[0] <= col) { 61 /* 62 Should use faster search here 63 */ 64 for (j=0; j<nz; j++) { 65 if (idx[j] >= col) { 66 if (idx[j] == col) y[i-rs] = v[j]; 67 break; 68 } 69 } 70 } 71 ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr); 72 } 73 ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 74 } 75 PetscFunctionReturn(0); 76 } 77