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

Implemented support for pre-generated LibDevice PTX modules. #1148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ Src/ILGPU/AtomicFunctions.cs
Src/ILGPU/Backends/PTX/PTXIntrinsics.Generated.cs
Src/ILGPU/Backends/PTX/PTXLibDeviceMethods.cs
Src/ILGPU/Backends/PTX/PTXLibDeviceNvvm.cs
Src/ILGPU/Backends/PTX/PTXLibDevicePtx.cs
Src/ILGPU/Backends/Velocity/Scalar/ScalarOperations.cs
Src/ILGPU/Backends/Velocity/VelocityIntrinsics.Generated.cs
Src/ILGPU/Frontend/Intrinsic/RemappedIntrinsics.Generated.cs
Expand Down Expand Up @@ -285,6 +286,7 @@ Src/ILGPU/Static/DllImports.cs
Src/ILGPU/StrideTypes.cs
Src/ILGPU/Util/DataBlocks.cs
Src/ILGPU/Util/PrimitiveDataBlocks.cs
Tools/CudaGenerateLibDeviceTool/CudaDriverVersionUtils.cs

# Ignore specific template outputs (Algorithms)
Src/ILGPU.Algorithms/AlgorithmContextMappings.cs
Expand Down
10 changes: 7 additions & 3 deletions Samples/LibDeviceKernel/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
Expand Down Expand Up @@ -28,8 +28,12 @@ public static void KernelWithLibDevice(Index1D index, ArrayView<float> data)

static void Main()
{
// Create default context and enable LibDevice library
using var context = Context.Create(builder => builder.Cuda().LibDevice());
// Create default context.
//
// ILGPU includes built-in support for LibDevice and should be compatible
// with most CUDA devices. If you have an older device, or wish to use
// a specific version of LibDevice, call LibDeviceOveride().
using var context = Context.Create(builder => builder.Cuda());

// For each available device...
foreach (var device in context)
Expand Down
115 changes: 54 additions & 61 deletions Src/ILGPU.Algorithms.Tests/Generic/AlgorithmsTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2020-2023 ILGPU Project
// Copyright (c) 2020-2024 ILGPU Project
// www.ilgpu.net
//
// File: AlgorithmsTestBase.cs
Expand All @@ -27,7 +27,7 @@ protected AlgorithmsTestBase(ITestOutputHelper output, TestContext testContext)
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class HalfPrecisionComparer
internal class HalfPrecisionComparer
: EqualityComparer<Half>
{
public readonly float Margin;
Expand Down Expand Up @@ -59,7 +59,7 @@ public override int GetHashCode(Half obj) =>
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class FloatPrecisionComparer
internal class FloatPrecisionComparer
: EqualityComparer<float>
{
public readonly float Margin;
Expand Down Expand Up @@ -91,7 +91,7 @@ public override int GetHashCode(float obj) =>
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class DoublePrecisionComparer
internal class DoublePrecisionComparer
: EqualityComparer<double>
{
public readonly double Margin;
Expand Down Expand Up @@ -123,7 +123,7 @@ public override int GetHashCode(double obj) =>
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class HalfRelativeErrorComparer
internal class HalfRelativeErrorComparer
: EqualityComparer<Half>
{
public readonly float RelativeError;
Expand Down Expand Up @@ -163,7 +163,7 @@ public override int GetHashCode(Half obj) =>
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class FloatRelativeErrorComparer
internal class FloatRelativeErrorComparer
: EqualityComparer<float>
{
public readonly float RelativeError;
Expand Down Expand Up @@ -203,7 +203,7 @@ public override int GetHashCode(float obj) =>
/// <summary>
/// Compares two numbers for equality, within a defined tolerance.
/// </summary>
private class DoubleRelativeErrorComparer
internal class DoubleRelativeErrorComparer
: EqualityComparer<double>
{
public readonly double RelativeError;
Expand Down Expand Up @@ -245,20 +245,33 @@ public override int GetHashCode(double obj) =>
/// </summary>
/// <param name="buffer">The target buffer.</param>
/// <param name="expected">The expected values.</param>
/// <param name="decimalPlaces">The acceptable error margin.</param>
public void VerifyWithinPrecision(
ArrayView<Half> buffer,
Half[] expected,
uint decimalPlaces)
/// <param name="comparer">The comparer to use.</param>
public void VerifyUsingComparer<T>(
ArrayView<T> buffer,
T[] expected,
IEqualityComparer<T> comparer)
where T : unmanaged
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new HalfPrecisionComparer(decimalPlaces);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
Assert.Equal(expected, data, comparer);
}

/// <summary>
/// Verifies the contents of the given memory buffer.
/// </summary>
/// <param name="buffer">The target buffer.</param>
/// <param name="expected">The expected values.</param>
/// <param name="decimalPlaces">The acceptable error margin.</param>
public void VerifyWithinPrecision(
ArrayView<Half> buffer,
Half[] expected,
uint decimalPlaces) =>
VerifyUsingComparer(
buffer,
expected,
new HalfPrecisionComparer(decimalPlaces));

/// <summary>
/// Verifies the contents of the given memory buffer.
/// </summary>
Expand All @@ -268,15 +281,11 @@ public void VerifyWithinPrecision(
public void VerifyWithinPrecision(
ArrayView<float> buffer,
float[] expected,
uint decimalPlaces)
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new FloatPrecisionComparer(decimalPlaces);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
}
uint decimalPlaces) =>
VerifyUsingComparer(
buffer,
expected,
new FloatPrecisionComparer(decimalPlaces));

/// <summary>
/// Verifies the contents of the given memory buffer.
Expand All @@ -287,15 +296,11 @@ public void VerifyWithinPrecision(
public void VerifyWithinPrecision(
ArrayView<double> buffer,
double[] expected,
uint decimalPlaces)
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new DoublePrecisionComparer(decimalPlaces);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
}
uint decimalPlaces) =>
VerifyUsingComparer(
buffer,
expected,
new DoublePrecisionComparer(decimalPlaces));

/// <summary>
/// Verifies the contents of the given memory buffer.
Expand All @@ -306,15 +311,11 @@ public void VerifyWithinPrecision(
public void VerifyWithinRelativeError(
ArrayView<Half> buffer,
Half[] expected,
double relativeError)
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new HalfRelativeErrorComparer((float)relativeError);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
}
double relativeError) =>
VerifyUsingComparer(
buffer,
expected,
new HalfRelativeErrorComparer((float)relativeError));

/// <summary>
/// Verifies the contents of the given memory buffer.
Expand All @@ -325,15 +326,11 @@ public void VerifyWithinRelativeError(
public void VerifyWithinRelativeError(
ArrayView<float> buffer,
float[] expected,
double relativeError)
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new FloatRelativeErrorComparer((float)relativeError);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
}
double relativeError) =>
VerifyUsingComparer(
buffer,
expected,
new FloatRelativeErrorComparer((float)relativeError));

/// <summary>
/// Verifies the contents of the given memory buffer.
Expand All @@ -344,14 +341,10 @@ public void VerifyWithinRelativeError(
public void VerifyWithinRelativeError(
ArrayView<double> buffer,
double[] expected,
double relativeError)
{
var data = buffer.GetAsArray(Accelerator.DefaultStream);
Assert.Equal(data.Length, expected.Length);

var comparer = new DoubleRelativeErrorComparer(relativeError);
for (int i = 0, e = data.Length; i < e; ++i)
Assert.Equal(expected[i], data[i], comparer);
}
double relativeError) =>
VerifyUsingComparer(
buffer,
expected,
new DoubleRelativeErrorComparer(relativeError));
}
}
42 changes: 41 additions & 1 deletion Src/ILGPU.Algorithms.Tests/XMathTests.Pow.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2020-2023 ILGPU Project
// Copyright (c) 2020-2024 ILGPU Project
// www.ilgpu.net
//
// File: XMathTests.Pow.tt/XMathTests.Pow.cs
Expand Down Expand Up @@ -48,6 +48,32 @@ namespace ILGPU.Algorithms.Tests
// and ensures a minimum error on each accelerator type.
partial class XMathTests
{
#region Nested Types

/// <summary>
/// WORKAROUND: The output of LibDevice __nv_pow(double, double) and
/// .NET Math.Pow(double, double) on Cuda Test Runner are different.
/// </summary>
private class CudaPowDoubleRelativeErrorComparer : DoubleRelativeErrorComparer
{
public CudaPowDoubleRelativeErrorComparer(double relativeError)
: base(relativeError)
{ }

public override bool Equals(double x, double y)
{
if ((double.IsPositiveInfinity(x) && double.IsNegativeInfinity(y)) ||
(double.IsNegativeInfinity(x) && double.IsPositiveInfinity(y)))
{
return true;
}

return base.Equals(x, y);
}
}

#endregion

<# foreach (var function in powFunctions) { #>
internal static void <#= function.KernelName #>(
Index1D index,
Expand Down Expand Up @@ -120,10 +146,24 @@ namespace ILGPU.Algorithms.Tests
v => Math<#= function.MathSuffix #>.<#= function.Name #>(v.X, v.Y))
.ToArray();
if (Accelerator.AcceleratorType == AcceleratorType.Cuda)
<#
if (function.DataType == "double") {
#>
VerifyUsingComparer(
output.View,
expected,
new CudaPowDoubleRelativeErrorComparer(
(<#= function.DataType #>)<#= function.RelativeError.Cuda #>));
<#
} else {
#>
VerifyWithinRelativeError(
output.View,
expected,
<#= function.RelativeError.Cuda #>);
<#
}
#>
else if (Accelerator.AcceleratorType == AcceleratorType.OpenCL)
VerifyWithinRelativeError(
output.View,
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Algorithms.Tests/XMathTests.Sqrt.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2020-2023 ILGPU Project
// Copyright (c) 2020-2024 ILGPU Project
// www.ilgpu.net
//
// File: XMathTests.Sqrt.tt/XMathTests.Sqrt.cs
Expand Down Expand Up @@ -32,7 +32,7 @@ using Xunit;

var rsqrtFunctions = new []
{
new XMathFunction("Rsqrt" , "float" , new Precision(15, 15, 7)),
new XMathFunction("Rsqrt" , "float" , new Precision(15, 6, 7)),
new XMathFunction("Rsqrt" , "double", new Precision(15, 15, 15)),
};
#>
Expand Down
Loading
Loading