xref: /petsc/src/mat/utils/getcolv.c (revision 8d0534bee39d3e9a56b8b83147d7d1a573bb8dcf)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
20925cdddSBarry Smith 
3b9147fbbSdalcinl #include "include/private/matimpl.h"  /*I   "petscmat.h"  I*/
40925cdddSBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
64a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnVector"
70925cdddSBarry Smith /*@
882bf6240SBarry Smith    MatGetColumnVector - Gets the values from a given column of a matrix.
90925cdddSBarry Smith 
1072257631SBarry Smith    Not Collective
11fee21e36SBarry Smith 
1298a79cdbSBarry Smith    Input Parameters:
135ed6d96aSBarry Smith +  A - the matrix
145ed6d96aSBarry Smith .  yy - the vector
155ed6d96aSBarry Smith -  c - the column requested (in global numbering)
1698a79cdbSBarry Smith 
1715091d37SBarry Smith    Level: advanced
1815091d37SBarry Smith 
199d006df2SBarry Smith    Notes:
209d006df2SBarry Smith    Each processor for which this is called gets the values for its rows.
219d006df2SBarry Smith 
229d006df2SBarry Smith    Since PETSc matrices are usually stored in compressed row format, this routine
239d006df2SBarry Smith    will generally be slow.
249d006df2SBarry Smith 
253d81755aSBarry Smith    The vector must have the same parallel row layout as the matrix.
263d81755aSBarry Smith 
2782bf6240SBarry Smith    Contributed by: Denis Vanderstraeten
280925cdddSBarry Smith 
2982bf6240SBarry Smith .keywords: matrix, column, get
300925cdddSBarry Smith 
3115091d37SBarry Smith .seealso: MatGetRow(), MatGetDiagonal()
3215091d37SBarry Smith 
330925cdddSBarry Smith @*/
34be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnVector(Mat A,Vec yy,PetscInt col)
350925cdddSBarry Smith {
368aa348c1SBarry Smith   PetscScalar        *y;
37b3cc6726SBarry Smith   const PetscScalar  *v;
38dfbe8321SBarry Smith   PetscErrorCode     ierr;
3938baddfdSBarry Smith   PetscInt           i,j,nz,N,Rs,Re,rs,re;
4038baddfdSBarry Smith   const PetscInt     *idx;
410925cdddSBarry Smith 
420925cdddSBarry Smith   PetscFunctionBegin;
434482741eSBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
444482741eSBarry Smith   PetscValidHeaderSpecific(yy,VEC_COOKIE,2);
4577431f27SBarry Smith   if (col < 0)  SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
46011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
4777431f27SBarry Smith   if (col >= N)  SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
4882bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
4982bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
5077431f27SBarry Smith   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);
5182bf6240SBarry Smith 
52*8d0534beSBarry Smith   if (A->ops->getcolumnvector) {
53*8d0534beSBarry Smith     ierr = (*A->ops->getcolumnvector)(A,yy,col);CHKERRQ(ierr);
54*8d0534beSBarry Smith   } else {
558aa348c1SBarry Smith     ierr = VecSet(yy,0.0);CHKERRQ(ierr);
5682bf6240SBarry Smith     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
5782bf6240SBarry Smith 
5882bf6240SBarry Smith     for (i=Rs; i<Re; i++) {
5982bf6240SBarry Smith       ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
6082bf6240SBarry Smith       if (nz && idx[0] <= col) {
6182bf6240SBarry Smith 	/*
6282bf6240SBarry Smith           Should use faster search here
6382bf6240SBarry Smith 	*/
6482bf6240SBarry Smith 	for (j=0; j<nz; j++) {
6582bf6240SBarry Smith 	  if (idx[j] >= col) {
6682bf6240SBarry Smith 	    if (idx[j] == col) y[i-rs] = v[j];
6782bf6240SBarry Smith 	    break;
680925cdddSBarry Smith 	  }
690925cdddSBarry Smith 	}
700925cdddSBarry Smith       }
7182bf6240SBarry Smith       ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
720925cdddSBarry Smith     }
7382bf6240SBarry Smith     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
74*8d0534beSBarry Smith   }
750925cdddSBarry Smith   PetscFunctionReturn(0);
760925cdddSBarry Smith }
77