-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_example_3D.jl
94 lines (79 loc) · 2.6 KB
/
fft_example_3D.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Random, Distributions
using MadNLPGPU, CUDA
using Test
Random.seed!(1)
include("fft_model.jl")
include("solver.jl")
## 3D
function fft_example_3D(N1::Int, N2::Int, N3::Int; gpu::Bool=false, rdft::Bool=false, check::Bool=false)
idx1 = collect(0:(N1-1))
idx2 = collect(0:(N2-1))
idx3 = collect(0:(N3-1))
x = [(cos(2*pi*1/N1*i)+ 2*sin(2*pi*1/N1*i))*(cos(2*pi*2/N2*j) + 2*sin(2*pi*2/N2*j))*(cos(2*pi*3/N3*k) + 2*sin(2*pi*3/N3*k)) for i in idx1, j in idx2, k in idx3]
y = check ? x : x + rand(N1, N2, N3) # noisy signal
w = fft(x) ./ sqrt(N1*N2*N3) # true DFT
DFTsize = size(x) # problem dim
DFTdim = length(DFTsize) # problem size
# randomly generate missing indices
if check
index_missing = Int[]
z_zero = y
else
missing_prob = 0.15
centers = centering(DFTdim, DFTsize, missing_prob)
radius = 1
index_missing, z_zero = punching(DFTdim, DFTsize, centers, radius, y)
# println("length(index_missing) = ", length(index_missing))
end
# unify parameters for barrier method
M_perptz = M_perp_tz_wei(DFTdim, DFTsize, z_zero)
if gpu
M_perptz = CuArray(M_perptz)
end
lambda = check ? 0 : 5
alpha_LS = 0.1
gamma_LS = 0.8
eps_NT = 1e-6
eps_barrier = 1e-6
mu_barrier = 10
parameters = FFTParameters(DFTdim, DFTsize, M_perptz, lambda, index_missing, alpha_LS, gamma_LS, eps_NT, mu_barrier, eps_barrier)
t_init = 1
beta_init = zeros(prod(DFTsize))
c_init = ones(prod(DFTsize))
S = gpu ? CuVector{Float64} : Vector{Float64}
nlp = FFTNLPModel{Float64, S}(parameters; rdft)
# Solve with MadNLP/LBFGS
# solver = MadNLP.MadNLPSolver(nlp; hessian_approximation=MadNLP.CompactLBFGS)
# results = MadNLP.solve!(solver)
# beta_MadNLP = results.solution[1:N1*N2*N3]
# Solve with MadNLP/CG
t1 = time()
solver = MadNLP.MadNLPSolver(
nlp;
max_iter=2000,
kkt_system=FFTKKTSystem,
print_level=MadNLP.INFO,
nlp_scaling=false,
dual_initialized=true,
richardson_max_iter=0,
tol=1e-8,
richardson_tol=Inf,
)
results = ipm_solve!(solver)
t2 = time()
if check
beta_MadNLP = results.solution[1:N1*N2*N3]
beta_true = DFT_to_beta(DFTdim, DFTsize, gpu ? CuArray(w) : w)
@test norm(beta_true - beta_MadNLP) ≤ 1e-6
end
return nlp, solver, results, t2-t1
end
N1 = 8
N2 = 8
N3 = 8
gpu = false
rdft = true
check = false
nlp, solver, results, timer = fft_example_3D(N1, N2, N3; gpu, rdft, check)
beta_MadNLP = results.solution[1:N1*N2*N3]
println("Timer: $(timer)")