Skip to content

Commit

Permalink
Added Velocity runtime classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt committed Nov 26, 2022
1 parent 19033dc commit e13209b
Show file tree
Hide file tree
Showing 8 changed files with 1,416 additions and 0 deletions.
468 changes: 468 additions & 0 deletions Src/ILGPU/Runtime/Velocity/VelocityAccelerator.cs

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions Src/ILGPU/Runtime/Velocity/VelocityContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2022 ILGPU Project
// www.ilgpu.net
//
// File: VelocityContextExtensions.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Backends;
using ILGPU.Backends.PTX;
using ILGPU.Resources;
using System;

namespace ILGPU.Runtime.Velocity
{
/// <summary>
/// Cuda specific context extensions.
/// </summary>
public static class VelocityContextExtensions
{
#region Builder

/// <summary>
/// Enables all velocity devices.
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="maxSharedMemoryPerGroup">
/// The maximum number bytes of shared memory per group.
/// </param>
/// <returns>The updated builder instance.</returns>
public static Context.Builder Velocity(
this Context.Builder builder,
int maxSharedMemoryPerGroup = VelocityDevice.MinSharedMemoryPerGroup)
{
if (!Backend.RuntimePlatform.Is64Bit())
{
throw new NotSupportedException(string.Format(
RuntimeErrorMessages.VelocityPlatform64,
Backend.RuntimePlatform));
}

builder.DeviceRegistry.Register(new VelocityDevice());
return builder;
}

#endregion

#region Context

/// <summary>
/// Gets a registered Velocity device.
/// </summary>
/// <param name="context">The ILGPU context.</param>
/// <returns>The registered Velocity device.</returns>
public static VelocityDevice GetVelocityDevice(this Context context) =>
context.GetDevice<VelocityDevice>(0);

/// <summary>
/// Creates a new Velocity accelerator.
/// </summary>
/// <param name="context">The ILGPU context.</param>
/// <returns>The created Velocity accelerator.</returns>
public static VelocityAccelerator CreateVelocityAccelerator(
this Context context) =>
context.GetVelocityDevice().CreateVelocityAccelerator(context);

#endregion
}

}

140 changes: 140 additions & 0 deletions Src/ILGPU/Runtime/Velocity/VelocityDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Numerics;
using System.Threading;

namespace ILGPU.Runtime.Velocity
{
/// <summary>
/// Represents a software-emulated velocity device for high-performance execution of
/// tasks on the CPU using vectorization.
/// </summary>
public sealed class VelocityDevice : Device
{
#region Constants

/// <summary>
/// The default maximum amount of shared memory in bytes (1024k).
/// </summary>
public const int MinSharedMemoryPerGroup = 1 << 20;

#endregion

#region Instance

/// <summary>
/// Creates a new velocity device with the default amount of shared memory per
/// group (refer to <see cref="MinSharedMemoryPerGroup"/> for more
/// information about the default size).
/// </summary>
public VelocityDevice()
: this(MinSharedMemoryPerGroup)
{ }

/// <summary>
/// Creates a new velocity device using the given amount of shared memory (min
/// amount is <see cref="MinSharedMemoryPerGroup"/> per group).
/// </summary>
/// <param name="maxSharedMemoryPerGroup">
/// The maximum amount of shared memory per group in bytes.
/// </param>
public VelocityDevice(int maxSharedMemoryPerGroup)
{
if (maxSharedMemoryPerGroup < MinSharedMemoryPerGroup)
throw new ArgumentOutOfRangeException(nameof(maxSharedMemoryPerGroup));

Name = nameof(VelocityAccelerator);
WarpSize = VelocityWarp32.RawVectorLength;
MinWarpSize = VelocityWarp64.RawVectorLength;
MaxNumThreadsPerGroup = MaxNumThreadsPerMultiprocessor = WarpSize;
NumMultiprocessors = Environment.ProcessorCount;
MaxGroupSize = new Index3D(
MaxNumThreadsPerGroup,
1,
1);

MemorySize = long.MaxValue;
MaxGridSize = new Index3D(int.MaxValue, ushort.MaxValue, ushort.MaxValue);
MaxSharedMemoryPerGroup = maxSharedMemoryPerGroup;
MaxConstantMemory = int.MaxValue;
NumThreads = MaxNumThreads;

// Get the endian type from the global BitConverter class
IsLittleEndian = BitConverter.IsLittleEndian;

// Allocate a sufficient amount of local memory per thread equal to
// the maximum number of shared memory per group in bytes
MaxLocalMemoryPerThread = maxSharedMemoryPerGroup;
}

#endregion

#region Properties

/// <summary>
/// Returns the minimum warp size of this device.
/// </summary>
public int MinWarpSize { get; }

/// <summary>
/// Returns the number of threads.
/// </summary>
public int NumThreads { get; }

/// <summary>
/// Returns true if this device operates in little endian mode.
/// </summary>
public bool IsLittleEndian { get; }

/// <summary>
/// Returns the maximum local memory per thread in bytes.
/// </summary>
public int MaxLocalMemoryPerThread { get; }

#endregion

#region Methods

/// <inheritdoc/>
public override Accelerator CreateAccelerator(Context context) =>
CreateVelocityAccelerator(context);

/// <summary>
/// Creates a new performance CPU accelerator using and the default thread
/// priority.
/// </summary>
/// <param name="context">The ILGPU context.</param>
/// <returns>The created CPU accelerator.</returns>
public VelocityAccelerator CreateVelocityAccelerator(
Context context) =>
CreateVelocityAccelerator(context, ThreadPriority.Normal);

/// <summary>
/// Creates a new performance CPU accelerator using and the default thread
/// priority.
/// </summary>
/// <param name="context">The ILGPU context.</param>
/// <param name="threadPriority">
/// The thread priority of the execution threads.
/// </param>
/// <returns>The created CPU accelerator.</returns>
public VelocityAccelerator CreateVelocityAccelerator(
Context context,
ThreadPriority threadPriority) =>
new VelocityAccelerator(context, this, threadPriority);

#endregion

#region Object

/// <inheritdoc/>
public override bool Equals(object obj) =>
obj is VelocityDevice device &&
device.MaxSharedMemoryPerGroup == MaxSharedMemoryPerGroup &&
base.Equals(obj);

/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode() ^ MaxSharedMemoryPerGroup;

#endregion
}
}
80 changes: 80 additions & 0 deletions Src/ILGPU/Runtime/Velocity/VelocityKernel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2022 ILGPU Project
// www.ilgpu.net
//
// File: VelocityKernel.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Backends.Velocity;
using System.Reflection;

namespace ILGPU.Runtime.Velocity
{
/// <summary>
/// Represents a single Velocity kernel.
/// </summary>
public sealed class VelocityKernel : Kernel
{
#region Static

/// <summary>
/// Represents the <see cref="KernelEntryPoint"/> property getter.
/// </summary>
internal static readonly MethodInfo GetKernelExecutionDelegate =
typeof(VelocityKernel).GetProperty(
nameof(KernelEntryPoint),
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
.GetGetMethod(true);

#endregion

#region Instance

/// <summary>
/// Loads a compiled kernel into the given Cuda context as kernel program.
/// </summary>
/// <param name="accelerator">The associated accelerator.</param>
/// <param name="kernel">The source kernel.</param>
/// <param name="launcher">The launcher method for the given kernel.</param>
/// <param name="kernelExecutionDelegate">The execution method.</param>
internal VelocityKernel(
VelocityAccelerator accelerator,
VelocityCompiledKernel kernel,
MethodInfo launcher)
: base(accelerator, kernel, launcher)
{
KernelEntryPoint = kernel.CreateKernelEntryPoint();
}

#endregion

#region Properties

/// <summary>
/// Returns the associated Velocity runtime.
/// </summary>
public VelocityAccelerator VelocityAccelerator =>
Accelerator as VelocityAccelerator;

/// <summary>
/// The main kernel entry point function to be called from each velocity
/// multiprocessor during exeution.
/// </summary>
internal VelocityKernelEntryPoint KernelEntryPoint { get; }

#endregion

#region IDisposable

/// <summary>
/// Does not perform any operation.
/// </summary>
protected override void DisposeAcceleratorObject(bool disposing) { }

#endregion
}
}
Loading

0 comments on commit e13209b

Please sign in to comment.