From 6fe9995893f3d5cb182d49ed03cc569b6e5a24d5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 14:52:58 +0200 Subject: [PATCH 01/10] Renamed tests and sample projects --- .../ComputeSharp.Sample.csproj} | 0 .../Program.cs | 3 +-- src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs | 2 +- src/ComputeSharp.sln | 6 +++--- .../BufferTests.cs | 2 +- .../ComputeSharp.Tests.Internals.csproj} | 0 .../BufferTests.cs | 4 ++-- .../ComputeSharp.Tests.csproj} | 0 .../Extensions/SpanExtensions.cs | 2 +- .../InitializationTests.cs | 4 ++-- .../InvalidShaderTests.cs | 2 +- .../Properties/AssemblyInfo.cs | 0 .../ShaderClosureTests.cs | 4 ++-- .../ShaderDispatchTests.cs | 4 ++-- 14 files changed, 16 insertions(+), 17 deletions(-) rename samples/{NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj => ComputeSharp.Sample/ComputeSharp.Sample.csproj} (100%) rename samples/{NetCoreComputeShaderSample => ComputeSharp.Sample}/Program.cs (96%) rename tests/{ComputeSharp.NetCore.Tests.Internals => ComputeSharp.Tests.Internals}/BufferTests.cs (96%) rename tests/{ComputeSharp.NetCore.Tests.Internals/ComputeSharp.NetCore.Tests.Internals.csproj => ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj} (100%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/BufferTests.cs (97%) rename tests/{ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj => ComputeSharp.Tests/ComputeSharp.Tests.csproj} (100%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/Extensions/SpanExtensions.cs (96%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/InitializationTests.cs (93%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/InvalidShaderTests.cs (91%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/Properties/AssemblyInfo.cs (100%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/ShaderClosureTests.cs (97%) rename tests/{ComputeSharp.NetCore.Tests => ComputeSharp.Tests}/ShaderDispatchTests.cs (97%) diff --git a/samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj b/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj similarity index 100% rename from samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj rename to samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj diff --git a/samples/NetCoreComputeShaderSample/Program.cs b/samples/ComputeSharp.Sample/Program.cs similarity index 96% rename from samples/NetCoreComputeShaderSample/Program.cs rename to samples/ComputeSharp.Sample/Program.cs index de9ba68f..bfbf7acf 100644 --- a/samples/NetCoreComputeShaderSample/Program.cs +++ b/samples/ComputeSharp.Sample/Program.cs @@ -1,9 +1,8 @@ using System; using System.Linq; -using ComputeSharp; using ComputeSharp.Graphics.Buffers; -namespace NetCoreComputeShaderSample +namespace ComputeSharp.Sample { public class Program { diff --git a/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs index d1a09416..cf072a97 100644 --- a/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs @@ -2,5 +2,5 @@ [assembly: InternalsVisibleTo("ComputeSharp")] [assembly: InternalsVisibleTo("ComputeSharp.Shaders")] -[assembly: InternalsVisibleTo("ComputeSharp.NetCore.Tests.Internals")] +[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")] diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index 6d3cf377..9de1062a 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "Com EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreComputeShaderSample", "..\samples\NetCoreComputeShaderSample\NetCoreComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample", "..\samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{1A9C4108-F61A-4ABE-B5CF-F524521EAE2D}" ProjectSection(SolutionItems) = preProject @@ -20,9 +20,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.NetCore.Tests", "..\tests\ComputeSharp.NetCore.Tests\ComputeSharp.NetCore.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests", "..\tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.NetCore.Tests.Internals", "..\tests\ComputeSharp.NetCore.Tests.Internals\ComputeSharp.NetCore.Tests.Internals.csproj", "{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals", "..\tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj", "{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs b/tests/ComputeSharp.Tests.Internals/BufferTests.cs similarity index 96% rename from tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs rename to tests/ComputeSharp.Tests.Internals/BufferTests.cs index cf67acc9..fb146dfe 100644 --- a/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs +++ b/tests/ComputeSharp.Tests.Internals/BufferTests.cs @@ -5,7 +5,7 @@ using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests.Internals +namespace ComputeSharp.Tests.Internals { [TestClass] [TestCategory("Buffers")] diff --git a/tests/ComputeSharp.NetCore.Tests.Internals/ComputeSharp.NetCore.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj similarity index 100% rename from tests/ComputeSharp.NetCore.Tests.Internals/ComputeSharp.NetCore.Tests.Internals.csproj rename to tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj diff --git a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs similarity index 97% rename from tests/ComputeSharp.NetCore.Tests/BufferTests.cs rename to tests/ComputeSharp.Tests/BufferTests.cs index 54553917..2b0b00c9 100644 --- a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -1,10 +1,10 @@ using System; using System.Linq; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.NetCore.Tests.Extensions; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests +namespace ComputeSharp.Tests { [TestClass] [TestCategory("Buffers")] diff --git a/tests/ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj similarity index 100% rename from tests/ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj rename to tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj diff --git a/tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs b/tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs similarity index 96% rename from tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs rename to tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs index 11ec9ddf..8918c7c7 100644 --- a/tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs @@ -2,7 +2,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -namespace ComputeSharp.NetCore.Tests.Extensions +namespace ComputeSharp.Tests.Extensions { /// /// An helper with methods to process fixed-size buffers diff --git a/tests/ComputeSharp.NetCore.Tests/InitializationTests.cs b/tests/ComputeSharp.Tests/InitializationTests.cs similarity index 93% rename from tests/ComputeSharp.NetCore.Tests/InitializationTests.cs rename to tests/ComputeSharp.Tests/InitializationTests.cs index 4e3b4c0f..32848321 100644 --- a/tests/ComputeSharp.NetCore.Tests/InitializationTests.cs +++ b/tests/ComputeSharp.Tests/InitializationTests.cs @@ -2,10 +2,10 @@ using System.Linq; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.NetCore.Tests.Extensions; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests +namespace ComputeSharp.Tests { [TestClass] [TestCategory("Initialization")] diff --git a/tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs b/tests/ComputeSharp.Tests/InvalidShaderTests.cs similarity index 91% rename from tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs rename to tests/ComputeSharp.Tests/InvalidShaderTests.cs index a0693718..cfd18b75 100644 --- a/tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs +++ b/tests/ComputeSharp.Tests/InvalidShaderTests.cs @@ -1,7 +1,7 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests +namespace ComputeSharp.Tests { [TestClass] [TestCategory("InvalidShader")] diff --git a/tests/ComputeSharp.NetCore.Tests/Properties/AssemblyInfo.cs b/tests/ComputeSharp.Tests/Properties/AssemblyInfo.cs similarity index 100% rename from tests/ComputeSharp.NetCore.Tests/Properties/AssemblyInfo.cs rename to tests/ComputeSharp.Tests/Properties/AssemblyInfo.cs diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.cs similarity index 97% rename from tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs rename to tests/ComputeSharp.Tests/ShaderClosureTests.cs index 9a23fcd5..74917ded 100644 --- a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.cs @@ -2,10 +2,10 @@ using System.Linq; using System.Numerics; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.NetCore.Tests.Extensions; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests +namespace ComputeSharp.Tests { [TestClass] [TestCategory("ShaderClosure")] diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs b/tests/ComputeSharp.Tests/ShaderDispatchTests.cs similarity index 97% rename from tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs rename to tests/ComputeSharp.Tests/ShaderDispatchTests.cs index 27fa6073..82ac7d77 100644 --- a/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs +++ b/tests/ComputeSharp.Tests/ShaderDispatchTests.cs @@ -1,10 +1,10 @@ using System; using System.Linq; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.NetCore.Tests.Extensions; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace ComputeSharp.NetCore.Tests +namespace ComputeSharp.Tests { [TestClass] [TestCategory("ShaderDispatch")] From 9d94a2e5a4189262efedce135f9c5ce9fbaf18f6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 15:05:07 +0200 Subject: [PATCH 02/10] Added empty benchmark project --- .../ComputeSharp.Benchmark.csproj | 8 ++++++++ samples/ComputeSharp.Benchmark/Program.cs | 12 ++++++++++++ .../ComputeSharp.Benchmark.csproj | 12 ++++++++++++ src/ComputeSharp.sln | 11 +++++++++++ 4 files changed, 43 insertions(+) create mode 100644 samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj create mode 100644 samples/ComputeSharp.Benchmark/Program.cs create mode 100644 src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj new file mode 100644 index 00000000..958d2f1d --- /dev/null +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git a/samples/ComputeSharp.Benchmark/Program.cs b/samples/ComputeSharp.Benchmark/Program.cs new file mode 100644 index 00000000..1bfa2a7a --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ComputeSharp.Benchmark +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj new file mode 100644 index 00000000..33fc9464 --- /dev/null +++ b/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.0 + + + + + + + diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index 9de1062a..3cedb543 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests", "..\te EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals", "..\tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj", "{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Benchmark", "..\samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj", "{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -80,6 +82,14 @@ Global {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -88,6 +98,7 @@ Global {7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} From 5d9e46c0327800415b08eb814e7652df58c635e6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 15:26:49 +0200 Subject: [PATCH 03/10] 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; + } + } +} From e864df9af3eea50715fc6181f60a16eb8117f46e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 15:35:51 +0200 Subject: [PATCH 04/10] Fixed a build error --- .../ComputeSharp.Benchmark.csproj | 6 +++++- .../ComputeSharp.Benchmark.csproj | 12 ------------ 2 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index 958d2f1d..4f1afcc1 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -1,8 +1,12 @@ - + Exe netcoreapp3.0 + + + + diff --git a/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj deleted file mode 100644 index 33fc9464..00000000 --- a/src/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - netcoreapp3.0 - - - - - - - From 784baf63b1a7ea3cdcda5192a479889c34144df1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:04:55 +0200 Subject: [PATCH 05/10] Minor code refactoring --- .../FullyConnectedLayerBenchmark.cs | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs index f4c8f88a..d78499ed 100644 --- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -12,31 +12,46 @@ namespace ComputeSharp.Benchmark /// /// The number of samples /// - private const int Count = 128; + private const int C = 128; /// - /// The nummber of rows in the matrix + /// 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) + /// 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 + /// The number of columns in the matrix /// private const int P = 100; - private readonly float[] A; + /// + /// The input tensor + /// + private readonly float[] X; + /// + /// The weights tensor + /// + private readonly float[] W; + + /// + /// The bias tensor + /// private readonly float[] B; - private readonly float[] C; - - private readonly float[] D; + /// + /// The result tensor + /// + private readonly float[] Y; + /// + /// A instance to initialize the tensors + /// private static readonly Random Random = new Random(); /// @@ -44,10 +59,10 @@ namespace ComputeSharp.Benchmark /// public FullyConnectedLayerBenchmark() { - A = CreateRandomArray(Count * N * M); - B = CreateRandomArray(M * P); - C = CreateRandomArray(N * P); - D = new float[Count * N * P]; + X = CreateRandomArray(C * N * M); + W = CreateRandomArray(M * P); + B = CreateRandomArray(P); + Y = CreateRandomArray(C * N * P); } /// From 245a8fecb362c11b3ca35f96c152e93b7d6934fa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:05:04 +0200 Subject: [PATCH 06/10] Added CPU forward pass --- samples/ComputeSharp.Benchmark/Dnn.cs | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 samples/ComputeSharp.Benchmark/Dnn.cs diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs new file mode 100644 index 00000000..3e9f812b --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Dnn.cs @@ -0,0 +1,51 @@ +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace ComputeSharp.Benchmark +{ + /// + /// A that contains primitives to run certain operations of a neural network + /// + internal static class Dnn + { + /// + /// Executes the forward pass on a fully connected layer on the CPU + /// + /// The number of samples in the input tensor + /// The number of rows in the input matrix + /// The number of columns in the input matrix + /// The number of columns in the output matrix + /// The input tensor + /// The weights tensor + /// The bias tensor + /// The result tensor + public static void FullyConnectedForward(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) + { + void Kernel(int s) + { + int offset = s * n * m; + ref float rx = ref x[s * n * m]; + ref float rw = ref w[0]; + ref float rb = ref b[0]; + ref float ry = ref y[s * n * p]; + + for (int i = 0; i < n; i++) + { + 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); + } + + Unsafe.Add(ref ry, i * m + j) = result + Unsafe.Add(ref rb, j); + } + } + } + + Parallel.For(0, c, Kernel); + } + } +} From 0caff1f47920d5c6fbcbf3f5bc8d28b5ba6485ac Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:17:34 +0200 Subject: [PATCH 07/10] Added GPU forward pass --- .../ComputeSharp.Benchmark.csproj | 4 ++ samples/ComputeSharp.Benchmark/Dnn.cs | 41 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index 4f1afcc1..f93e3c60 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs index 3e9f812b..4e5a6c8c 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.Buffers; namespace ComputeSharp.Benchmark { @@ -19,11 +20,10 @@ namespace ComputeSharp.Benchmark /// The weights tensor /// The bias tensor /// The result tensor - public static void FullyConnectedForward(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) + public static void FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) { void Kernel(int s) { - int offset = s * n * m; ref float rx = ref x[s * n * m]; ref float rw = ref w[0]; ref float rb = ref b[0]; @@ -40,12 +40,47 @@ namespace ComputeSharp.Benchmark result += Unsafe.Add(ref rx, i * m + k) * Unsafe.Add(ref rw, k * p + j); } - Unsafe.Add(ref ry, i * m + j) = result + Unsafe.Add(ref rb, j); + Unsafe.Add(ref ry, i * p + j) = result + Unsafe.Add(ref rb, j); } } } Parallel.For(0, c, Kernel); } + + /// + /// Executes the forward pass on a fully connected layer on the GPU + /// + /// The number of samples in the input tensor + /// The number of rows in the input matrix + /// The number of columns in the input matrix + /// The number of columns in the output matrix + /// The input tensor + /// 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) + { + 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); + + void Kernel(ThreadIds id) + { + float result = 0f; + + for (int k = 0; k < m; k++) + { + result += x_gpu[(uint)(id.X * n * p + id.Y * m + k)] * w_gpu[(uint)(k * p + id.Z)]; + } + + y_gpu[(uint)(id.X * n * p + id.Y * p + id.Z)] = result + b_gpu[id.Z]; + } + + Gpu.Default.For(c, n, p, Kernel); + + y_gpu.GetData(y); + } } } From df032526f33c0d5aff858f7180abd2d026305344 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:31:07 +0200 Subject: [PATCH 08/10] Removed BenchmarkDotNet, switched to manual --- .../ComputeSharp.Benchmark.csproj | 4 --- .../FullyConnectedLayerBenchmark.cs | 16 +++++++++--- samples/ComputeSharp.Benchmark/Program.cs | 26 ++++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index f93e3c60..bfb20336 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -5,10 +5,6 @@ netcoreapp3.0 - - - - diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs index d78499ed..3f1488a3 100644 --- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -17,17 +17,17 @@ namespace ComputeSharp.Benchmark /// /// The nummber of rows in the matrix /// - private const int N = 100; + private const int N = 128; /// /// The number of columns in the matrix (same as the number of rows in the matrix) /// - private const int M = 100; + private const int M = 128; /// /// The number of columns in the matrix /// - private const int P = 100; + private const int P = 128; /// /// The input tensor @@ -81,5 +81,15 @@ namespace ComputeSharp.Benchmark return array; } + + /// + /// Runs a fully connected forward operation on the CPU + /// + public void Cpu() => Dnn.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y); + + /// + /// Runs a fully connected forward operation on the GPU + /// + public void Gpu() => Dnn.FullyConnectedForwardGpu(C, N, M, P, X, W, B, Y); } } diff --git a/samples/ComputeSharp.Benchmark/Program.cs b/samples/ComputeSharp.Benchmark/Program.cs index 1bfa2a7a..d7d613f9 100644 --- a/samples/ComputeSharp.Benchmark/Program.cs +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; namespace ComputeSharp.Benchmark { @@ -6,7 +7,30 @@ namespace ComputeSharp.Benchmark { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + var benchmark = new FullyConnectedLayerBenchmark(); + benchmark.Gpu(); // Warmup + return; + + Stopwatch timer = new Stopwatch(); + TimeSpan cpuTime = TimeSpan.Zero, gpuTime = TimeSpan.Zero; + + for (int i = 0; i < 10; i++) + { + benchmark.Cpu(); + cpuTime += timer.Elapsed; + Console.WriteLine($"CPU: {timer.Elapsed:g}"); + timer.Restart(); + benchmark.Gpu(); + gpuTime += timer.Elapsed; + Console.WriteLine($"GPU: {timer.Elapsed:g}"); + timer.Restart(); + } + + Console.WriteLine("======== AVERAGE ========"); + Console.WriteLine($"CPU: {cpuTime:g}"); + Console.WriteLine($"GPU: {gpuTime:g}"); + + Console.ReadKey(); } } } From ad1053835f5b894d8a5f8db27c6b375b51643748 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:31:26 +0200 Subject: [PATCH 09/10] Workaround for an HLSL compiler error --- samples/ComputeSharp.Benchmark/Dnn.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs index 4e5a6c8c..bcf8a938 100644 --- a/samples/ComputeSharp.Benchmark/Dnn.cs +++ b/samples/ComputeSharp.Benchmark/Dnn.cs @@ -22,7 +22,7 @@ namespace ComputeSharp.Benchmark /// The result tensor public static void FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) { - void Kernel(int s) + Parallel.For(0, c, s => { ref float rx = ref x[s * n * m]; ref float rw = ref w[0]; @@ -43,9 +43,7 @@ namespace ComputeSharp.Benchmark Unsafe.Add(ref ry, i * p + j) = result + Unsafe.Add(ref rb, j); } } - } - - Parallel.For(0, c, Kernel); + }); } /// @@ -66,7 +64,7 @@ namespace ComputeSharp.Benchmark using ReadOnlyBuffer b_gpu = Gpu.Default.AllocateReadOnlyBuffer(b); using ReadWriteBuffer y_gpu = Gpu.Default.AllocateReadWriteBuffer(y); - void Kernel(ThreadIds id) + Gpu.Default.For(c, n, p, id => { float result = 0f; @@ -76,9 +74,7 @@ namespace ComputeSharp.Benchmark } y_gpu[(uint)(id.X * n * p + id.Y * p + id.Z)] = result + b_gpu[id.Z]; - } - - Gpu.Default.For(c, n, p, Kernel); + }); y_gpu.GetData(y); } From 0bda834cf2d9c063945d87a1b822b59934560d9e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 16:38:07 +0200 Subject: [PATCH 10/10] Compute benchmark improved --- .../FullyConnectedLayerBenchmark.cs | 6 +++--- samples/ComputeSharp.Benchmark/Program.cs | 11 ++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs index 3f1488a3..3fe025d8 100644 --- a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -17,17 +17,17 @@ namespace ComputeSharp.Benchmark /// /// The nummber of rows in the matrix /// - private const int N = 128; + private const int N = 512; /// /// The number of columns in the matrix (same as the number of rows in the matrix) /// - private const int M = 128; + private const int M = 512; /// /// The number of columns in the matrix /// - private const int P = 128; + private const int P = 256; /// /// The input tensor diff --git a/samples/ComputeSharp.Benchmark/Program.cs b/samples/ComputeSharp.Benchmark/Program.cs index d7d613f9..8f033fac 100644 --- a/samples/ComputeSharp.Benchmark/Program.cs +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -5,13 +5,12 @@ namespace ComputeSharp.Benchmark { class Program { - static void Main(string[] args) + static void Main() { var benchmark = new FullyConnectedLayerBenchmark(); benchmark.Gpu(); // Warmup - return; - Stopwatch timer = new Stopwatch(); + Stopwatch timer = Stopwatch.StartNew(); TimeSpan cpuTime = TimeSpan.Zero, gpuTime = TimeSpan.Zero; for (int i = 0; i < 10; i++) @@ -27,10 +26,8 @@ namespace ComputeSharp.Benchmark } Console.WriteLine("======== AVERAGE ========"); - Console.WriteLine($"CPU: {cpuTime:g}"); - Console.WriteLine($"GPU: {gpuTime:g}"); - - Console.ReadKey(); + Console.WriteLine($"CPU: {cpuTime / 10:g}"); + Console.WriteLine($"GPU: {gpuTime / 10:g}"); } } }