xref: /petsc/src/mat/utils/getcolv.c (revision be1d678a52e6eff2808b2fa31ae986cdbf03c9fe)
1*be1d678aSKris Buschelman #define PETSCMAT_DLL
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 
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 @*/
34*be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnVector(Mat A,Vec yy,PetscInt col)
350925cdddSBarry Smith {
36b3cc6726SBarry Smith   PetscScalar        *y,zero = 0.0;
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;
41011b8408SBarry Smith   MPI_Comm           comm;
420925cdddSBarry Smith 
430925cdddSBarry Smith   PetscFunctionBegin;
444482741eSBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
454482741eSBarry Smith   PetscValidHeaderSpecific(yy,VEC_COOKIE,2);
460925cdddSBarry Smith 
4777431f27SBarry Smith   if (col < 0)  SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
48011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
4977431f27SBarry Smith   if (col >= N)  SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
500925cdddSBarry Smith 
5182bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
52011b8408SBarry Smith 
53011b8408SBarry Smith   ierr = PetscObjectGetComm((PetscObject)yy,&comm);CHKERRQ(ierr);
5482bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
5577431f27SBarry 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);
5682bf6240SBarry Smith 
5782bf6240SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
5882bf6240SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
5982bf6240SBarry Smith 
6082bf6240SBarry Smith   for (i=Rs; i<Re; i++) {
6182bf6240SBarry Smith     ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
6282bf6240SBarry Smith     if (nz && idx[0] <= col) {
6382bf6240SBarry Smith       /*
6482bf6240SBarry Smith           Should use faster search here
6582bf6240SBarry Smith       */
6682bf6240SBarry Smith       for (j=0; j<nz; j++) {
6782bf6240SBarry Smith         if (idx[j] >= col) {
6882bf6240SBarry Smith           if (idx[j] == col) y[i-rs] = v[j];
6982bf6240SBarry Smith           break;
700925cdddSBarry Smith         }
710925cdddSBarry Smith       }
720925cdddSBarry Smith     }
7382bf6240SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
740925cdddSBarry Smith   }
750925cdddSBarry Smith 
7682bf6240SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
770925cdddSBarry Smith   PetscFunctionReturn(0);
780925cdddSBarry Smith }
79