From 1a58153b609f4bbbdf56966a0476d5e46e2c6af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 1 May 2025 07:57:15 +0200 Subject: [PATCH 1/4] Add docstring to _reinterpret_unsafe --- src/Nonlinear/ReverseAD/utils.jl | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Nonlinear/ReverseAD/utils.jl b/src/Nonlinear/ReverseAD/utils.jl index 231eb173b8..6b2a7bd325 100644 --- a/src/Nonlinear/ReverseAD/utils.jl +++ b/src/Nonlinear/ReverseAD/utils.jl @@ -164,9 +164,41 @@ function _UnsafeLowerTriangularMatrixView(x::Vector{Float64}, N::Int) return _UnsafeLowerTriangularMatrixView(N, pointer(x)) end +""" + _reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R} + +Return an `_UnsafeVectorView` that act as a vector of element type +`T` over the same bytes as `x`. Note that if `length(x) * sizeof(R)` is not +a multiple of `sizeof(T)`, the last bits will be ignored. This is a key +difference with `reinterpret` which errors in that case. + +Given a vector of `Float64` of length equal to the maximum number of nodes of a +set of expressions time the maximum chunk size, this function is used to +reinterpret it as a vector of `ForwardDiff.Partials{N,T}` where `N` is the +chunk size of one of the expressions of the set. In that case, we know that +the vector has enough bytes and we don't care about the leftover bytes at the +end. + +## Examples + +```jldoctest +julia> x = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] +3-element Vector{Tuple{Int64, Int64, Int64}}: + (1, 2, 3) + (4, 5, 6) + (7, 8, 9) + +julia> MOI.Nonlinear.ReverseAD._reinterpret_unsafe(NTuple{2,Int}, x) +4-element MathOptInterface.Nonlinear.ReverseAD._UnsafeVectorView{Tuple{Int64, Int64}}: + (1, 2) + (3, 4) + (5, 6) + (7, 8) +``` +""" function _reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R} - # how many T's fit into x? @assert isbitstype(T) && isbitstype(R) + # how many T's fit into x? len = length(x) * sizeof(R) p = reinterpret(Ptr{T}, pointer(x)) return _UnsafeVectorView(0, div(len, sizeof(T)), p) From 3f5bfd48459f31fcc66b72ff13f946f93dafed39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 1 May 2025 08:21:51 +0200 Subject: [PATCH 2/4] Add DocTestSetup --- docs/make.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/make.jl b/docs/make.jl index 644a5fb389..f3a7a9828c 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -123,6 +123,13 @@ end # link checking. Inn production we replace this by running the LaTeX build. write(joinpath(@__DIR__, "src", "MathOptInterface.pdf"), "") +DocMeta.setdocmeta!( + MathOptInterface, + :DocTestSetup, + :(import MathOptInterface as MOI); + recursive=true, +) + @time Documenter.makedocs( sitename = "MathOptInterface", authors = "The JuMP core developers and contributors", From c9e0f70640a3e00c04f5330b98fd63977812b18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 1 May 2025 08:41:08 +0200 Subject: [PATCH 3/4] Fix format --- docs/make.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index f3a7a9828c..83a82dd7b6 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -123,11 +123,11 @@ end # link checking. Inn production we replace this by running the LaTeX build. write(joinpath(@__DIR__, "src", "MathOptInterface.pdf"), "") -DocMeta.setdocmeta!( +Documenter.DocMeta.setdocmeta!( MathOptInterface, :DocTestSetup, :(import MathOptInterface as MOI); - recursive=true, + recursive = true, ) @time Documenter.makedocs( From 2701a423aafc6a19d08ca64a702707c5dcf7e207 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 1 May 2025 19:50:57 +1200 Subject: [PATCH 4/4] Update --- docs/make.jl | 7 ------- src/Nonlinear/ReverseAD/utils.jl | 2 ++ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index 83a82dd7b6..644a5fb389 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -123,13 +123,6 @@ end # link checking. Inn production we replace this by running the LaTeX build. write(joinpath(@__DIR__, "src", "MathOptInterface.pdf"), "") -Documenter.DocMeta.setdocmeta!( - MathOptInterface, - :DocTestSetup, - :(import MathOptInterface as MOI); - recursive = true, -) - @time Documenter.makedocs( sitename = "MathOptInterface", authors = "The JuMP core developers and contributors", diff --git a/src/Nonlinear/ReverseAD/utils.jl b/src/Nonlinear/ReverseAD/utils.jl index 6b2a7bd325..1b92fcd33b 100644 --- a/src/Nonlinear/ReverseAD/utils.jl +++ b/src/Nonlinear/ReverseAD/utils.jl @@ -182,6 +182,8 @@ end. ## Examples ```jldoctest +julia> import MathOptInterface as MOI + julia> x = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] 3-element Vector{Tuple{Int64, Int64, Int64}}: (1, 2, 3)