Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features for allocating 2D/3D in shared memory #843

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions Src/ILGPU.Tests/SharedMemory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2022 ILGPU Project
// www.ilgpu.net
//
// File: SharedMemory.cs
Expand Down Expand Up @@ -245,13 +245,79 @@ public void MultiDimensionalSharedMemory2D()
var expected = Enumerable.Repeat(42, groupSize).ToArray();
Verify(buffer.View, expected);
}

internal static void MultiDimensionalSharedMemoryKernel2DDenseX(
ArrayView1D<int, Stride1D.Dense> output)
{
var sharedMemory = ILGPU.SharedMemory.Allocate2DDenseX<int>(
new Index2D(20, 100));
if (Group.IsFirstThread) {
sharedMemory[0, 0] = 0;
sharedMemory[1, 0] = 0;
}
if (Grid.GlobalIndex.X < 100) {
sharedMemory[0, Grid.GlobalIndex.X] = Grid.GlobalIndex.X;
sharedMemory[1, Grid.GlobalIndex.X] = 7*Grid.GlobalIndex.X;
}
Group.Barrier();
if (Grid.GlobalIndex.X < 100)
output[Grid.GlobalIndex.X] = sharedMemory[1, Grid.GlobalIndex.X];
else
output[Grid.GlobalIndex.X] = sharedMemory[0, 0];
}

[Fact]
[KernelMethod(nameof(MultiDimensionalSharedMemoryKernel2DDenseX))]
public void MultiDimensionalSharedMemory2DDenseX()
{
int groupSize = Accelerator.MaxNumThreadsPerGroup;
using var buffer = Accelerator.Allocate1D<int>(groupSize);
Execute(new KernelConfig(1, groupSize), buffer.View);
var expected =
Enumerable.Range(0, groupSize).Select(
x => x < 100 ? 7*x : 0).ToArray();
Verify(buffer.View, expected);
}

internal static void MultiDimensionalSharedMemoryKernel2DDenseY(
ArrayView1D<int, Stride1D.Dense> output)
{
var sharedMemory = ILGPU.SharedMemory.Allocate2DDenseY<int>(
new Index2D(100, 20));
if (Group.IsFirstThread) {
sharedMemory[0, 0] = 0;
sharedMemory[0, 1] = 0;
}
if (Grid.GlobalIndex.X < 100) {
sharedMemory[Grid.GlobalIndex.X, 0] = Grid.GlobalIndex.X;
sharedMemory[Grid.GlobalIndex.X, 1] = 7*Grid.GlobalIndex.X;
}
Group.Barrier();
if (Grid.GlobalIndex.X < 100)
output[Grid.GlobalIndex.X] = sharedMemory[Grid.GlobalIndex.X, 1];
else
output[Grid.GlobalIndex.X] = sharedMemory[0, 0];
}

[Fact]
[KernelMethod(nameof(MultiDimensionalSharedMemoryKernel2DDenseY))]
public void MultiDimensionalSharedMemory2DDenseY()
{
int groupSize = Accelerator.MaxNumThreadsPerGroup;
using var buffer = Accelerator.Allocate1D<int>(groupSize);
Execute(new KernelConfig(1, groupSize), buffer.View);
var expected =
Enumerable.Range(0, groupSize).Select(
x => x < 100 ? 7*x : 0).ToArray();
Verify(buffer.View, expected);
}

internal static void MultiDimensionalSharedMemoryKernel3D(
ArrayView1D<int, Stride1D.Dense> output)
{
var sharedMemory = ILGPU.SharedMemory.Allocate3D<int, Stride3D.DenseZY>(
new Index3D(5, 7, 3),
new Stride3D.DenseZY(5 * 7, 7));
new Stride3D.DenseZY(3 * 7, 3));
if (Group.IsFirstThread)
sharedMemory[2, 6, 1] = 42;
Group.Barrier();
Expand All @@ -269,5 +335,51 @@ public void MultiDimensionalSharedMemory3D()
var expected = Enumerable.Repeat(42, groupSize).ToArray();
Verify(buffer.View, expected);
}

internal static void MultiDimensionalSharedMemoryKernel3DDenseXY(
ArrayView1D<int, Stride1D.Dense> output)
{
var sharedMemory = ILGPU.SharedMemory.Allocate3DDenseXY<int>(
new Index3D(11, 17, 13));
if (Group.IsFirstThread)
sharedMemory[4, 5, 2] = 42;
Group.Barrier();
output[Grid.GlobalIndex.X] = sharedMemory[4, 5, 2];
}

[Fact]
[KernelMethod(nameof(MultiDimensionalSharedMemoryKernel3DDenseXY))]
public void MultiDimensionalSharedMemory3DDenseXY()
{
int groupSize = Accelerator.MaxNumThreadsPerGroup;
using var buffer = Accelerator.Allocate1D<int>(groupSize);
Execute(new KernelConfig(1, groupSize), buffer.View);

var expected = Enumerable.Repeat(42, groupSize).ToArray();
Verify(buffer.View, expected);
}

internal static void MultiDimensionalSharedMemoryKernel3DDenseZY(
ArrayView1D<int, Stride1D.Dense> output)
{
var sharedMemory = ILGPU.SharedMemory.Allocate3DDenseZY<int>(
new Index3D(11, 17, 13));
if (Group.IsFirstThread)
sharedMemory[4, 5, 2] = 42;
Group.Barrier();
output[Grid.GlobalIndex.X] = sharedMemory[4, 5, 2];
}

[Fact]
[KernelMethod(nameof(MultiDimensionalSharedMemoryKernel3DDenseZY))]
public void MultiDimensionalSharedMemory3DDenseZY()
{
int groupSize = Accelerator.MaxNumThreadsPerGroup;
using var buffer = Accelerator.Allocate1D<int>(groupSize);
Execute(new KernelConfig(1, groupSize), buffer.View);

var expected = Enumerable.Repeat(42, groupSize).ToArray();
Verify(buffer.View, expected);
}
}
}
}
61 changes: 61 additions & 0 deletions Src/ILGPU/Memory.tt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,65 @@ namespace ILGPU

<# } #>
<# } #>


<#
var axes = new string[]
{
"X",
"Y",
};
#>

<# foreach (var axis in axes) { #>
partial class SharedMemory
{
/// <summary>
/// Allocates a 2D chunk of shared memory with <#= axis #>
/// as the leading dimension.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="extent">The number of elements to allocate.</param>
/// <returns>An allocated 2D buffer on shared memory.</returns>
public static ArrayView2D<T, Stride2D.Dense<#= axis #>>
Allocate2DDense<#= axis #><T>(
in Index2D extent)
where T : unmanaged =>
Allocate2D<T, Stride2D.Dense<#= axis #>> (
extent,
new Stride2D.Dense<#= axis #>(extent.<#= axis #>));
}
<# } #>

<#
var twoaxes = new (string, string, string)[]
{
("XY", "extent.X", "extent.X * extent.Y"),
("ZY", "extent.Z * extent.Y", "extent.Z")
};
#>

<# foreach (var (axis, axis1, axis2) in twoaxes) { #>
partial class SharedMemory
{
/// <summary>
/// Allocates a 3D chunk of shared memory with <#= axis #>
/// as the leading dimensions.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="extent">The number of elements to allocate.</param>
/// <returns>An allocated 3D buffer on shared memory.</returns>
/// <remarks>
/// Since <#= axis #> are the leading dimension, combined dimension
/// (multiplied sizes) must be less or equal to <see cref="int.MaxValue"/>.
/// </remarks>
public static ArrayView3D<T, Stride3D.Dense<#= axis #>>
Allocate3DDense<#= axis #><T>(
in Index3D extent)
where T : unmanaged =>
Allocate3D<T, Stride3D.Dense<#= axis #>> (
extent,
new Stride3D.Dense<#= axis #>(<#= axis1 #>, <#= axis2 #>));
}
<# } #>
}