unsafe_wrap from a pointer from a Julia array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

unsafe_wrap from a pointer from a Julia array

Sheehan Olver

In some high performance code I want to convert an 2 x n `Matrix{Float64}` to a n long `Matrix{Vec{2,Float64}}`with no memory allocation (both types will store memory in the same order).  The following works:

v=rand(2,100)
w=unsafe_wrap(Vector{Vec{2,Float64}},
            reinterpret(Ptr{Vec{2,Float64}},pointer(v)),
            size(pts,1),true)

The variable v is a throw away variable, however, and as I understand it, the way its written the memory for w will be freed as soon as v is no longer referenced.  Is there any way to tell the garbage collector to not collect v?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: unsafe_wrap from a pointer from a Julia array

Gunnar Farnebäck
Is this what you're looking for?

julia> using SIMD

julia
> v = rand(2, 5)
2×5 Array{Float64,2}:
 
0.391832  0.785861  0.352291  0.874891  0.874593
 
0.697768  0.51637   0.711525  0.400077  0.266559

julia
> w = reinterpret(Vec{2, Float64}, vec(v))
5-element Array{SIMD.Vec{2,Float64},1}:
 
Float640.391832,0.697768
 
Float640.785861,0.51637
 
Float640.352291,0.711525
 
Float640.874891,0.400077
 
Float640.874593,0.266559


Den onsdag 16 november 2016 kl. 05:52:14 UTC+1 skrev Sheehan Olver:

In some high performance code I want to convert an 2 x n `Matrix{Float64}` to a n long `Matrix{Vec{2,Float64}}`with no memory allocation (both types will store memory in the same order).  The following works:

v=rand(2,100)
w=unsafe_wrap(Vector{Vec{2,Float64}},
            reinterpret(Ptr{Vec{2,Float64}},pointer(v)),
            size(pts,1),true)

The variable v is a throw away variable, however, and as I understand it, the way its written the memory for w will be freed as soon as v is no longer referenced.  Is there any way to tell the garbage collector to not collect v?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: unsafe_wrap from a pointer from a Julia array

Sheehan Olver
👍 

Sent from my iPhone

On 16 Nov. 2016, at 18:39, Gunnar Farnebäck <[hidden email]> wrote:

Is this what you're looking for?

julia> using SIMD

julia
> v = rand(2, 5)
2×5 Array{Float64,2}:
 
0.391832  0.785861  0.352291  0.874891  0.874593
 
0.697768  0.51637   0.711525  0.400077  0.266559

julia
> w = reinterpret(Vec{2, Float64}, vec(v))
5-element Array{SIMD.Vec{2,Float64},1}:
 
Float640.391832,0.697768
 
Float640.785861,0.51637
 
Float640.352291,0.711525
 
Float640.874891,0.400077
 
Float640.874593,0.266559


Den onsdag 16 november 2016 kl. 05:52:14 UTC+1 skrev Sheehan Olver:

In some high performance code I want to convert an 2 x n `Matrix{Float64}` to a n long `Matrix{Vec{2,Float64}}`with no memory allocation (both types will store memory in the same order).  The following works:

v=rand(2,100)
w=unsafe_wrap(Vector{Vec{2,Float64}},
            reinterpret(Ptr{Vec{2,Float64}},pointer(v)),
            size(pts,1),true)

The variable v is a throw away variable, however, and as I understand it, the way its written the memory for w will be freed as soon as v is no longer referenced.  Is there any way to tell the garbage collector to not collect v?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: unsafe_wrap from a pointer from a Julia array

Sheehan Olver
In reply to this post by Gunnar Farnebäck
Oh wait, I was referring to Vec from FixedSizeArrays.jl , not from SIMD


On 16 Nov. 2016, at 6:39 pm, Gunnar Farnebäck <[hidden email]> wrote:

Is this what you're looking for?

julia> using SIMD

julia
> v = rand(2, 5)
2×5 Array{Float64,2}:
 
0.391832  0.785861  0.352291  0.874891  0.874593
 
0.697768  0.51637   0.711525  0.400077  0.266559

julia
> w = reinterpret(Vec{2, Float64}, vec(v))
5-element Array{SIMD.Vec{2,Float64},1}:
 
Float640.391832,0.697768
 
Float640.785861,0.51637
 
Float640.352291,0.711525
 
Float640.874891,0.400077
 
Float640.874593,0.266559


Den onsdag 16 november 2016 kl. 05:52:14 UTC+1 skrev Sheehan Olver:

In some high performance code I want to convert an 2 x n `Matrix{Float64}` to a n long `Matrix{Vec{2,Float64}}`with no memory allocation (both types will store memory in the same order).  The following works:

v=rand(2,100)
w=unsafe_wrap(Vector{Vec{2,Float64}},
            reinterpret(Ptr{Vec{2,Float64}},pointer(v)),
            size(pts,1),true)

The variable v is a throw away variable, however, and as I understand it, the way its written the memory for w will be freed as soon as v is no longer referenced.  Is there any way to tell the garbage collector to not collect v?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: unsafe_wrap from a pointer from a Julia array

Sheehan Olver
Though that code works too for FixedSizeArrays


On 17 Nov. 2016, at 8:53 am, Sheehan Olver <[hidden email]> wrote:

Oh wait, I was referring to Vec from FixedSizeArrays.jl , not from SIMD


On 16 Nov. 2016, at 6:39 pm, Gunnar Farnebäck <[hidden email]> wrote:

Is this what you're looking for?

julia> using SIMD

julia
> v = rand(2, 5)
2×5 Array{Float64,2}:
 
0.391832  0.785861  0.352291  0.874891  0.874593
 
0.697768  0.51637   0.711525  0.400077  0.266559

julia
> w = reinterpret(Vec{2, Float64}, vec(v))
5-element Array{SIMD.Vec{2,Float64},1}:
 
Float640.391832,0.697768
 
Float640.785861,0.51637
 
Float640.352291,0.711525
 
Float640.874891,0.400077
 
Float640.874593,0.266559


Den onsdag 16 november 2016 kl. 05:52:14 UTC+1 skrev Sheehan Olver:

In some high performance code I want to convert an 2 x n `Matrix{Float64}` to a n long `Matrix{Vec{2,Float64}}`with no memory allocation (both types will store memory in the same order).  The following works:

v=rand(2,100)
w=unsafe_wrap(Vector{Vec{2,Float64}},
            reinterpret(Ptr{Vec{2,Float64}},pointer(v)),
            size(pts,1),true)

The variable v is a throw away variable, however, and as I understand it, the way its written the memory for w will be freed as soon as v is no longer referenced.  Is there any way to tell the garbage collector to not collect v?


Loading...