Actual source code: ex49.c

petsc-3.4.5 2014-06-29
  2: static char help[] = "Tests MatTranspose(), MatNorm(), and MatAXPY().\n\n";

  4: #include <petscmat.h>

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

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

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

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

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

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


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

 85:   /* Free data structures */
 86:   MatDestroy(&tmat);
 87:   if (mat) {MatDestroy(&mat);}

 89:   PetscFinalize();
 90:   return 0;
 91: }