m4rs-mt / ILGPU

ILGPU JIT Compiler for high-performance .Net GPU programs
http://www.ilgpu.net
Other
1.34k stars 115 forks source link

[QUESTION]: Cannot find reason for "internal compiler error" #1251

Closed AmosEgel closed 2 months ago

AmosEgel commented 2 months ago

Question

I am running a very simple kernel, but it throws an internal compiler error. The kernel reads:

static void HelloKernel(Index1D i, ArrayView<float> output)
{
    output[i] = MathF.Sin(0.1F * i);
}

Any idea what I can check to narrow down the problem?

Environment

Additional context

My laptop has two GPUs, the other one is an Intel(R) UHD Graphics 620 and does correctly process the kernel. The Intel GPU also does the display of my laptop.

Error message:

Exception: An internal compiler error has been detected at ILGPU.Backends.Backend.Compile[TBackendHook](Method kernelMethod, EntryPointDescription& entry, KernelSpecialization& specialization, TBackendHook backendHook) in //Src/ILGPU/Backends/Backend.cs:line 725 at ILGPU.Backends.Backend.Compile[TBackendHook](EntryPointDescription& entry, KernelSpecialization& specialization, TBackendHook backendHook) in //Src/ILGPU/Backends/Backend.cs:line 643 at ILGPU.Backends.Backend.Compile(EntryPointDescription& entry, KernelSpecialization& specialization) in //Src/ILGPU/Backends/Backend.cs:line 625 at ILGPU.Runtime.Accelerator.CompileKernel(EntryPointDescription& entry, KernelSpecialization& specialization) in //Src/ILGPU/Runtime/KernelCache.cs:line 574 at ILGPU.Runtime.Accelerator.<>c__DisplayClass157_01.<LoadGenericKernel>b__0(TKernelLoader& loader, KernelInfo& info) in /_/Src/ILGPU/Runtime/KernelCache.cs:line 474 at ILGPU.Runtime.Accelerator.LoadCachedKernel[TKernelLoader,T](EntryPointDescription& entry, KernelSpecialization& specialization, TKernelLoader& kernelLoader, CachedKernelLoader2 cachedLoader, KernelInfo& kernelInfo) in //Src/ILGPU/Runtime/KernelCache.cs:line 408 at ILGPU.Runtime.Accelerator.LoadGenericKernel[TDelegate,TKernelLoader](EntryPointDescription& entry, KernelSpecialization& specialization, TKernelLoader& kernelLoader, KernelInfo& kernelInfo) in //Src/ILGPU/Runtime/KernelLoading.cs:line 329 at ILGPU.Runtime.Accelerator.LoadAutoGroupedKernel[TDelegate](MethodInfo method, KernelInfo& kernelInfo) in //Src/ILGPU/Runtime/KernelLoading.cs:line 559 at ILGPU.Runtime.KernelLoaders.LoadAutoGroupedKernel[TIndex,T1](Accelerator accelerator, Action`2 action) in //Src/ILGPU/Runtime/KernelLoaders.cs:line 345 at ILGPU.Runtime.KernelLoaders.LoadAutoGroupedStreamKernel[TIndex,T1](Accelerator accelerator, Action`2 action) in /_/Src/ILGPU/Runtime/KernelLoaders.cs:line 0

The following kernels run fine:

static void HelloKernel(Index1D i, ArrayView<float> output)
{
    output[i] = 0.1F;
}
static void HelloKernel(Index1D i, ArrayView<float> output)
{
    output[i] = MathF.Sin(0.1F);
}

I use the following method to call the kernel:

public static float[] HelloGPU(string deviceName, int numAngles)
{
    using (Context context = Context.CreateDefault())
    {
        Device d = null;
        foreach (Device dev in context)
        {
            if (dev.Name == deviceName) d = dev;
        }
        if (d == null) return null;

        using (Accelerator accelerator = d.CreateAccelerator(context))
        {
            MemoryBuffer1D<float, Stride1D.Dense> deviceOutput = accelerator.Allocate1D<float>(numAngles);
            Action<Index1D, ArrayView<float>> loadedKernel =
                accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<float>>(HelloKernel);
            loadedKernel((int)deviceOutput.Length, deviceOutput.View);
            accelerator.Synchronize();
            float[] hostOutput = deviceOutput.GetAsArray1D();
            return hostOutput;
        }
    }
}
MoFtZ commented 2 months ago

hi @AmosEgel.

The internal compiler exception should have an InnerException property with more detailed information, including other helpful things such as line number.

Please have a look, and let us know what the inner exception says.

AmosEgel commented 2 months ago

Thanks @MoFtZ.

The inner exception has an inner exception which says:

"The function 'SinF' does not have an intrinsic implementation for this backend. 'EnableAlgorithms' from the Algorithms library not invoked?"

MoFtZ commented 2 months ago

OK, so you will need to add the ILGPU.Algorithms nuget package (matching the same version number as your ILGPU nuget package). And call EnableAlgortihms when creating the context.

Some sample code can be found here: https://github.com/m4rs-mt/ILGPU/blob/master/Samples/AlgorithmsMath/Program.cs

AmosEgel commented 2 months ago

Nice, that worked. Thank you so much @MoFtZ !