File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ module MyModule
2+ include (" ./optimization_once_per_process.jl" )
3+ end
4+
5+ function (@main )(argv:: Vector{String} ):: Cint
6+ λ = parse (Float64, argv[1 ])
7+ sol = MyModule. TestModuleTrimmable. minimize (λ)
8+ println (Core. stdout , sum (sol. u))
9+ return 0
10+ end
Original file line number Diff line number Diff line change 1+ module TestModuleTrimmable
2+ using NonlinearSolveFirstOrder
3+ using DiffEqBase
4+ using ADTypes: AutoForwardDiff
5+ using ForwardDiff
6+ using LinearAlgebra
7+ using StaticArrays
8+ using LinearSolve
9+ import SciMLBase
10+ const LS = LinearSolve
11+
12+ function f (u, p)
13+ L, U = cholesky (p. Σ)
14+ rhs = (u .* u .- p. λ)
15+ # there are some issues currently with LinearSolve and triangular matrices,
16+ # so we just make `L` dense here.
17+ linprob = LinearProblem (Matrix (L), rhs)
18+ alg = LS. GenericLUFactorization ()
19+ sol = LinearSolve. solve (linprob, alg)
20+ return sol. u
21+ end
22+
23+ struct MyParams{T, M}
24+ λ:: T
25+ Σ:: M
26+ end
27+
28+ const cache = OncePerProcess () do
29+ autodiff = AutoForwardDiff (; chunksize = 1 )
30+ alg = TrustRegion (; autodiff, linsolve = LS. CholeskyFactorization ())
31+ prob = NonlinearLeastSquaresProblem {false} (
32+ f,
33+ rand (2 ),
34+ MyParams (rand (), hermitianpart (rand (2 , 2 ) + 2 I))
35+ )
36+ init (prob, alg)
37+ end
38+
39+ function minimize (x)
40+ ps = MyParams (x, hermitianpart (rand (2 , 2 ) + 2 I))
41+ reinit! (cache (), rand (2 ); p = ps)
42+ solve! (cache ())
43+ return cache
44+ end
45+ end
You can’t perform that action at this time.
0 commit comments