diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs
index c6a3cc53..3f8087ba 100644
--- a/samples/ComputeSharp.Benchmark/Dnn.cs
+++ b/samples/ComputeSharp.Benchmark/Dnn.cs
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
+using ComputeSharp.Graphics;
namespace ComputeSharp.Benchmark
{
@@ -53,6 +54,7 @@ namespace ComputeSharp.Benchmark
///
/// Executes the forward pass on a fully connected layer on the GPU.
///
+ /// The device to run the computation on.
/// The number of samples in the input tensor.
/// The number of rows in the input matrix.
/// The number of columns in the input matrix.
@@ -63,10 +65,17 @@ namespace ComputeSharp.Benchmark
/// The result tensor.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FullyConnectedForwardGpu(
- int c, int n, int m, int p,
- ReadOnlyBuffer x, ReadOnlyBuffer w, ReadOnlyBuffer b, ReadWriteBuffer y)
+ GraphicsDevice device,
+ int c,
+ int n,
+ int m,
+ int p,
+ ReadOnlyBuffer x,
+ ReadOnlyBuffer w,
+ ReadOnlyBuffer b,
+ ReadWriteBuffer y)
{
- Gpu.Default.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
+ device.For(c, n, p, new FullyConnectedForwardKernel(n, m, p, x, w, b, y));
}
///
diff --git a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs
index 1b163220..45b5a191 100644
--- a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs
+++ b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs
@@ -1,6 +1,5 @@
using System;
using System.Buffers;
-using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
@@ -54,25 +53,45 @@ namespace ComputeSharp.Benchmark
private float[] Y;
///
- /// The input tensor.
+ /// The input tensor (GPU).
///
private ReadOnlyBuffer BufferX;
///
- /// The weights tensor.
+ /// The weights tensor (GPU).
///
private ReadOnlyBuffer BufferW;
///
- /// The bias tensor.
+ /// The bias tensor (GPU).
///
private ReadOnlyBuffer BufferB;
///
- /// The result tensor.
+ /// The result tensor (GPU).
///
private ReadWriteBuffer BufferY;
+ ///
+ /// The input tensor (CPU).
+ ///
+ private ReadOnlyBuffer FallbackBufferX;
+
+ ///
+ /// The weights tensor (CPU).
+ ///
+ private ReadOnlyBuffer FallbackBufferW;
+
+ ///
+ /// The bias tensor (CPU).
+ ///
+ private ReadOnlyBuffer FallbackBufferB;
+
+ ///
+ /// The result tensor (CPU).
+ ///
+ private ReadWriteBuffer FallbackBufferY;
+
///
/// A instance to initialize the tensors.
///
@@ -84,6 +103,19 @@ namespace ComputeSharp.Benchmark
[GlobalSetup]
public void Setup()
{
+ // Creates a new random normalized float[] array of a given size
+ float[] CreateRandomArray(int size)
+ {
+ float[] array = ArrayPool.Shared.Rent(size);
+ ref float r = ref array[0];
+ for (int i = 0; i < size; i++)
+ {
+ Unsafe.Add(ref r, i) = (float)Random.NextDouble();
+ }
+
+ return array;
+ }
+
X = CreateRandomArray(C * N * M);
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
@@ -93,6 +125,11 @@ namespace ComputeSharp.Benchmark
BufferW = Gpu.Default.AllocateReadOnlyBuffer(W);
BufferB = Gpu.Default.AllocateReadOnlyBuffer(B);
BufferY = Gpu.Default.AllocateReadWriteBuffer(Y);
+
+ FallbackBufferX = Gpu.Fallback.AllocateReadOnlyBuffer(X);
+ FallbackBufferW = Gpu.Fallback.AllocateReadOnlyBuffer(W);
+ FallbackBufferB = Gpu.Fallback.AllocateReadOnlyBuffer(B);
+ FallbackBufferY = Gpu.Fallback.AllocateReadWriteBuffer(Y);
}
///
@@ -110,23 +147,11 @@ namespace ComputeSharp.Benchmark
BufferW.Dispose();
BufferB.Dispose();
BufferY.Dispose();
- }
- ///
- /// Creates a new array of the specified size.
- ///
- /// The size of the new random array to create.
- [Pure]
- private float[] CreateRandomArray(int size)
- {
- float[] array = ArrayPool.Shared.Rent(size);
- ref float r = ref array[0];
- for (int i = 0; i < size; i++)
- {
- Unsafe.Add(ref r, i) = (float)Random.NextDouble();
- }
-
- return array;
+ FallbackBufferX.Dispose();
+ FallbackBufferW.Dispose();
+ FallbackBufferB.Dispose();
+ FallbackBufferY.Dispose();
}
///
@@ -139,7 +164,7 @@ namespace ComputeSharp.Benchmark
/// Runs a fully connected forward operation on the GPU.
///
[Benchmark]
- public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(C, N, M, P, BufferX, BufferW, BufferB, BufferY);
+ public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX, BufferW, BufferB, BufferY);
///
/// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations.
@@ -152,7 +177,29 @@ namespace ComputeSharp.Benchmark
using ReadOnlyBuffer b = Gpu.Default.AllocateReadOnlyBuffer(B);
using ReadWriteBuffer y = Gpu.Default.AllocateReadWriteBuffer(Y);
- Dnn.FullyConnectedForwardGpu(C, N, M, P, x, w, b, y);
+ Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y);
+
+ y.GetData(Y);
+ }
+
+ ///
+ /// Runs a fully connected forward operation on the fallback device.
+ ///
+ [Benchmark]
+ public void FallbackWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(Gpu.Fallback, C, N, M, P, FallbackBufferX, FallbackBufferW, FallbackBufferB, FallbackBufferY);
+
+ ///
+ /// Runs a fully connected forward operation on the fallback device, creating temporary buffers to perform the operations.
+ ///
+ [Benchmark]
+ public void FallbackWithTemporaryBuffers()
+ {
+ using ReadOnlyBuffer x = Gpu.Fallback.AllocateReadOnlyBuffer(X);
+ using ReadOnlyBuffer w = Gpu.Fallback.AllocateReadOnlyBuffer(W);
+ using ReadOnlyBuffer b = Gpu.Fallback.AllocateReadOnlyBuffer(B);
+ using ReadWriteBuffer y = Gpu.Fallback.AllocateReadWriteBuffer(Y);
+
+ Dnn.FullyConnectedForwardGpu(Gpu.Fallback, C, N, M, P, x, w, b, y);
y.GetData(Y);
}