Skip to content

Commit

Permalink
some additions
Browse files Browse the repository at this point in the history
  • Loading branch information
VarLad committed Dec 20, 2024
1 parent 5c0ab5c commit 2dc6858
Show file tree
Hide file tree
Showing 6 changed files with 548 additions and 201 deletions.
1 change: 1 addition & 0 deletions lib/cl/CL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Base.:(==)(a::CLObject, b::CLObject) = pointer(a) == pointer(b)
Base.hash(obj::CLObject, h::UInt) = hash(pointer(obj), h)

# API wrappers
include("intelfns.jl")
include("error.jl")
include("platform.jl")
include("device.jl")
Expand Down
37 changes: 37 additions & 0 deletions lib/cl/intelfns.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ocl_extension(s) = cl.clGetExtensionFunctionAddressForPlatform(cl.platform(), s)

function clHostMemAllocINTEL(context, properties, size, alignment, errcode_ret)
ocl_intel = ocl_extension("clHostMemAllocINTEL")

ccall(ocl_intel, Ptr{Cvoid}, (cl.cl_context, Ptr{cl.cl_mem_properties_intel}, Csize_t, cl.cl_uint, Ptr{cl.cl_int}), context, properties, size, alignment, errcode_ret)
end

function clDeviceMemAllocINTEL(context, device, properties, size, alignment, errcode_ret)
ocl_intel = ocl_extension("clDeviceMemAllocINTEL")

@ccall $ocl_intel(context::cl.cl_context, device::cl.cl_device_id, properties::Ptr{cl.cl_mem_properties_intel}, size::Csize_t, alignment::cl.cl_uint, errcode_ret::Ptr{cl.cl_int})::Ptr{Cvoid}
end

function clSharedMemAllocINTEL(context, device, properties, size, alignment, errcode_ret)
ocl_intel = ocl_extension("clSharedMemAllocINTEL")

@ccall $ocl_intel(context::cl.cl_context, device::cl.cl_device_id, properties::Ptr{cl.cl_mem_properties_intel}, size::Csize_t, alignment::cl.cl_uint, errcode_ret::Ptr{cl.cl_int})::Ptr{Cvoid}
end

function clMemFreeINTEL(context, ptr)
ocl_intel = ocl_extension("clMemFreeINTEL")

@ccall $ocl_intel(context::cl.cl_context, ptr::Ptr{Cvoid})::cl.cl_int
end

function clMemBlockingFreeINTEL(context, ptr)
ocl_intel = ocl_extension("clMemBlockingFreeINTEL")

@ccall $ocl_intel(context::cl.cl_context, ptr::Ptr{Cvoid})::cl.cl_int
end

function clGetMemAllocInfoINTEL(context, ptr, param_name, param_value_size, param_value, param_value_size_ret)
ocl_intel = ocl_extension("clGetMemAllocInfoINTEL")

@ccall $ocl_intel(context::cl.cl_context, ptr::Ptr{Cvoid}, param_name::cl.cl_mem_info_intel, param_value_size::Csize_t, param_value::Ptr{Cvoid}, param_value_size_ret::Ptr{Csize_t})::cl.cl_int
end
64 changes: 30 additions & 34 deletions lib/cl/memory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function free(buf::AbstractBuffer; blocking = false)
clMemFreeINTEL
end
success = freefun(ctx, Ptr{Nothing}(UInt(buf.ptr)))
@assert success == cl.CL_SUCCESS
@assert success == CL_SUCCESS
return success
end

Expand All @@ -40,35 +40,35 @@ the device that owns it.
struct DeviceBuffer <: AbstractBuffer
ptr::CLPtr{Cvoid}
bytesize::Int
context::cl.Context
device::cl.Device
context::Context
device::Device
end

function device_alloc(ctx::cl.Context, dev::cl.Device, bytesize::Integer;
function device_alloc(ctx::Context, dev::Device, bytesize::Integer;
alignment::Integer=0, error_code::Ref{Int32}=Ref{Int32}(), properties::Tuple{Vararg{Symbol}}=())
flags = 0
if !isempty(properties)
for i in properties
if i == :wc
flags |= cl.CL_MEM_ALLOC_WRITE_COMBINED_INTEL
flags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL
else
@warn "$i not recognized, ignoring flag. Valid optinos include `:wc`, `:ipd`, and `:iph`"
end
end
end

ptr = clDeviceMemAllocINTEL(ctx, dev,cl.cl_mem_properties_intel[cl.CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)
ptr = clDeviceMemAllocINTEL(ctx, dev, cl_mem_properties_intel[CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)

@assert error_code[] == cl.CL_SUCCESS
@assert error_code[] == CL_SUCCESS
#=
@info ptr error_code[]
result = Ref{UInt64}()
@warn result
success = clGetMemAllocInfoINTEL(ctx, ptr, cl.CL_MEM_ALLOC_BASE_PTR_INTEL,
success = clGetMemAllocInfoINTEL(ctx, ptr, CL_MEM_ALLOC_BASE_PTR_INTEL,
sizeof(UInt64), result, C_NULL)
@error success result
@assert success == cl.CL_SUCCESS
@assert success == CL_SUCCESS
=#
return DeviceBuffer(reinterpret(CLPtr{Cvoid}, ptr), bytesize, ctx, dev)
end
Expand All @@ -92,42 +92,38 @@ Base.convert(::Type{CLPtr{T}}, buf::DeviceBuffer) where {T} =
A buffer of memory on the host. May be accessed by the host, and all devices within the
host driver. Frequently used as staging areas to transfer data to or from devices.
Note that these buffers need to be made resident to the device, e.g., by using the
ZE_KERNEL_FLAG_FORCE_RESIDENCY module flag, the ZE_KERNEL_SET_ATTR_INDIRECT_HOST_ACCESS
kernel attribute, or by calling zeDeviceMakeMemoryResident.
"""
struct HostBuffer <: AbstractBuffer
ptr::Ptr{Cvoid}
bytesize::Int
context::cl.Context
context::Context
end

function host_alloc(ctx::cl.Context, bytesize::Integer;
function host_alloc(ctx::Context, bytesize::Integer;
alignment::Integer=0, error_code::Ref{Int32}=Ref{Int32}(), properties::Tuple{Vararg{Symbol}}=())
flags = 0
if !isempty(properties)
for i in properties
if i == :wc
flags |= cl.CL_MEM_ALLOC_WRITE_COMBINED_INTEL
flags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL
else
@warn "$i not recognized, ignoring flag. Valid optinos include `:wc`"
end
end
end

ptr = clDeviceMemAllocINTEL(ctx, cl.cl_mem_properties_intel[cl.CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)
ptr = clHostMemAllocINTEL(ctx, cl_mem_properties_intel[CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)

@assert error_code[] == cl.CL_SUCCESS
@assert error_code[] == CL_SUCCESS
#=
@info ptr error_code[]
result = Ref{UInt64}()
@warn result
success = clGetMemAllocInfoINTEL(ctx, ptr, cl.CL_MEM_ALLOC_BASE_PTR_INTEL,
success = clGetMemAllocInfoINTEL(ctx, ptr, CL_MEM_ALLOC_BASE_PTR_INTEL,
sizeof(UInt64), result, C_NULL)
@error success result
@assert success == cl.CL_SUCCESS
@assert success == CL_SUCCESS
=#
return HostBuffer(ptr, bytesize, ctx)
end
Expand Down Expand Up @@ -169,11 +165,11 @@ A managed buffer that is shared between the host and one or more devices.
struct SharedBuffer <: AbstractBuffer
ptr::CLPtr{Cvoid}
bytesize::Int
context::cl.Context
device::Union{Nothing,cl.Device}
context::Context
device::Union{Nothing,Device}
end

function shared_alloc(ctx::cl.Context, dev::cl.Device, bytesize::Integer;
function shared_alloc(ctx::Context, dev::Device, bytesize::Integer;
alignment::Integer=0, error_code::Ref{Int32}=Ref{Int32}(), properties::Tuple{Vararg{Symbol}}=())
flags = 0
if !isempty(properties)
Expand All @@ -182,29 +178,29 @@ function shared_alloc(ctx::cl.Context, dev::cl.Device, bytesize::Integer;
end
for i in properties
if i == :wc
flags |= cl.CL_MEM_ALLOC_WRITE_COMBINED_INTEL
flags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL
elseif i == :ipd
flags |= cl.CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL
flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL
elseif i == :iph
flags |= cl.CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL
flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL
else
@warn "$i not recognized, ignoring flag. Valid optinos include `:wc`, `:ipd`, and `:iph`"
end
end
end

ptr = clSharedMemAllocINTEL(ctx, dev, cl.cl_mem_properties_intel[cl.CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)
ptr = clSharedMemAllocINTEL(ctx, dev, cl_mem_properties_intel[CL_MEM_ALLOC_FLAGS_INTEL, flags, 0], bytesize, alignment, error_code)

@assert error_code[] == cl.CL_SUCCESS
@assert error_code[] == CL_SUCCESS
#=
@info ptr error_code[]
result = Ref{UInt64}()
@warn result
success = clGetMemAllocInfoINTEL(ctx, ptr, cl.CL_MEM_ALLOC_BASE_PTR_INTEL,
success = clGetMemAllocInfoINTEL(ctx, ptr, CL_MEM_ALLOC_BASE_PTR_INTEL,
sizeof(UInt64), result, C_NULL)
@error success result
@assert success == cl.CL_SUCCESS
@assert success == CL_SUCCESS
=#
return SharedBuffer(reinterpret(CLPtr{Cvoid}, ptr), bytesize, ctx, dev)
end
Expand Down Expand Up @@ -257,13 +253,13 @@ function properties(buf::AbstractBuffer)
zeMemGetAllocProperties(buf.context, pointer(buf), props_ref, dev_ref)
result = Ref{}()
success = clGetMemAllocInfoINTEL(ctx, ptr, cl.CL_MEM_ALLOC_BASE_PTR_INTEL,
success = clGetMemAllocInfoINTEL(ctx, ptr, CL_MEM_ALLOC_BASE_PTR_INTEL,
sizeof(UInt64), result, C_NULL)
props = props_ref[]
return (
device=cl.Device(dev_ref[], buf.context.driver),
device=Device(dev_ref[], buf.context.driver),
type=props.type,
id=props.id,
)
Expand All @@ -272,7 +268,7 @@ end
struct UnknownBuffer <: AbstractBuffer
ptr::Ptr{Cvoid}
bytesize::Int
context::cl.Context
context::Context
end

Base.pointer(buf::UnknownBuffer) = buf.ptr
Expand All @@ -283,7 +279,7 @@ Base.show(io::IO, buf::UnknownBuffer) =
@printf(io, "UnknownBuffer(%s at %p)", Base.format_bytes(sizeof(buf)), Int(pointer(buf)))

#=
function lookup_alloc(ctx::cl.Context, ptr::Union{Ptr,CLPtr})
function lookup_alloc(ctx::Context, ptr::Union{Ptr,CLPtr})
base_ref = Ref{Ptr{Cvoid}}()
bytesize_ref = Ref{Csize_t}()
Expand Down
4 changes: 4 additions & 0 deletions src/OpenCL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ include("device/runtime.jl")
import SPIRVIntrinsics
SPIRVIntrinsics.@import_all
SPIRVIntrinsics.@reexport_public

include("device/array.jl")
include("device/quirks.jl")
include("util.jl")
include("pool.jl")
include("array.jl")

#=
# compiler implementation
Expand Down
Loading

0 comments on commit 2dc6858

Please sign in to comment.