Ask Your Question

# @parallel Parallel Pollard-Rho

I am currently working on the implementation of a parallelized Pollard-Rho algorithm. I want to use the @parallel command to

D = {}
@parallel(4)
def dist_points(P,Q,dist_prop,r):
ord = P.order()
seed = randint(1,ord)
X = [seed*P,seed, 0]
counter = 0
counter4 = 0
m = [randrange(0,ord) for i in range(r)]
n = [randrange(0,ord) for k in range(r)]
T = [(m[j]*P + n[j]*Q) for j in range(r)]
while(True):
#step function (linear walk)
X = step_linear(T,m,n,ord,X,r)
counter += 1
counter4 += 1
(Pi, ai, bi) = X
x_coord = Pi
global D
if ZZ(x_coord)%dist_prop==0:
if Pi in D:
(a,b) = D[Pi]
if gcd((b-bi),ord)==1:
log = ((ai-a)/(b-bi))%ord
return log, counter4
else:
D[Pi] = (ai,bi)
seed = randint(1,P.order())
X = [seed*P, seed, 0]
counter = 0
elif counter > 100*dist_prop:
seed = randint(1,P.order())
X = [seed*P,seed, 0]
counter = 0


The result of the function works. But I have the feeling that the parallelization does not work. I want my 4 CPU's to work parallel on a same list D looking for points until they find a match. But when I change the 6th line to

X = [P,1,0]


so that all the CPU's do the same walk (since my step_linear function is deterministic), I get the same timing. So it seems that the parallization does not work...

Also this program should work faster for a dist_prop>1. Since we put otherwise all points in the list, and then there is no need for a parallelization right? But my program gets slower the bigger the 'dist_prop' is.

So I thought that might be due to a wrong use of the @parallel command.

I hope someone can help me with the parallel problem!

edit retag close merge delete

## 1 answer

Sort by » oldest newest most voted

The default parallel strategy is using "fork", so the global dictionary D isn't actually shared between the different processes. You only get to communicate in one way: the result you pass back. If you want to do more, you'll have to look into shared memory, and all the locking issues that come with it (which can create lock contention that can seriously hamper the gains to be had by parallel execution).

It might seem that using threading instead of processes would solve this problem in a nicer way, but you'd probably find that Python's GIL gives similar (or worse!) lock contention.

more

## Comments

How do I use fork? Do I just change the @parallel to @fork(4)? And how could I use 'threading'? If you have the time it would be really helpfull if you could explain this is some more detail!! (or an example?) But in any case thank you for the answer!

the @fork does not seem to work either .. or I don't know how to implement it the right way.. Could you maybe explain a bit more about how to do this?

The default strategy that @parallel uses is by forking off several processes. So, if there is a @fork decorator (and if it's meant to fork off parallel workers) then you'd get the same behaviour as with @parallel.

How can I implement that? Neither works. If I comment out the @parallel, my program has the same speed as with the @parallel. Something is not working... Where would I need toput the @fork decorator? (I'm really not that good a programmer.. and I don't seem to understand the parallel logic sage uses.)

In my example, how can I realize that different CPUs are working on the same list looking for a match?

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2018-06-13 06:47:04 -0500

Seen: 161 times

Last updated: Jun 13 '18