Skip to content

Commit

Permalink
Implemented override for Cuda device instruction set.
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ committed Feb 13, 2023
1 parent 10b2fbe commit d168ed1
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 18 deletions.
8 changes: 3 additions & 5 deletions Src/ILGPU/Runtime/Cuda/CudaAccelerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2016-2021 ILGPU Project
// Copyright (c) 2016-2023 ILGPU Project
// www.ilgpu.net
//
// File: CudaAccelerator.cs
Expand All @@ -9,7 +9,6 @@
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Backends;
using ILGPU.Backends.IL;
using ILGPU.Backends.PTX;
using ILGPU.Resources;
Expand Down Expand Up @@ -188,7 +187,7 @@ internal CudaAccelerator(
Context,
Capabilities,
Architecture,
InstructionSet,
(CudaInstructionSet)Device.InstructionSet,
nvvmAPI));
}

Expand Down Expand Up @@ -220,8 +219,7 @@ internal CudaAccelerator(
/// <summary>
/// Returns the PTX instruction set.
/// </summary>
public CudaInstructionSet InstructionSet =>
(CudaInstructionSet)Device.InstructionSet;
public CudaInstructionSet InstructionSet => Backend.InstructionSet;

/// <summary>
/// Returns the clock rate.
Expand Down
45 changes: 39 additions & 6 deletions Src/ILGPU/Runtime/Cuda/CudaContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021-2022 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: CudaContextExtensions.cs
Expand Down Expand Up @@ -29,11 +29,7 @@ public static class CudaContextExtensions
/// <param name="builder">The builder instance.</param>
/// <returns>The updated builder instance.</returns>
public static Context.Builder Cuda(this Context.Builder builder) =>
builder.Cuda(desc =>
desc.Architecture.HasValue &&
desc.InstructionSet.HasValue &&
PTXCodeGenerator.SupportedInstructionSets.Contains(
desc.InstructionSet.Value));
builder.Cuda(@override => { });

/// <summary>
/// Enables all Cuda devices.
Expand All @@ -45,6 +41,42 @@ public static Context.Builder Cuda(this Context.Builder builder) =>
/// <returns>The updated builder instance.</returns>
public static Context.Builder Cuda(
this Context.Builder builder,
Predicate<CudaDevice> predicate) =>
builder.CudaInternal(@override => { }, predicate);

/// <summary>
/// Enables and configures all Cuda devices.
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="configure">
/// The action to configure a given device.
/// </param>
/// <returns>The updated builder instance.</returns>
public static Context.Builder Cuda(
this Context.Builder builder,
Action<CudaDeviceOverride> configure) =>
builder.CudaInternal(
configure,
desc =>
desc.Architecture.HasValue &&
desc.InstructionSet.HasValue &&
PTXCodeGenerator.SupportedInstructionSets.Contains(
desc.InstructionSet.Value));

/// <summary>
/// Enables and configures all Cuda devices.
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="configure">
/// The action to configure a given device.
/// </param>
/// <param name="predicate">
/// The predicate to include a given device.
/// </param>
/// <returns>The updated builder instance.</returns>
public static Context.Builder CudaInternal(
this Context.Builder builder,
Action<CudaDeviceOverride> configure,
Predicate<CudaDevice> predicate)
{
if (!Backend.RuntimePlatform.Is64Bit())
Expand All @@ -55,6 +87,7 @@ public static Context.Builder Cuda(
}

CudaDevice.GetDevices(
configure,
predicate,
builder.DeviceRegistry);
return builder;
Expand Down
24 changes: 18 additions & 6 deletions Src/ILGPU/Runtime/Cuda/CudaDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: CudaDevice.cs
Expand All @@ -9,7 +9,6 @@
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Backends;
using ILGPU.Resources;
using System;
using System.Collections.Immutable;
Expand Down Expand Up @@ -39,14 +38,16 @@ public static ImmutableArray<Device> GetDevices(
Predicate<CudaDevice> predicate)
{
var registry = new DeviceRegistry();
GetDevices(predicate, registry);
GetDevices(@override => { }, predicate, registry);
return registry.ToImmutable();
}


/// <summary>
/// Detects Cuda devices.
/// </summary>
/// <param name="configure">
/// The action to configure a given device.
/// </param>
/// <param name="predicate">
/// The predicate to include a given device.
/// </param>
Expand All @@ -56,17 +57,20 @@ public static ImmutableArray<Device> GetDevices(
"CA1031:Do not catch general exception types",
Justification = "We want to hide all exceptions at this level")]
internal static void GetDevices(
Action<CudaDeviceOverride> configure,
Predicate<CudaDevice> predicate,
DeviceRegistry registry)
{
if (configure is null)
throw new ArgumentNullException(nameof(configure));
if (registry is null)
throw new ArgumentNullException(nameof(registry));
if (predicate is null)
throw new ArgumentNullException(nameof(predicate));

try
{
GetDevicesInternal(predicate, registry);
GetDevicesInternal(configure, predicate, registry);
}
catch (Exception)
{
Expand All @@ -77,11 +81,15 @@ internal static void GetDevices(
/// <summary>
/// Detects Cuda devices.
/// </summary>
/// <param name="configure">
/// The action to configure a given device.
/// </param>
/// <param name="predicate">
/// The predicate to include a given device.
/// </param>
/// <param name="registry">The registry to add all devices to.</param>
private static void GetDevicesInternal(
Action<CudaDeviceOverride> configure,
Predicate<CudaDevice> predicate,
DeviceRegistry registry)
{
Expand All @@ -99,6 +107,10 @@ private static void GetDevicesInternal(
continue;

var desc = new CudaDevice(device);
var deviceOverride = new CudaDeviceOverride(desc);
configure(deviceOverride);
deviceOverride.ApplyOverrides();

if (predicate(desc))
registry.Register(desc);
}
Expand Down Expand Up @@ -376,7 +388,7 @@ private void InitPCIInfo()
/// <summary>
/// Returns the PTX instruction set (if supported).
/// </summary>
public CudaInstructionSet? InstructionSet { get; private set; }
public CudaInstructionSet? InstructionSet { get; internal set; }

/// <summary>
/// Returns the clock rate.
Expand Down
48 changes: 48 additions & 0 deletions Src/ILGPU/Runtime/Cuda/CudaDeviceOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: CudaDeviceOverride.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

namespace ILGPU.Runtime.Cuda
{
/// <summary>
/// Represents overridable settings of a Cuda device.
/// </summary>
public sealed class CudaDeviceOverride
{
/// <summary>
/// The Cuda device to configure.
/// </summary>
public CudaDevice Device { get; }

/// <summary>
/// Forces the Cuda device to use the specified Instruction Set.
/// </summary>
public CudaInstructionSet? InstructionSet { get; set; }

/// <summary>
/// Constructs a new instance with the overridable settings.
/// </summary>
/// <param name="device">The Cuda device.</param>
internal CudaDeviceOverride(CudaDevice device)
{
Device = device;
InstructionSet = device.InstructionSet;
}

/// <summary>
/// Applies all the overridden settings to the Cuda device.
/// </summary>
internal void ApplyOverrides()
{
if (InstructionSet.HasValue)
Device.InstructionSet = InstructionSet;
}
}
}
7 changes: 6 additions & 1 deletion Src/ILGPU/Runtime/Cuda/CudaInstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ namespace ILGPU.Runtime.Cuda

#region Instance

private CudaInstructionSet(int major, int minor)
/// <summary>
/// Creates the instruction set from major/minor values.
/// </summary>
/// <param name="major">The major version.</param>
/// <param name="minor">The minor version.</param>
public CudaInstructionSet(int major, int minor)
{
Major = major;
Minor = minor;
Expand Down

0 comments on commit d168ed1

Please sign in to comment.