Ask Your Question
2

Why does @parallel change my outputs?

asked 13 years ago

benjaminfjones gravatar image

updated 11 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 13 years ago

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
Preview: (hide)
link

Comments

Of course! Thanks, that was a dumb question.

benjaminfjones gravatar imagebenjaminfjones ( 13 years ago )

Ha! No worries :)

niles gravatar imageniles ( 13 years ago )

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

kcrisman gravatar imagekcrisman ( 13 years ago )

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

StevenPollack gravatar imageStevenPollack ( 13 years ago )

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 ( 13 years ago )

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: 13 years ago

Seen: 568 times

Last updated: Aug 12 '11