python multiprocessing is loosing values
up vote
2
down vote
favorite
I tried to speed up a calculation using Pool from the multiprocessing package.
While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.
I share my variables with all processes through the mp.value() class.
Where did i go wrong and how can i fix this?
poss = [x+1 for x in range(20)]
all_rolls = itertools.product(poss, repeat=6)
win = mp.Value('i', 0)
draw = mp.Value('i', 0)
loose = mp.Value('i', 0)
def some_func(roll):
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
with Pool(8) as p:
p.map(some_func, all_rolls)
On 16 cores i got 55,923,638 values instead of 64,000,000
python multiprocessing
add a comment |
up vote
2
down vote
favorite
I tried to speed up a calculation using Pool from the multiprocessing package.
While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.
I share my variables with all processes through the mp.value() class.
Where did i go wrong and how can i fix this?
poss = [x+1 for x in range(20)]
all_rolls = itertools.product(poss, repeat=6)
win = mp.Value('i', 0)
draw = mp.Value('i', 0)
loose = mp.Value('i', 0)
def some_func(roll):
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
with Pool(8) as p:
p.map(some_func, all_rolls)
On 16 cores i got 55,923,638 values instead of 64,000,000
python multiprocessing
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I tried to speed up a calculation using Pool from the multiprocessing package.
While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.
I share my variables with all processes through the mp.value() class.
Where did i go wrong and how can i fix this?
poss = [x+1 for x in range(20)]
all_rolls = itertools.product(poss, repeat=6)
win = mp.Value('i', 0)
draw = mp.Value('i', 0)
loose = mp.Value('i', 0)
def some_func(roll):
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
with Pool(8) as p:
p.map(some_func, all_rolls)
On 16 cores i got 55,923,638 values instead of 64,000,000
python multiprocessing
I tried to speed up a calculation using Pool from the multiprocessing package.
While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.
I share my variables with all processes through the mp.value() class.
Where did i go wrong and how can i fix this?
poss = [x+1 for x in range(20)]
all_rolls = itertools.product(poss, repeat=6)
win = mp.Value('i', 0)
draw = mp.Value('i', 0)
loose = mp.Value('i', 0)
def some_func(roll):
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
with Pool(8) as p:
p.map(some_func, all_rolls)
On 16 cores i got 55,923,638 values instead of 64,000,000
python multiprocessing
python multiprocessing
asked yesterday
Eumel
15911
15911
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You need to protect the modification of your values with Lock (see this article).
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
add a comment |
up vote
2
down vote
In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You need to protect the modification of your values with Lock (see this article).
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
add a comment |
up vote
3
down vote
accepted
You need to protect the modification of your values with Lock (see this article).
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You need to protect the modification of your values with Lock (see this article).
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
You need to protect the modification of your values with Lock (see this article).
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
answered yesterday
jfowkes
662311
662311
add a comment |
add a comment |
up vote
2
down vote
In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
add a comment |
up vote
2
down vote
In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
answered yesterday
Or Dinari
700318
700318
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
add a comment |
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
1
1
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
– jfowkes
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409126%2fpython-multiprocessing-is-loosing-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown