-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PyArray conversion speedups and PyArrayFromBuffer
* PyArray_Info parameterised T,N and immutable * PyArray_Info size and stride changed to Tuples (from Vectors) * PyArray_Info data changed from Ptr{Cvoid} to Ptr{T} * `PyArrayInfoFromBuffer(o::PyObject)` faster way to get PyArray_Info from numpy than numpy's __array_interface__ * `PyArrayFromBuffer(o::PyObject)` 4x faster PyArray conversion * ArrayFromBuffer no copy conversion to Julia Array * moved PyBuffer tests to separate file
- Loading branch information
Showing
6 changed files
with
203 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using PyCall, BenchmarkTools, DataStructures | ||
using PyCall: PyArrayInfoFromBuffer | ||
|
||
results = OrderedDict{String,Any}() | ||
|
||
let | ||
np = pyimport("numpy") | ||
nprand = np["random"]["rand"] | ||
# nparray_pyo(x) = pycall(np["array"], PyObject, x) | ||
# pytestarray(sz::Int...) = pycall(np["reshape"], PyObject, nparray_pyo(1:prod(sz)), sz) | ||
|
||
# no convert baseline | ||
nprand_pyo(sz...) = pycall(nprand, PyObject, sz...) | ||
|
||
for arr_size in [(2,2), (100,100)] | ||
pyo_arr = nprand_pyo(arr_size...) | ||
results["nprand_pyo$arr_size"] = @benchmark $nprand_pyo($arr_size...) | ||
println("nprand_pyo $arr_size:\n"); display(results["nprand_pyo$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
results["convert_pyarr$arr_size"] = @benchmark $convert(PyArray, $pyo_arr) | ||
println("convert_pyarr $arr_size:\n"); display(results["convert_pyarr$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
results["PyArrayInfoFromBuffer$arr_size"] = @benchmark $PyArrayInfoFromBuffer($pyo_arr) | ||
println("PyArrayInfoFromBuffer $arr_size:\n"); display(results["PyArrayInfoFromBuffer$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
results["convert_pyarrbuf$arr_size"] = @benchmark $PyArrayFromBuffer($pyo_arr) | ||
println("convert_pyarrbuf $arr_size:\n"); display(results["convert_pyarrbuf$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
results["convert_arr$arr_size"] = @benchmark convert(Array, $pyo_arr) | ||
println("convert_arr $arr_size:\n"); display(results["convert_arr$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
results["convert_arrbuf$arr_size"] = @benchmark $ArrayFromBuffer($pyo_arr) | ||
println("convert_arrbuf $arr_size:\n"); display(results["convert_arrbuf$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
pyarr = convert(PyArray, pyo_arr) | ||
results["setdata!$arr_size"] = @benchmark $setdata!($pyarr, $pyo_arr) | ||
println("setdata!:\n"); display(results["setdata!$arr_size"]) | ||
println("--------------------------------------------------") | ||
|
||
pyarr = convert(PyArray, pyo_arr) | ||
pybuf=PyBuffer() | ||
results["setdata! bufprealloc$arr_size"] = | ||
@benchmark $setdata!($pyarr, $pyo_arr, $pybuf) | ||
println("setdata! bufprealloc:\n"); display(results["setdata! bufprealloc$arr_size"]) | ||
println("--------------------------------------------------") | ||
end | ||
end | ||
println() | ||
println("Mean times") | ||
println("----------") | ||
foreach((r)->println(rpad(r[1],27), ": ", mean(r[2])), results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Compat.Test, PyCall, Compat | ||
|
||
pyutf8(s::PyObject) = pycall(s["encode"], PyObject, "utf-8") | ||
pyutf8(s::String) = pyutf8(PyObject(s)) | ||
|
||
const np = pyimport("numpy") | ||
|
||
@testset "PyBuffer" begin | ||
@testset "String Buffers" begin | ||
b = PyCall.PyBuffer(pyutf8("test string")) | ||
@test ndims(b) == 1 | ||
@test (length(b),) == (length("test string"),) == (size(b, 1),) == size(b) | ||
@test stride(b, 1) == 1 | ||
@test PyCall.iscontiguous(b) == true | ||
end | ||
|
||
@testset "Non-native-endian" begin | ||
wrong_endian_str = ENDIAN_BOM == 0x01020304 ? "<" : ">" | ||
wrong_endian_arr = | ||
pycall(np["ndarray"], PyObject, 2; buffer=UInt8[0,1,3,2], | ||
dtype=wrong_endian_str*"i2") | ||
# Not supported, so throws | ||
@test_throws ErrorException ArrayFromBuffer(wrong_endian_arr) | ||
end | ||
end |