Skip to content

Commit

Permalink
Merge pull request #404 from Sergio0694/dev/editorconfig
Browse files Browse the repository at this point in the history
Add .editorconfig file
  • Loading branch information
Sergio0694 authored Oct 15, 2022
2 parents df21131 + abcb1e7 commit 0e0830c
Show file tree
Hide file tree
Showing 913 changed files with 4,343 additions and 3,285 deletions.
350 changes: 350 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Set default file-type attributes

# All file types
* text=auto

# Markups
*.md text=auto diff=markdown
*.mdx text=auto diff=markdown

# Solutions
*.sln text=auto eol=crlf
*.slnx text=auto

# Projects
*.*proj text=auto
*.tasks text=auto
*.props text=auto
*.targets text=auto

# Sources
*.cs text=auto diff=csharp
*.csx text=auto diff=csharp
*.fs text=auto diff=fsharp
*.resx text=auto

# Scripts
*.in text eol=lf
*.sh text eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

# Images
*.png binary
*.jpg binary
*.gif binary

# Keys
*.snk binary
*.pfx binary
*.cer binary

# Others

# Preserve Line endings in diff and patch files
*.diff -text
*.patch -text

# Exclude git meta files from exporting
.gitattributes export-ignore
.gitignore export-ignore
.gitkeep export-ignore
1 change: 1 addition & 0 deletions ComputeSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internal
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{1A9C4108-F61A-4ABE-B5CF-F524521EAE2D}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
src\Directory.Build.props = src\Directory.Build.props
Expand Down
40 changes: 36 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,45 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<Features>strict</Features>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<!--
Enable the latest warning wave, which shows additional warnings for invalid language features that are disabled by default.
For additional info, see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/warning-waves.
-->
<AnalysisLevel>7</AnalysisLevel>

<!-- Import the global configs from the CodeStyle package (enables all IDExxxx warnings)-->
<AnalysisLevelStyle>7-all</AnalysisLevelStyle>

<!--
Enable the compiler strict mode (see https://www.meziantou.net/csharp-compiler-strict-mode.htm).
This (poorly documented) mode enables additional warnings for incorrect usages of some features.
For instance, this will warn when using the == operator to compare a struct with a null literal.
-->
<Features>strict</Features>
</PropertyGroup>

<PropertyGroup>

<!--
This ignores the following warning:
"MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the
processor architecture of the reference". It pops up because all samples are only targeting x64/Arm64.
-->
<NoWarn>$(NoWarn);MSB3270</NoWarn>

<!--
This ignores the following warning:
"CA1060: Move P/Invokes to NativeMethods class". The whole repository is full of P/Invoke-s everywhere.
See: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1060.
-->
<NoWarn>$(NoWarn);CA1060</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.4.0-2.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.4.0-2.final" PrivateAssets="all" />
</ItemGroup>

<!-- Centralized location for all generated artifacts -->
Expand Down Expand Up @@ -37,7 +69,7 @@
</PropertyGroup>

<!-- If the current branch and action match a release for NuGet, override the relative build properties -->
<PropertyGroup Condition="'$(GITHUB_EVENT_NAME)' == 'push' AND '$(GITHUB_REF_NAME)' != '' AND '$(IsBranchNameStartingWithRefPrefix)' == 'true'">
<PropertyGroup Condition="'$(GITHUB_EVENT_NAME)' == 'push' AND '$(GITHUB_REF_NAME)' != '' AND '$(IsBranchNameStartingWithRefPrefix)' == 'true'">
<IsCommitOnReleaseBranch>true</IsCommitOnReleaseBranch>
<ComputeSharpPackageVersionFromReleaseBranch>$([System.Text.RegularExpressions.Regex]::Match($(GITHUB_REF_NAME), $(ReleaseVersionParsingRegex)).Groups[1].Value)</ComputeSharpPackageVersionFromReleaseBranch>
<ComputeSharpPackageVersionSuffixFromReleaseBranch>$([System.Text.RegularExpressions.Regex]::Match($(GITHUB_REF_NAME), $(ReleaseVersionParsingRegex)).Groups[2].Value)</ComputeSharpPackageVersionSuffixFromReleaseBranch>
Expand All @@ -58,7 +90,7 @@
<RepositoryUrl>https://github.com/Sergio0694/ComputeSharp/</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<ProjectUrl>https://github.com/Sergio0694/ComputeSharp/</ProjectUrl>
<ProjectUrl>https://github.com/Sergio0694/ComputeSharp/</ProjectUrl>
<Authors>Sergio Pedri</Authors>
<Owners>Sergio Pedri</Owners>
<Company>Sergio Pedri</Company>
Expand All @@ -71,7 +103,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>

Expand Down
80 changes: 41 additions & 39 deletions samples/ComputeSharp.Benchmark/Blas/BlasBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;

#pragma warning disable CA1063

namespace ComputeSharp.Benchmark.Blas;

/// <summary>
Expand All @@ -17,64 +19,64 @@ public class BlasBenchmark : IDisposable
private const int C = 128;

/// <summary>
/// The nummber of rows in the <see cref="X"/> matrix.
/// The nummber of rows in the <see cref="x"/> matrix.
/// </summary>
private const int N = 512;

/// <summary>
/// The number of columns in the <see cref="X"/> matrix (same as the number of rows in the <see cref="W"/> matrix)
/// The number of columns in the <see cref="x"/> matrix (same as the number of rows in the <see cref="w"/> matrix)
/// </summary>
private const int M = 512;

/// <summary>
/// The number of columns in the <see cref="W"/> matrix.
/// The number of columns in the <see cref="w"/> matrix.
/// </summary>
private const int P = 256;

/// <summary>
/// The input tensor.
/// </summary>
private float[]? X;
private float[]? x;

/// <summary>
/// The weights tensor.
/// </summary>
private float[]? W;
private float[]? w;

/// <summary>
/// The bias tensor.
/// </summary>
private float[]? B;
private float[]? b;

/// <summary>
/// The result tensor.
/// </summary>
private float[]? Y;
private float[]? y;

/// <summary>
/// The input tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferX;
private ReadOnlyBuffer<float>? bufferX;

/// <summary>
/// The weights tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferW;
private ReadOnlyBuffer<float>? bufferW;

/// <summary>
/// The bias tensor (GPU).
/// </summary>
private ReadOnlyBuffer<float>? BufferB;
private ReadOnlyBuffer<float>? bufferB;

/// <summary>
/// The result tensor (GPU).
/// </summary>
private ReadWriteBuffer<float>? BufferY;
private ReadWriteBuffer<float>? bufferY;

/// <summary>
/// A <see cref="System.Random"/> instance to initialize the tensors.
/// </summary>
private readonly Random Random = new Random();
private readonly Random random = new();

/// <summary>
/// Initial setup for a benchmarking session.
Expand All @@ -89,21 +91,21 @@ float[] CreateRandomArray(int size)

foreach (ref float x in array.AsSpan())
{
x = (float)Random.NextDouble();
x = (float)this.random.NextDouble();
}

return array;
}

X = CreateRandomArray(C * N * M);
W = CreateRandomArray(M * P);
B = CreateRandomArray(P);
Y = CreateRandomArray(C * N * P);
this.x = CreateRandomArray(C * N * M);
this.w = CreateRandomArray(M * P);
this.b = CreateRandomArray(P);
this.y = CreateRandomArray(C * N * P);

BufferX = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(X);
BufferW = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(W);
BufferB = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(B);
BufferY = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(Y);
this.bufferX = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.x);
this.bufferW = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.w);
this.bufferB = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.b);
this.bufferY = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(this.y);

Cpu();
GpuWithNoTemporaryBuffers();
Expand All @@ -114,15 +116,15 @@ float[] CreateRandomArray(int size)
[GlobalCleanup]
public void Dispose()
{
ArrayPool<float>.Shared.Return(X!);
ArrayPool<float>.Shared.Return(W!);
ArrayPool<float>.Shared.Return(B!);
ArrayPool<float>.Shared.Return(Y!);

BufferX!.Dispose();
BufferW!.Dispose();
BufferB!.Dispose();
BufferY!.Dispose();
ArrayPool<float>.Shared.Return(this.x!);
ArrayPool<float>.Shared.Return(this.w!);
ArrayPool<float>.Shared.Return(this.b!);
ArrayPool<float>.Shared.Return(this.y!);

this.bufferX!.Dispose();
this.bufferW!.Dispose();
this.bufferB!.Dispose();
this.bufferY!.Dispose();
}

/// <summary>
Expand All @@ -131,7 +133,7 @@ public void Dispose()
[Benchmark(Baseline = true)]
public void Cpu()
{
BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, X!, W!, B!, Y!);
BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, this.x!, this.w!, this.b!, this.y!);
}

/// <summary>
Expand All @@ -140,7 +142,7 @@ public void Cpu()
[Benchmark]
public void GpuWithNoTemporaryBuffers()
{
BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.GetDefault(), C, N, M, P, BufferX!, BufferW!, BufferB!, BufferY!);
BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.GetDefault(), C, N, M, P, this.bufferX!, this.bufferW!, this.bufferB!, this.bufferY!);
}

/// <summary>
Expand All @@ -149,13 +151,13 @@ public void GpuWithNoTemporaryBuffers()
[Benchmark]
public void GpuWithTemporaryBuffers()
{
using ReadOnlyBuffer<float> x = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(X!);
using ReadOnlyBuffer<float> w = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(W!);
using ReadOnlyBuffer<float> b = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(B!);
using ReadWriteBuffer<float> y = GraphicsDevice.GetDefault().AllocateReadWriteBuffer<float>(Y!.Length);
using ReadOnlyBuffer<float> x = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.x!);
using ReadOnlyBuffer<float> w = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.w!);
using ReadOnlyBuffer<float> b = GraphicsDevice.GetDefault().AllocateReadOnlyBuffer(this.b!);
using ReadWriteBuffer<float> y = GraphicsDevice.GetDefault().AllocateReadWriteBuffer<float>(this.y!.Length);

BlasHelpers.FullyConnectedForwardGpu(GraphicsDevice.GetDefault(), C, N, M, P, x, w, b, y);

y.CopyTo(Y);
y.CopyTo(this.y);
}
}
}
30 changes: 15 additions & 15 deletions samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace ComputeSharp.Benchmark.Blas;
Expand Down Expand Up @@ -39,15 +39,15 @@ void Kernel(int s)

for (int k = 0; k < m; k++)
{
result += Unsafe.Add(ref rx, x_offset + 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, y_offset + j) = result + Unsafe.Add(ref rb, j);
}
}
}

Parallel.For(0, c, Kernel);
_ = Parallel.For(0, c, Kernel);
}

/// <summary>
Expand Down Expand Up @@ -83,26 +83,26 @@ public static void FullyConnectedForwardGpu(
[AutoConstructor]
public readonly partial struct FullyConnectedForwardKernel : IComputeShader
{
public readonly int n;
public readonly int m;
public readonly int p;
public readonly ReadOnlyBuffer<float> x;
public readonly ReadOnlyBuffer<float> w;
public readonly ReadOnlyBuffer<float> b;
public readonly ReadWriteBuffer<float> y;
private readonly int n;
private readonly int m;
private readonly int p;
private readonly ReadOnlyBuffer<float> x;
private readonly ReadOnlyBuffer<float> w;
private readonly ReadOnlyBuffer<float> b;
private readonly ReadWriteBuffer<float> y;

/// <inheritdoc/>
public void Execute()
{
int x_offset = ThreadIds.X * n * p + ThreadIds.Y * m;
int x_offset = (ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.m);
float result = 0f;

for (int k = 0; k < m; k++)
for (int k = 0; k < this.m; k++)
{
result += x[x_offset + k] * w[k * p + ThreadIds.Z];
result += this.x[x_offset + k] * this.w[(k * this.p) + ThreadIds.Z];
}

y[ThreadIds.X * n * p + ThreadIds.Y * p + ThreadIds.Z] = result + b[ThreadIds.Z];
this.y[(ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.p) + ThreadIds.Z] = result + this.b[ThreadIds.Z];
}
}
}
}
Loading

0 comments on commit 0e0830c

Please sign in to comment.