Actual source code: solve_performance.c

  1: const char help[] = "Profile the performance of MATLMVM MatSolve() in a loop";

  3: #include <petscksp.h>

  5: int main(int argc, char **argv)
  6: {
  7:   PetscInt      n        = 1000;
  8:   PetscInt      n_epochs = 10;
  9:   PetscInt      n_iters  = 10;
 10:   Vec           x, g, dx, df, p;
 11:   PetscRandom   rand;
 12:   PetscLogStage matsolve_loop, main_stage;
 13:   Mat           B, J0;

 15:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 16:   PetscCall(KSPInitializePackage());
 17:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, help, "KSP");
 18:   PetscCall(PetscOptionsInt("-n", "Vector size", __FILE__, n, &n, NULL));
 19:   PetscCall(PetscOptionsInt("-epochs", "Number of epochs", __FILE__, n_epochs, &n_epochs, NULL));
 20:   PetscCall(PetscOptionsInt("-iters", "Number of iterations per epoch", __FILE__, n_iters, &n_iters, NULL));
 21:   PetscOptionsEnd();
 22:   PetscCall(VecCreateMPI(PETSC_COMM_WORLD, PETSC_DETERMINE, n, &x));
 23:   PetscCall(VecSetFromOptions(x));
 24:   PetscCall(VecDuplicate(x, &g));
 25:   PetscCall(VecDuplicate(x, &dx));
 26:   PetscCall(VecDuplicate(x, &df));
 27:   PetscCall(VecDuplicate(x, &p));
 28:   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
 29:   PetscCall(MatSetType(B, MATLMVMBFGS));
 30:   PetscCall(MatLMVMAllocate(B, x, g));
 31:   PetscCall(MatSetFromOptions(B));
 32:   PetscCall(MatLMVMGetJ0(B, &J0));
 33:   PetscCall(MatZeroEntries(J0));
 34:   PetscCall(MatShift(J0, 1.0));
 35:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rand));
 36:   PetscCall(PetscRandomSetInterval(rand, -1.0, 1.0));
 37:   PetscCall(PetscRandomSetFromOptions(rand));
 38:   PetscCall(PetscLogStageRegister("LMVM MatSolve Loop", &matsolve_loop));
 39:   PetscCall(PetscLogStageGetId("Main Stage", &main_stage));
 40:   PetscCall(PetscLogStageSetVisible(main_stage, PETSC_FALSE));
 41:   for (PetscInt epoch = 0; epoch < n_epochs + 1; epoch++) {
 42:     PetscScalar dot;
 43:     PetscReal   xscale, fscale, absdot;
 44:     PetscInt    history_size;

 46:     PetscCall(VecSetRandom(dx, rand));
 47:     PetscCall(VecSetRandom(df, rand));
 48:     PetscCall(VecDot(dx, df, &dot));
 49:     absdot = PetscAbsScalar(dot);
 50:     PetscCall(VecSetRandom(x, rand));
 51:     PetscCall(VecSetRandom(g, rand));
 52:     xscale = 1.0;
 53:     fscale = absdot / PetscRealPart(dot);
 54:     PetscCall(MatLMVMGetHistorySize(B, &history_size));

 56:     PetscCall(MatLMVMUpdate(B, x, g));
 57:     for (PetscInt iter = 0; iter < history_size; iter++, xscale *= -1.0, fscale *= -1.0) {
 58:       PetscCall(VecAXPY(x, xscale, dx));
 59:       PetscCall(VecAXPY(g, fscale, df));
 60:       PetscCall(MatLMVMUpdate(B, x, g));
 61:       PetscCall(MatSolve(B, g, p));
 62:     }
 63:     if (epoch > 0) PetscCall(PetscLogStagePush(matsolve_loop));
 64:     for (PetscInt iter = 0; iter < n_iters; iter++, xscale *= -1.0, fscale *= -1.0) {
 65:       PetscCall(VecAXPY(x, xscale, dx));
 66:       PetscCall(VecAXPY(g, fscale, df));
 67:       PetscCall(MatLMVMUpdate(B, x, g));
 68:       PetscCall(MatSolve(B, g, p));
 69:     }
 70:     PetscCall(MatLMVMReset(B, PETSC_FALSE));
 71:     if (epoch > 0) PetscCall(PetscLogStagePop());
 72:   }
 73:   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD), PETSC_VIEWER_ASCII_INFO_DETAIL));
 74:   PetscCall(MatView(B, PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD)));
 75:   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD)));
 76:   PetscCall(PetscRandomDestroy(&rand));
 77:   PetscCall(MatDestroy(&B));
 78:   PetscCall(VecDestroy(&p));
 79:   PetscCall(VecDestroy(&df));
 80:   PetscCall(VecDestroy(&dx));
 81:   PetscCall(VecDestroy(&g));
 82:   PetscCall(VecDestroy(&x));
 83:   PetscCall(PetscFinalize());
 84:   return 0;
 85: }

 87: /*TEST

 89:   test:
 90:     suffix: 0
 91:     args: -mat_lmvm_scale_type none

 93: TEST*/