Skip to content

Block Chain Scan #884

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 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion examples_tests
34 changes: 34 additions & 0 deletions include/nbl/builtin/hlsl/scan/arithmetic.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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_SCAN_ARITHMETIC_INCLUDED_
#define _NBL_BUILTIN_HLSL_SCAN_ARITHMETIC_INCLUDED_

#include "nbl/builtin/hlsl/scan/arithmetic_impl.hlsl"

namespace nbl
{
namespace hlsl
{
namespace scan
{

template<class Config, class BinOp, bool ForwardProgressGuarantees, class device_capabilities=void>
Copy link
Member Author

Choose a reason for hiding this comment

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

we should make a fake device feature called forwardProgressGuarantees which is basically always false

struct reduction
{
using scalar_t = typename BinOp::type_t;

template<class ReadOnlyDataAccessor, class ScratchAccessor NBL_FUNC_REQUIRES(ArithmeticDataAccessor<ReadOnlyDataAccessor> && ArithmeticSharedMemoryAccessor<ScratchAccessor>)
static scalar_t __call(NBL_REF_ARG(ReadOnlyDataAccessor) dataAccessor, NBL_REF_ARG(ScratchAccessor) sharedMemScratchAccessor) // scratch bda?
{
impl::reduce<Config, BinOp, ForwardProgressGuarantees, device_capabilities> fn;
scalar_t value = fn.template __call<ReadOnlyDataAccessor,ScratchAccessor>(dataAccessor, sharedMemScratchAccessor);
return value;
}
};

}
}
}

#endif
120 changes: 120 additions & 0 deletions include/nbl/builtin/hlsl/scan/arithmetic_impl.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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_SCAN_ARITHMETIC_IMPL_INCLUDED_
#define _NBL_BUILTIN_HLSL_SCAN_ARITHMETIC_IMPL_INCLUDED_

#include "nbl/builtin/hlsl/workgroup2/arithmetic.hlsl"

namespace nbl
{
namespace hlsl
{
namespace scan
{

template<uint16_t _WorkgroupSizeLog2, uint16_t _SubgroupSizeLog2, uint16_t _ItemsPerInvocation>
struct ScanConfiguration
{
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;
NBL_CONSTEXPR_STATIC_INLINE uint16_t ItemsPerInvocation = _ItemsPerInvocation;

using arith_config_t = workgroup2::ArithmeticConfiguration<config_t::WorkgroupSizeLog2, config_t::SubgroupSizeLog2, config_t::ItemsPerInvocation>;
NBL_CONSTEXPR_STATIC_INLINE uint32_t SharedScratchElementCount = arith_config_t::SharedScratchElementCount;
};

namespace impl
{

template<typename T> // only uint32_t or uint64_t for now?
struct Constants
{
NBL_CONSTEXPR_STATIC_INLINE T NOT_READY = 0;
NBL_CONSTEXPR_STATIC_INLINE T LOCAL_COUNT = T(0x1u) << (sizeof(T)*8-2);
NBL_CONSTEXPR_STATIC_INLINE T GLOBAL_COUNT = T(0x1u) << (sizeof(T)*8-1);
NBL_CONSTEXPR_STATIC_INLINE T STATUS_MASK = LOCAL_COUNT | GLOBAL_COUNT;
};
Comment on lines +33 to +40
Copy link
Member Author

Choose a reason for hiding this comment

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

you can use enum class if you update DXC btw also with : uint16_t or : uint64_t

not everything needs to be a crazy template


template<class Config, class BinOp, bool ForwardProgressGuarantees, class device_capabilities>
struct reduce
{
using scalar_t = typename BinOp::type_t;
using constants_t = Constants<scalar_t>;
using config_t = Config;
using arith_config_t = typename Config::arith_config_t;
using workgroup_reduce_t = workgroup2::reduction<arith_config_t, BinOp, device_capabilities>;
using binop_t = BinOp;

template<class DataAccessor, class ScratchAccessor>
scalar_t __call(NBL_REF_ARG(DataAccessor) dataAccessor, NBL_REF_ARG(ScratchAccessor) sharedMemScratchAccessor)
Comment on lines +121 to +122
Copy link
Member Author

Choose a reason for hiding this comment

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

you have the readonly accessor to get your element, and you have the scratch memory accessor (for the workgroup scans/reductions) but you don't have:

  • accessor for the Device-Scope scratch
  • accessor for where to store the reduction result (reduction is special compared to a scan, you can't get the result right away)

{
const scalar_t localReduction = workgroup_reduce_t::__call<DataAccessor, ScratchAccessor>(dataAccessor, sharedMemScratchAccessor);
bda::__ptr<T> scratch = dataAccessor.getScratchPtr(); // scratch data should be at least T[NumWorkgroups]
Copy link
Member Author

Choose a reason for hiding this comment

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

ask for a separate accessor for the workgroup flags

Also 32bit atomics are always present and probably the cheapest, there's no harm in packing 16 workgroup flags into the same uint32_t but please benchmark if its a net gain (more likely to sit in GPU's L2 cache) or loss (contention)

Copy link
Member Author

Choose a reason for hiding this comment

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

aaah that might not be compatible with using UMax though


const bool lastInvocation = (workgroup::SubgroupContiguousIndex() == WorkgroupSize-1);
if (lastInvocation)
{
bda::__ref<T> scratchId = (scratch + glsl::gl_WorkgroupID()).deref();
spirv::atomicUMax(scratchId.__get_spv_ptr(), spv::ScopeWorkgroup, spv::MemorySemanticsReleaseMask, localReduction|constants_t::LOCAL_COUNT);
Copy link
Member Author

Choose a reason for hiding this comment

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

you want to separate the storage of the reduction from the flags I think.

}

scalar_t prefix = scalar_t(0);
// decoupled lookback
if (ForwardProgressGuarantees)
{
if (lastInvocation) // don't make whole block work and do busy stuff
{
for (uint32_t prevID = glsl::gl_WorkgroupID()-1; prevID > 0u; prevID--)
{
scalar_t value = scalar_t(0);
{
// spin until something is ready
while (value == constants_t::NOT_READY)
{
bda::__ref<uint32_t,4> scratchPrev = (scratch-1).deref();
value = spirv::atomicLoad(scratchPrev.__get_spv_ptr(), spv::ScopeWorkgroup, spv::MemorySemanticsAcquireMask);
}
}
prefix += value & (~constants_t::STATUS_MASK);

// last was actually a global sum, we have the prefix, we can quit
if (value & constants_t::GLOBAL_COUNT)
break;
}
}
prefix = workgroup::Broadcast(prefix, sharedMemScratchAccessor, WorkgroupSize-1);
}

binop_t binop;
scalar_t globalReduction = binop(prefix,localReduction);
if (lastInvocation)
{
bda::__ref<uint32_t,4> scratchId = (scratch + glsl::gl_WorkgroupID()).deref();
spirv::atomicUMax(scratchId.__get_spv_ptr(), spv::ScopeWorkgroup, spv::MemorySemanticsReleaseMask, globalReduction|constants_t::GLOBAL_COUNT);
}

// get last item from scratch
uint32_t lastWorkgroup = glsl::gl_NumWorkgroups() - 1;
bda::__ref<uint32_t,4> scratchLast = (scratch + lastWorkgroup).deref();
uint32_t value;
{
// wait until last workgroup does reduction
while (value & constants_t::GLOBAL_COUNT)
{
value = spirv::atomicLoad(scratchLast.__get_spv_ptr(), spv::ScopeWorkgroup, spv::MemorySemanticsAcquireMask);
}
}
return value & (~constants_t::STATUS_MASK);
}
}

}

}
}
}

#endif
66 changes: 0 additions & 66 deletions include/nbl/builtin/hlsl/scan/declarations.hlsl

This file was deleted.

Loading
Loading