Ask Your Question
2

Why does @parallel change my outputs?

asked 2011-08-12 05:46:40 -0600

benjaminfjones gravatar image

updated 2013-06-03 04:17:14 -0600

tmonteil gravatar image

Can someone explain this behavior to me:

sage: @parallel
....: def foo(n):
....:         return str(factor(n))
....:     
sage: foo(10)
'2 * 5'
sage: for x in foo([1..10]):
....:         print x[0][0][0]
....: 
1
2
3
4
5
6
7
8
9
10

I expected to get the factorizations of 1 .. 10, not the numbers 1 .. 10. It seems like the output of foo() is getting evaluated somehow when it goes through the @parallel decorator. Is this a bug?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2011-08-12 06:45:37 -0600

niles gravatar image

x[0] holds the arguments to foo, x[1] holds the output :) Try

sage: @parallel
....: def foo(n):
....:         return str(factor(n))
....:     
sage: foo(10)
'2 * 5'
sage: for x in foo([1..10]):
....:         print x[0][0][0],'->', x[1]
....: 
2 -> 2
1 -> 1
3 -> 3
4 -> 2^2
5 -> 5
6 -> 2 * 3
7 -> 7
8 -> 2^3
9 -> 3^2
10 -> 2 * 5
edit flag offensive delete link more

Comments

Of course! Thanks, that was a dumb question.

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-12 06:52:29 -0600 )edit

Ha! No worries :)

niles gravatar imageniles ( 2011-08-12 07:00:49 -0600 )edit

I totally missed this when I was looking at it the other day - good thing Niles is on the job :)

kcrisman gravatar imagekcrisman ( 2011-08-13 14:05:08 -0600 )edit

Sorry, why are you printing x[0][0][0], why doesn't x[0] suffice?

StevenPollack gravatar imageStevenPollack ( 2011-08-13 14:59:33 -0600 )edit

As @niles says, x[0] contains the inputs to the parallel function, but the decorator puts them in a normal form which involves a triple nested tuple. That's what I was getting inside of with x[0][0][0].

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-13 16:00:49 -0600 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-08-12 05:46:40 -0600

Seen: 148 times

Last updated: Aug 12 '11