Actual source code: ex49.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

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

  4:  #include <petscmat.h>

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

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

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

 43:   /* Print info about original matrix */
 44:   MatGetInfo(mat,MAT_GLOBAL_SUM,&info);
 45:   PetscPrintf(PETSC_COMM_WORLD,"original matrix nonzeros = %D, allocated nonzeros = %D\n",
 46:                      (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
 47:   MatNorm(mat,NORM_FROBENIUS,&normf);
 48:   MatNorm(mat,NORM_1,&norm1);
 49:   MatNorm(mat,NORM_INFINITY,&normi);
 50:   PetscPrintf(PETSC_COMM_WORLD,"original: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
 51:   MatView(mat,PETSC_VIEWER_STDOUT_WORLD);

 53:   /* Form matrix transpose */
 54:   PetscOptionsHasName(NULL,NULL,"-in_place",&flg);
 55:   if (flg) {
 56:     MatTranspose(mat,MAT_INPLACE_MATRIX,&mat);   /* in-place transpose */
 57:     tmat = mat; mat = 0;
 58:   } else {      /* out-of-place transpose */
 59:     MatTranspose(mat,MAT_INITIAL_MATRIX,&tmat);
 60:   }

 62:   /* Print info about transpose matrix */
 63:   MatGetInfo(tmat,MAT_GLOBAL_SUM,&info);
 64:   PetscPrintf(PETSC_COMM_WORLD,"transpose matrix nonzeros = %D, allocated nonzeros = %D\n",
 65:                      (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
 66:   MatNorm(tmat,NORM_FROBENIUS,&normf);
 67:   MatNorm(tmat,NORM_1,&norm1);
 68:   MatNorm(tmat,NORM_INFINITY,&normi);
 69:   PetscPrintf(PETSC_COMM_WORLD,"transpose: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
 70:   MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);


 73:   /* Test MatAXPY */
 74:   if (mat && !rect) {
 75:     PetscScalar alpha = 1.0;
 76:     PetscOptionsGetScalar(NULL,NULL,"-alpha",&alpha,NULL);
 77:     PetscPrintf(PETSC_COMM_WORLD,"matrix addition:  B = B + alpha * A\n");
 78:     MatAXPY(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN);
 79:     MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
 80:   }

 82:   /* Free data structures */
 83:   MatDestroy(&tmat);
 84:   if (mat) {MatDestroy(&mat);}

 86:   PetscFinalize();
 87:   return ierr;
 88: }

 90: /*TEST

 92:    test:

 94:    testset:
 95:      args: -rect1
 96:      test:
 97:        suffix: r1
 98:        output_file: output/ex49_r1.out
 99:      test:
100:        suffix: r1_inplace
101:        args: -in_place
102:        output_file: output/ex49_r1.out
103:      test:
104:        suffix: r1_par
105:        nsize: 2
106:        output_file: output/ex49_r1_par.out
107:      test:
108:        suffix: r1_par_inplace
109:        args: -in_place
110:        nsize: 2
111:        output_file: output/ex49_r1_par.out

113: TEST*/