Ask Your Question
1

How can I add a timeout to a charpoly calculation?

asked 2011-07-11 17:06:42 -0500

Max Flander gravatar image

For some reason after a bunch of matrix charpoly calculations, charpoly hangs on me. I suppose it's some memory problem, but rather that fix that, I just want to put a timeout on the calculation and then restart it if it hangs.

The problem is I can't use the "alarm" solution given in the answer to this question because sage.matrix.matrix_integer_dense.Matrix_integer_dense.charpoly doesn't catch the KeyboardInterrupt:

sage: M = random_matrix(ZZ,1000,1000)
sage: alarm(30)
sage: try:
....:     P = M.charpoly()
....: except KeyboardInterrupt:  
....:     print "did not complete!"
....:

What is another approach I could use?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2011-07-12 21:50:08 -0500

Max Flander gravatar image

updated 2011-07-13 03:56:41 -0500

OK here's my horrible workaround using rob hooft's task.py:

Write my matrix to a text-file. Put the charpoly calculation in a separate file called charpoly.sage which reads in the matrix from that text-file, and outputs its result to another text-file. Then in the main code run the following:

  from os import system
  load task.py
  maxtime = 3600
  success = 0 
  while success == 0:
     task = Task("sage charpoly.sage")
     task.Run()
     time = 0
     while time <= maxtime:
        sleep(1)
        time += 1
        if task.Done() == 1:
           success = 1
           break

     if time > maxtime:
        task.Kill()
        system("PIDS=$(ps -ef| grep -e 'python charpoly.py' | grep -v grep |awk '{print $2}')")
        system("kill -9 $PIDS")
        print ("timeout!")

That last rubbish is because sage spawns some other python processes which eat up all the cpu and are not killed by task.Kill(). It's not pretty but it'll do the trick for now. The reason that I simply restart the calculation after a timeout is that it only sporadically gets stuck, and restarting seems to get it going again, go figure...

edit flag offensive delete link more

Comments

Horrible is a matter of taste - at least you got it working!

parzan gravatar imageparzan ( 2011-07-13 06:44:25 -0500 )edit

that's true :)

Max Flander gravatar imageMax Flander ( 2011-07-13 15:51:34 -0500 )edit
0

answered 2011-07-11 23:00:43 -0500

parzan gravatar image
edit flag offensive delete link more

Comments

thanks for the suggestions, i can't get any of them to work though... for some reason charpoly ignores KeyboardException, SystemExit and so on, in fact sometimes when you do ctrl-c to stop it, you get a seg-fault... so maybe there's no easy answer?

Max Flander gravatar imageMax Flander ( 2011-07-12 05:09:14 -0500 )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-07-11 17:06:42 -0500

Seen: 306 times

Last updated: Jul 13 '11