Date: 2013-07-02T14:43:57+00:00

Actual source code: ex9.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Tests MPI parallel matrix creation.\n\n";

      4: #include <petscmat.h>

      8: int main(int argc,char **args)
      9: {
     10:   Mat            C;
     11:   MatInfo        info;
     12:   PetscMPIInt    rank,size;
     13:   PetscInt       i,j,m = 3,n = 2,low,high,iglobal;
     14:   PetscInt       Ii,J,ldim;
     16:   PetscBool      flg;
     17:   PetscScalar    v,one = 1.0;
     18:   Vec            u,b;

     20:   PetscInitialize(&argc,&args,(char*)0,help);
     21:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     22:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     23:   n    = 2*size;

     25:   MatCreate(PETSC_COMM_WORLD,&C);
     26:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
     27:   MatSetFromOptions(C);
     28:   MatSetUp(C);

     30:   /* Create the matrix for the five point stencil, YET AGAIN */
     31:   for (i=0; i<m; i++) {
     32:     for (j=2*rank; j<2*rank+2; j++) {
     33:       v = -1.0;  Ii = j + n*i;
     34:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
     35:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
     36:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
     37:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
     38:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
     39:     }
     40:   }

     42:   /* Add extra elements (to illustrate variants of MatGetInfo) */
     43:   Ii   = n; J = n-2; v = 100.0;
     44:   MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
     45:   Ii   = n-2; J = n; v = 100.0;
     46:   MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);

     48:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
     49:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

     51:   /* Form vectors */
     52:   VecCreate(PETSC_COMM_WORLD,&u);
     53:   VecSetSizes(u,PETSC_DECIDE,m*n);
     54:   VecSetFromOptions(u);
     55:   VecDuplicate(u,&b);
     56:   VecGetLocalSize(u,&ldim);
     57:   VecGetOwnershipRange(u,&low,&high);
     58:   for (i=0; i<ldim; i++) {
     59:     iglobal = i + low;
     60:     v       = one*((PetscReal)i) + 100.0*rank;
     61:     VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
     62:   }
     63:   VecAssemblyBegin(u);
     64:   VecAssemblyEnd(u);

     66:   MatMult(C,u,b);

     68:   VecView(u,PETSC_VIEWER_STDOUT_WORLD);
     69:   VecView(b,PETSC_VIEWER_STDOUT_WORLD);

     71:   VecDestroy(&u);
     72:   VecDestroy(&b);

     74:   PetscOptionsHasName(NULL,"-view_info",&flg);
     75:   if (flg)  {PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);}
     76:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

     78:   MatGetInfo(C,MAT_GLOBAL_SUM,&info);
     79:   PetscPrintf(PETSC_COMM_WORLD,"matrix information (global sums):\n\
     80:      nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
     81:   MatGetInfo (C,MAT_GLOBAL_MAX,&info);
     82:   PetscPrintf(PETSC_COMM_WORLD,"matrix information (global max):\n\
     83:      nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);

     85:   MatDestroy(&C);
     86:   PetscFinalize();
     87:   return 0;
     88: }