diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs
index e5ebc6b0..58e6d2aa 100644
--- a/samples/ComputeSharp.Benchmark/Dnn.cs
+++ b/samples/ComputeSharp.Benchmark/Dnn.cs
@@ -1,5 +1,4 @@
-using System;
-using System.Runtime.CompilerServices;
+using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ComputeSharp.Graphics.Buffers;
@@ -32,16 +31,19 @@ namespace ComputeSharp.Benchmark
for (int i = 0; i < n; i++)
{
+ int x_offset = i * m;
+ int y_offset = i * p;
+
for (int j = 0; j < p; j++)
{
float result = 0f;
for (int k = 0; k < m; k++)
{
- result += Unsafe.Add(ref rx, i * m + k) * Unsafe.Add(ref rw, k * p + j);
+ result += Unsafe.Add(ref rx, x_offset + k) * Unsafe.Add(ref rw, k * p + j);
}
- Unsafe.Add(ref ry, i * p + j) = result + Unsafe.Add(ref rb, j);
+ Unsafe.Add(ref ry, y_offset + j) = result + Unsafe.Add(ref rb, j);
}
}
}
@@ -60,28 +62,28 @@ namespace ComputeSharp.Benchmark
/// The weights tensor
/// The bias tensor
/// The result tensor
- public static void FullyConnectedForwardGpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y)
+ public static void FullyConnectedForwardGpu(
+ int c, int n, int m, int p,
+ ReadOnlyBuffer x, ReadOnlyBuffer w, ReadOnlyBuffer b, ReadWriteBuffer y)
{
- using ReadOnlyBuffer x_gpu = Gpu.Default.AllocateReadOnlyBuffer(x);
- using ReadOnlyBuffer w_gpu = Gpu.Default.AllocateReadOnlyBuffer(w);
- using ReadOnlyBuffer b_gpu = Gpu.Default.AllocateReadOnlyBuffer(b);
- using ReadWriteBuffer y_gpu = Gpu.Default.AllocateReadWriteBuffer(y);
+ uint u_n = (uint)n;
+ uint u_m = (uint)m;
+ uint u_p = (uint)p;
void Kernel(ThreadIds id)
{
+ uint x_offset = id.X * u_n * u_p + id.Y * u_m;
float result = 0f;
- for (int k = 0; k < m; k++)
+ for (uint k = 0; k < u_m; k++)
{
- result += x_gpu[(uint)(id.X * n * p + id.Y * m + k)] * w_gpu[(uint)(k * p + id.Z)];
+ result += x[x_offset + k] * w[k * u_p + id.Z];
}
- y_gpu[(uint)(id.X * n * p + id.Y * p + id.Z)] = result + b_gpu[id.Z];
+ y[id.X * u_n * u_p + id.Y * u_p + id.Z] = result + b[id.Z];
};
Gpu.Default.For(c, n, p, Kernel);
-
- y_gpu.GetData(y);
}
}
}
diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs
index 3fe025d8..1339790d 100644
--- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs
+++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs
@@ -1,13 +1,14 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
+using ComputeSharp.Graphics.Buffers;
namespace ComputeSharp.Benchmark
{
///
/// A that benchmarks a fully connected layer forward pass on both CPU and GPU
///
- public class FullyConnectedLayerBenchmark
+ internal sealed class FullyConnectedLayerBenchmark : IDisposable
{
///
/// The number of samples
@@ -49,6 +50,26 @@ namespace ComputeSharp.Benchmark
///
private readonly float[] Y;
+ ///
+ /// The input tensor
+ ///
+ private readonly ReadOnlyBuffer BufferX;
+
+ ///
+ /// The weights tensor
+ ///
+ private readonly ReadOnlyBuffer BufferW;
+
+ ///
+ /// The bias tensor
+ ///
+ private readonly ReadOnlyBuffer BufferB;
+
+ ///
+ /// The result tensor
+ ///
+ private readonly ReadWriteBuffer BufferY;
+
///
/// A instance to initialize the tensors
///
@@ -63,6 +84,11 @@ namespace ComputeSharp.Benchmark
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
Y = CreateRandomArray(C * N * P);
+
+ BufferX = ComputeSharp.Gpu.Default.AllocateReadOnlyBuffer(X);
+ BufferW = ComputeSharp.Gpu.Default.AllocateReadOnlyBuffer(W);
+ BufferB = ComputeSharp.Gpu.Default.AllocateReadOnlyBuffer(B);
+ BufferY = ComputeSharp.Gpu.Default.AllocateReadWriteBuffer(Y);
}
///
@@ -90,6 +116,30 @@ namespace ComputeSharp.Benchmark
///
/// Runs a fully connected forward operation on the GPU
///
- public void Gpu() => Dnn.FullyConnectedForwardGpu(C, N, M, P, X, W, B, Y);
+ public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(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
+ ///
+ public void GpuWithTemporaryBuffers()
+ {
+ using ReadOnlyBuffer x = Gpu.Default.AllocateReadOnlyBuffer(X);
+ using ReadOnlyBuffer w = Gpu.Default.AllocateReadOnlyBuffer(W);
+ using ReadOnlyBuffer b = Gpu.Default.AllocateReadOnlyBuffer(B);
+ using ReadWriteBuffer y = Gpu.Default.AllocateReadWriteBuffer(Y);
+
+ Dnn.FullyConnectedForwardGpu(C, N, M, P, x, w, b, y);
+
+ y.GetData(Y);
+ }
+
+ ///
+ public void Dispose()
+ {
+ BufferX.Dispose();
+ BufferW.Dispose();
+ BufferB.Dispose();
+ BufferY.Dispose();
+ }
}
}