Skip to content

WIP add task_local for duplication things for multithreading#1070

Draft
fredrikekre wants to merge 2 commits intomasterfrom
fe/task_local
Draft

WIP add task_local for duplication things for multithreading#1070
fredrikekre wants to merge 2 commits intomasterfrom
fe/task_local

Conversation

@fredrikekre
Copy link
Copy Markdown
Member

No description provided.

Base automatically changed from fe/ohmythreads to master September 26, 2024 09:54
Comment thread src/FEValues/FunctionValues.jl
@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Sep 26, 2024

I don't have time to look in detail, but I really like to have this interface!

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 27, 2024

Codecov Report

Attention: Patch coverage is 97.14286% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.54%. Comparing base (5153307) to head (217bae0).
Report is 36 commits behind head on master.

Files with missing lines Patch % Lines
src/multithreading.jl 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1070      +/-   ##
==========================================
- Coverage   93.57%   93.54%   -0.04%     
==========================================
  Files          39       40       +1     
  Lines        6071     6086      +15     
==========================================
+ Hits         5681     5693      +12     
- Misses        390      393       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@termi-official termi-official left a comment

Choose a reason for hiding this comment

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

Is there a sane way to unit test this?

@fredrikekre
Copy link
Copy Markdown
Member Author

Thoughts on the name? task_local is OK I guess, but then it sounds like it would return something like https://github.com/vchuravy/TaskLocalValues.jl, which it doesn't (but perhaps it should?). scratch_copy?

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Nov 8, 2024

I think having task in the name makes sense. Some suggestions are
task_copy, task_scratch, or task_local_copy

Copy link
Copy Markdown
Member

@KnutAM KnutAM left a comment

Choose a reason for hiding this comment

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

Nice!

Took a bit deeper look, looks good. Docs/changelog etc of course needed as well, but still just WIP AFAIU.

Should it also be added to InterfaceValues?

Comment thread src/FEValues/FunctionValues.jl
Comment on lines +323 to +321
# TODO: For typical use the quadrature rule is read-only, but seems safer to copy anyway?
# And might even be beneficial with e.g. NUMA?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree, I think the cost of copying here would always be negligible, and there is a risk when using e.g. PointValues where the quadrature point is changed!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would argue that task_local should always be safe and that the caller is responsible to not call task_local on immutable stuff, if he does not want the duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IIRC, at least in the past, it was beneficial that the thread that used the data was the one allocating it so thats why I thought maybe it is better to duplicate this too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure if that is also true for constant shared memory data. I suggest that we keep the rationale in a doc string here tho, so if we revisit this we have the exact arguments at hand (and also for users).

Comment thread src/multithreading.jl Outdated
Comment thread src/multithreading.jl Outdated
Comment thread test/test_multithreading.jl Outdated
Comment thread src/FEValues/CellValues.jl
Comment on lines +323 to +321
# TODO: For typical use the quadrature rule is read-only, but seems safer to copy anyway?
# And might even be beneficial with e.g. NUMA?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would argue that task_local should always be safe and that the caller is responsible to not call task_local on immutable stuff, if he does not want the duplication.

Comment thread src/multithreading.jl
Comment thread src/multithreading.jl Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some code paths are not covered yet (see CodeCov result).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still.

@KristofferC
Copy link
Copy Markdown
Collaborator

FWIW, I am not 100% sold on this. What was the issue with the previous way?

Also, calling it task_local when it basically seems to create a (somewhat selective) copy feels strange to me? What's task local about that?

Comment thread src/Quadrature/quadrature.jl Outdated
Comment thread src/FEValues/FacetValues.jl Outdated
@fredrikekre
Copy link
Copy Markdown
Member Author

What was the issue with the previous way?

The issue is basically that there isn't a previous way:

  • CellValues you can copy (so you don't have to thread the quadrature and interpolation all the way into the global assembly in order to call the CellValue constructor to obtain a copy)
  • CellCache is not a problem to call the constructor for each task because the input dh should be available
  • local matrix/vector is OK to recreate with zeros or whatever
  • To get a task local assembler you have to call start_assemble(K, f; fillzero = false)... what?

The point of this PR is to get something consistent I guess. I am not really fond of the name either, but since it is typically used when you set up a new task the name "looks good", i.e.

cv = task_local(cellvalues)
Threads.@spawn assemble!( cv...)

Would a name like scratch_copy or something be better?

@termi-official
Copy link
Copy Markdown
Member

Would a name like scratch_copy or something be better?

To get the ball rolling on this, in Thunderbolt I have implemented this exact concept some time ago decided for the name duplicate_for_parallel.

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Nov 11, 2024

copy_for_task?

IMO scratch isn't so nice since we also pass information (e.g. DofHandler in CellCache)

(But I'm also good withtask_local or task_local_copy)

@fredrikekre
Copy link
Copy Markdown
Member Author

I still think something like this would be nice to have. Can we come up with a better name? I don't think copy is the right verb exactly...

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Sep 22, 2025

I still think something like this would be nice to have. Can we come up with a better name? I don't think copy is the right verb exactly...

What about task_local_instance if we don't like xxx_copy and don't want just task_local?

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

Would be good to get this settled I think, and also related to the discussion in #1291.

I suggest we make a vote - add 👍 or 👎 for the suggestions you like or don't like below, and ❤️ for your favourite. If not wanting to have a function like this at all, add a 💀 on this comment :)

(Hope I didn't miss any suggestions, if so please add)

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

task_local

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

scratch_copy

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

task_copy

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

task_scratch

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

task_local_copy

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

duplicate_for_parallel

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

copy_for_task

@KnutAM
Copy link
Copy Markdown
Member

KnutAM commented Apr 11, 2026

task_local_instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants