unsafe_wrap from a pointer from a Julia array

186 views
Skip to first unread message

Sheehan Olver

unread,
Nov 15, 2016, 11:52:14 PM11/15/16
to julia-users

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?

Gunnar Farnebäck

unread,
Nov 16, 2016, 2:39:34 AM11/16/16
to julia-users
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

Sheehan Olver

unread,
Nov 16, 2016, 3:10:27 AM11/16/16
to julia...@googlegroups.com
👍 

Sent from my iPhone

Sheehan Olver

unread,
Nov 16, 2016, 4:53:31 PM11/16/16
to julia...@googlegroups.com
Oh wait, I was referring to Vec from FixedSizeArrays.jl , not from SIMD

Sheehan Olver

unread,
Nov 16, 2016, 4:54:39 PM11/16/16
to julia...@googlegroups.com
Though that code works too for FixedSizeArrays
Reply all
Reply to author
Forward
0 new messages