Actual source code: ex38.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Test interface of Elemental. \n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            C,Caij;
 11:   PetscInt       i,j,m = 5,n,nrows,ncols;
 12:   const PetscInt *rows,*cols;
 13:   IS             isrows,iscols;
 15:   PetscBool      flg,Test_MatMatMult=PETSC_FALSE,mats_view=PETSC_FALSE;
 16:   PetscScalar    *v;
 17:   PetscMPIInt    rank,size;

 19:   PetscInitialize(&argc,&args,(char*)0,help);
 20:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 21:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 22:   PetscOptionsHasName(NULL,NULL,"-mats_view",&mats_view);

 24:   /* Get local block or element size*/
 25:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 26:   n    = m;
 27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 29:   MatCreate(PETSC_COMM_WORLD,&C);
 30:   MatSetSizes(C,m,n,PETSC_DECIDE,PETSC_DECIDE);
 31:   MatSetType(C,MATELEMENTAL);
 32:   MatSetFromOptions(C);
 33:   MatSetUp(C);

 35:   PetscOptionsHasName(NULL,NULL,"-row_oriented",&flg);
 36:   if (flg) {MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);}
 37:   MatGetOwnershipIS(C,&isrows,&iscols);
 38:   PetscOptionsHasName(NULL,NULL,"-Cexp_view_ownership",&flg);
 39:   if (flg) { /* View ownership of explicit C */
 40:     IS tmp;
 41:     PetscPrintf(PETSC_COMM_WORLD,"Ownership of explicit C:\n");
 42:     PetscPrintf(PETSC_COMM_WORLD,"Row index set:\n");
 43:     ISOnComm(isrows,PETSC_COMM_WORLD,PETSC_USE_POINTER,&tmp);
 44:     ISView(tmp,PETSC_VIEWER_STDOUT_WORLD);
 45:     ISDestroy(&tmp);
 46:     PetscPrintf(PETSC_COMM_WORLD,"Column index set:\n");
 47:     ISOnComm(iscols,PETSC_COMM_WORLD,PETSC_USE_POINTER,&tmp);
 48:     ISView(tmp,PETSC_VIEWER_STDOUT_WORLD);
 49:     ISDestroy(&tmp);
 50:   }

 52:   /* Set local matrix entries */
 53:   ISGetLocalSize(isrows,&nrows);
 54:   ISGetIndices(isrows,&rows);
 55:   ISGetLocalSize(iscols,&ncols);
 56:   ISGetIndices(iscols,&cols);
 57:   PetscMalloc1(nrows*ncols,&v);
 58:   for (i=0; i<nrows; i++) {
 59:     for (j=0; j<ncols; j++) {
 60:       /*v[i*ncols+j] = (PetscReal)(rank);*/
 61:       v[i*ncols+j] = (PetscReal)(rank*10000+100*rows[i]+cols[j]);
 62:     }
 63:   }
 64:   MatSetValues(C,nrows,rows,ncols,cols,v,INSERT_VALUES);
 65:   ISRestoreIndices(isrows,&rows);
 66:   ISRestoreIndices(iscols,&cols);
 67:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 68:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 70:   /* Test MatView() */
 71:   if (mats_view) {
 72:     MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 73:   }

 75:   /* Set unowned matrix entries - add subdiagonals and diagonals from proc[0] */
 76:   if (!rank) {
 77:     PetscInt M,N,cols[2];
 78:     MatGetSize(C,&M,&N);
 79:     for (i=0; i<M; i++) {
 80:       cols[0] = i;   v[0] = i + 0.5;
 81:       cols[1] = i-1; v[1] = 0.5;
 82:       if (i) {
 83:         MatSetValues(C,1,&i,2,cols,v,ADD_VALUES);
 84:       } else {
 85:         MatSetValues(C,1,&i,1,&i,v,ADD_VALUES);
 86:       }
 87:     }
 88:   }
 89:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 90:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 92:   /* Test MatMult() */
 93:   MatComputeExplicitOperator(C,&Caij);
 94:   MatMultEqual(C,Caij,5,&flg);
 95:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultEqual() fails");
 96:   MatMultTransposeEqual(C,Caij,5,&flg);
 97:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultTransposeEqual() fails");

 99:   /* Test MatMultAdd() and MatMultTransposeAddEqual() */
100:   MatMultAddEqual(C,Caij,5,&flg);
101:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultAddEqual() fails");
102:   MatMultTransposeAddEqual(C,Caij,5,&flg);
103:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultTransposeAddEqual() fails");

105:   /* Test MatMatMult() */
106:   PetscOptionsHasName(NULL,NULL,"-test_matmatmult",&Test_MatMatMult);
107:   if (Test_MatMatMult) {
108:     Mat CCelem,CCaij;
109:     MatMatMult(C,C,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&CCelem);
110:     MatMatMult(Caij,Caij,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&CCaij);
111:     MatMultEqual(CCelem,CCaij,5,&flg);
112:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"CCelem != CCaij. MatMatMult() fails");
113:     MatDestroy(&CCaij);
114:     MatDestroy(&CCelem);
115:   }

117:   PetscFree(v);
118:   ISDestroy(&isrows);
119:   ISDestroy(&iscols);
120:   MatDestroy(&C);
121:   MatDestroy(&Caij);
122:   PetscFinalize();
123:   return 0;
124: }