From 7ca671d9b1674cc4afc0c2f064af93dfc451a56e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 31 Aug 2019 12:48:37 +0200 Subject: [PATCH] Switched benchmark sample to BenchmarkDotNet --- NuGet.config | 2 +- .../ComputeSharp.Benchmark.csproj | 4 + ...ectedLayerBenchmark.cs => DnnBenchmark.cs} | 96 ++++++++----------- samples/ComputeSharp.Benchmark/Program.cs | 67 ++----------- 4 files changed, 53 insertions(+), 116 deletions(-) rename samples/ComputeSharp.Benchmark/{FullyConnectedLayerBenchmark.cs => DnnBenchmark.cs} (65%) diff --git a/NuGet.config b/NuGet.config index 1e83bce6..4d878f6d 100644 --- a/NuGet.config +++ b/NuGet.config @@ -2,4 +2,4 @@ - \ No newline at end of file + diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index bfb20336..f93e3c60 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -5,6 +5,10 @@ netcoreapp3.0 + + + + diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs similarity index 65% rename from samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs rename to samples/ComputeSharp.Benchmark/DnnBenchmark.cs index 450800f4..7cdcd870 100644 --- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/DnnBenchmark.cs @@ -1,13 +1,15 @@ using System; +using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using BenchmarkDotNet.Attributes; namespace ComputeSharp.Benchmark { /// - /// A that benchmarks a fully connected layer forward pass on both CPU and GPU + /// A that benchmarks the APIs in the class, on both CPU and GPU /// - internal sealed class FullyConnectedLayerBenchmark : IDisposable + public class DnnBenchmark : IDisposable { /// /// The number of samples @@ -32,52 +34,53 @@ namespace ComputeSharp.Benchmark /// /// The input tensor /// - private readonly float[] X; + private float[] X; /// /// The weights tensor /// - private readonly float[] W; + private float[] W; /// /// The bias tensor /// - private readonly float[] B; + private float[] B; /// /// The result tensor /// - private readonly float[] Y; + private float[] Y; /// /// The input tensor /// - private readonly ReadOnlyBuffer BufferX; + private ReadOnlyBuffer BufferX; /// /// The weights tensor /// - private readonly ReadOnlyBuffer BufferW; + private ReadOnlyBuffer BufferW; /// /// The bias tensor /// - private readonly ReadOnlyBuffer BufferB; + private ReadOnlyBuffer BufferB; /// /// The result tensor /// - private readonly ReadWriteBuffer BufferY; + private ReadWriteBuffer BufferY; /// /// A instance to initialize the tensors /// - private static readonly Random Random = new Random(); + private readonly Random Random = new Random(); /// - /// Creates a new instance + /// Initial setup for a benchmarking session /// - public FullyConnectedLayerBenchmark() + [GlobalSetup] + public void Setup() { X = CreateRandomArray(C * N * M); W = CreateRandomArray(M * P); @@ -90,14 +93,31 @@ namespace ComputeSharp.Benchmark BufferY = Gpu.Default.AllocateReadWriteBuffer(Y); } + /// + /// Clears the memory from the current session + /// + [GlobalCleanup] + public void Dispose() + { + ArrayPool.Shared.Return(X); + ArrayPool.Shared.Return(W); + ArrayPool.Shared.Return(B); + ArrayPool.Shared.Return(Y); + + BufferX.Dispose(); + 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 static float[] CreateRandomArray(int size) + private float[] CreateRandomArray(int size) { - float[] array = new float[size]; + float[] array = ArrayPool.Shared.Rent(size); ref float r = ref array[0]; for (int i = 0; i < size; i++) { @@ -110,16 +130,19 @@ namespace ComputeSharp.Benchmark /// /// Runs a fully connected forward operation on the CPU /// + [Benchmark] public void Cpu() => Dnn.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y); /// /// Runs a fully connected forward operation on the GPU /// + [Benchmark] 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 /// + [Benchmark] public void GpuWithTemporaryBuffers() { using ReadOnlyBuffer x = Gpu.Default.AllocateReadOnlyBuffer(X); @@ -131,48 +154,5 @@ namespace ComputeSharp.Benchmark y.GetData(Y); } - - /// - /// Checks whether or not thhe CPU and GPU implementations produce the same output - /// - public bool EnsureImplementationsMatch() - { - // Run the CPU implementation and copy the result - Cpu(); - float[] y_cpu = new float[Y.Length]; - Y.AsSpan().CopyTo(y_cpu); - - // Run the GPU implementation with temporary buffers - GpuWithTemporaryBuffers(); - - // Compare the two results - ref float r_cpu = ref y_cpu[0]; - ref float r_gpu = ref Y[0]; - - for (int i = 0; i < y_cpu.Length; i++) - { - float x_cpu = Unsafe.Add(ref r_cpu, i); - float x_gpu = Unsafe.Add(ref r_gpu, i); - float absolute = MathF.Abs(x_cpu - x_gpu); - float relative = 1e-1f * MathF.Max(MathF.Abs(x_cpu), MathF.Abs(x_gpu)); - - if (absolute >= 1e-2f && - absolute > MathF.Max(1e-2f, relative)) - { - return false; - } - } - - return true; - } - - /// - public void Dispose() - { - BufferX.Dispose(); - BufferW.Dispose(); - BufferB.Dispose(); - BufferY.Dispose(); - } } } diff --git a/samples/ComputeSharp.Benchmark/Program.cs b/samples/ComputeSharp.Benchmark/Program.cs index 09d064b0..3667f4c3 100644 --- a/samples/ComputeSharp.Benchmark/Program.cs +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -1,65 +1,18 @@ -using System; -using System.Diagnostics; +using BenchmarkDotNet.Running; namespace ComputeSharp.Benchmark { - class Program + public class Program { - static void Main() + public static void Main() { - using var benchmark = new FullyConnectedLayerBenchmark(); - - Console.WriteLine("======== WARMUP ========"); - if (!benchmark.EnsureImplementationsMatch()) throw new InvalidOperationException("CPU and GPU code don't match"); - - Console.WriteLine(); - Console.WriteLine("======== GPU with temporary buffer ========"); - Stopwatch timer = Stopwatch.StartNew(); - TimeSpan cpuTime = TimeSpan.Zero, gpuTime = TimeSpan.Zero; - - // GPU with temporary buffers - for (int i = 0; i < 10; i++) - { - benchmark.Cpu(); - cpuTime += timer.Elapsed; - Console.WriteLine($"CPU: {timer.Elapsed:g}"); - timer.Restart(); - - benchmark.GpuWithTemporaryBuffers(); - gpuTime += timer.Elapsed; - Console.WriteLine($"GPU: {timer.Elapsed:g}"); - timer.Restart(); - } - - Console.WriteLine(); - Console.WriteLine("======== AVERAGE ========"); - Console.WriteLine($"CPU: {cpuTime / 10:g}"); - Console.WriteLine($"GPU: {gpuTime / 10:g}"); - Console.WriteLine("========================="); - Console.WriteLine(); - - Console.WriteLine("======== GPU with no temporary buffer ========"); - timer = Stopwatch.StartNew(); - cpuTime = gpuTime = TimeSpan.Zero; - - // GPU with no temporary buffers - for (int i = 0; i < 10; i++) - { - benchmark.Cpu(); - cpuTime += timer.Elapsed; - Console.WriteLine($"CPU: {timer.Elapsed:g}"); - timer.Restart(); - - benchmark.GpuWithNoTemporaryBuffers(); - gpuTime += timer.Elapsed; - Console.WriteLine($"GPU: {timer.Elapsed:g}"); - timer.Restart(); - } - - Console.WriteLine(); - Console.WriteLine("======== AVERAGE ========"); - Console.WriteLine($"CPU: {cpuTime / 10:g}"); - Console.WriteLine($"GPU: {gpuTime / 10:g}"); + /* ================= + * BenchmarkDotNet + * ================ + * In order to run this benchmark, compile this project in Release mode, + * then go to its bin\Release\netcoreapp3.0 folder, open a cmd prompt + * and type "dotnet ComputeSharp.Benchmark.dll */ + BenchmarkRunner.Run(); } } }