Skip to content

Commit c8a1414

Browse files
@turbo function(;kwag) work around (#494)
* @turbo kwargs work arround in getting_started.md * add passing of kwargs to ReadMe * change length of shown output
1 parent d0c61d3 commit c8a1414

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,30 @@ BenchmarkTools.Trial:
273273
evals/sample: 6
274274
```
275275

276+
Note: `@turbo` does not support passing of kwargs to function calls to which it is applied, e.g:
277+
```julia
278+
julia> @turbo round.(rand(10))
279+
280+
julia> @turbo round.(rand(10); digits = 3)
281+
ERROR: TypeError: in typeassert, expected Expr, got a value of type GlobalRef
282+
```
283+
284+
You can work around this by creating a anonymous function before applying `@turbo` as follows:
285+
```julia
286+
struct KwargCall{F,T}
287+
f::F
288+
x::T
289+
end
290+
@inline (f::KwargCall)(args...) = f.f(args...; f.x...)
291+
292+
f = KwargCall(round, (digits = 3,));
293+
@turbo f.(rand(10))
294+
10-element Vector{Float64}:
295+
0.763
296+
297+
0.851
298+
```
299+
276300
</p>
277301
</details>
278302

docs/src/getting_started.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ Aside from loops, `LoopVectorization.jl` also supports broadcasting.
3838
!!! danger
3939
Broadcasting an `Array` `A` when `size(A,1) == 1` is NOT SUPPORTED, unless this is known at compile time (e.g., broadcasting a transposed vector is fine). Otherwise, you will probably crash Julia.
4040

41+
Note: `@turbo` does not support passing of kwargs to function calls to which it is applied, e.g:
42+
```julia
43+
julia> @turbo round.(rand(10));
44+
45+
julia> @turbo round.(rand(10); digits = 3);
46+
ERROR: TypeError: in typeassert, expected Expr, got a value of type GlobalRef
47+
```
48+
49+
You can work around this by creating a anonymous function before applying `@turbo` as follows:
50+
```julia
51+
struct KwargCall{F,T}
52+
f::F
53+
x::T
54+
end
55+
@inline (f::KwargCall)(args...) = f.f(args...; f.x...)
56+
57+
f = KwargCall(round, (digits = 3,));
58+
@turbo f.(rand(10))
59+
10-element Vector{Float64}:
60+
0.763
61+
62+
0.851
63+
```
64+
65+
4166
```julia
4267
julia> using LoopVectorization, BenchmarkTools
4368

0 commit comments

Comments
 (0)