Actual source code: ex1.c
1: const char help[] = "Test TAOLMVM on a least-squares problem";
3: #include <petsctao.h>
5: typedef struct _n_AppCtx {
6: Mat A;
7: Vec b;
8: Vec r;
9: } AppCtx;
11: static PetscErrorCode LSObjAndGrad(Tao tao, Vec x, PetscReal *obj, Vec g, void *_ctx)
12: {
13: PetscFunctionBegin;
14: AppCtx *ctx = (AppCtx *)_ctx;
15: PetscCall(VecAXPBY(ctx->r, -1.0, 0.0, ctx->b));
16: PetscCall(MatMultAdd(ctx->A, x, ctx->r, ctx->r));
17: PetscCall(VecDotRealPart(ctx->r, ctx->r, obj));
18: *obj *= 0.5;
19: PetscCall(MatMultTranspose(ctx->A, ctx->r, g));
20: PetscFunctionReturn(PETSC_SUCCESS);
21: }
23: int main(int argc, char **argv)
24: {
25: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
26: AppCtx ctx;
27: Vec sol;
28: PetscBool flg, cuda = PETSC_FALSE;
30: PetscInt M = 10;
31: PetscInt N = 10;
32: PetscOptionsBegin(PETSC_COMM_WORLD, "", help, "TAO");
33: PetscCall(PetscOptionsInt("-m", "data size", NULL, M, &M, NULL));
34: PetscCall(PetscOptionsInt("-n", "data size", NULL, N, &N, NULL));
35: PetscCall(PetscOptionsGetBool(NULL, NULL, "-cuda", &cuda, &flg));
36: PetscOptionsEnd();
38: if (cuda) {
39: VecType vec_type;
40: PetscCall(VecCreateSeqCUDA(PETSC_COMM_WORLD, N, &ctx.b));
41: PetscCall(VecGetType(ctx.b, &vec_type));
42: PetscCall(MatCreateDenseFromVecType(PETSC_COMM_WORLD, vec_type, M, N, PETSC_DECIDE, PETSC_DECIDE, -1, NULL, &ctx.A));
43: PetscCall(MatCreateVecs(ctx.A, &sol, NULL));
44: } else {
45: PetscCall(MatCreateDense(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, M, N, NULL, &ctx.A));
46: PetscCall(MatCreateVecs(ctx.A, &sol, &ctx.b));
47: }
48: PetscCall(VecDuplicate(ctx.b, &ctx.r));
50: PetscRandom rand;
51: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rand));
52: PetscCall(PetscRandomSetFromOptions(rand));
53: PetscCall(MatSetRandom(ctx.A, rand));
54: PetscCall(VecSetRandom(ctx.b, rand));
55: PetscCall(PetscRandomDestroy(&rand));
57: Tao tao;
58: PetscCall(TaoCreate(PETSC_COMM_WORLD, &tao));
59: PetscCall(TaoSetSolution(tao, sol));
60: PetscCall(TaoSetObjectiveAndGradient(tao, NULL, LSObjAndGrad, &ctx));
61: PetscCall(TaoSetType(tao, TAOLMVM));
62: PetscCall(TaoSetFromOptions(tao));
63: PetscCall(TaoSolve(tao));
64: PetscCall(TaoDestroy(&tao));
66: PetscCall(VecDestroy(&ctx.r));
67: PetscCall(VecDestroy(&sol));
68: PetscCall(VecDestroy(&ctx.b));
69: PetscCall(MatDestroy(&ctx.A));
71: PetscCall(PetscFinalize());
72: return 0;
73: }
75: /*TEST
77: build:
78: requires: !complex !__float128 !single !defined(PETSC_USE_64BIT_INDICES)
80: test:
81: suffix: 0
82: 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
84: test:
85: suffix: 1
86: 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
88: test:
89: suffix: 2
90: 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}}
92: TEST*/