Actual source code: ex1.c

  1: const char help[] = "Test TAOLMVM on a least-squares problem";

  3: #include <petsctao.h>
  4: #include <petscdevice.h>

  6: typedef struct _n_AppCtx {
  7:   Mat A;
  8:   Vec b;
  9:   Vec r;
 10: } AppCtx;

 12: static PetscErrorCode LSObjAndGrad(Tao tao, Vec x, PetscReal *obj, Vec g, void *_ctx)
 13: {
 14:   PetscFunctionBegin;
 15:   AppCtx *ctx = (AppCtx *)_ctx;
 16:   PetscCall(VecAXPBY(ctx->r, -1.0, 0.0, ctx->b));
 17:   PetscCall(MatMultAdd(ctx->A, x, ctx->r, ctx->r));
 18:   PetscCall(VecDotRealPart(ctx->r, ctx->r, obj));
 19:   *obj *= 0.5;
 20:   PetscCall(MatMultTranspose(ctx->A, ctx->r, g));
 21:   PetscFunctionReturn(PETSC_SUCCESS);
 22: }

 24: int main(int argc, char **argv)
 25: {
 26:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 27:   AppCtx    ctx;
 28:   Vec       sol;
 29:   PetscBool flg, cuda = PETSC_FALSE;

 31:   PetscInt M = 10;
 32:   PetscInt N = 10;
 33:   PetscOptionsBegin(PETSC_COMM_WORLD, "", help, "TAO");
 34:   PetscCall(PetscOptionsInt("-m", "data size", NULL, M, &M, NULL));
 35:   PetscCall(PetscOptionsInt("-n", "data size", NULL, N, &N, NULL));
 36:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-cuda", &cuda, &flg));
 37:   PetscOptionsEnd();

 39:   if (cuda) {
 40:     VecType vec_type;
 41:     PetscCall(VecCreateSeqCUDA(PETSC_COMM_WORLD, N, &ctx.b));
 42:     PetscCall(VecGetType(ctx.b, &vec_type));
 43:     PetscCall(MatCreateDenseFromVecType(PETSC_COMM_WORLD, vec_type, M, N, PETSC_DECIDE, PETSC_DECIDE, -1, NULL, &ctx.A));
 44:     PetscCall(MatCreateVecs(ctx.A, &sol, NULL));
 45:   } else {
 46:     PetscCall(MatCreateDense(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, M, N, NULL, &ctx.A));
 47:     PetscCall(MatCreateVecs(ctx.A, &sol, &ctx.b));
 48:   }
 49:   PetscCall(VecDuplicate(ctx.b, &ctx.r));

 51:   PetscRandom rand;
 52:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rand));
 53:   PetscCall(PetscRandomSetFromOptions(rand));
 54:   PetscCall(MatSetRandom(ctx.A, rand));
 55:   PetscCall(VecSetRandom(ctx.b, rand));
 56:   PetscCall(PetscRandomDestroy(&rand));

 58:   Tao tao;
 59:   PetscCall(TaoCreate(PETSC_COMM_WORLD, &tao));
 60:   PetscCall(TaoSetSolution(tao, sol));
 61:   PetscCall(TaoSetObjectiveAndGradient(tao, NULL, LSObjAndGrad, &ctx));
 62:   PetscCall(TaoSetType(tao, TAOLMVM));
 63:   PetscCall(TaoSetFromOptions(tao));
 64:   PetscCall(TaoSolve(tao));
 65:   PetscCall(TaoDestroy(&tao));

 67:   PetscCall(VecDestroy(&ctx.r));
 68:   PetscCall(VecDestroy(&sol));
 69:   PetscCall(VecDestroy(&ctx.b));
 70:   PetscCall(MatDestroy(&ctx.A));

 72:   PetscCall(PetscFinalize());
 73:   return 0;
 74: }

 76: /*TEST

 78:   build:
 79:     requires: !complex !__float128 !single !defined(PETSC_USE_64BIT_INDICES)

 81:   test:
 82:     suffix: 0
 83:     args: -tao_monitor -tao_ls_gtol 1.e-6 -tao_view -tao_lmvm_mat_lmvm_hist_size 20 -tao_ls_type more-thuente -tao_lmvm_mat_lmvm_scale_type none -tao_lmvm_mat_type lmvmbfgs

 85:   test:
 86:     suffix: 1
 87:     args: -tao_monitor -tao_ls_gtol 1.e-6 -tao_view -tao_lmvm_mat_lmvm_hist_size 20 -tao_ls_type more-thuente -tao_lmvm_mat_lmvm_scale_type none -tao_lmvm_mat_type lmvmdbfgs

 89:   test:
 90:     suffix: 2
 91:     args: -tao_monitor -tao_ls_gtol 1.e-6 -tao_view -tao_lmvm_mat_lmvm_hist_size 20 -tao_ls_type more-thuente -tao_lmvm_mat_type lmvmdbfgs -tao_lmvm_mat_lmvm_scale_type none -tao_lmvm_mat_lbfgs_type {{inplace reorder}}

 93: TEST*/