Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KernelIntrinsics #562

Open
wants to merge 1 commit into
base: vc/pocl
Choose a base branch
from
Open
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 @@

unsafe_free!(::AbstractArray) = return

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

###
# Kernel language
# - @localmem
Expand Down Expand Up @@ -460,13 +464,27 @@
# 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

Check warning on line 468 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L467-L468

Added lines #L467 - L468 were not covered by tests
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

Check warning on line 472 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L471-L472

Added lines #L471 - L472 were not covered by tests
end

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

Check warning on line 476 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L475-L476

Added lines #L475 - L476 were not covered by tests
end

function __index_Local_Cartesian(ctx)
return @inbounds workitems(__iterspace(ctx))[KernelIntrinsics.get_local_id().x]

Check warning on line 480 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L479-L480

Added lines #L479 - L480 were not covered by tests
end
function __index_Group_Cartesian(ctx)
return @inbounds blocks(__iterspace(ctx))[KernelIntrinsics.get_group_id().x]

Check warning on line 483 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L482-L483

Added lines #L482 - L483 were not covered by tests
end
function __index_Global_Cartesian(ctx)
return @inbounds expand(__iterspace(ctx), KernelIntrinsics.get_group_id().x, KernelIntrinsics.get_local_id().x)

Check warning on line 486 in src/KernelAbstractions.jl

View check run for this annotation

Codecov / codecov/patch

src/KernelAbstractions.jl#L485-L486

Added lines #L485 - L486 were not covered by tests
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}
Copy link
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
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
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.

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


## 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))

Check warning on line 145 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L144-L145

Added lines #L144 - L145 were not covered by tests
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))

Check warning on line 149 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L148-L149

Added lines #L148 - L149 were not covered by tests
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))

Check warning on line 153 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L152-L153

Added lines #L152 - L153 were not covered by tests
end

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