Actual source code: ex9.c

  1: static char help[] = "Tests MatCreateComposite()\n\n";

  3: /*
  4:   Include "petscmat.h" so that we can use matrices.
  5:   automatically includes:
  6:      petscsys.h       - base PETSc routines   petscvec.h    - vectors
  7:      petscmat.h    - matrices
  8:      petscis.h     - index sets            petscviewer.h - viewers
  9: */
 10: #include <petscmat.h>

 12: int main(int argc, char **args)
 13: {
 14:   Mat             *A, B; /* matrix */
 15:   Vec              x, y, v, v2, z, z2;
 16:   PetscReal        rnorm;
 17:   PetscInt         n    = 20; /* size of the matrix */
 18:   PetscInt         nmat = 3;  /* number of matrices */
 19:   PetscRandom      rctx;
 20:   MatCompositeType type;
 21:   PetscScalar      scalings[5] = {2, 3, 4, 5, 6};

 23:   PetscFunctionBeginUser;
 24:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 25:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 26:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nmat", &nmat, NULL));

 28:   /*
 29:      Create random matrices
 30:   */
 31:   PetscCall(PetscMalloc1(nmat + 3, &A));
 32:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rctx));
 33:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, n, n / 2, 3, NULL, 3, NULL, &A[0]));
 34:   for (PetscInt i = 1; i < nmat + 1; i++) PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, n, n, 3, NULL, 3, NULL, &A[i]));
 35:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, n / 2, n, 3, NULL, 3, NULL, &A[nmat + 1]));
 36:   for (PetscInt i = 0; i < nmat + 2; i++) PetscCall(MatSetRandom(A[i], rctx));

 38:   PetscCall(MatCreateVecs(A[1], &x, &y));
 39:   PetscCall(VecDuplicate(y, &z));
 40:   PetscCall(VecDuplicate(z, &z2));
 41:   PetscCall(MatCreateVecs(A[0], &v, NULL));
 42:   PetscCall(VecDuplicate(v, &v2));

 44:   /* Test MatMult of an ADDITIVE MatComposite B made up of A[1],A[2],A[3] with separate scalings */

 46:   /* Do MatMult with A[1],A[2],A[3] by hand and store the result in z */
 47:   PetscCall(VecSet(x, 1.0));
 48:   PetscCall(MatMult(A[1], x, z));
 49:   PetscCall(VecScale(z, scalings[1]));
 50:   for (PetscInt i = 2; i < nmat + 1; i++) {
 51:     PetscCall(MatMult(A[i], x, z2));
 52:     PetscCall(VecAXPY(z, scalings[i], z2));
 53:   }

 55:   /* Do MatMult using MatComposite and store the result in y */
 56:   PetscCall(VecSet(y, 0.0));
 57:   PetscCall(MatCreateComposite(PETSC_COMM_WORLD, nmat, A + 1, &B));
 58:   PetscCall(MatSetFromOptions(B));
 59:   PetscCall(MatCompositeSetScalings(B, &scalings[1]));
 60:   PetscCall(MatMultAdd(B, x, y, y));

 62:   /* Diff y and z */
 63:   PetscCall(VecAXPY(y, -1.0, z));
 64:   PetscCall(VecNorm(y, NORM_2, &rnorm));
 65:   if (rnorm > 10000.0 * PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with composite add %g\n", (double)rnorm));

 67:   /* Test MatCompositeMerge on ADDITIVE MatComposite */
 68:   PetscCall(MatCompositeSetMatStructure(B, DIFFERENT_NONZERO_PATTERN)); /* default */
 69:   PetscCall(MatCompositeMerge(B));
 70:   PetscCall(MatMult(B, x, y));
 71:   PetscCall(MatDestroy(&B));
 72:   PetscCall(VecAXPY(y, -1.0, z));
 73:   PetscCall(VecNorm(y, NORM_2, &rnorm));
 74:   if (rnorm > 10000.0 * PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with composite add after merge %g\n", (double)rnorm));

 76:   /*
 77:      Test n x n/2 multiplicative composite B made up of A[0],A[1],A[2] with separate scalings
 78:   */

 80:   /* Do MatMult with A[0],A[1],A[2] by hand and store the result in z */
 81:   PetscCall(VecSet(v, 1.0));
 82:   PetscCall(MatMult(A[0], v, z));
 83:   PetscCall(VecScale(z, scalings[0]));
 84:   for (PetscInt i = 1; i < nmat; i++) {
 85:     PetscCall(MatMult(A[i], z, y));
 86:     PetscCall(VecScale(y, scalings[i]));
 87:     PetscCall(VecCopy(y, z));
 88:   }

 90:   /* Do MatMult using MatComposite and store the result in y */
 91:   PetscCall(MatCreateComposite(PETSC_COMM_WORLD, nmat, A, &B));
 92:   PetscCall(MatCompositeSetType(B, MAT_COMPOSITE_MULTIPLICATIVE));
 93:   PetscCall(MatCompositeSetMergeType(B, MAT_COMPOSITE_MERGE_LEFT));
 94:   PetscCall(MatSetFromOptions(B));
 95:   PetscCall(MatCompositeSetScalings(B, &scalings[0]));
 96:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
 97:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); /* do MatCompositeMerge() if -mat_composite_merge 1 */
 98:   PetscCall(MatMult(B, v, y));
 99:   PetscCall(MatDestroy(&B));

101:   /* Diff y and z */
102:   PetscCall(VecAXPY(y, -1.0, z));
103:   PetscCall(VecNorm(y, NORM_2, &rnorm));
104:   if (rnorm > 10000.0 * PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with composite multiplicative %g\n", (double)rnorm));

106:   /*
107:      Test n/2 x n multiplicative composite B made up of A[2], A[3], A[4] without separate scalings
108:   */
109:   PetscCall(VecSet(x, 1.0));
110:   PetscCall(MatMult(A[2], x, z));
111:   for (PetscInt i = 3; i < nmat + 1; i++) {
112:     PetscCall(MatMult(A[i], z, y));
113:     PetscCall(VecCopy(y, z));
114:   }
115:   PetscCall(MatMult(A[nmat + 1], z, v));

117:   PetscCall(MatCreateComposite(PETSC_COMM_WORLD, nmat, A + 2, &B));
118:   PetscCall(MatCompositeSetType(B, MAT_COMPOSITE_MULTIPLICATIVE));
119:   PetscCall(MatSetFromOptions(B));
120:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
121:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); /* do MatCompositeMerge() if -mat_composite_merge 1 */
122:   PetscCall(MatMult(B, x, v2));
123:   PetscCall(MatDestroy(&B));

125:   PetscCall(VecAXPY(v2, -1.0, v));
126:   PetscCall(VecNorm(v2, NORM_2, &rnorm));
127:   if (rnorm > 10000.0 * PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with composite multiplicative %g\n", (double)rnorm));

129:   /*
130:      Test get functions
131:   */
132:   PetscCall(MatCreateComposite(PETSC_COMM_WORLD, nmat, A, &B));
133:   PetscCall(MatCompositeGetNumberMat(B, &n));
134:   if (nmat != n) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with GetNumberMat %" PetscInt_FMT " != %" PetscInt_FMT "\n", nmat, n));
135:   PetscCall(MatCompositeGetMat(B, 0, &A[nmat + 2]));
136:   if (A[0] != A[nmat + 2]) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with GetMat\n"));
137:   PetscCall(MatCompositeGetType(B, &type));
138:   if (type != MAT_COMPOSITE_ADDITIVE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error with GetType\n"));
139:   PetscCall(MatDestroy(&B));

141:   /*
142:      Free work space.  All PETSc objects should be destroyed when they
143:      are no longer needed.
144:   */
145:   PetscCall(VecDestroy(&x));
146:   PetscCall(VecDestroy(&y));
147:   PetscCall(VecDestroy(&v));
148:   PetscCall(VecDestroy(&v2));
149:   PetscCall(VecDestroy(&z));
150:   PetscCall(VecDestroy(&z2));
151:   PetscCall(PetscRandomDestroy(&rctx));
152:   for (PetscInt i = 0; i < nmat + 2; i++) PetscCall(MatDestroy(&A[i]));
153:   PetscCall(PetscFree(A));

155:   PetscCall(PetscFinalize());
156:   return 0;
157: }

159: /*TEST

161:    test:
162:       nsize: 2
163:       requires: double
164:       args: -mat_composite_merge {{0 1}shared output} -mat_composite_merge_mvctx {{0 1}shared output}
165:       output_file: output/empty.out

167: TEST*/