Actual source code: getcolv.c
petsc-3.13.6 2020-09-29
2: #include <petsc/private/matimpl.h>
4: /*@
5: MatGetColumnVector - Gets the values from a given column of a matrix.
7: Not Collective
9: Input Parameters:
10: + A - the matrix
11: . yy - the vector
12: - col - the column requested (in global numbering)
14: Level: advanced
16: Notes:
17: Each processor for which this is called gets the values for its rows.
19: Since PETSc matrices are usually stored in compressed row format, this routine
20: will generally be slow.
22: The vector must have the same parallel row layout as the matrix.
24: Contributed by: Denis Vanderstraeten
26: .seealso: MatGetRow(), MatGetDiagonal()
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;
40: if (col < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
41: MatGetSize(A,NULL,&N);
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: MatGetOwnershipRange(A,&Rs,&Re);
44: VecGetOwnershipRange(yy,&rs,&re);
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);
47: if (A->ops->getcolumnvector) {
48: (*A->ops->getcolumnvector)(A,yy,col);
49: } else {
50: VecSet(yy,0.0);
51: VecGetArray(yy,&y);
53: for (i=Rs; i<Re; i++) {
54: MatGetRow(A,i,&nz,&idx,&v);
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: MatRestoreRow(A,i,&nz,&idx,&v);
67: }
68: VecRestoreArray(yy,&y);
69: }
70: return(0);
71: }
76: /*@
77: MatGetColumnNorms - Gets the norms of each column of a sparse or dense matrix.
79: Input Parameter:
80: + A - the matrix
81: - type - NORM_2, NORM_1 or NORM_INFINITY
83: Output Parameter:
84: . norms - an array as large as the TOTAL number of columns in the matrix
86: Level: intermediate
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.
92: .seealso: NormType, MatNorm()
94: @*/
95: PetscErrorCode MatGetColumnNorms(Mat A,NormType type,PetscReal norms[])
96: {
101: if (A->ops->getcolumnnorms) {
102: (*A->ops->getcolumnnorms)(A,type,norms);
103: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for this matrix type");
104: return(0);
105: }