Skip to content

Commit

Permalink
define basic intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Feb 6, 2025
1 parent f5c1025 commit 6a3e192
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
30 changes: 24 additions & 6 deletions src/KernelAbstractions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ function unsafe_free! end

unsafe_free!(::AbstractArray) = return

include("intrinsics.jl")
import .KernelIntrinsics
export KernelIntrinsics

###
# Kernel language
# - @localmem
Expand Down Expand Up @@ -447,13 +451,27 @@ end
# Internal kernel functions
###

function __index_Local_Linear end
function __index_Group_Linear end
function __index_Global_Linear end
function __index_Local_Linear(ctx)
return KernelIntrinsics.get_local_id().x
end

function __index_Local_Cartesian end
function __index_Group_Cartesian end
function __index_Global_Cartesian end
function __index_Group_Linear(ctx)
return KernelIntrinsics.get_group_id().x
end

function __index_Global_Linear(ctx)
return KernelIntrinsics.get_global_id().x
end

function __index_Local_Cartesian(ctx)
return @inbounds workitems(__iterspace(ctx))[KernelIntrinsics.get_local_id().x]
end
function __index_Group_Cartesian(ctx)
return @inbounds blocks(__iterspace(ctx))[KernelIntrinsics.get_group_id().x]
end
function __index_Global_Cartesian(ctx)
return @inbounds expand(__iterspace(ctx), KernelIntrinsics.get_group_id().x, KernelIntrinsics.get_local_id().x)
end

@inline __index_Local_NTuple(ctx, I...) = Tuple(__index_Local_Cartesian(ctx, I...))
@inline __index_Group_NTuple(ctx, I...) = Tuple(__index_Group_Cartesian(ctx, I...))
Expand Down
52 changes: 52 additions & 0 deletions src/intrinsics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module KernelIntrinsics

"""
get_global_size()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Return the number of global work-items specified.
!!! note
1-based.
"""
function get_global_size end

"""
get_global_id()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Returns the unique global work-item ID.
"""
function get_global_id end

"""
get_local_size()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Return the number of local work-items specified.
"""
function get_local_size end

"""
get_local_id()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Returns the unique local work-item ID.
"""
function get_local_id end

"""
get_num_groups()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Returns the number of groups.
"""
function get_num_groups end

"""
get_group_id()::@NamedTuple{x::Int32, y::Int32, z::Int32}
Returns the unique group ID.
"""
function get_group_id end

function localmemory end
function barrier end
function print end

end
25 changes: 7 additions & 18 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,29 +139,18 @@ end


## Indexing Functions
const KI = KA.KernelIntrinsics

@device_override @inline function KA.__index_Local_Linear(ctx)
return get_local_id(1)
@device_override @inline function KI.get_local_id()
return (; x = get_local_id(1), y = get_local_id(2), z = get_local_id(3))
end

@device_override @inline function KA.__index_Group_Linear(ctx)
return get_group_id(1)
@device_override @inline function KI.get_group_id()
return (; x = get_group_id(1), y = get_group_id(2), z = get_group_id(3))
end

@device_override @inline function KA.__index_Global_Linear(ctx)
return get_global_id(1)
end

@device_override @inline function KA.__index_Local_Cartesian(ctx)
@inbounds KA.workitems(KA.__iterspace(ctx))[get_local_id(1)]
end

@device_override @inline function KA.__index_Group_Cartesian(ctx)
@inbounds KA.blocks(KA.__iterspace(ctx))[get_group_id(1)]
end

@device_override @inline function KA.__index_Global_Cartesian(ctx)
return @inbounds KA.expand(KA.__iterspace(ctx), get_group_id(1), get_local_id(1))
@device_override @inline function KI.get_global_id()
return (; x = get_global_id(1), y = get_global_id(2), z = get_global_id(3))
end

@device_override @inline function KA.__validindex(ctx)
Expand Down

0 comments on commit 6a3e192

Please sign in to comment.