Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Spack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
eic_tools
Spack
Commits
908a93a4
Commit
908a93a4
authored
9 years ago
by
Todd Gamblin
Browse files
Options
Downloads
Patches
Plain Diff
Add a multiprocess Barrier class to use for testing parallel code.
parent
5fda7daf
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/spack/spack/util/multiproc.py
+49
-1
49 additions, 1 deletion
lib/spack/spack/util/multiproc.py
with
49 additions
and
1 deletion
lib/spack/spack/util/multiproc.py
+
49
−
1
View file @
908a93a4
...
...
@@ -27,9 +27,11 @@
than multiprocessing.Pool.apply() can. For example, apply() will fail
to pickle functions if they
'
re passed indirectly as parameters.
"""
from
multiprocessing
import
Process
,
Pipe
from
multiprocessing
import
Process
,
Pipe
,
Semaphore
,
Value
from
itertools
import
izip
__all__
=
[
'
spawn
'
,
'
parmap
'
,
'
Barrier
'
]
def
spawn
(
f
):
def
fun
(
pipe
,
x
):
pipe
.
send
(
f
(
x
))
...
...
@@ -43,3 +45,49 @@ def parmap(f,X):
[
p
.
join
()
for
p
in
proc
]
return
[
p
.
recv
()
for
(
p
,
c
)
in
pipe
]
class
Barrier
:
"""
Simple reusable semaphore barrier.
Python 2.6 doesn
'
t have multiprocessing barriers so we implement this.
See http://greenteapress.com/semaphores/downey08semaphores.pdf, p. 41.
"""
def
__init__
(
self
,
n
,
timeout
=
None
):
self
.
n
=
n
self
.
to
=
timeout
self
.
count
=
Value
(
'
i
'
,
0
)
self
.
mutex
=
Semaphore
(
1
)
self
.
turnstile1
=
Semaphore
(
0
)
self
.
turnstile2
=
Semaphore
(
1
)
def
wait
(
self
):
if
not
self
.
mutex
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
count
.
value
+=
1
if
self
.
count
.
value
==
self
.
n
:
if
not
self
.
turnstile2
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
turnstile1
.
release
()
self
.
mutex
.
release
()
if
not
self
.
turnstile1
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
turnstile1
.
release
()
if
not
self
.
mutex
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
count
.
value
-=
1
if
self
.
count
.
value
==
0
:
if
not
self
.
turnstile1
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
turnstile2
.
release
()
self
.
mutex
.
release
()
if
not
self
.
turnstile2
.
acquire
(
timeout
=
self
.
to
):
raise
BarrierTimeoutError
()
self
.
turnstile2
.
release
()
class
BarrierTimeoutError
:
pass
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment