# What is the best way to wrap C double pointers into a Julia \`Matrix\`?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472>\
**Category:** Performance\
**Tags:** ccall, c\
**Created:** [June 6, 2021, 7:27pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472 "2021-06-06T19:27:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sapo](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sapo](https://discourse.julialang.org/u/sapo)\
**Post date:** [June 6, 2021, 7:27pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472/1 "2021-06-06T19:27:21Z")

</div>

Take this code which represents a matrix in C.

```nohighlight
int**mat = (int**) malloc(rows * sizeof(int*))

for (int index=0;index<row;++index) {
    mat[index] = (int*) malloc(cols * sizeof(int));
}

```

What is the best way to wrap `mat` into a `Matrix{Float32}`?

Right now, I came up with this solution:

```julia
result = Matrix{V}(undef, m, n)
rows = Base.unsafe_wrap(Vector{Ptr{V}}, mat, m)
@inbounds @simd for i in 1:length(rows)
    result[i, :] = Base.unsafe_wrap(Vector{V}, rows[i], n)
end
return result

```

Any ideas on optimizing it?

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [June 6, 2021, 7:50pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472/2 "2021-06-06T19:50:08Z")

</div>

This is not possible. An m \times n Julia Matrix is stored as a single linear array of length mn, with column-major ordering, not as an array of m pointers to m arrays of length n. Just like BLAS and LAPACK.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [June 8, 2021, 7:56am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472/3 "2021-06-08T07:56:36Z")

</div>

I think you have two options: either copy the data into a new matrix as you have or make an `AbstractMatrix` subtype which accesses the data directly (there might be a package for this).

Your code looks fine. I doubt that `@simd` is doing anything though but you could try explicitly copying each element. The implementation of the row copy probably uses simd anyway.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 8, 2021, 7:38pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-wrap-c-double-pointers-into-a-julia-matrix/62472/4 "2021-06-08T19:38:01Z")

</div>

Probably it can be wrapped as a `Vector{Vector{Int}}`
