m4rs-mt / ILGPU

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

System.AccessViolationException when adding useless if statement in kernel #1076

Closed GijsVanoppen closed 1 year ago

GijsVanoppen commented 1 year ago

Hello,

Let me preface this by saying I'm still quite new to GPGPU, so if my questions are simple, I apologise for wasting your time.

My program has one kernel:

        static void Kernel(Index1D kernelIndex, ArrayView<int> PixelPosX, ArrayView<int> PixelPosY, ArrayView<int> windowPixelElementsArray, int dataPointsAmount)
        {
            int index = 0;

            if (kernelIndex %  2 == 0)
            {
                for (int y = PixelPosY[kernelIndex] - (StepSize * (WindowSize / 2)); y < PixelPosY[kernelIndex] + (StepSize * (WindowSize / 2 + 1)); y += StepSize)
                {
                    for (int x = PixelPosX[kernelIndex] - (StepSize * (WindowSize / 2)); x < PixelPosX[kernelIndex] + (StepSize * (WindowSize / 2 + 1)); x += StepSize)
                    {
                        windowPixelElementsArray[kernelIndex * WindowSize * WindowSize * 2 + index] = x;
                        windowPixelElementsArray[kernelIndex * WindowSize * WindowSize * 2 + index + 1] = y;
                        index += 2;
                    }
                }
            }            
        }

The details inside of the if statement does not matter. The problem I'm having is that when I remove the if statement (keeping the body of course), the program works. It does crash my laptop screen for a bit (the CPU and my integrated graphics card are then at 100% usage). I wanted to limit how much my resources are used, so that is why I added the if statement. However, the program crashes with the following error message: System.AccessViolationException: 'Attempted to read or write protected memory. This often indicates that other memory is corrupted." (the error message was written in my native language instead of English so the exact wording may be different)

Even when I replace the if statement with something stupid like if (kernelIndex > 0), the same error message appears.

How can I avoid this? Or better yet, is there a better way to make sure my program doesnt take up 100% of my CPU and GPU?

Thank you in advance!

P.S. In case you need it, this is how I loaded and executed my kernel

Context context = Context.CreateDefault();
Accelerator accelerator = context.GetPreferredDevice(preferCPU: false).CreateAccelerator(context);
var loadedKernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ArrayView<int>, ArrayView<int>, int>(Kernel);
loadedKernel(DataPointsAmount, DevicePixelPosX.View, DevicePixelPosY.View, DeviceWindowPixelElementsArray.View, DataPointsAmount);
accelerator.Synchronize();
accelerator.Dispose();
context.Dispose();

And my specs: Processor: AMD Ryzen 7 PRO 3700U w/ Radeon Vega Mobile Gfx GPU: AMD Radeon(TM) Vega 10 Graphics

MoFtZ commented 1 year ago

hi @GijsVanoppen. what version of ILGPU are you using?

This is sounding a bit like #1075.

GijsVanoppen commented 1 year ago

I'm using version 1.5.0

MoFtZ commented 1 year ago

Could you please try downgrading to ILGPU v1.4 and see if that helps?

GijsVanoppen commented 1 year ago

Nope, that didn't help. The same issue pops up

GijsVanoppen commented 1 year ago

I also updated the driver of my processor with integrated graphics, and that didn't help.

GijsVanoppen commented 1 year ago

Moreover, if I replace if (kernelIndex % 2 == 0) with

So I can assign the value of the kernelIndex to a test variable just fine, but then when I do something with test or kernelIndex like applying the '%' operator or applying '==' or '>', the program crashes. And it always does this with the same error message

MoFtZ commented 1 year ago

@GijsVanoppen If you are able, please join our Discord server. It might be easier for us to provide support there.

https://discord.gg/X6RBCff

m4rs-mt commented 1 year ago

This issue was fixed in #1084.