From f7c2d420bf68dce0bf45e903807a6797754a3d77 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 22:09:52 +0200 Subject: [PATCH] Added API to control the number of threads --- src/ComputeSharp.Shaders/ShaderRunner.cs | 39 +++++++++++++++---- .../Extensions/GraphicsDeviceExtensions.cs | 29 +++++++++++--- .../ShaderDispatchTests.cs | 30 ++++++++++++++ 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index b59b6ffe..15fceec9 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -21,20 +21,43 @@ namespace ComputeSharp.Shaders /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader - /// The number of threads to run on the X axis - /// The number of threads to run on the Y axis - /// The number of threads to run on the Z axis + /// The number of iterations to run on the X axis + /// The number of iterations to run on the Y axis + /// The number of iterations to run on the Z axis /// The input representing the compute shader to run public static void Run(GraphicsDevice device, int x, int y, int z, Action action) { - // Calculate the optimized thread num and group values + // Calculate the optimized thread num values int threadsX = device.WavefrontSize, threadsY = y > 1 ? device.WavefrontSize : 1, - threadsZ = z > 1 ? device.WavefrontSize : 1, - groupsX = (x - 1) / device.WavefrontSize + 1, - groupsY = (y - 1) / device.WavefrontSize + 1, - groupsZ = (z - 1) / device.WavefrontSize + 1; + threadsZ = z > 1 ? device.WavefrontSize : 1; + + Run(device, x, y, z, threadsX, threadsY, threadsZ, action); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The number of iterations to run on the X axis + /// The number of iterations to run on the Y axis + /// The number of iterations to run on the Z axis + /// The number of threads in each thread group for the X axis + /// The number of threads in each thread group for the Y axis + /// The number of threads in each thread group for the Z axis + /// The input representing the compute shader to run + public static void Run( + GraphicsDevice device, + int x, int y, int z, + int threadsX, int threadsY, int threadsZ, + Action action) + { + // Calculate the dispatch values + int + groupsX = x / threadsX + (x % threadsX == 0 ? 0 : 1), + groupsY = y / threadsY + (y % threadsY == 0 ? 0 : 1), + groupsZ = z / threadsZ + (z % threadsZ == 0 ? 0 : 1); // Load the input shader ShaderLoader shaderLoader = ShaderLoader.Load(action); diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index f9bdc634..db36a328 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -13,7 +13,7 @@ namespace ComputeSharp /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader - /// The number of threads to run on the X axis + /// The number of iterations to run on the X axis /// The input representing the compute shader to run public static void For(this GraphicsDevice device, int x, Action action) => ShaderRunner.Run(device, x, 1, 1, action); @@ -21,8 +21,8 @@ namespace ComputeSharp /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader - /// The number of threads to run on the X axis - /// The number of threads to run on the Y axis + /// The number of iterations to run on the X axis + /// The number of iterations to run on the Y axis /// The input representing the compute shader to run public static void For(this GraphicsDevice device, int x, int y, Action action) => ShaderRunner.Run(device, x, y, 1, action); @@ -30,10 +30,27 @@ namespace ComputeSharp /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader - /// The number of threads to run on the X axis - /// The number of threads to run on the Y axis - /// The number of threads to run on the Z axis + /// The number of iterations to run on the X axis + /// The number of iterations to run on the Y axis + /// The number of iterations to run on the Z axis /// The input representing the compute shader to run public static void For(this GraphicsDevice device, int x, int y, int z, Action action) => ShaderRunner.Run(device, x, y, z, action); + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The number of iterations to run on the X axis + /// The number of iterations to run on the Y axis + /// The number of iterations to run on the Z axis + /// The number of threads in each thread group for the X axis + /// The number of threads in each thread group for the Y axis + /// The number of threads in each thread group for the Z axis + /// The input representing the compute shader to run + public static void For( + this GraphicsDevice device, + int x, int y, int z, + int threadsX, int threadsY, int threadsZ, + Action action) => ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, action); } } diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs b/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs index 7e912983..27fa6073 100644 --- a/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs +++ b/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs @@ -25,6 +25,21 @@ namespace ComputeSharp.NetCore.Tests Assert.IsTrue(array.AsSpan().ContentEquals(expected)); } + [TestMethod] + public void WriteToReadWriteBufferManualDispatch1D() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(100); + + Action action = id => buffer[id.X] = id.X; + + Gpu.Default.For(100, 1, 1, 64, 1, 1, action); + + float[] array = buffer.GetData(); + float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); + + Assert.IsTrue(array.AsSpan().ContentEquals(expected)); + } + [TestMethod] public void WriteToReadWriteBufferDispatch2D() { @@ -54,5 +69,20 @@ namespace ComputeSharp.NetCore.Tests Assert.IsTrue(array.AsSpan().ContentEquals(expected)); } + + [TestMethod] + public void WriteToReadWriteBufferManualDispatch3D() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1000); + + Action action = id => buffer[id.X + id.Y * 10 + id.Z * 100] = id.X + id.Y * 10 + id.Z * 100; + + Gpu.Default.For(10, 10, 10, 4, 4, 4, action); + + float[] array = buffer.GetData(); + float[] expected = Enumerable.Range(0, 1000).Select(i => (float)i).ToArray(); + + Assert.IsTrue(array.AsSpan().ContentEquals(expected)); + } } }