-
Notifications
You must be signed in to change notification settings - Fork 88
Parallelism support: PoC/Suggestion #1694
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| (include_subdirs no) | ||
|
|
||
| (library | ||
| (name goblint_parallel) | ||
| (public_name goblint.parallel) | ||
| (libraries | ||
| domain-local-await) | ||
| (preprocess | ||
| (pps | ||
| ppx_optcomp | ||
| )) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| [%%if ocaml_version >= (5, 0, 0)] | ||
| (* Simple Mutex Implementation using Domain-Local Await (https://github.com/ocaml-multicore/domain-local-await) | ||
| Copyright © 2023 Vesa Karvonen | ||
|
|
||
| Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, | ||
| provided that the above copyright notice and this permission notice appear in all copies. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
| OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | ||
| DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | ||
| ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) | ||
|
|
||
| type state = | ||
| | Unlocked | ||
| | Locked of (unit -> unit) list | ||
|
|
||
| type t = state Atomic.t | ||
|
|
||
| let create () = Atomic.make Unlocked | ||
|
|
||
| let unlock t = | ||
| match Atomic.exchange t Unlocked with | ||
| | Unlocked -> invalid_arg "mutex: already unlocked" | ||
| | Locked awaiters -> List.iter ((|>) ()) awaiters | ||
|
|
||
| let rec lock t = | ||
| match Atomic.get t with | ||
| | Unlocked -> | ||
| if not (Atomic.compare_and_set t Unlocked (Locked [])) then | ||
| lock t | ||
| | Locked awaiters as before -> | ||
| let dla = Domain_local_await.prepare_for_await () in | ||
| let after = Locked (dla.release :: awaiters) in | ||
| if Atomic.compare_and_set t before after then | ||
| match dla.await () with | ||
| | () -> lock t | ||
| | exception cancellation_exn -> | ||
| let rec cleanup () = | ||
| match Atomic.get t with | ||
| | Unlocked -> () | ||
| | Locked awaiters as before -> | ||
| if List.for_all ((==) dla.release) awaiters then | ||
| let after = | ||
| Locked (List.filter ((!=) dla.release) awaiters) | ||
| in | ||
| if not (Atomic.compare_and_set t before after) then | ||
| cleanup () | ||
| in | ||
| cleanup (); | ||
| raise cancellation_exn | ||
| else | ||
| lock t | ||
|
|
||
| [%%else] | ||
|
|
||
| type state = NoOp | ||
|
|
||
| let create () = NoOp | ||
| let unlock _ = () | ||
| let lock _ = () | ||
|
|
||
| [%%endif] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's described as
Is this actually needed? Do the parallel solvers also use effects-based schedulers per-domain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the time of writing it was needed for solvers using the domainslib library. I have included this as it is one of the simplest examples.
I am currently evaluating this and other workarounds that were implemented at the time, so this in particular might be replaced by the Stdlib or Batteries Mutex.
However, even then we will need to benchmark this. With 5.0.0 even mutexes that were guaranteed to always be free caused slowdowns. If this is still the case, a No-Op mutex could still be useful. (Of course, we can also go the middle-way and configure the behaviour of the Mutex at runtime)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the used Domainslib uses an internal scheduler. This incompatibility was something that took us a while to track down in one of our solvers (Helmut's variant iirc):
See also ocaml-multicore/domainslib#127