ILGPU.Samples icon indicating copy to clipboard operation
ILGPU.Samples copied to clipboard

Math.Round() Unsupported!

Open SnowmanTackler opened this issue 6 years ago • 5 comments

This is very cool stuff! Thank you for supporting this.

I can't seem to use Math.Round() inside a kernel, and I was under the impression the Math functions were supported. The error I get back trying to compile said kernel is pretty cryptic. I'm on Windows 10, .NetCore 3.1. Here's code to reproduce:

class Program {
        static void KernelF(ILGPU.Index index, float i) {
            int x = (int) Math.Round(i);
        }
        static void Main(string[] args) {
            using var CudaContext = new Context();
            using var CudaAccelerator = Accelerator.Create(CudaContext, Accelerator.Accelerators[0]);
            var kernel = CudaAccelerator.LoadAutoGroupedStreamKernel<ILGPU.Index, float>(KernelF);
        }
    }

And here's the error I get:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
This exception was originally thrown at this call stack:
	ILGPU.Frontend.CodeGenerator.CreateConversion(ILGPU.IR.Construction.IRBuilder, ILGPU.IR.Value, ILGPU.IR.Types.TypeNode, ILGPU.IR.Values.ConvertFlags)
	ILGPU.Frontend.CodeGenerator.MakeConvert(ILGPU.Frontend.Block, ILGPU.IR.Construction.IRBuilder, System.Type, ILGPU.Frontend.ILInstructionFlags)
	ILGPU.Frontend.CodeGenerator.TryGenerateCode(ILGPU.Frontend.Block, ILGPU.IR.Construction.IRBuilder, ILGPU.Frontend.ILInstruction)
	ILGPU.Frontend.CodeGenerator.GenerateCodeForBlock(ILGPU.Frontend.Block)
	ILGPU.Frontend.CodeGenerator.GenerateCode()
	ILGPU.Frontend.CodeGenerationPhase.GenerateCodeInternal(System.Reflection.MethodBase, bool, System.Collections.Generic.HashSet<System.Reflection.MethodBase>, out ILGPU.IR.Method)
	ILGPU.Frontend.ILFrontend.DoWork()
	System.Threading.ThreadHelper.ThreadStart_Context(object)
	System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
	System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    ...
    [Call Stack Truncated]

SnowmanTackler avatar Apr 09 '20 01:04 SnowmanTackler

@SnowmanTackler Thank you for submitting this bug report. The first thing you should do is call EnableAlgorithms (provided by the Algorithms library) on the ILGPU.Context instance to enable support for math functions.

However, it turns out that you have accidentally encountered a frontend error 👍. I will fix this in one of the next commits.

m4rs-mt avatar Apr 09 '20 10:04 m4rs-mt

Your reported issue has been fixed. Please note that you have to enable the algorithms library in order to use math functions from the classes System.Math and System.MathF.

m4rs-mt avatar Apr 13 '20 15:04 m4rs-mt

Interesting. So when I add the EnableAlgorithms() call to my example code,

using var CudaContext = new Context();
CudaContext.EnableAlgorithms();
using var CudaAccelerator = Accelerator.Create(CudaContext, Accelerator.Accelerators[0]);
var kernel = CudaAccelerator.LoadAutoGroupedStreamKernel<ILGPU.Index, float>(KernelF);

I'm still getting the same error. I'm still using ILGPU v0.7.1 (not building from source). Is that expected?

SnowmanTackler avatar Apr 18 '20 19:04 SnowmanTackler

hi @SnowmanTackler, how did you go with your issue?

FYI, it looks like the issue was related to casting/converting the result to an int, and was fixed in ILGPU v0.8 beta2.

MoFtZ avatar May 08 '20 00:05 MoFtZ

Works now, thanks so much!

SnowmanTackler avatar Jun 07 '20 02:06 SnowmanTackler