Skip to content

Improvements to workgroup reduce + scan #876

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

Open
wants to merge 56 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
09f16c2
minor fixes, example
keptsecret Apr 28, 2025
6f5f8b0
bug fixes and example
keptsecret Apr 28, 2025
1bac247
fix to data accessor indexing
keptsecret Apr 29, 2025
305ac7b
added template spec for vector dim 1
keptsecret Apr 29, 2025
c08063d
added inclusive scan
keptsecret Apr 29, 2025
b1d804f
exclusive scan working
keptsecret Apr 30, 2025
3cf98ab
removed outdated comment
keptsecret Apr 30, 2025
7b310e0
minor changes to config usage
keptsecret May 1, 2025
4b4e7e8
add 1 level scans
keptsecret May 1, 2025
2e5f29f
fixes to 1 level scans
keptsecret May 2, 2025
054b269
added handling >1 vectors on level 1 scan (untested)
keptsecret May 2, 2025
1b5282c
move load/store smem into scan funcs, setup config for 3 levels
keptsecret May 5, 2025
c6dc5bc
change to use coalesced indexing for 2-level scans
keptsecret May 6, 2025
aa0c36c
added 3-level scans
keptsecret May 6, 2025
74c359b
minor bug fixes
keptsecret May 6, 2025
ce244e2
changes to data accessor usage
keptsecret May 7, 2025
90b19d8
wg reduction uses reduce instead of scan
keptsecret May 8, 2025
d2a1663
fixes to calculating levels in config
keptsecret May 9, 2025
ea39d9e
fixes to 3-level scan
keptsecret May 12, 2025
2982e5e
Merge branch 'master' into improve-workgroup-scan-2
keptsecret May 13, 2025
1c0e72e
split config into new file
keptsecret May 14, 2025
59d02fe
merge master
keptsecret May 15, 2025
507904f
minor fixes
keptsecret May 15, 2025
542592f
soome changes to arithmetic config
keptsecret May 15, 2025
a9930a0
removed referencing workgroupID in scans
keptsecret May 15, 2025
55d89c5
no need to store locals in reduce
keptsecret May 16, 2025
4e4f26e
added workgroup accessor concepts, refactor accessor usage
keptsecret May 16, 2025
56f013e
Merge branch 'master' into improve-workgroup-scan-2
keptsecret May 19, 2025
004c95a
fixed minor bug
keptsecret May 20, 2025
ccacddb
store temporaries with data accessor
keptsecret May 20, 2025
9c59677
minor fixes
keptsecret May 21, 2025
eb44262
moved indexing functionality to config struct
keptsecret May 21, 2025
573ce44
reduction returns value instead of saving directly to storage
keptsecret May 21, 2025
49ca655
fixes to 2-level scan indexing
keptsecret May 21, 2025
a639145
fixes to 3-level scan and minor stuff
keptsecret May 22, 2025
7751359
some minor fixes
keptsecret May 22, 2025
fd6f527
latest example
keptsecret May 22, 2025
27d84c8
merge master, fix conflicts
keptsecret May 26, 2025
350c6a3
more util funcs in config, fix some calculations
keptsecret May 27, 2025
14e5d15
added generic data/shared mem accessors
keptsecret May 27, 2025
f07329e
fix include guard
keptsecret May 27, 2025
48a7d16
changes to arithmetic accessor concepts
keptsecret May 27, 2025
20a54be
concept macro for checking types
keptsecret May 27, 2025
d83ac5c
revert concept macro addition
keptsecret May 27, 2025
00787bf
added generic read/write accessors
keptsecret May 27, 2025
c0dfc1e
more refactor for accessor concept changes
keptsecret May 27, 2025
55840a3
don't pass scalar_t as index type
keptsecret May 27, 2025
d758ff7
refactor accessor to match accessor template
keptsecret May 27, 2025
b062ede
simplified indexing functions
keptsecret May 27, 2025
472aa0b
more fixes to indexing
keptsecret May 28, 2025
c483941
share level 0 scan between 2-level and 3-level scans (and reduce)
keptsecret May 28, 2025
951ff99
reduce duplicate vars in config
keptsecret May 28, 2025
127c6d9
some fixes to indexing
keptsecret May 29, 2025
90d3579
fix scans for level 1+
keptsecret May 30, 2025
203c03a
some indexing fixes for 3-level reduce/scan
keptsecret May 30, 2025
0b16307
fix 3-level scan downsweep step
keptsecret May 30, 2025
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
13 changes: 13 additions & 0 deletions include/nbl/builtin/hlsl/subgroup2/ballot.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ namespace hlsl
namespace subgroup2
{

uint32_t LastSubgroupInvocation()
{
// why this code was wrong before:
// - only compute can use SubgroupID
// - but there's no mapping of InvocationID to SubgroupID and Index
return glsl::subgroupBallotFindMSB(glsl::subgroupBallot(true));
}

bool ElectLast()
{
return glsl::gl_SubgroupInvocationID()==LastSubgroupInvocation();
}

template<uint32_t SubgroupSizeLog2>
struct Configuration
{
Expand Down
1 change: 1 addition & 0 deletions include/nbl/builtin/hlsl/vector_utils/vector_traits.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct vector_traits<vector<T, DIMENSION> >\
NBL_CONSTEXPR_STATIC_INLINE bool IsVector = true;\
};\

DEFINE_VECTOR_TRAITS_TEMPLATE_SPECIALIZATION(1)
DEFINE_VECTOR_TRAITS_TEMPLATE_SPECIALIZATION(2)
DEFINE_VECTOR_TRAITS_TEMPLATE_SPECIALIZATION(3)
DEFINE_VECTOR_TRAITS_TEMPLATE_SPECIALIZATION(4)
Expand Down
58 changes: 58 additions & 0 deletions include/nbl/builtin/hlsl/workgroup2/arithmetic.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2025 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_BUILTIN_HLSL_WORKGROUP2_ARITHMETIC_INCLUDED_
#define _NBL_BUILTIN_HLSL_WORKGROUP2_ARITHMETIC_INCLUDED_


#include "nbl/builtin/hlsl/functional.hlsl"
#include "nbl/builtin/hlsl/workgroup/ballot.hlsl"
#include "nbl/builtin/hlsl/workgroup/broadcast.hlsl"
#include "nbl/builtin/hlsl/workgroup2/shared_scan.hlsl"


namespace nbl
{
namespace hlsl
{
namespace workgroup2
{

template<class Config, class BinOp, class device_capabilities=void>
struct reduction
{
template<class DataAccessor, class ScratchAccessor>
static void __call(NBL_REF_ARG(DataAccessor) dataAccessor, NBL_REF_ARG(ScratchAccessor) scratchAccessor)
{
impl::reduce<Config,BinOp,Config::LevelCount,device_capabilities> fn;
fn.template __call<DataAccessor,ScratchAccessor>(dataAccessor, scratchAccessor);
}
};

template<class Config, class BinOp, class device_capabilities=void>
struct inclusive_scan
{
template<class DataAccessor, class ScratchAccessor>
static void __call(NBL_REF_ARG(DataAccessor) dataAccessor, NBL_REF_ARG(ScratchAccessor) scratchAccessor)
{
impl::scan<Config,BinOp,false,Config::LevelCount,device_capabilities> fn;
fn.template __call<DataAccessor,ScratchAccessor>(dataAccessor, scratchAccessor);
}
};

template<class Config, class BinOp, class device_capabilities=void>
struct exclusive_scan
{
template<class DataAccessor, class ScratchAccessor>
static void __call(NBL_REF_ARG(DataAccessor) dataAccessor, NBL_REF_ARG(ScratchAccessor) scratchAccessor)
{
impl::scan<Config,BinOp,true,Config::LevelCount,device_capabilities> fn;
fn.template __call<DataAccessor,ScratchAccessor>(dataAccessor, scratchAccessor);
}
};

}
}
}

#endif
88 changes: 88 additions & 0 deletions include/nbl/builtin/hlsl/workgroup2/config.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (C) 2025 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_BUILTIN_HLSL_WORKGROUP2_CONFIG_INCLUDED_
#define _NBL_BUILTIN_HLSL_WORKGROUP2_CONFIG_INCLUDED_

#include "nbl/builtin/hlsl/cpp_compat.hlsl"

namespace nbl
{
namespace hlsl
{
namespace workgroup2
{

namespace impl
{
template<uint16_t WorkgroupSizeLog2, uint16_t SubgroupSizeLog2>
struct virtual_wg_size_log2
{
NBL_CONSTEXPR_STATIC_INLINE uint16_t levels = conditional_value<(WorkgroupSizeLog2>SubgroupSizeLog2),uint16_t,conditional_value<(WorkgroupSizeLog2>SubgroupSizeLog2*2+2),uint16_t,3,2>::value,1>::value;
NBL_CONSTEXPR_STATIC_INLINE uint16_t value = mpl::max_v<uint32_t, WorkgroupSizeLog2-SubgroupSizeLog2, SubgroupSizeLog2>+SubgroupSizeLog2;
};

template<class VirtualWorkgroup, uint16_t BaseItemsPerInvocation, uint16_t WorkgroupSizeLog2, uint16_t SubgroupSizeLog2>
struct items_per_invocation
{
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocationProductLog2 = mpl::max_v<int16_t,WorkgroupSizeLog2-SubgroupSizeLog2*VirtualWorkgroup::levels,0>;
NBL_CONSTEXPR_STATIC_INLINE uint16_t value0 = BaseItemsPerInvocation;
NBL_CONSTEXPR_STATIC_INLINE uint16_t value1 = uint16_t(0x1u) << conditional_value<VirtualWorkgroup::levels==3, uint16_t,mpl::min_v<uint16_t,ItemsPerInvocationProductLog2,2>, ItemsPerInvocationProductLog2>::value;
NBL_CONSTEXPR_STATIC_INLINE uint16_t value2 = uint16_t(0x1u) << mpl::max_v<int16_t,ItemsPerInvocationProductLog2-2,0>;
};
}

template<uint16_t _WorkgroupSizeLog2, uint16_t _SubgroupSizeLog2, uint16_t _ItemsPerInvocation>
struct Configuration
{
NBL_CONSTEXPR_STATIC_INLINE uint16_t WorkgroupSizeLog2 = _WorkgroupSizeLog2;
NBL_CONSTEXPR_STATIC_INLINE uint16_t WorkgroupSize = uint16_t(0x1u) << WorkgroupSizeLog2;
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupSizeLog2 = _SubgroupSizeLog2;
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupSize = uint16_t(0x1u) << SubgroupSizeLog2;
static_assert(WorkgroupSizeLog2>=_SubgroupSizeLog2, "WorkgroupSize cannot be smaller than SubgroupSize");

// must have at least enough level 0 outputs to feed a single subgroup
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupsPerVirtualWorkgroupLog2 = mpl::max_v<uint16_t, WorkgroupSizeLog2-SubgroupSizeLog2, SubgroupSizeLog2>;
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupsPerVirtualWorkgroup = uint16_t(0x1u) << SubgroupsPerVirtualWorkgroupLog2;

using virtual_wg_t = impl::virtual_wg_size_log2<WorkgroupSizeLog2, SubgroupSizeLog2>;
NBL_CONSTEXPR_STATIC_INLINE uint16_t LevelCount = virtual_wg_t::levels;
NBL_CONSTEXPR_STATIC_INLINE uint16_t VirtualWorkgroupSize = uint16_t(0x1u) << virtual_wg_t::value;
using items_per_invoc_t = impl::items_per_invocation<virtual_wg_t, _ItemsPerInvocation, WorkgroupSizeLog2, SubgroupSizeLog2>;
// NBL_CONSTEXPR_STATIC_INLINE uint32_t2 ItemsPerInvocation; TODO? doesn't allow inline definitions for uint32_t2 for some reason, uint32_t[2] as well ; declaring out of line results in not constant expression
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_0 = items_per_invoc_t::value0;
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_1 = items_per_invoc_t::value1;
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_2 = items_per_invoc_t::value2;
static_assert(ItemsPerInvocation_1<=4, "3 level scan would have been needed with this config!");

NBL_CONSTEXPR_STATIC_INLINE uint16_t SharedMemSize = conditional_value<LevelCount==3,uint16_t,SubgroupSize*ItemsPerInvocation_2,0>::value + SubgroupsPerVirtualWorkgroup*ItemsPerInvocation_1;
};

// special case when workgroup size 2048 and subgroup size 16 needs 3 levels and virtual workgroup size 4096 to get a full subgroup scan each on level 1 and 2 16x16x16=4096
// specializing with macros because of DXC bug: https://github.com/microsoft/DirectXShaderCom0piler/issues/7007
#define SPECIALIZE_CONFIG_CASE_2048_16(ITEMS_PER_INVOC) template<>\
struct Configuration<11, 4, ITEMS_PER_INVOC>\
{\
NBL_CONSTEXPR_STATIC_INLINE uint16_t WorkgroupSize = uint16_t(0x1u) << 11u;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupSizeLog2 = uint16_t(4u);\
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupSize = uint16_t(0x1u) << SubgroupSizeLog2;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupsPerVirtualWorkgroupLog2 = 7u;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t SubgroupsPerVirtualWorkgroup = 128u;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t LevelCount = 3u;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t VirtualWorkgroupSize = uint16_t(0x1u) << 4096;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_0 = ITEMS_PER_INVOC;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_1 = 1u;\
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation_2 = 1u;\
};\

SPECIALIZE_CONFIG_CASE_2048_16(1)
SPECIALIZE_CONFIG_CASE_2048_16(2)
SPECIALIZE_CONFIG_CASE_2048_16(4)

}
}
}

#undef SPECIALIZE_CONFIG_CASE_2048_16

#endif
Loading