Using 'try' and 'alarm' to time limit computations

356 views
Skip to first unread message

Andrew Chan

unread,
May 15, 2012, 6:50:12 AM5/15/12
to Macaulay2
I am trying to get M2 to skip certain computations if they take too
long, and judging from previous discussions on here the solution is to
use 'alarm' in conjunction with 'try'.

I do not understand how to implement this properly. As an example, I
tried using the example from this thread:
http://groups.google.com/group/macaulay2/browse_thread/thread/e8172d7d8230db2a/42bcbf73789e8bbd?#42bcbf73789e8bbd

using the code

for i in L do (
try (alarm 1; print (i, i!!))
else print (i, "too long")
)

which for L= {2,3,22,4}

gives the desired output. However if I input

L={2,3,12,4}

I get stuck on computing 12!! longer than the 1 second that it should
take for the alarm to kick in.

I would appreciate any advice on what I'm doing wrong, or alternative
way of achieving the same goal.

Thanks!

Grayson, Daniel R.

unread,
May 15, 2012, 1:57:00 PM5/15/12
to maca...@googlegroups.com
This is a problem with the way exceptions are handled in Macaulay2:
when an alarm sounds, our interrupt handler routine sets a global
variable and expects the code to check the value periodically to see
whether it's time to stop a computation. But the factorial function
is implemented in a third-party subroutine library, gmp or mpir, which doesn't check
Macaulay2's stopping variable.

It occurs to me now that if we were to switch to compiling the interpreter
in C++ instead of C we should be able to switch to using C++ exceptions
to handle that. (When we started the project, we shied
away from using C++ exceptions.)

On the other hand, we are developing the ability to run Macaulay2 simultaneously
in separate threads. When an alarm sounds, which thread should stop? I don't
know.
> --
> You received this message because you are subscribed to the Google Groups "Macaulay2" group.
> To post to this group, send email to maca...@googlegroups.com.
> To unsubscribe from this group, send email to macaulay2+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/macaulay2?hl=en.
>

Daniel R. Grayson

unread,
May 15, 2012, 5:38:25 PM5/15/12
to maca...@googlegroups.com


On Tuesday, May 15, 2012 12:57:00 PM UTC-5, Daniel R. Grayson wrote:
It occurs to me now that if we were to switch to compiling the interpreter
in C++ instead of C we should be able to switch to using C++ exceptions
to handle that.  (When we started the project, we shied
away from using C++ exceptions.)


Bill Furnish has convinced me that can't work, too bad.
 

kroeker

unread,
May 16, 2012, 5:40:04 AM5/16/12
to maca...@googlegroups.com
Hello,


> limit execution time of computations

I think in principle it should be possible to limit time of computations
using threads
( thread support is to my knowledge currently only available in
Macaulay trunk )

In practice, 'cancelTask' seems not to interrupt/kill a task as
expected(?) , thus some investigation is required.

Example:
---------------------------
fkt := ()->(sleep 1000; return 1;)
currTask:=createTask(fkt);
schedule(currTask);
-- now start a timer and cancel currTask if timer is expired
--....
cancelTask(currTask); --does not kill the thread with running 'currTask'...
---------------------------


Best,


Jakob

Andrew Chan

unread,
May 16, 2012, 9:59:49 AM5/16/12
to Macaulay2
Thanks for the replies.

Dan, do I understand correctly that the problem is with the double
factorials? In my package, I am trying to time out certain Groebner
basis computations (in place of the double factorials from the
example) where they take to long. Should this work or would I
experience the same problems?

On May 15, 6:57 pm, "Grayson, Daniel R." <d...@math.uiuc.edu> wrote:
> This is a problem with the way exceptions are handled in Macaulay2:
> when an alarm sounds, our interrupt handler routine sets a global
> variable and expects the code to check the value periodically to see
> whether it's time to stop a computation.  But the factorial function
> is implemented in a third-party subroutine library, gmp or mpir, which doesn't check
> Macaulay2's stopping variable.
>
> It occurs to me now that if we were to switch to compiling the interpreter
> in C++ instead of C we should be able to switch to using C++ exceptions
> to handle that.  (When we started the project, we shied
> away from using C++ exceptions.)
>
> On the other hand, we are developing the ability to run Macaulay2 simultaneously
> in separate threads.  When an alarm sounds, which thread should stop?  I don't
> know.
>
> On May 15, 2012, at 5:50 AM, Andrew Chan wrote:
>
>
>
>
>
>
>
> > I am trying to get M2 to skip certain computations if they take too
> > long, and judging from previous discussions on here the solution is to
> > use 'alarm' in conjunction with 'try'.
>
> > I do not understand how to implement this properly. As an example, I
> > tried using the example from this thread:
> >http://groups.google.com/group/macaulay2/browse_thread/thread/e8172d7...

Grayson, Daniel R.

unread,
May 16, 2012, 10:02:20 PM5/16/12
to maca...@googlegroups.com

On May 16, 2012, at 8:59 AM, Andrew Chan wrote:

> Thanks for the replies.
>
> Dan, do I understand correctly that the problem is with the double
> factorials? In my package, I am trying to time out certain Groebner
> basis computations (in place of the double factorials from the
> example) where they take to long. Should this work or would I
> experience the same problems?

Groebner basis computations should be interruptable -- if not, we
can fix it. M2 has no double factorials, but yes, if you compute
a factorial, it will not be interruptable.
Reply all
Reply to author
Forward
0 new messages