diff --git a/samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj similarity index 100% rename from samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj rename to samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Dnn.cs new file mode 100644 index 00000000..bcf8a938 --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Dnn.cs @@ -0,0 +1,82 @@ +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using ComputeSharp.Graphics.Buffers; + +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 FullyConnectedForwardCpu(int c, int n, int m, int p, float[] x, float[] w, float[] b, float[] y) + { + Parallel.For(0, c, s => + { + 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 * p + j) = result + Unsafe.Add(ref rb, j); + } + } + }); + } + + /// + /// 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); + + Gpu.Default.For(c, n, p, 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]; + }); + + y_gpu.GetData(y); + } + } +} diff --git a/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs new file mode 100644 index 00000000..3fe025d8 --- /dev/null +++ b/samples/ComputeSharp.Benchmark/FullyConnectedLayerBenchmark.cs @@ -0,0 +1,95 @@ +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 C = 128; + + /// + /// The nummber of rows in the matrix + /// + 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 = 512; + + /// + /// The number of columns in the matrix + /// + private const int P = 256; + + /// + /// The input tensor + /// + private readonly float[] X; + + /// + /// The weights tensor + /// + private readonly float[] W; + + /// + /// The bias tensor + /// + private readonly float[] B; + + /// + /// The result tensor + /// + private readonly float[] Y; + + /// + /// A instance to initialize the tensors + /// + private static readonly Random Random = new Random(); + + /// + /// Creates a new instance + /// + public FullyConnectedLayerBenchmark() + { + X = CreateRandomArray(C * N * M); + W = CreateRandomArray(M * P); + B = CreateRandomArray(P); + Y = CreateRandomArray(C * 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; + } + + /// + /// 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 new file mode 100644 index 00000000..8f033fac --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Program.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics; + +namespace ComputeSharp.Benchmark +{ + class Program + { + static void Main() + { + var benchmark = new FullyConnectedLayerBenchmark(); + benchmark.Gpu(); // Warmup + + Stopwatch timer = Stopwatch.StartNew(); + 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 / 10:g}"); + Console.WriteLine($"GPU: {gpuTime / 10:g}"); + } + } +} diff --git a/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj b/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj new file mode 100644 index 00000000..bfb20336 --- /dev/null +++ b/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.0 + + + + + + + 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..3cedb543 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,11 @@ 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 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Benchmark", "..\samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj", "{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -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} 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")]