Actual source code: getcolv.c

  1: #define PETSCMAT_DLL

 3:  #include include/private/matimpl.h

  7: /*@
  8:    MatGetColumnVector - Gets the values from a given column of a matrix.

 10:    Not Collective

 12:    Input Parameters:
 13: +  A - the matrix
 14: .  yy - the vector
 15: -  c - the column requested (in global numbering)

 17:    Level: advanced

 19:    Notes:
 20:    Each processor for which this is called gets the values for its rows.

 22:    Since PETSc matrices are usually stored in compressed row format, this routine
 23:    will generally be slow.

 25:    The vector must have the same parallel row layout as the matrix.

 27:    Contributed by: Denis Vanderstraeten

 29: .keywords: matrix, column, get 

 31: .seealso: MatGetRow(), MatGetDiagonal()

 33: @*/
 34: PetscErrorCode  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: 
 45:   if (col < 0)  SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
 46:   MatGetSize(A,PETSC_NULL,&N);
 47:   if (col >= N)  SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
 48:   MatGetOwnershipRange(A,&Rs,&Re);
 49:   VecGetOwnershipRange(yy,&rs,&re);
 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);

 52:   if (A->ops->getcolumnvector) {
 53:     (*A->ops->getcolumnvector)(A,yy,col);
 54:   } else {
 55:     VecSet(yy,0.0);
 56:     VecGetArray(yy,&y);
 57: 
 58:     for (i=Rs; i<Re; i++) {
 59:       MatGetRow(A,i,&nz,&idx,&v);
 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:       MatRestoreRow(A,i,&nz,&idx,&v);
 72:     }
 73:     VecRestoreArray(yy,&y);
 74:   }
 75:   return(0);
 76: }