Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1011 - Convert to Vector in polyhedron's LP #1013

Merged
merged 4 commits into from
Jan 16, 2019
Merged

#1011 - Convert to Vector in polyhedron's LP #1013

merged 4 commits into from
Jan 16, 2019

Conversation

mforets
Copy link
Member

@mforets mforets commented Jan 15, 2019

Closes #1011.

@@ -152,7 +152,7 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real}
end

function σ_helper(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real}
c = -d
c = convert(Vector{N}, -d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this looks like it creates two vectors (I am not 100% sure, though).

julia> f(d) = convert(Vector{N}, -d);

julia> @code_lowered f(d)
CodeInfo(
1 1%1 = (Core.apply_type)(Main.Vector, Main.N)                                                                                │
  │   %2 = -d                                                                                                                    │
  │   %3 = (Main.convert)(%1, %2)                                                                                                │
  └──      return %3                                                                                                             │
)

Since this is a critical function, I would care for such things. What about this:

julia> g(d) = [-x for x in d];

julia> @code_lowered g(d)
CodeInfo(
1 1%1 = (Base.Generator)(Main.:-, d)                                                                                          │
  │   %2 = (Base.collect)(%1)                                                                                                    │
  └──      return %2                                                                                                             │
)

Copy link
Member Author

@mforets mforets Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider these 3 ways:

using SparseArrays, BenchmarkTools

function f1(d::AbstractVector{N}) where {N}
    return convert(Vector{N}, -d)
end

function f2(d::AbstractVector{N}) where {N}
    return [-x for x in d]
end

function f3(d::AbstractVector{N}) where {N}
    return Vector(-d)
end

(in julia v1.0.2) they give similar but not the same timings, especially in higher dims:

for n in [10, 100, 1000, 10000]
    println("\n n = $n ")
    d = sprandn(n, 0.1)
    @btime f1($d)
    @btime f2($d)
    @btime f3($d)
end

 n = 10 
  122.701 ns (4 allocations: 352 bytes)
  64.208 ns (2 allocations: 176 bytes)
  118.216 ns (4 allocations: 352 bytes)

 n = 100 
  489.149 ns (4 allocations: 1.19 KiB)
  495.479 ns (2 allocations: 912 bytes)
  162.628 ns (4 allocations: 1.19 KiB)

 n = 1000 
  11.803 μs (4 allocations: 9.94 KiB)
  8.526 μs (2 allocations: 7.95 KiB)
  796.970 ns (4 allocations: 9.94 KiB)

 n = 10000 
  161.891 μs (5 allocations: 94.11 KiB)
  171.660 μs (3 allocations: 78.22 KiB)
  7.230 μs (5 allocations: 94.11 KiB)

Seems like the 2nd option is preferable for "small" vectors (< 100) and the third one for > 100 vectors.

Copy link
Member Author

@mforets mforets Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A faster version for sparse vectors:

function f4(d::SparseVector{N}) where {N}
    c = zeros(length(d))
    for (ni, i) in enumerate(d.nzind)
        @inbounds c[i] = -d.nzval[ni]
    end
    return c
end

# (see 4th row)
>
>  n = 10 
>   143.405 ns (4 allocations: 384 bytes)
>   73.122 ns (2 allocations: 176 bytes)
>   118.938 ns (4 allocations: 384 bytes)
>   42.025 ns (1 allocation: 160 bytes)
> 
>  n = 100 
>   520.623 ns (4 allocations: 1.19 KiB)
>   542.469 ns (2 allocations: 912 bytes)
>   162.908 ns (4 allocations: 1.19 KiB)
>   82.381 ns (1 allocation: 896 bytes)
> 
>  n = 1000 
>   8.543 μs (4 allocations: 9.41 KiB)
>   7.568 μs (2 allocations: 7.95 KiB)
>   735.254 ns (4 allocations: 9.41 KiB)
>   542.567 ns (1 allocation: 7.94 KiB)
> 
>  n = 10000 
>   159.176 μs (5 allocations: 93.48 KiB)
>   168.338 μs (3 allocations: 78.22 KiB)
>   7.569 μs (5 allocations: 93.48 KiB)
>   6.690 μs (2 allocations: 78.20 KiB)
> 
> 

EDIT: combining these codes for dense/sparse vectors we arrive at this solution:

@inline function _to_minus_vector(d::SparseVector)
    c = zeros(length(d))
    for (ni, i) in enumerate(d.nzind)
        @inbounds c[i] = -d.nzval[ni]
    end
    return c
end

@inline function _to_minus_vector(d::AbstractVector{N}) where {N}
    return convert(Vector{N}, -d)
end

function f5(d::AbstractVector{N}) where {N}
    return _to_minus_vector(d)
end


for n in [10, 100, 1000, 10000]
    println("\n n = $n ")

    println("\n  dense\n")
    d = randn(n)
    @btime f1($d)
    @btime f2($d)
    @btime f3($d)
    @btime f5($d)
    
    println("\n   sparse\n")
    d = sprandn(n, 0.1)
    @btime f1($d)
    @btime f2($d)
    @btime f3($d)
    @btime f5($d)
end

 n = 10 
    dense

  41.935 ns (1 allocation: 160 bytes)
  48.410 ns (2 allocations: 176 bytes)
  84.796 ns (2 allocations: 320 bytes)
  41.692 ns (1 allocation: 160 bytes)
    sparse

  140.612 ns (4 allocations: 384 bytes)
  79.601 ns (2 allocations: 176 bytes)
  121.206 ns (4 allocations: 384 bytes)
  40.700 ns (1 allocation: 160 bytes)

 n = 100 
    dense

  80.613 ns (1 allocation: 896 bytes)
  82.760 ns (2 allocations: 912 bytes)
  161.918 ns (2 allocations: 1.75 KiB)
  81.906 ns (1 allocation: 896 bytes)
    sparse

  576.656 ns (4 allocations: 1.25 KiB)
  555.898 ns (2 allocations: 912 bytes)
  168.000 ns (4 allocations: 1.25 KiB)
  82.758 ns (1 allocation: 896 bytes)

 n = 1000 
    dense

  539.077 ns (1 allocation: 7.94 KiB)
  523.103 ns (2 allocations: 7.95 KiB)
  1.179 μs (2 allocations: 15.88 KiB)
  514.635 ns (1 allocation: 7.94 KiB)
    sparse

  8.839 μs (4 allocations: 9.56 KiB)
  7.727 μs (2 allocations: 7.95 KiB)
  752.535 ns (4 allocations: 9.56 KiB)
  591.906 ns (1 allocation: 7.94 KiB)

 n = 10000 
    dense

  5.393 μs (2 allocations: 78.20 KiB)
  5.553 μs (3 allocations: 78.22 KiB)
  9.688 μs (4 allocations: 156.41 KiB)
  5.403 μs (2 allocations: 78.20 KiB)
    sparse

  162.125 μs (5 allocations: 93.86 KiB)
  171.141 μs (3 allocations: 78.22 KiB)
  7.579 μs (5 allocations: 93.86 KiB)
  6.501 μs (2 allocations: 78.20 KiB)

@@ -151,8 +151,23 @@ function σ(d::AbstractVector{N}, P::HPoly{N}) where {N<:Real}
end
end

@inline function _to_minus_vector(d::SparseVector)
c = zeros(length(d))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use zeros(N, ...)

src/HPolyhedron.jl Outdated Show resolved Hide resolved
src/HPolyhedron.jl Outdated Show resolved Hide resolved
@mforets mforets merged commit 3ff5b21 into master Jan 16, 2019
@mforets mforets deleted the mforets/1011 branch January 16, 2019 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants