Skip to content

Commit

Permalink
Merge pull request #212 from JuliaGPU/tb/cleanups
Browse files Browse the repository at this point in the history
Various clean-ups
  • Loading branch information
maleadt authored Sep 6, 2024
2 parents e875124 + bc0fef4 commit 4914b30
Show file tree
Hide file tree
Showing 26 changed files with 276 additions and 382 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ steps:
cuda: "*"
if: build.message !~ /\[skip tests\]/
env:
JULIA_OPENCL_BACKEND: "CUDA"
JULIA_OPENCL_BACKEND: "NVIDIA"
OCL_ICD_FILENAMES: "libnvidia-opencl.so.1"
timeout_in_minutes: 60
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coverage:
ignore:
- "lib"
- "lib/lib*.jl"
- "src/kernels"
status:
patch: false
Expand Down
31 changes: 31 additions & 0 deletions lib/CL.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module cl

export CLObject, CLString

abstract type CLObject end

Base.hash(x::CLObject) = hash(pointer(x))
Base.isequal(x::T, y::T) where {T <: CLObject} = Base.hash(x) == Base.hash(y)
Base.:(==)(x::T, y::T) where {T <: CLObject} = Base.hash(x) == Base.hash(y)

# The arrays contain a nullbyte that we pop first
function CLString(v::Array{Cchar})
pop!(v)
String(reinterpret(UInt8, v))
end

include("api.jl")

# API wrappers
include("error.jl")
include("platform.jl")
include("device.jl")
include("context.jl")
include("queue.jl")
include("event.jl")
include("memory.jl")
include("buffer.jl")
include("program.jl")
include("kernel.jl")

end
12 changes: 6 additions & 6 deletions src/api.jl → lib/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ const initialized = Ref{Bool}(false)
end
end

function parse_version(version_string)
mg = match(r"^OpenCL ([0-9]+)\.([0-9]+) .*$", version_string)
if mg === nothing
error("Non conforming version string: $(ver)")
function __init__()
if !OpenCL_jll.is_available()
@error "OpenCL_jll is not available for your platform, OpenCL.jl. will not work."
end
return VersionNumber(parse(Int, mg.captures[1]),
parse(Int, mg.captures[2]))
end

const _versionDict = Dict{Ptr, VersionNumber}()
_deletecached!(obj::CLObject) = delete!(_versionDict, pointer(obj))
65 changes: 31 additions & 34 deletions src/buffer.jl → lib/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,43 +316,40 @@ function enqueue_map_mem(q::CmdQueue,
return (mapped_arr, Event(ret_evt[]))
end

@ocl_v1_2_only begin

# low level enqueue fill operation, return event
function enqueue_fill_buffer(q::CmdQueue, buf::Buffer{T},
pattern::T, offset::Csize_t,
nbytes::Csize_t,
wait_for::Union{Vector{Event},Nothing}) where T
if wait_for === nothing
evt_ids = C_NULL
n_evts = cl_uint(0)
else
evt_ids = [evt.id for evt in wait_for]
n_evts = cl_uint(length(evt_ids))
end
ret_evt = Ref{cl_event}()
nbytes_pattern = sizeof(pattern)
@assert nbytes_pattern > 0
clEnqueueFillBuffer(q.id, buf.id, [pattern],
unsigned(nbytes_pattern), offset, nbytes,
n_evts, evt_ids, ret_evt)
@return_event ret_evt[]
# low level enqueue fill operation, return event
function enqueue_fill_buffer(q::CmdQueue, buf::Buffer{T},
pattern::T, offset::Csize_t,
nbytes::Csize_t,
wait_for::Union{Vector{Event},Nothing}) where T
if wait_for === nothing
evt_ids = C_NULL
n_evts = cl_uint(0)
else
evt_ids = [evt.id for evt in wait_for]
n_evts = cl_uint(length(evt_ids))
end
ret_evt = Ref{cl_event}()
nbytes_pattern = sizeof(pattern)
@assert nbytes_pattern > 0
clEnqueueFillBuffer(q.id, buf.id, [pattern],
unsigned(nbytes_pattern), offset, nbytes,
n_evts, evt_ids, ret_evt)
@return_event ret_evt[]
end

# enqueue a fill operation, return an event
function enqueue_fill(q::CmdQueue, buf::Buffer{T}, x::T) where T
nbytes = sizeof(buf)
evt = enqueue_fill_buffer(q, buf, x, unsigned(0),
unsigned(nbytes), nothing)
return evt
end
# enqueue a fill operation, return an event
function enqueue_fill(q::CmdQueue, buf::Buffer{T}, x::T) where T
nbytes = sizeof(buf)
evt = enqueue_fill_buffer(q, buf, x, unsigned(0),
unsigned(nbytes), nothing)
return evt
end

# (blocking) fill the contents of a buffer with with a given value
function fill!(q::CmdQueue, buf::Buffer{T}, x::T) where T
evt = enqueue_fill(q, buf, x)
wait(evt)
return evt
end
# (blocking) fill the contents of a buffer with with a given value
function fill!(q::CmdQueue, buf::Buffer{T}, x::T) where T
evt = enqueue_fill(q, buf, x)
wait(evt)
return evt
end

# copy the contents of a buffer into an array
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
125 changes: 72 additions & 53 deletions src/event.jl → lib/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ end

NannyEvent(evt::Event, obj; retain=false) = NannyEvent(evt.id, obj, retain=retain)

macro return_event(evt)
quote
evt = $(esc(evt))
try
return Event(evt, retain=false)
catch err
clReleaseEvent(evt)
throw(err)
end
end
end

macro return_nanny_event(evt, obj)
quote
evt = $(esc(evt))
try
return NannyEvent(evt, $(esc(obj)))
catch err
clReleaseEvent(evt)
throw(err)
end
end
end

Base.pointer(evt::CLEvent) = evt.id

function Base.show(io::IO, evt::Event)
Expand All @@ -53,45 +77,42 @@ end

Base.getindex(evt::CLEvent, evt_info::Symbol) = info(evt, evt_info)

@ocl_v1_1_only begin

mutable struct UserEvent <: CLEvent
id::cl_event
mutable struct UserEvent <: CLEvent
id::cl_event

function UserEvent(evt_id::cl_event, retain=false)
if retain
clRetainEvent(evt_id)
end
evt = new(evt_id)
finalizer(_finalize, evt)
return evt
function UserEvent(evt_id::cl_event, retain=false)
if retain
clRetainEvent(evt_id)
end
evt = new(evt_id)
finalizer(_finalize, evt)
return evt
end
end

function UserEvent(ctx::Context; retain=false)
status = Ref{Cint}()
evt_id = clCreateUserEvent(ctx.id, status)
if status[] != CL_SUCCESS
throw(CLError(status[]))
end
try
return UserEvent(evt_id, retain)
catch err
clReleaseEvent(evt_id)
throw(err)
end
function UserEvent(ctx::Context; retain=false)
status = Ref{Cint}()
evt_id = clCreateUserEvent(ctx.id, status)
if status[] != CL_SUCCESS
throw(CLError(status[]))
end

function Base.show(io::IO, evt::UserEvent)
ptr_val = convert(UInt, Base.pointer(evt))
ptr_address = "0x$(string(ptr_val, base = 16, pad = Sys.WORD_SIZE>>2))"
print(io, "OpenCL.UserEvent(@$ptr_address)")
try
return UserEvent(evt_id, retain)
catch err
clReleaseEvent(evt_id)
throw(err)
end
end

function complete(evt::UserEvent)
clSetUserEventStatus(evt.id, CL_COMPLETE)
return evt
end
function Base.show(io::IO, evt::UserEvent)
ptr_val = convert(UInt, Base.pointer(evt))
ptr_address = "0x$(string(ptr_val, base = 16, pad = Sys.WORD_SIZE>>2))"
print(io, "OpenCL.UserEvent(@$ptr_address)")
end

function complete(evt::UserEvent)
clSetUserEventStatus(evt.id, CL_COMPLETE)
return evt
end

struct _EventCB
Expand Down Expand Up @@ -156,28 +177,26 @@ function wait(evts::Vector{CLEvent})
return evts
end

@ocl_v1_2_only begin
function enqueue_marker_with_wait_list(q::CmdQueue,
wait_for::Vector{CLEvent})
n_wait_events = cl_uint(length(wait_for))
wait_evt_ids = [evt.id for evt in wait_for]
ret_evt = Ref{cl_event}()
clEnqueueMarkerWithWaitList(q.id, n_wait_events,
isempty(wait_evt_ids) ? C_NULL : wait_evt_ids,
ret_evt)
@return_event ret_evt[]
end
function enqueue_marker_with_wait_list(q::CmdQueue,
wait_for::Vector{CLEvent})
n_wait_events = cl_uint(length(wait_for))
wait_evt_ids = [evt.id for evt in wait_for]
ret_evt = Ref{cl_event}()
clEnqueueMarkerWithWaitList(q.id, n_wait_events,
isempty(wait_evt_ids) ? C_NULL : wait_evt_ids,
ret_evt)
@return_event ret_evt[]
end

function enqueue_barrier_with_wait_list(q::CmdQueue,
wait_for::Vector{CLEvent})
n_wait_events = cl_uint(length(wait_for))
wait_evt_ids = [evt.id for evt in wait_for]
ret_evt = Ref{cl_event}()
clEnqueueBarrierWithWaitList(q.id, n_wait_events,
isempty(wait_evt_ids) ? C_NULL : wait_evt_ids,
ret_evt)
@return_event ret_evt[]
end
function enqueue_barrier_with_wait_list(q::CmdQueue,
wait_for::Vector{CLEvent})
n_wait_events = cl_uint(length(wait_for))
wait_evt_ids = [evt.id for evt in wait_for]
ret_evt = Ref{cl_event}()
clEnqueueBarrierWithWaitList(q.id, n_wait_events,
isempty(wait_evt_ids) ? C_NULL : wait_evt_ids,
ret_evt)
@return_event ret_evt[]
end

function enqueue_marker(q::CmdQueue)
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 9 additions & 15 deletions src/platform.jl → lib/platform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,16 @@ function info(p::Platform, pinfo)
end

function devices(p::Platform, dtype)
try
ndevices = Ref{Cuint}()
clGetDeviceIDs(p.id, dtype, 0, C_NULL, ndevices)
if ndevices[] == 0
return Device[]
end
result = Vector{cl_device_id}(undef, ndevices[])
clGetDeviceIDs(p.id, dtype, ndevices[], result, C_NULL)
return Device[Device(id) for id in result]
catch err
if err.desc == :CL_DEVICE_NOT_FOUND || err.code == -1
return Device[]
else
throw(err)
end
ndevices = Ref{Cuint}()
ret = unchecked_clGetDeviceIDs(p.id, dtype, 0, C_NULL, ndevices)
if ret == CL_DEVICE_NOT_FOUND || ndevices[] == 0
return Device[]
elseif ret != CL_SUCCESS
throw(CLError(ret))
end
result = Vector{cl_device_id}(undef, ndevices[])
clGetDeviceIDs(p.id, dtype, ndevices[], result, C_NULL)
return Device[Device(id) for id in result]
end

devices(p::Platform) = devices(p, CL_DEVICE_TYPE_ALL)
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 4914b30

Please sign in to comment.