xref: /petsc/src/mat/utils/getcolv.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
1 
2 #include <petsc/private/matimpl.h>  /*I   "petscmat.h"  I*/
3 
4 /*@
5    MatGetColumnVector - Gets the values from a given column of a matrix.
6 
7    Not Collective
8 
9    Input Parameters:
10 +  A - the matrix
11 .  yy - the vector
12 -  col - the column requested (in global numbering)
13 
14    Level: advanced
15 
16    Notes:
17    Each processor for which this is called gets the values for its rows.
18 
19    Since PETSc matrices are usually stored in compressed row format, this routine
20    will generally be slow.
21 
22    The vector must have the same parallel row layout as the matrix.
23 
24    Contributed by: Denis Vanderstraeten
25 
26 .seealso: MatGetRow(), MatGetDiagonal()
27 
28 @*/
29 PetscErrorCode  MatGetColumnVector(Mat A,Vec yy,PetscInt col)
30 {
31   PetscScalar       *y;
32   const PetscScalar *v;
33   PetscErrorCode    ierr;
34   PetscInt          i,j,nz,N,Rs,Re,rs,re;
35   const PetscInt    *idx;
36 
37   PetscFunctionBegin;
38   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
39   PetscValidHeaderSpecific(yy,VEC_CLASSID,2);
40   if (col < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
41   ierr = MatGetSize(A,NULL,&N);CHKERRQ(ierr);
42   if (col >= N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
43   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
44   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
45   if (Rs != rs || Re != re) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Matrix %D %D does not have same ownership range (size) as vector %D %D",Rs,Re,rs,re);
46 
47   if (A->ops->getcolumnvector) {
48     ierr = (*A->ops->getcolumnvector)(A,yy,col);CHKERRQ(ierr);
49   } else {
50     ierr = VecSet(yy,0.0);CHKERRQ(ierr);
51     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
52 
53     for (i=Rs; i<Re; i++) {
54       ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
55       if (nz && idx[0] <= col) {
56         /*
57           Should use faster search here
58         */
59         for (j=0; j<nz; j++) {
60           if (idx[j] >= col) {
61             if (idx[j] == col) y[i-rs] = v[j];
62             break;
63           }
64         }
65       }
66       ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
67     }
68     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
69   }
70   PetscFunctionReturn(0);
71 }
72 
73 
74 
75 
76 /*@
77     MatGetColumnNorms - Gets the norms of each column of a sparse or dense matrix.
78 
79   Input Parameter:
80 +  A - the matrix
81 -  type - NORM_2, NORM_1 or NORM_INFINITY
82 
83   Output Parameter:
84 .  norms - an array as large as the TOTAL number of columns in the matrix
85 
86    Level: intermediate
87 
88    Notes:
89     Each process has ALL the column norms after the call. Because of the way this is computed each process gets all the values,
90     if each process wants only some of the values it should extract the ones it wants from the array.
91 
92 .seealso: NormType, MatNorm()
93 
94 @*/
95 PetscErrorCode MatGetColumnNorms(Mat A,NormType type,PetscReal norms[])
96 {
97   PetscErrorCode ierr;
98 
99   PetscFunctionBegin;
100   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
101   if (A->ops->getcolumnnorms) {
102     ierr = (*A->ops->getcolumnnorms)(A,type,norms);CHKERRQ(ierr);
103   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for this matrix type");
104   PetscFunctionReturn(0);
105 }
106 
107