Unsafe_wrap with array of arrays


#1

I have data (originally pulled in from python via pycall) that is naturally stored in a 2D array (N, 3) (i…e, it is an array of points in 3D). Right now, I can wrap this in Julia with the command:

X = unsafe_wrap(Array{Float64,2}, Ptr{Float64}(Xptr), (3,N))

What I would like to do is have this wrapped as an array of arrays, so that I can easily just iterate through each of the points. Is this possible? How so?


#2

You could reinterpret the data as a vector of SVectors from StaticArrays:

using StaticArrays
julia> x = [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> reinterpret(SVector{2, Int}, x, (size(x, 2),))
3-element Array{SVector{2,Int64},1}:
 [1, 4]
 [2, 5]
 [3, 6]

Alternatively, you could create a view of each column of the array and then store those views in a Vector.


#3

Does reinterpreting copy the data, or just change the indexing mechanism? I want the latter.


#4

It just creates a new interpretation of the same data.


#5

Note that on 0.7 this will create a ReinterpretedArray which unfortunately has overhead in some cases: https://github.com/JuliaLang/julia/issues/25014


#6

What about just doing this?

using StaticArrays

X = unsafe_wrap(Array{SVector{3,Float64},1}, Ptr{SVector{3,Float64}}(Xptr), (N,))

#7

But that has the same problem as described in ANN: InplaceRealFFT.jl : inplace real-to-complex and complex-to-real FFT?


#8

No, the memory was allocated by python and will be freed by python. gideonsimpson needs to deal with this problem anyway. Or did I misunderstand the context?


#9

I missed the whole pycall thing…