Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Allow and test anonymous functions and closures.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Mar 22, 2019
1 parent 91ed41d commit d4d6aca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/execution.jl
Expand Up @@ -448,23 +448,16 @@ No keyword arguments are supported.

const delayed_cufunctions = Vector{Tuple{Core.Function,Type}}()
@generated function delayed_cufunction(::Val{f}, ::Val{tt}) where {f,tt}
if sizeof(f) > 0
Core.println(Core.stderr, "ERROR: @cuda dynamic parallelism does not support closures")
quote
trap()
DeviceKernel{f,tt}(C_NULL)
end
else
global delayed_cufunctions
push!(delayed_cufunctions, (f,tt))
id = length(delayed_cufunctions)
global delayed_cufunctions
push!(delayed_cufunctions, (f,tt))
id = length(delayed_cufunctions)

quote
# drop a marker which will get picked up during compilation
quote
# TODO: add an edge to this method instance to support method redefinitions
fptr = ccall("extern cudanativeCompileKernel", llvmcall, Ptr{Cvoid}, (Int,), $id)
DeviceKernel{f,tt}(fptr)
end
# TODO: add an edge to this method instance to support method redefinitions
fptr = ccall("extern cudanativeCompileKernel", llvmcall, Ptr{Cvoid}, (Int,), $id)

DeviceKernel{f,tt}(fptr)
end
end

Expand Down
31 changes: 31 additions & 0 deletions test/device/execution.jl
Expand Up @@ -828,6 +828,37 @@ end
@test out == "Hello, World!"
end

@testset "anonymous functions" begin
function hello()
@cuprintf("Hello, ")
world = () -> (@cuprintf("World!"); nothing)
@cuda dynamic=true world()
return
end

_, out = @grab_output begin
@cuda hello()
synchronize()
end
@test out == "Hello, World!"
end

@testset "closures" begin
function hello()
x = 1
@cuprintf("Hello, ")
world = () -> (@cuprintf("World %ld!", x); nothing)
@cuda dynamic=true world()
return
end

_, out = @grab_output begin
@cuda hello()
synchronize()
end
@test out == "Hello, World 1!"
end

@testset "argument passing" begin
function kernel(a, b, c)
@cuprintf("%ld %ld %ld", Int64(a), Int64(b), Int64(c))
Expand Down

0 comments on commit d4d6aca

Please sign in to comment.