Skip to content

Commit

Permalink
Improved performance of unit tests using shared contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt committed May 3, 2020
1 parent f2bd4fc commit 30a86a4
Show file tree
Hide file tree
Showing 38 changed files with 229 additions and 186 deletions.
24 changes: 21 additions & 3 deletions Src/ILGPU.Tests.CPU/Configurations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
using ILGPU.IR.Transformations;
using Xunit;
using Xunit.Abstractions;

<#
var configurationFile = Host.ResolvePath("../ILGPU.Tests/Configurations.txt");
var configurations = TestConfig.Parse(configurationFile);
var configurations = TestConfig.Parse(configurationFile, out var levels);
#>
namespace ILGPU.Tests.CPU
{
<# foreach (var optLevel in levels) { #>
public sealed class CPUTestContext<#= optLevel #> : CPUTestContext
{
public CPUTestContext<#= optLevel #>()
: base(OptimizationLevel.<#= optLevel #>)
{ }
}

[CollectionDefinition("CPUContextCollection<#= optLevel #>")]
public class CPUContextCollection<#= optLevel #> :
ICollectionFixture<CPUTestContext<#= optLevel #>> { }

<# } #>

<# foreach (var config in configurations) { #>
<# foreach (var optLevel in config.OptimizationLevels) { #>
<# var name = "CPU" + config.Name + "_" + optLevel; #>
[Collection("CPUContextCollection<#= optLevel #>")]
public sealed partial class <#= name #> : <#= config.Name #>
{
public <#= name #>(ITestOutputHelper output)
: base(output, new CPUContextProvider(OptimizationLevel.<#= optLevel #>))
public <#= name #>(
ITestOutputHelper output,
CPUTestContext<#= optLevel #> context)
: base(output, context)
{ }
}

Expand Down
22 changes: 0 additions & 22 deletions Src/ILGPU.Tests.CPU/ContextProvider.cs

This file was deleted.

22 changes: 0 additions & 22 deletions Src/ILGPU.Tests.CPU/RuntimeClasses.cs

This file was deleted.

18 changes: 18 additions & 0 deletions Src/ILGPU.Tests.CPU/TestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ILGPU.IR.Transformations;
using ILGPU.Runtime.CPU;
using System;

namespace ILGPU.Tests.CPU
{
public abstract class CPUTestContext : TestContext
{
private static int NumThreads = Math.Max(Math.Min(
Environment.ProcessorCount, 4), 2);

public CPUTestContext(OptimizationLevel optimizationLevel)
: base(
optimizationLevel,
context => new CPUAccelerator(context, NumThreads))
{ }
}
}
24 changes: 21 additions & 3 deletions Src/ILGPU.Tests.Cuda/Configurations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
using ILGPU.IR.Transformations;
using Xunit;
using Xunit.Abstractions;

<#
var configurationFile = Host.ResolvePath("../ILGPU.Tests/Configurations.txt");
var configurations = TestConfig.Parse(configurationFile);
var configurations = TestConfig.Parse(configurationFile, out var levels);
#>
namespace ILGPU.Tests.Cuda
{
<# foreach (var optLevel in levels) { #>
public sealed class CudaTestContext<#= optLevel #> : CudaTestContext
{
public CudaTestContext<#= optLevel #>()
: base(OptimizationLevel.<#= optLevel #>)
{ }
}

[CollectionDefinition("CudaContextCollection<#= optLevel #>")]
public class CudaContextCollection<#= optLevel #> :
ICollectionFixture<CudaTestContext<#= optLevel #>> { }

<# } #>

<# foreach (var config in configurations) { #>
<# foreach (var optLevel in config.OptimizationLevels) { #>
<# var name = "Cuda" + config.Name + "_" + optLevel; #>
[Collection("CudaContextCollection<#= optLevel #>")]
public sealed partial class <#= name #> : <#= config.Name #>
{
public <#= name #>(ITestOutputHelper output)
: base(output, new CudaContextProvider(OptimizationLevel.<#= optLevel #>))
public <#= name #>(
ITestOutputHelper output,
CudaTestContext<#= optLevel #> testContext)
: base(output, testContext)
{ }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using ILGPU.IR.Transformations;
using ILGPU.Runtime;
using ILGPU.Runtime.Cuda;
using System;

namespace ILGPU.Tests.Cuda
{
sealed class CudaContextProvider : ContextProvider
public abstract class CudaTestContext : TestContext
{
public CudaContextProvider(OptimizationLevel optimizationLevel)
: base(optimizationLevel)
{ }

public override Accelerator CreateAccelerator(Context context)
private static CudaAccelerator CreateAccelerator(Context context)
{
if (CudaAccelerator.CudaAccelerators.Length < 1)
throw new NotSupportedException();
return new CudaAccelerator(context);
}

public CudaTestContext(OptimizationLevel optimizationLevel)
: base(optimizationLevel, CreateAccelerator)
{ }
}
}
23 changes: 20 additions & 3 deletions Src/ILGPU.Tests.OpenCL/Configurations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
using ILGPU.IR.Transformations;
using Xunit;
using Xunit.Abstractions;

<#
var configurationFile = Host.ResolvePath("../ILGPU.Tests/Configurations.txt");
var configurations = TestConfig.Parse(configurationFile);
var configurations = TestConfig.Parse(configurationFile, out var levels);
#>
namespace ILGPU.Tests.OpenCL
{
<# foreach (var optLevel in levels) { #>
public sealed class CLTestContext<#= optLevel #> : CLTestContext
{
public CLTestContext<#= optLevel #>()
: base(OptimizationLevel.<#= optLevel #>)
{ }
}

[CollectionDefinition("CLContextCollection<#= optLevel #>")]
public class CLContextCollection<#= optLevel #> :
ICollectionFixture<CLTestContext<#= optLevel #>> { }

<# } #>
<# foreach (var config in configurations) { #>
<# foreach (var optLevel in config.OptimizationLevels) { #>
<# var name = "CL" + config.Name + "_" + optLevel; #>
[Collection("CLContextCollection<#= optLevel #>")]
public sealed partial class <#= name #> : <#= config.Name #>
{
public <#= name #>(ITestOutputHelper output)
: base(output, new OpenCLContextProvider(OptimizationLevel.<#= optLevel #>))
public <#= name #>(
ITestOutputHelper output,
CLTestContext<#= optLevel #> testContext)
: base(output, testContext)
{ }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using ILGPU.IR.Transformations;
using ILGPU.Runtime;
using ILGPU.Runtime.OpenCL;
using System;

namespace ILGPU.Tests.OpenCL
{
sealed class OpenCLContextProvider : ContextProvider
public abstract class CLTestContext : TestContext
{
public OpenCLContextProvider(OptimizationLevel optimizationLevel)
: base(optimizationLevel)
{ }

public override Accelerator CreateAccelerator(Context context)
private static CLAccelerator CreateAccelerator(Context context)
{
if (CLAccelerator.CLAccelerators.Length < 1)
throw new NotSupportedException();
var mainAccelerator = CLAccelerator.CLAccelerators[0];
return new CLAccelerator(context, mainAccelerator);
}

public CLTestContext(OptimizationLevel optimizationLevel)
: base(optimizationLevel, CreateAccelerator)
{ }
}
}
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/ArrayViews.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace ILGPU.Tests
{
public abstract class ArrayViews : TestBase
{
protected ArrayViews(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected ArrayViews(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

internal static void ArrayViewValidKernel(Index1 index, ArrayView<int> data)
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace ILGPU.Tests
{
public abstract class Arrays : TestBase
{
protected Arrays(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected Arrays(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

public static TheoryData<object, object> ArraySimpleTestData =>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/AtomicCASOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace ILGPU.Tests
{
public abstract class AtomicCASOperations : TestBase
{
protected AtomicCASOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected AtomicCASOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (typeName, type) in atomicTypes) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/AtomicOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace ILGPU.Tests
Justification = "Only used for testing")]
public abstract class AtomicOperations : TestBase
{
protected AtomicOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected AtomicOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (operationName, floatSupport) in operationConfigurations) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/BasicIfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public abstract class BasicIfs : TestBase
{
private const int Length = 32;

protected BasicIfs(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected BasicIfs(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

internal static void IfTrueKernel(
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/BasicJumps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace ILGPU.Tests
{
public abstract class BasicJumps : TestBase
{
protected BasicJumps(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected BasicJumps(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0059:Unnecessary assignment of a value", Justification = "Testing unconditional jump")]
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/BasicLoops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public abstract class BasicLoops : TestBase
{
private const int Length = 128;

protected BasicLoops(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected BasicLoops(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

internal static void WhileFalseKernel(
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/BasicSwitches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace ILGPU.Tests
{
public abstract class BasicSwitches : TestBase
{
protected BasicSwitches(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected BasicSwitches(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

internal static void BasicSwitchKernel(
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/BinaryIntOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace ILGPU.Tests
{
public abstract class BinaryIntOperations : TestBase
{
protected BinaryIntOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected BinaryIntOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (operationName, prefix, infix, postfix) in operationConfigurations) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/CompareFloatOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace ILGPU.Tests
{
public abstract class CompareFloatOperations : TestBase
{
protected CompareFloatOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected CompareFloatOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (operationName, infix) in operationConfigurations) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/CompareIntOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace ILGPU.Tests
{
public abstract class CompareIntOperations : TestBase
{
protected CompareIntOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected CompareIntOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (operationName, infix) in operationConfigurations) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/ConvertFloatOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace ILGPU.Tests
{
public abstract class ConvertFloatOperations : TestBase
{
protected ConvertFloatOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected ConvertFloatOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (typeName, type, suffix) in FloatTypes) { #>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/ConvertIntOperations.tt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace ILGPU.Tests
{
public abstract class ConvertIntOperations : TestBase
{
protected ConvertIntOperations(ITestOutputHelper output, ContextProvider contextProvider)
: base(output, contextProvider)
protected ConvertIntOperations(ITestOutputHelper output, TestContext testContext)
: base(output, testContext)
{ }

<# foreach (var (typeName, type) in IntTypes) { #>
Expand Down
Loading

0 comments on commit 30a86a4

Please sign in to comment.