Accessing rows without copying

Hello,

I am optimizing my code, and I wanted to know how I could access the row of a matrix directly (not making a copy of it, and pass it to a function.
right now I do:

@threads for i=1:n_rows
      results[i] = myfunction(MyMatrix[i,:])
end

However I was told that MyMatrix[i,:] is making a copy of the row.
How could I give to myfunction the original row ? The only solution I found yes is to rewrite myfunction(MyMatrix, m) so that it take the complete array and the row index… I think there must a more efficient solution…

Best

view(M, i, :) or, for convenience, @view M[i, :] creates a “view” into the array.

Keep in mind that Julia uses column-major storage, meaning that elements in a row (or a row view) are non-contiguous in memory. That means, working on a copy which has the same elements stored contiguously may happen to be faster despite the need for an extra allocation.

If possible, it’s better to rewrite your program so that the relevant data are stored in rows of MyMatrix.

6 Likes