From 5d9e46c0327800415b08eb814e7652df58c635e6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 15:26:49 +0200 Subject: [PATCH] Created base FullyConnectedLayerBenchmark class --- .../FullyConnectedLayerBenchmark.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs new file mode 100644 index 00000000..f4c8f88a --- /dev/null +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -0,0 +1,70 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Benchmark +{ + /// + /// A that benchmarks a fully connected layer forward pass on both CPU and GPU + /// + public class FullyConnectedLayerBenchmark + { + /// + /// The number of samples + /// + private const int Count = 128; + + /// + /// The nummber of rows in the matrix + /// + private const int N = 100; + + /// + /// The number of columns in the matrix (same as the number of rows in the matrix) + /// + private const int M = 100; + + /// + /// The number of columns in the matrix + /// + private const int P = 100; + + private readonly float[] A; + + private readonly float[] B; + + private readonly float[] C; + + private readonly float[] D; + + private static readonly Random Random = new Random(); + + /// + /// Creates a new instance + /// + public FullyConnectedLayerBenchmark() + { + A = CreateRandomArray(Count * N * M); + B = CreateRandomArray(M * P); + C = CreateRandomArray(N * P); + D = new float[Count * N * P]; + } + + /// + /// Creates a new array of the specified size + /// + /// The size of the new random array to create + [Pure] + private static float[] CreateRandomArray(int size) + { + float[] array = new float[size]; + ref float r = ref array[0]; + for (int i = 0; i < size; i++) + { + Unsafe.Add(ref r, i) = (float)Random.NextDouble(); + } + + return array; + } + } +}