Concurrent GPU access
See original GitHub issueHi @Sergio0694!
I am trying to utilize a GPU for report building in my system, but when two users request a report at the same time, GPU access fails with the following error:
System.InvalidOperationException : Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
Is this expected? Does this mean that any instance of ComputeSharp.GraphicsDevice
can be used from only one thread at a given moment of time?
Here is a simple reproduction scenario:
public void GpuMultiThread()
{
int count = 10;
List<Task> tasks = new List<Task>();
for (int i = 0; i < count; i++)
{
int index = i;
tasks.Add(Task.Run(() =>
{
ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer<int>(count);
Gpu.Default.For(count, thread =>
{
buffer[thread.X] = thread.X;
});
int sum = buffer.GetData().Sum();
this.TestOutput.WriteLine($"Thread: {index}, sum: {sum}.");
}));
}
Task.WhenAll(tasks).GetAwaiter().GetResult();
}
Message:
System.ArgumentException : An item with the same key has already been added. Key: (39086322, 32, 1, 1)
Stack Trace:
Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
ShaderRunner.Run(GraphicsDevice device, Int32 x, Int32 y, Int32 z, Int32 threadsX, Int32 threadsY, Int32 threadsZ, Action`1 action)
ShaderRunner.Run(GraphicsDevice device, Int32 x, Int32 y, Int32 z, Action`1 action)
GraphicsDeviceExtensions.For(GraphicsDevice device, Int32 x, Action`1 action)
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
GPU Programming: Concurrent access to the GPU
Concurrently execute two kernels on the same GPU. So far we only focused on completing one operation at the time on the GPU,...
Read more >Concurrency, 4 CUDA Applications competing to get GPU ...
What would happen if there are four concurrent CUDA Applications competing for resources in one single GPU so they can offload the work...
Read more >CUDA C/C++ Streams and Concurrency
A sequence of operations that execute in issue-order on the GPU. Programming model used to effect concurrency. CUDA operations in different streams may...
Read more >GPU CONCURRENCY
Processes share GPU through time-slicing. Scheduling managed by system. Concurrent scheduling. Processes run on GPU simultaneously.
Read more >Chapter 6. GPU Programming with Accelerate
Get Parallel and Concurrent Programming in Haskell now with the O'Reilly learning platform. O'Reilly members experience books, live events, courses curated by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @miyconst - sorry for the delay, I finally have an update on this 😅
So, couple of things:
ImageSharp
), which makes life a lot easier for the reflection system that creates the GPU shaders. In that branch, I’ve also made a fix for the concurrent accesses to theGpu.Default
property (here) - that property is now loaded directly from the static constructor, which should make it perfectly safe for concurrent access at any time. If you try that branch out (or just wait for the updated NuGet packages), let me know how it goes! That change should hopefully solve the issue you were having here.Lazy<T>
?