Actual source code: ex2.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Tests MatTranspose(), MatNorm(), MatAXPY() and MatAYPX().\n\n";

  4:  #include <petscmat.h>

  6: int main(int argc,char **argv)
  7: {
  8:   Mat            mat,tmat = 0;
  9:   PetscInt       m = 7,n,i,j,rstart,rend,rect = 0;
 11:   PetscMPIInt    size,rank;
 12:   PetscBool      flg;
 13:   PetscScalar    v, alpha;
 14:   PetscReal      normf,normi,norm1;

 16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 17:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 18:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 19:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 20:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 21:   n    = m;
 22:   PetscOptionsHasName(NULL,NULL,"-rectA",&flg);
 23:   if (flg) {n += 2; rect = 1;}
 24:   PetscOptionsHasName(NULL,NULL,"-rectB",&flg);
 25:   if (flg) {n -= 2; rect = 1;}

 27:   /* ------- Assemble matrix, test MatValid() --------- */
 28:   MatCreate(PETSC_COMM_WORLD,&mat);
 29:   MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,m,n);
 30:   MatSetFromOptions(mat);
 31:   MatSetUp(mat);
 32:   MatGetOwnershipRange(mat,&rstart,&rend);
 33:   for (i=rstart; i<rend; i++) {
 34:     for (j=0; j<n; j++) {
 35:       v    = 10.0*i+j;
 36:       MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
 37:     }
 38:   }
 39:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
 40:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

 42:   /* ----------------- Test MatNorm()  ----------------- */
 43:   MatNorm(mat,NORM_FROBENIUS,&normf);
 44:   MatNorm(mat,NORM_1,&norm1);
 45:   MatNorm(mat,NORM_INFINITY,&normi);
 46:   PetscPrintf(PETSC_COMM_WORLD,"original A: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
 47:   MatView(mat,PETSC_VIEWER_STDOUT_WORLD);

 49:   /* --------------- Test MatTranspose()  -------------- */
 50:   PetscOptionsHasName(NULL,NULL,"-in_place",&flg);
 51:   if (!rect && flg) {
 52:     MatTranspose(mat,MAT_REUSE_MATRIX,&mat);   /* in-place transpose */
 53:     tmat = mat; mat = 0;
 54:   } else {      /* out-of-place transpose */
 55:     MatTranspose(mat,MAT_INITIAL_MATRIX,&tmat);
 56:   }

 58:   /* ----------------- Test MatNorm()  ----------------- */
 59:   /* Print info about transpose matrix */
 60:   MatNorm(tmat,NORM_FROBENIUS,&normf);
 61:   MatNorm(tmat,NORM_1,&norm1);
 62:   MatNorm(tmat,NORM_INFINITY,&normi);
 63:   PetscPrintf(PETSC_COMM_WORLD,"B = A^T: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
 64:   MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);

 66:   /* ----------------- Test MatAXPY(), MatAYPX()  ----------------- */
 67:   if (mat && !rect) {
 68:     alpha = 1.0;
 69:     PetscOptionsGetScalar(NULL,NULL,"-alpha",&alpha,NULL);
 70:     PetscPrintf(PETSC_COMM_WORLD,"MatAXPY:  B = B + alpha * A\n");
 71:     MatAXPY(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN);
 72:     MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);

 74:     PetscPrintf(PETSC_COMM_WORLD,"MatAYPX:  B = alpha*B + A\n");
 75:     MatAYPX(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN);
 76:     MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
 77:   }

 79:   {
 80:     Mat C;
 81:     alpha = 1.0;
 82:     PetscPrintf(PETSC_COMM_WORLD,"MatAXPY:  C = C + alpha * A, C=A, SAME_NONZERO_PATTERN\n");
 83:     MatDuplicate(mat,MAT_COPY_VALUES,&C);
 84:     MatAXPY(C,alpha,mat,SAME_NONZERO_PATTERN);
 85:     MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 86:     MatDestroy(&C);
 87:   }

 89:   {
 90:     Mat matB;
 91:     /* get matB that has nonzeros of mat in all even numbers of row and col */
 92:     MatCreate(PETSC_COMM_WORLD,&matB);
 93:     MatSetSizes(matB,PETSC_DECIDE,PETSC_DECIDE,m,n);
 94:     MatSetFromOptions(matB);
 95:     MatSetUp(matB);
 96:     MatGetOwnershipRange(matB,&rstart,&rend);
 97:     if (rstart % 2 != 0) rstart++;
 98:     for (i=rstart; i<rend; i += 2) {
 99:       for (j=0; j<n; j += 2) {
100:         v    = 10.0*i+j;
101:         MatSetValues(matB,1,&i,1,&j,&v,INSERT_VALUES);
102:       }
103:     }
104:     MatAssemblyBegin(matB,MAT_FINAL_ASSEMBLY);
105:     MatAssemblyEnd(matB,MAT_FINAL_ASSEMBLY);
106:     PetscPrintf(PETSC_COMM_WORLD," A: original matrix:\n");
107:     MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
108:     PetscPrintf(PETSC_COMM_WORLD," B(a subset of A):\n");
109:     MatView(matB,PETSC_VIEWER_STDOUT_WORLD);
110:     PetscPrintf(PETSC_COMM_WORLD,"MatAXPY:  B = B + alpha * A, SUBSET_NONZERO_PATTERN\n");
111:     MatAXPY(mat,alpha,matB,SUBSET_NONZERO_PATTERN);
112:     MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
113:     MatDestroy(&matB);
114:   }

116:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
117:   /* Free data structures */
118:   MatDestroy(&mat);
119:   MatDestroy(&tmat);
120:   PetscFinalize();
121:   return ierr;
122: }