This project is read-only.

Context problems

Jun 18, 2013 at 5:57 AM
I have this code:
                    GPGPU gpu = GPUcontrol.getGpu(iGPU);
                    if(!gpu.IsCurrentContext) throw new Exception("GPU is not current context");
which is throwing an "Invalid Context" error on the gpu.Lock() line.

How is this possible? Have I not set the context and then checked it in the lines above? What am I missing?
Jun 21, 2013 at 9:32 AM
Can you show a bit more code? I can't see the full context from this snippet (pun intended).
Jun 24, 2013 at 10:03 AM
I thought that code was enough, but clearly I misunderstood something - isn't gpu.SetCurrentContext enough to assign the context to the "gpu" variable, which should mean that the Lock would work?

anyhow if it helps getGpu looks like this:
        public static Cudafy.Host.GPGPU getGpu(int iDevice)
            return CudafyHost.GetDevice(CudafyModes.Target, iDevice);
Jun 24, 2013 at 10:11 AM
I cannot see in which thread you first got the device, allocated memory, enabled multithreading etc.
Best reference is MultiGPUTests.cs in the Cudafy.Host.UnitTests project.
Jun 25, 2013 at 2:01 AM
looking at MultiGPUTests.cs in the 2 thread/2 gpu examples, the context is only set after GetDevice and before Allocate.
It did exist in Test_TwoThreadTwoGPU_Thread0V2 and Test_TwoThreadTwoGPU_Thread1V2, but has been commented out. Is it not required?

Interestingly if I remove the Lock (and Unlock), my code runs fine. I'm performing CopyToDevice, LaunchAsync, Syncronize, CopyFromDevice and FreeAll without a problem.
Jun 25, 2013 at 2:31 AM
Nick, maybe this isn't very helpful, but in the past (way in the past) I remember my multithreaded algo having wierd context problems with gpu.Lock to the extent that I had to give up on it and instead simply implement my own locking mechanism enclosing a forceful SetCurrentContext. Alas, I don't remember any specifics. I should have dug into cudafy's source, but I has short of time. Most likely I was doing it wrong.
I'm sorry that in this context I can't contextualize it better.
Jun 25, 2013 at 2:41 AM
how will that work if I have 2 separate applications using the GPU?
Jun 25, 2013 at 8:45 AM
Yes, the example code needs cleaned up. SetCurrentContext is required in the main host thread. Locking is needed when multiple threads access the same GPU.
Jun 25, 2013 at 11:09 AM
how will that work if I have 2 separate applications using the GPU?
You don't have to do anything; as long as you have compute >= 2, it will just work.
Jun 25, 2013 at 11:18 AM
great - it does seem to be working fine, good to hear that is as expected.

So no problem if seperate applications, manual lock if within one application.
Jun 25, 2013 at 2:07 PM
Hmm I'm not saying manual lock is the way to go. I merely used it because I had little time to figure out why cudafy's way wasn't working for me. If it happened today, I'd probably go about it differently.