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

Commit

Permalink
Include a bump allocator example
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanvdc committed Jun 17, 2019
1 parent ede33da commit 560daa0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/bump-allocator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Test

using CUDAdrv, CUDAnative
include(joinpath(@__DIR__, "..", "test", "array.jl")) # real applications: use CuArrays.jl

mutable struct Box{T}
value::T
end

function vcopy(a, b)
i = (blockIdx().x-1) * blockDim().x + threadIdx().x
box = Box(a[i])
b[i] = box.value
return
end

dims = (3,4)
a = round.(rand(Float32, dims) * 100)
b = similar(a)

d_a = CuTestArray(a)
d_b = CuTestArray(b)

len = prod(dims)

# Allocate a 1 MiB heap for the bump allocator.
heap_capacity = 1024 * 1024
heap = Mem.alloc(Mem.DeviceBuffer, heap_capacity)
heap_start_address = pointer(heap)
# Create an initialization callback for the bump allocator.
function init(kernel)
CUDAnative.Runtime.bump_alloc_init!(kernel, heap_start_address, heap_capacity)
end
# Run the kernel.
@cuda threads=len init=init malloc="ptx_bump_alloc" vcopy(d_a, d_b)
# Free the heap.
Mem.free(heap)

b = Array(d_b)
@test a b

0 comments on commit 560daa0

Please sign in to comment.