Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Julia ReentrantLock rather than fftw_make_planner_threadsafe #160

Merged
merged 8 commits into from
Aug 20, 2020

Conversation

stevengj
Copy link
Member

Should fix #141. Closes #157.

@vtjnash
Copy link
Contributor

vtjnash commented Aug 17, 2020

This is a large portion of it, definitely! Though doesn't yet fix the deadlock. While this is a re-entrant lock, we shouldn't ever see it be re-entered from a given thread (whether on the same or a different task) as fftw doesn't like that. However, we might see that some finalizer tries to acquire this lock. To protect against that, the finalizers also need to be delayed until the unlock call here, to ensure that they destroy call doesn't get blocked.

@stevengj
Copy link
Member Author

I don't quite follow you. If a given thread holds the lock and FFTW is busy calling some thread-unsafe routine (e.g. creating or destroying a thread), how could a finalizer concurrently execute fftw_destroy_plan on the same thread?

src/FFTW.jl Show resolved Hide resolved
@stevengj
Copy link
Member Author

I see, so the scenario we are worried about is something like:

  1. User starts creating a plan in a thread. Acquires lock.
  2. FFTW planner, while exploring different algorithms, calls spawnloop.
  3. During Julia spawnloop execution, one of the spawned threads runs GC and tries to destroy an old plan. Deadlocks.

@stevengj
Copy link
Member Author

stevengj commented Aug 19, 2020

Maybe when fftwlock is acquired (i.e. whenever islocked(fftwlock) is true), all destroy_plan calls can be deferred to a queue, and @exclusive can flush the queue right before after it releases the lock?

That will get rid of all the business with a timer in #157, and allow garbage collection to function normally most of the time.

@vtjnash
Copy link
Contributor

vtjnash commented Aug 19, 2020

That sounds great

@stevengj
Copy link
Member Author

Okay, I updated the thread to defer plan destruction while the lock is held.

(The plan-destruction queue itself requires a lock, and the logic of flushing the queue is a bit tricky to avoid deadlocks or race conditions, so I'd appreciate another pair of eyes to double-check my logic.)

Copy link
Contributor

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is on the right track, but may have some of the locked sections in the wrong order so that they don’t do what they are supposed to

src/fft.jl Outdated Show resolved Hide resolved
src/fft.jl Outdated Show resolved Hide resolved
src/FFTW.jl Show resolved Hide resolved
Copy link
Contributor

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems correct to me.

src/FFTW.jl Outdated Show resolved Hide resolved
@stevengj stevengj merged commit 1618ebf into master Aug 20, 2020
@stevengj stevengj deleted the threadsafety branch August 20, 2020 02:13
@stevengj
Copy link
Member Author

stevengj commented Aug 20, 2020

Now that I think of it, it should actually be safe to use the regular trylock in maybe_destroy_plan — the FFTW planner is not thread-safe, but re-entrant calls to destroy_plan in the same thread should be fine (as long as we don't call make_planner_thread_safe, which adds an internal mutex lock). (The planner does lots of plan destruction already.)

So, it should be fine to destroy a plan in the thread that acquired the planner lock, even if that occurs during planning.

reverted in b9e0000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deadlock with multiple FFTW plan construction / destruction in serial and threaded runs
2 participants