Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/KernelAbstractions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ function unsafe_free! end

unsafe_free!(::AbstractArray) = return

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

###
# Kernel language
# - @localmem
Expand Down Expand Up @@ -460,13 +464,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.
Comment on lines +8 to +9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure sure I understand what a 1-based size means, is this supposed to be in the _id() docstrings instead?

"""
function get_global_size end

"""
get_global_id()::@NamedTuple{x::Int32, y::Int32, z::Int32}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Int32 or Int64?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenCL defines these as Csize_t

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to allow the platform to define the specific <:Integer index type? Metal uses uint3 by default, which are three UInt32 values. I liked that CUDA Thrust allowed the indices to be templated, so I could use Int64 only when dealing with billions of datapoints.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPU dev call: We should make these Int/size_t


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So IIUC, backends should implement these like below, right?

function get_local_id()
    return (threadIdx().x, threadIdx().y, threadIdx().z)
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah basically, and my goal is to replace the old internal functions the people had to override with definitions based on these functions.


"""
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