@@ -112,11 +112,15 @@ function SparseEnzymeADJacobian(
112112 x0:: AbstractVector = rand (nvar),
113113 coloring_algorithm:: AbstractColoringAlgorithm = GreedyColoringAlgorithm {:direct} (),
114114 detector:: AbstractSparsityDetector = TracerSparsityDetector (),
115+ show_time:: Bool = false ,
115116 kwargs... ,
116117)
117- output = similar (x0, ncon)
118- J = compute_jacobian_sparsity (c!, output, x0, detector = detector)
119- SparseEnzymeADJacobian (nvar, f, ncon, c!, J; x0, coloring_algorithm, kwargs... )
118+ timer = @elapsed begin
119+ output = similar (x0, ncon)
120+ J = compute_jacobian_sparsity (c!, output, x0, detector = detector)
121+ end
122+ show_time && println (" • Sparsity pattern detection of the Jacobian: $timer seconds." )
123+ SparseEnzymeADJacobian (nvar, f, ncon, c!, J; x0, coloring_algorithm, show_time, kwargs... )
120124end
121125
122126function SparseEnzymeADJacobian (
@@ -127,18 +131,26 @@ function SparseEnzymeADJacobian(
127131 J:: SparseMatrixCSC{Bool, Int} ;
128132 x0:: AbstractVector{T} = rand (nvar),
129133 coloring_algorithm:: AbstractColoringAlgorithm = GreedyColoringAlgorithm {:direct} (),
134+ show_time:: Bool = false ,
130135 kwargs... ,
131136) where {T}
132- # We should support :row and :bidirectional in the future
133- problem = ColoringProblem {:nonsymmetric, :column} ()
134- result_coloring = coloring (J, problem, coloring_algorithm, decompression_eltype = T)
137+ timer = @elapsed begin
138+ # We should support :row and :bidirectional in the future
139+ problem = ColoringProblem {:nonsymmetric, :column} ()
140+ result_coloring = coloring (J, problem, coloring_algorithm, decompression_eltype = T)
141+
142+ rowval = J. rowval
143+ colptr = J. colptr
144+ nzval = T .(J. nzval)
145+ compressed_jacobian = similar (x0, ncon)
146+ end
147+ show_time && println (" • Coloring of the sparse Jacobian: $timer seconds." )
135148
136- rowval = J. rowval
137- colptr = J. colptr
138- nzval = T .(J. nzval)
139- compressed_jacobian = similar (x0, ncon)
140- v = similar (x0)
141- cx = zeros (T, ncon)
149+ timer = @elapsed begin
150+ v = similar (x0)
151+ cx = zeros (T, ncon)
152+ end
153+ show_time && println (" • Allocation of the AD buffers for the sparse Jacobian: $timer seconds." )
142154
143155 SparseEnzymeADJacobian (
144156 nvar,
@@ -177,10 +189,14 @@ function SparseEnzymeADHessian(
177189 x0:: AbstractVector = rand (nvar),
178190 coloring_algorithm:: AbstractColoringAlgorithm = GreedyColoringAlgorithm {:substitution} (),
179191 detector:: AbstractSparsityDetector = TracerSparsityDetector (),
192+ show_time:: Bool = false ,
180193 kwargs... ,
181194)
182- H = compute_hessian_sparsity (f, nvar, c!, ncon, detector = detector)
183- SparseEnzymeADHessian (nvar, f, ncon, c!, H; x0, coloring_algorithm, kwargs... )
195+ timer = @elapsed begin
196+ H = compute_hessian_sparsity (f, nvar, c!, ncon, detector = detector)
197+ end
198+ show_time && println (" • Sparsity pattern detection of the Hessian: $timer seconds." )
199+ SparseEnzymeADHessian (nvar, f, ncon, c!, H; x0, coloring_algorithm, show_time, kwargs... )
184200end
185201
186202function SparseEnzymeADHessian (
@@ -191,38 +207,48 @@ function SparseEnzymeADHessian(
191207 H:: SparseMatrixCSC{Bool, Int} ;
192208 x0:: AbstractVector{T} = rand (nvar),
193209 coloring_algorithm:: AbstractColoringAlgorithm = GreedyColoringAlgorithm {:substitution} (),
210+ show_time:: Bool = false ,
194211 kwargs... ,
195212) where {T}
196- problem = ColoringProblem {:symmetric, :column} ()
197- result_coloring = coloring (H, problem, coloring_algorithm, decompression_eltype = T)
198-
199- trilH = tril (H)
200- rowval = trilH. rowval
201- colptr = trilH. colptr
202- nzval = T .(trilH. nzval)
203- if coloring_algorithm isa GreedyColoringAlgorithm{:direct }
204- coloring_mode = :direct
205- compressed_hessian_icol = similar (x0)
206- compressed_hessian = compressed_hessian_icol
207- else
208- coloring_mode = :substitution
209- group = column_groups (result_coloring)
210- ncolors = length (group)
211- compressed_hessian_icol = similar (x0)
212- compressed_hessian = similar (x0, (nvar, ncolors))
213+ timer = @elapsed begin
214+ problem = ColoringProblem {:symmetric, :column} ()
215+ result_coloring = coloring (H, problem, coloring_algorithm, decompression_eltype = T)
216+
217+ trilH = tril (H)
218+ rowval = trilH. rowval
219+ colptr = trilH. colptr
220+ nzval = T .(trilH. nzval)
221+ if coloring_algorithm isa GreedyColoringAlgorithm{:direct }
222+ coloring_mode = :direct
223+ compressed_hessian_icol = similar (x0)
224+ compressed_hessian = compressed_hessian_icol
225+ else
226+ coloring_mode = :substitution
227+ group = column_groups (result_coloring)
228+ ncolors = length (group)
229+ compressed_hessian_icol = similar (x0)
230+ compressed_hessian = similar (x0, (nvar, ncolors))
231+ end
213232 end
214- v = similar (x0)
215- y = similar (x0, ncon)
216- cx = similar (x0, ncon)
217- grad = similar (x0)
218- function ℓ (x, y, obj_weight, cx)
219- res = obj_weight * f (x)
220- if ncon != 0
221- c! (cx, x)
222- res += sum (cx[i] * y[i] for i = 1 : ncon)
233+ show_time && println (" • Coloring of the sparse Hessian: $timer seconds." )
234+
235+ timer = @elapsed begin
236+ v = similar (x0)
237+ y = similar (x0, ncon)
238+ cx = similar (x0, ncon)
239+ grad = similar (x0)
240+
241+ function ℓ (x, y, obj_weight, cx)
242+ res = obj_weight * f (x)
243+ if ncon != 0
244+ c! (cx, x)
245+ res += sum (cx[i] * y[i] for i = 1 : ncon)
246+ end
247+ return res
223248 end
224- return res
225249 end
250+ show_time && println (" • Allocation of the AD buffers for the sparse Hessian: $timer seconds." )
251+
226252
227253 return SparseEnzymeADHessian (
228254 nvar,
0 commit comments