Skip to content

remove overload macro #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name = "IntelVectorMath"
uuid = "c8ce9da6-5d36-5c03-b118-5a70151be7bc"
version = "0.3"
version = "0.3.0"

[deps]
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
CpuId = "adafc99b-e345-5852-983c-f28acb93d879"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[compat]
BinaryProvider = "0.5.8"
CpuId = "0.2"
SpecialFunctions = "0.8, 0.9, 0.10"
julia = "0.7, 1.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"


[targets]
test = ["Test"]
test = ["Test", "SpecialFunctions"]
44 changes: 1 addition & 43 deletions src/IntelVectorMath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export IVM
const IVM = IntelVectorMath

# import Base: .^, ./
using SpecialFunctions
# using Libdl
include("../deps/deps.jl")

Expand Down Expand Up @@ -104,47 +103,6 @@ for t in (Float32, Float64)
# def_binary_op(Complex{t}, Complex{t}, :multiply_conj, :multiply_conj!, :Mul, false)
end

"""
@overload exp log sin

This macro adds a method to each function in `Base` (or perhaps in `SpecialFunctions`),
so that when acting on an array (or two arrays) it calls the `IntelVectorMath` function of the same name.

The existing action on scalars is unaffected. However, `exp(M::Matrix)` will now mean
element-wise `IntelVectorMath.exp(M) == exp.(M)`, rather than matrix exponentiation.
"""
macro overload(funs...)
out = quote end
say = []
for f in funs
if f in _UNARY
if isdefined(Base, f)
push!(out.args, :( Base.$f(A::Array) = IntelVectorMath.$f(A) ))
push!(say, "Base.$f(A)")
elseif isdefined(SpecialFunctions, f)
push!(out.args, :( IntelVectorMath.SpecialFunctions.$f(A::Array) = IntelVectorMath.$f(A) ))
push!(say, "SpecialFunctions.$f(A)")
else
@error "function IntelVectorMath.$f is not defined in Base or SpecialFunctions, so there is nothing to overload"
end
end
if f in _BINARY
if isdefined(Base, f)
push!(out.args, :( Base.$f(A::Array, B::Array) = IntelVectorMath.$f(A, B) ))
push!(say, "Base.$f(A, B)")
else
@error "function IntelVectorMath.$f is not defined in Base, so there is nothing to overload"
end
end
if !(f in _UNARY) && !(f in _BINARY)
error("there is no function $f defined by IntelVectorMath.jl")
end
end
str = string("Overloaded these functions: \n ", join(say, " \n "))
push!(out.args, str)
esc(out)
end

export VML_LA, VML_HA, VML_EP, vml_set_accuracy, vml_get_accuracy, @overload
export VML_LA, VML_HA, VML_EP, vml_set_accuracy, vml_get_accuracy

end
7 changes: 1 addition & 6 deletions src/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const VML_LA = VMLAccuracy(0x00000001)
const VML_HA = VMLAccuracy(0x00000002)
const VML_EP = VMLAccuracy(0x00000003)

const _UNARY = [] # for @overload to check
const _BINARY = []

Base.show(io::IO, m::VMLAccuracy) = print(io, m == VML_LA ? "VML_LA" :
m == VML_HA ? "VML_HA" : "VML_EP")
vml_get_mode() = ccall((:_vmlGetMode, libmkl_vml_avx), Cuint, ())
Expand Down Expand Up @@ -59,13 +56,12 @@ function vml_prefix(t::DataType)
error("unknown type $t")
end

function def_unary_op(tin, tout, jlname, jlname!, mklname;
function def_unary_op(tin, tout, jlname, jlname!, mklname;
vmltype = tin)
mklfn = Base.Meta.quot(Symbol("$(vml_prefix(vmltype))$mklname"))
exports = Symbol[]
(@isdefined jlname) || push!(exports, jlname)
(@isdefined jlname!) || push!(exports, jlname!)
push!(_UNARY, jlname)
@eval begin
function ($jlname!)(out::Array{$tout,N}, A::Array{$tin,N}) where {N}
size(out) == size(A) || throw(DimensionMismatch())
Expand Down Expand Up @@ -97,7 +93,6 @@ function def_binary_op(tin, tout, jlname, jlname!, mklname, broadcast)
exports = Symbol[]
(@isdefined jlname) || push!(exports, jlname)
(@isdefined jlname!) || push!(exports, jlname!)
push!(_BINARY, jlname)
@eval begin
$(isempty(exports) ? nothing : Expr(:export, exports...))
function ($jlname!)(out::Array{$tout,N}, A::Array{$tin,N}, B::Array{$tin,N}) where {N}
Expand Down
18 changes: 2 additions & 16 deletions test/real.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const fns = [[x[1:2] for x in base_unary_real]; [x[1:2] for x in base_binary_rea
@testset "Definitions and Comparison with Base for Reals" begin

for t in (Float32, Float64), i = 1:length(fns)
inp = input[t][i]
inp = input[t][i]
mod, fn = fns[i]
base_fn = getproperty(mod, fn)
vml_fn = getproperty(IntelVectorMath, fn)
Expand All @@ -30,13 +30,12 @@ const fns = [[x[1:2] for x in base_unary_real]; [x[1:2] for x in base_binary_rea
Test.@test vml_fn(inp...) ≈ base_fn.(inp...)

# cis changes type (float to complex, does not have mutating function)

if length(inp) == 1
if fn != :cis
vml_fn!(inp[1])
Test.@test inp[1] ≈ baseres
end
elseif length(inp) == 2
elseif length(inp) == 2
out = similar(inp[1])
vml_fn!(out, inp...)
Test.@test out ≈ baseres
Expand All @@ -60,16 +59,3 @@ end
Test.@test vml_get_accuracy() == VML_EP

end

@testset "@overload macro" begin

@test IntelVectorMath.exp([1.0]) ≈ exp.([1.0])
@test_throws MethodError Base.exp([1.0])
@test (@overload log exp) isa String
@test Base.exp([1.0]) ≈ exp.([1.0])

@test_throws MethodError Base.atan([1.0], [2.0])
@test (@overload atan) isa String
@test Base.atan([1.0], [2.0]) ≈ atan.([1.0], [2.0])

end