Added API to control the number of threads

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-14 22:09:52 +02:00
1 親 bf790f0195
コミット f7c2d420bf
3ファイル変更
+84 -14

検索条件に一致するファイルはありません

+31 -8
ファイルの表示
@@ -21,20 +21,43 @@ namespace ComputeSharp.Shaders
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="z">The number of threads to run on the Z axis</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="z">The number of iterations to run on the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void Run(GraphicsDevice device, int x, int y, int z, Action<ThreadIds> 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);
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="z">The number of iterations to run on the Z axis</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void Run(
GraphicsDevice device,
int x, int y, int z,
int threadsX, int threadsY, int threadsZ,
Action<ThreadIds> 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);
+23 -6
ファイルの表示
@@ -13,7 +13,7 @@ namespace ComputeSharp
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, Action<ThreadIds> action) => ShaderRunner.Run(device, x, 1, 1, action);
@@ -21,8 +21,8 @@ namespace ComputeSharp
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, int y, Action<ThreadIds> action) => ShaderRunner.Run(device, x, y, 1, action);
@@ -30,10 +30,27 @@ namespace ComputeSharp
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="z">The number of threads to run on the Z axis</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="z">The number of iterations to run on the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, int y, int z, Action<ThreadIds> action) => ShaderRunner.Run(device, x, y, z, action);
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="z">The number of iterations to run on the Z axis</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(
this GraphicsDevice device,
int x, int y, int z,
int threadsX, int threadsY, int threadsZ,
Action<ThreadIds> action) => ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, action);
}
}
+30
ファイルの表示
@@ -25,6 +25,21 @@ namespace ComputeSharp.NetCore.Tests
Assert.IsTrue(array.AsSpan().ContentEquals(expected));
}
[TestMethod]
public void WriteToReadWriteBufferManualDispatch1D()
{
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(100);
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1000);
Action<ThreadIds> 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));
}
}
}