xref: /petsc/src/mat/utils/getcolv.c (revision 3d81755a6a49a36298642be8722900feec604107)
1*3d81755aSBarry Smith /*$Id: getcolv.c,v 1.22 2001/08/08 15:46:54 bsmith Exp bsmith $*/
20925cdddSBarry Smith 
3e090d566SSatish Balay #include "src/mat/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 
25*3d81755aSBarry Smith    The vector must have the same parallel row layout as the matrix.
26*3d81755aSBarry 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 @*/
3482bf6240SBarry Smith int MatGetColumnVector(Mat A,Vec yy,int col)
350925cdddSBarry Smith {
36ea709b57SSatish Balay   PetscScalar   *y,*v,zero = 0.0;
374a2ae208SSatish Balay   int      ierr,i,j,nz,*idx,N,Rs,Re,rs,re;
38011b8408SBarry Smith   MPI_Comm comm;
390925cdddSBarry Smith 
400925cdddSBarry Smith   PetscFunctionBegin;
4182bf6240SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
4282bf6240SBarry Smith   PetscValidHeaderSpecific(yy,VEC_COOKIE);
430925cdddSBarry Smith 
4429bbc08cSBarry Smith   if (col < 0)  SETERRQ1(1,"Requested negative column: %d",col);
45011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
4629bbc08cSBarry Smith   if (col >= N)  SETERRQ2(1,"Requested column %d larger than number columns in matrix %d",col,N);
470925cdddSBarry Smith 
4882bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
49011b8408SBarry Smith 
50011b8408SBarry Smith   ierr = PetscObjectGetComm((PetscObject)yy,&comm);CHKERRQ(ierr);
5182bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
5229bbc08cSBarry Smith   if (Rs != rs || Re != re) SETERRQ4(1,"Matrix %d %d does not have same ownership range (size) as vector %d %d",Rs,Re,rs,re);
5382bf6240SBarry Smith 
5482bf6240SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
5582bf6240SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
5682bf6240SBarry Smith 
5782bf6240SBarry Smith   for (i=Rs; i<Re; i++) {
5882bf6240SBarry Smith     ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
5982bf6240SBarry Smith     if (nz && idx[0] <= col) {
6082bf6240SBarry Smith       /*
6182bf6240SBarry Smith           Should use faster search here
6282bf6240SBarry Smith       */
6382bf6240SBarry Smith       for (j=0; j<nz; j++) {
6482bf6240SBarry Smith         if (idx[j] >= col) {
6582bf6240SBarry Smith           if (idx[j] == col) y[i-rs] = v[j];
6682bf6240SBarry Smith           break;
670925cdddSBarry Smith         }
680925cdddSBarry Smith       }
690925cdddSBarry Smith     }
7082bf6240SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
710925cdddSBarry Smith   }
720925cdddSBarry Smith 
7382bf6240SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
740925cdddSBarry Smith   PetscFunctionReturn(0);
750925cdddSBarry Smith }
76