Ask Your Question
1

Creating plots in parallel

asked 2011-08-13 01:11:09 +0100

benjaminfjones gravatar image

Ok, even though my last question about @parallel was foolish, here is another one. I want to create a simple animation that consists of many plots. It takes a minute or two on my computer to produce. But it would be nice if I could harness the idle CPU's I have and create the frames for the animation in parellel. I thought this would be easy like this:

x = var('x')    
FRAMES = 10

@parallel
def frame(i):
    return plot(sin(i*x), (x, 0, pi))

P = [ p[1] for p in frame(range(1,FRAMES+1)) ]
#A = animate(blah, blah, ...)

and what I get back (in the Sage notebook) is:

invalid load key, 'x'.
Killing any remaining workers...

Any idea what is going wrong here?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2011-08-15 09:19:20 +0100

niles gravatar image

Ok, I couldn't resist looking into this more. I'm using sage 4.7, and the code I posted above works for me even without ticket 11461 applied. Strangely, I get the error "invalid load key, 'x'" only when I return the graphics object. Moreover, I get that same error (with x) even when I change the variable name to t. So I think this is a bug. I wonder if it has something to do with passing the bare graphics object between separate sage processes. There seems to be no error if I return something containing the graphics object:

FRAMES = 10

@parallel
def frame(i):
    t = var('t')
    g = plot(sin(i*t), (t,0, pi))
    return (g,)

frame_iter = frame(range(2,FRAMES+1))
P = list(frame_iter)
Q = [p[1][0] for p in P]
animate(Q)
Animation with 9 frames

p.s. For longer animations, I really like ffmpeg. An option to use it with the animate command should be coming in sage 4.7.1 (see ticket 11170 if you can't wait).


UPDATE: Searching for "invalid load key" with search_src yields only one use in the entire Sage library, in a doctest for load in structure/sage_object.pyx.

edit flag offensive delete link more

Comments

Yeah, I was thinking that `invalid load key` has something to do with the inputs / outputs being pickled and then unpickled so the error is coming from Python..

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-16 14:25:50 +0100 )edit
1

answered 2011-08-13 11:20:10 +0100

niles gravatar image

No, no idea what's wrong, but the following does work:

x = var('x')
FRAMES = 10

@parallel
def frame(i):
    g = plot(sin(i*x), (x, 0, pi))
    g.show()

P = [ p[1] for p in frame(range(1,FRAMES+1)) ]

p.s. parallel is awesome, but the documentation is sparse, and confusing, so keep the questions coming :) If you haven't found it yet, Trac ticket 11461 implements parallel for classes; I've been using it for a long animation project, and it works great!

edit flag offensive delete link more

Comments

Is there any documentation except '@parallel?' default help?

Eugene gravatar imageEugene ( 2011-08-13 16:23:11 +0100 )edit

Well, there are the other docstrings in the source code, if that counts . . . http://hg.sagemath.org/sage-main/file/ce324e28c333/sage/parallel/decorate.py

niles gravatar imageniles ( 2011-08-13 16:47:58 +0100 )edit

That doesn't work for me, the notebook displays a 10 identical plots (not sure why they're identical) and after it's done, `P = [None, None, None, None, None, None, None, None, None, None]`.

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-14 17:07:57 +0100 )edit

bummer . . . the version of sage I'm using does have ticket 11461 applied to it, so that might be why it's working for me. The result you're getting for P is because `frame` doesn't have a return value anymore. Could you try running it with `g.save('~/tmp_%03d.png'%i)`, and see if the saved files are also all the same? For this, you should be able to just run `frame(range(1,FRAMES+1))` too, without the `P`

niles gravatar imageniles ( 2011-08-14 20:44:42 +0100 )edit

Thanks, I've got the plots written to image files (had to use p.save(os.path.expanduser('~/tmp/tmp_%03d.png'%i)) to get the tilde to resolve to a directory), now I need to assemble them into an animation. To use `animate` I guess I need to read the files back into Graphics objects. Or else use some other software to create the animation. I wish I could get Graphics objects created in parallel. I'll have to look into the @parallel code and try to see what's going on.

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-15 00:40:29 +0100 )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-13 01:11:09 +0100

Seen: 638 times

Last updated: Aug 15 '11