Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Alexander R. Hankin
EE194_Manduca_Simulator_Demo
Commits
c3d28872
Commit
c3d28872
authored
May 07, 2018
by
David A.. Werner
Browse files
Added script to determine speedup of multi-threading
parent
73534ae1
Changes
1
Hide whitespace changes
Inline
Side-by-side
time_multi_threaded.py
0 → 100644
View file @
c3d28872
""" This is a script that compares run-times of single-threaded vs multi-threaded simulation """
import
time
import
numpy
as
np
import
multiprocessing
from
functools
import
wraps
from
manduca
import
SimpleManduca
,
EvolutionSimulator
def
timed_function
(
return_result
=
True
,
return_time
=
True
,
print_time
=
False
):
def
timed_function_decorator
(
fn
):
@
wraps
(
fn
)
def
timed
(
*
args
,
**
kwargs
):
start_time
=
time
.
time
()
result
=
fn
(
*
args
,
**
kwargs
)
end_time
=
time
.
time
()
time_ms
=
(
end_time
-
start_time
)
*
1000
if
print_time
:
print
"{} {:2.2f} ms"
.
format
(
fn
.
__name__
,
time_ms
)
if
return_result
:
if
return_time
:
return
result
,
time_ms
else
:
return
result
elif
return_time
:
return
time_ms
return
return
timed
return
timed_function_decorator
@
timed_function
(
return_result
=
False
)
def
run_sim
():
return
simulator
.
run_simulation
(
NUM_GENERATIONS
)
POPULATION_SIZE
,
NUM_GENERATIONS
=
10
,
10
num_legs
,
time_segments
,
time_step
=
5
,
10
,
10
def
random_manduca
():
return
SimpleManduca
.
random_individual
(
num_legs
,
time_segments
,
time_step
)
simulator
=
EvolutionSimulator
((
random_manduca
,
POPULATION_SIZE
))
print
"Running single-threaded code.."
time_ms
=
run_sim
()
try
:
pool
=
multiprocessing
.
Pool
(
int
(
multiprocessing
.
cpu_count
()))
simulator
=
EvolutionSimulator
((
random_manduca
,
POPULATION_SIZE
),
pool
=
pool
)
print
"Running multi-threaded code.."
time_ms_multi
=
run_sim
()
finally
:
pool
.
close
()
pool
.
join
()
print
"Number of cores: {}"
.
format
(
multiprocessing
.
cpu_count
())
print
"Speedup: {}"
.
format
(
time_ms
/
time_ms_multi
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment