"sgl-kernel/python/vscode:/vscode.git/clone" did not exist on "c9db79117f9117545ae936658e7d56f94112a8af"
Unverified Commit 2bbb50c4 authored by Paul Fultz II's avatar Paul Fultz II Committed by GitHub
Browse files

Improve kernel code generation (#1285)

* Only run __syncthreads when there is data to preload
* Improve loops
* Add const attribute to improve optimizations
parent 05b13c9f
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <migraphx/kernels/hip.hpp> #include <migraphx/kernels/hip.hpp>
#include <migraphx/kernels/types.hpp> #include <migraphx/kernels/types.hpp>
#include <migraphx/kernels/integral_constant.hpp> #include <migraphx/kernels/integral_constant.hpp>
#include <migraphx/kernels/type_traits.hpp>
namespace migraphx { namespace migraphx {
...@@ -53,29 +54,51 @@ struct index ...@@ -53,29 +54,51 @@ struct index
return blockDim.x; // NOLINT return blockDim.x; // NOLINT
} }
#endif #endif
template <class N, class Stride>
static constexpr auto max_stride_iterations(N n, Stride stride)
{
return (n - _c<1>) / stride + _c<1>;
}
template <class F> template <class F, class N, class Stride>
__device__ void global_stride(index_int n, F f) const static constexpr void for_stride(index_int start, N n, Stride stride, F f)
{ {
const auto stride = nglobal(); if constexpr(not is_integral<N>{} and not is_integral<Stride>{} and
for(index_int i = global; i < n; i += stride) max_stride_iterations(n, stride) == 1)
{ {
f(i); if constexpr(stride > n)
{
if(start < n)
f(start);
} }
else
{
f(start);
} }
}
template <class F> else
__device__ void local_stride(index_int n, F f) const
{ {
const auto stride = nlocal(); for(index_int i = start; i < n; i += stride)
for(index_int i = local; i < n; i += stride)
{ {
f(i); f(i);
} }
} }
}
template <class F, class N>
__device__ void global_stride(N n, F f) const
{
for_stride(global, n, nglobal(), f);
}
template <class F, class N>
__device__ void local_stride(N n, F f) const
{
for_stride(local, n, nlocal(), f);
}
}; };
inline __device__ index make_index() inline __device__ __attribute__((const)) index make_index()
{ {
return index{blockIdx.x * blockDim.x + threadIdx.x, threadIdx.x, blockIdx.x}; // NOLINT return index{blockIdx.x * blockDim.x + threadIdx.x, threadIdx.x, blockIdx.x}; // NOLINT
} }
......
...@@ -186,6 +186,7 @@ __device__ auto auto_preload(index idx) ...@@ -186,6 +186,7 @@ __device__ auto auto_preload(index idx)
{ {
return make_transform([=](auto f, auto... xs) { return make_transform([=](auto f, auto... xs) {
auto invoke = [=](auto... ys) { auto invoke = [=](auto... ys) {
if constexpr((Bs or ...))
__syncthreads(); __syncthreads();
f(ys...); f(ys...);
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment