question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Concurrent GPU access

See original GitHub issue

Hi @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:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
Sergio0694commented, Apr 13, 2020

Hey @miyconst - sorry for the delay, I finally have an update on this 😅

So, couple of things:

  1. Turns out static constructors in C# are in fact thread-safe (see here, cc. @JimBobSquarePants FYI).
  1. I’m doing a major refactoring of the whole library, moving away from lambda expressions to generic structs (similar to the value delegate approach we use in 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 the Gpu.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.
1reaction
JimBobSquarePantscommented, Nov 25, 2019

Default property is initialized in the static constructor, which is not thread safe), you might end up with different GPU instances if two threads are accessing the Default property at the same time.

Lazy<T>?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found