Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a pointer to a subarray with BLAS function?

Tags:

pointers

julia

I am using Julia v0.3.5, which comes with WinPython 3.4.2.5 build 4. I am new to Julia. I am testing how fast Julia is compared to using SciPy's BLAS wrapper for ddot(), which has the following arguments: x,y,n,offx,incx,offy,incy. Julia's OpenBLAS library does not have the offset arguments, so I am trying to figure out how to emulate them while maximizing speed. I am passing 100MB subarrays of a 1GB array (vector) multiple times, so I don't want Julia to create a copy of each subarray, which would reduce the speed. Python's SciPy function is taking a couple of hours to execute, so would like to optimize Julia's speed. I have been reading about how Julia 0.4 will offer array views that avoid the unnecessary copy, but I am unclear about how Julia 0.3.5 handles this.

So far, I learned using REPL that the BLAS dot() function conflicts with the method in linalg/matmul.jl. Therefore, I learned to access it this way:

import Base.LinAlg.BLAS
methods(Base.LinAlg.BLAS.dot)

From the method display, I see that I can pass pointers to x and y subarrays and thus avoid a copy. For example:

x = [1., 2., 3.]
y = [4., 5., 6.]
Base.LinAlg.BLAS.dot(2, pointer(x), 1, pointer(y), 1)

However, when I add an integer offset to a pointer (to access a subarray), REPL crashes.

How can I pass a pointer to a subarray or a subarray to Base.LinAlg.BLAS.dot without the slowdown of a copy of that subarray? Anything else I missed?

like image 741
hiccup Avatar asked Sep 29 '22 14:09

hiccup


1 Answers

It segfaults because pointer arithmatic doesn't work like you probably think it does (i.e. the C way). pointer(x)+1 is one byte after pointer(x), but you probably want pointer(x)+8, e.g.

Base.LinAlg.BLAS.dot(2, pointer(x)+1*sizeof(Float64), 1, pointer(y)+1*sizeof(Float64), 1)

or, more user friendly and recommended:

Base.LinAlg.dot(x,2:3,y,2:3)

which is defined here.

I'd say using pointers like that in Julia is really not recommended, but I imagine if you are doing this at all then it is a special circumstance.

like image 170
IainDunning Avatar answered Oct 18 '22 10:10

IainDunning