ILGPU icon indicating copy to clipboard operation
ILGPU copied to clipboard

INumber constraint support

Open QsROg8320 opened this issue 2 years ago • 2 comments

Support INumber generic constraint. I want to write something like that:

using ILGPU.Runtime;
using ILGPU;
using ILGPU.Runtime.Cuda;

CudaWrapper<float> wrapper = new CudaWrapper<float>();

Console.WriteLine(String.Join(",", wrapper.Add(new[] {1f,2f,3f },4f)));


public class CudaWrapper<T> where T: unmanaged, INumber<T>
{
    public const int DeviceIndex = 0;
    private static Context ContextDevice { get; } = Context.CreateDefault();
    private static Accelerator Accelerator { get; } = ContextDevice.GetCudaDevice(DeviceIndex).CreateAccelerator(ContextDevice);
    private static Action<Index1D,
        ArrayView1D<T, Stride1D.Dense>,
        T,
        ArrayView1D<T, Stride1D.Dense>> AddKernelConst = Accelerator.LoadAutoGroupedStreamKernel<
            Index1D,
            ArrayView1D<T, Stride1D.Dense>,
            T,
            ArrayView1D<T, Stride1D.Dense>>(
            AddKernelConstAction);
    static void AddKernelConstAction(
           Index1D index,
           ArrayView1D<T, Stride1D.Dense> aView,
           T b,
           ArrayView1D<T, Stride1D.Dense> cView)
    {
        cView[index] = aView[index] + b;
    }

    public  T[] Add(T[] a,T b)
    {

        using var cTensor = Accelerator.Allocate1D<T>(a.Length);
        using var aTensor = Accelerator.Allocate1D<T>(a);

        AddKernelConst(a.Length, aTensor, b, cTensor);
        Accelerator.Synchronize();
        return cTensor.GetAsArray1D();
    }

   
}

QsROg8320 avatar May 03 '22 07:05 QsROg8320

hi @QsROg8320, thanks for this feature request. This is one that I'm personally interested in too.

The Generic Math functionality was originally announced in the net6.0 preview. This functionality makes use of the new Static Abstract Interface methods that will be made available in C# 11.

I can confirm that the current implementation of ILGPU (v1.1.0) compiled with the preview features enabled will fail to generate a kernel to run on the GPU.

At this stage, it is probably a bit premature. Once the feature has been confirmed for release, and the .NET implementation is stable, we can try adding support for this functionality into ILGPU.

MoFtZ avatar May 04 '22 02:05 MoFtZ

Hi @QsROg8320, thank you for your feature request! I also shared your opinions on this topic, @QsROg8320 and @MoFtZ, as I would also like to see support for this feature in ILGPU. Moreover, I think we should add support for this after the feature is integrated into .Net7.

m4rs-mt avatar May 05 '22 11:05 m4rs-mt