Actual source code: lmvm_test.c

  1: const char help[] = "Coverage and edge case test for LMVM";

  3: #include <petscksp.h>

  5: static PetscErrorCode CheckProperties(Mat B, PetscBool symmetric, PetscBool spd)
  6: {
  7:   MPI_Comm  comm = PetscObjectComm((PetscObject)B);
  8:   PetscBool known, flg;
  9:   MatType   type;

 11:   PetscFunctionBeginUser;
 12:   PetscCall(MatGetType(B, &type));
 13:   PetscCall(MatIsHermitianKnown(B, &known, &flg));
 14:   PetscCheck(symmetric ? known && flg : !known, comm, PETSC_ERR_PLIB, "Incorrect Hermitian metadata for %s", type);
 15:   if (!PetscDefined(USE_COMPLEX)) {
 16:     PetscCall(MatIsSymmetricKnown(B, &known, &flg));
 17:     PetscCheck(symmetric ? known && flg : !known, comm, PETSC_ERR_PLIB, "Incorrect symmetry metadata for %s", type);
 18:   }
 19:   PetscCall(MatIsSPDKnown(B, &known, &flg));
 20:   PetscCheck(spd ? known && flg : !known, comm, PETSC_ERR_PLIB, "Incorrect SPD metadata for %s", type);
 21:   if (symmetric) {
 22:     Mat       A, AH;
 23:     PetscReal norm, err;

 25:     PetscCall(MatComputeOperator(B, MATDENSE, &A));
 26:     PetscCall(MatNorm(A, NORM_FROBENIUS, &norm));
 27:     PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, &AH));
 28:     PetscCall(MatAXPY(AH, -1.0, A, SAME_NONZERO_PATTERN));
 29:     PetscCall(MatNorm(AH, NORM_FROBENIUS, &err));
 30:     PetscCheck(err <= 100.0 * PETSC_MACHINE_EPSILON * norm, comm, PETSC_ERR_PLIB, "%s is not Hermitian: relative error %g", type, (double)(err / norm));
 31:     PetscCall(MatDestroy(&AH));
 32:     if (spd) {
 33:       KSP         ksp;
 34:       PC          pc;
 35:       PetscInt    n;
 36:       PetscMPIInt count;
 37:       PetscReal  *real, *imag;

 39:       PetscCall(MatGetSize(A, &n, NULL));
 40:       PetscCall(PetscMalloc2(n, &real, n, &imag));
 41:       PetscCall(KSPCreate(comm, &ksp));
 42:       PetscCall(KSPSetType(ksp, KSPMINRES));
 43:       PetscCall(KSPSetPCSide(ksp, PC_LEFT));
 44:       PetscCall(KSPGetPC(ksp, &pc));
 45:       PetscCall(PCSetType(pc, PCNONE));
 46:       PetscCall(KSPSetOperators(ksp, A, A));
 47:       PetscCall(KSPComputeEigenvaluesExplicitly(ksp, n, real, imag));
 48:       PetscCall(PetscMPIIntCast(n, &count));
 49:       PetscCallMPI(MPI_Bcast(real, count, MPIU_REAL, 0, comm));
 50:       PetscCallMPI(MPI_Bcast(imag, count, MPIU_REAL, 0, comm));
 51:       for (PetscInt i = 0; i < n; ++i) PetscCheck(real[i] > 0.0 && PetscAbsReal(imag[i]) <= 100.0 * PETSC_MACHINE_EPSILON * norm, comm, PETSC_ERR_PLIB, "%s has eigenvalue %g + %gi", type, (double)real[i], (double)imag[i]);
 52:       PetscCall(KSPDestroy(&ksp));
 53:       PetscCall(PetscFree2(real, imag));
 54:     }
 55:     PetscCall(MatDestroy(&A));
 56:   }
 57:   PetscFunctionReturn(PETSC_SUCCESS);
 58: }

 60: static PetscErrorCode TestProperties(Mat B)
 61: {
 62:   Vec          x, f, d;
 63:   PetscRandom  rand;
 64:   PetscInt     m, start, end;
 65:   PetscScalar *a;
 66:   PetscBool    symmetric, spd, sr1;

 68:   PetscFunctionBeginUser;
 69:   PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &spd, MATLMVMBFGS, MATLMVMDFP, MATLMVMSYMBROYDEN, MATLMVMSYMBADBROYDEN, MATLMVMDIAGBROYDEN, MATLMVMDBFGS, MATLMVMDDFP, MATLMVMDQN, ""));
 70:   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATLMVMSR1, &sr1));
 71:   symmetric = (PetscBool)(spd || sr1);
 72:   PetscCall(MatCreateVecs(B, &x, &f));
 73:   PetscCall(VecDuplicate(x, &d));
 74:   PetscCall(VecGetOwnershipRange(d, &start, &end));
 75:   PetscCall(VecGetArray(d, &a));
 76:   for (PetscInt i = start; i < end; ++i) a[i - start] = 1.0 + i;
 77:   PetscCall(VecRestoreArray(d, &a));
 78:   PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)B), &rand));
 79:   PetscCall(MatLMVMGetHistorySize(B, &m));
 80:   PetscCall(CheckProperties(B, symmetric, spd));
 81:   for (PetscInt i = 0; i <= 2 * m; ++i) {
 82:     PetscCall(VecSetRandom(x, rand));
 83:     PetscCall(VecPointwiseMult(f, d, x));
 84:     PetscCall(MatLMVMUpdate(B, x, f));
 85:     PetscCall(CheckProperties(B, symmetric, spd));
 86:   }

 88:   /* Repeated assembly must preserve the declared properties. */
 89:   if (!PetscDefined(USE_COMPLEX)) {
 90:     for (PetscInt i = 0; i < 2; ++i) {
 91:       PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
 92:       PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
 93:       PetscCall(CheckProperties(B, symmetric, spd));
 94:     }
 95:   }
 96:   PetscCall(MatLMVMReset(B, PETSC_FALSE));
 97:   PetscCall(CheckProperties(B, symmetric, spd));
 98:   PetscCall(MatLMVMReset(B, PETSC_TRUE));
 99:   PetscCall(MatSetUp(B));
100:   PetscCall(CheckProperties(B, symmetric, spd));
101:   for (PetscInt i = 0; i <= m; ++i) {
102:     PetscCall(VecSetRandom(x, rand));
103:     PetscCall(VecPointwiseMult(f, d, x));
104:     PetscCall(MatLMVMUpdate(B, x, f));
105:     PetscCall(CheckProperties(B, symmetric, spd));
106:   }
107:   PetscCall(PetscRandomDestroy(&rand));
108:   PetscCall(VecDestroy(&d));
109:   PetscCall(VecDestroy(&f));
110:   PetscCall(VecDestroy(&x));
111:   PetscFunctionReturn(PETSC_SUCCESS);
112: }

114: int main(int argc, char **argv)
115: {
116:   PetscInt  type = 0, n = 10;
117:   Mat       B;
118:   PetscBool properties = PETSC_FALSE;

120:   PetscFunctionBeginUser;
121:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
122:   PetscCall(KSPInitializePackage());
123:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, help, "KSP");
124:   /* LMVM Types. 0: LMVMDBFGS, 1: LMVMDDFP, 2: LMVMDQN */
125:   PetscCall(PetscOptionsInt("-type", "LMVM Type", __FILE__, type, &type, NULL));
126:   PetscCall(PetscOptionsBool("-test_properties", "Test symmetry and positive definiteness", NULL, properties, &properties, NULL));
127:   PetscOptionsEnd();
128:   if (properties) {
129:     PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
130:     PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, n, n));
131:   } else if (type == 0) {
132:     PetscCall(MatCreateLMVMDBFGS(PETSC_COMM_WORLD, PETSC_DECIDE, n, &B));
133:   } else if (type == 1) {
134:     PetscCall(MatCreateLMVMDDFP(PETSC_COMM_WORLD, PETSC_DECIDE, n, &B));
135:   } else if (type == 2) {
136:     PetscCall(MatCreateLMVMDQN(PETSC_COMM_WORLD, PETSC_DECIDE, n, &B));
137:   } else {
138:     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP, "Incompatible LMVM Type.");
139:   }
140:   PetscCall(MatSetFromOptions(B));
141:   PetscCall(MatSetUp(B));
142:   if (properties) PetscCall(TestProperties(B));
143:   else PetscCall(MatLMVMDenseSetType(B, MAT_LMVM_DENSE_INPLACE));
144:   PetscCall(MatDestroy(&B));
145:   PetscCall(PetscFinalize());
146:   return 0;
147: }

149: /*TEST

151:   test:
152:     suffix: 0
153:     output_file: output/empty.out
154:     nsize: {{1 2}}
155:     args: -mat_lmvm_scale_type {{none scalar diagonal}} -type {{0 1 2}}

157:   test:
158:     suffix: properties
159:     requires: !single
160:     output_file: output/empty.out
161:     nsize: {{1 2}}
162:     args: -test_properties -mat_type {{lmvmbfgs lmvmdfp lmvmsymbroyden lmvmsymbadbroyden lmvmdiagbroyden lmvmdbfgs lmvmddfp lmvmdqn lmvmsr1 lmvmbroyden lmvmbadbroyden}}

164: TEST*/