[GAP Forum] control the execution time of a function

Alexander Konovalov alexander.konovalov at gmail.com
Wed Jan 26 14:33:56 GMT 2011


Dear Georges,

On 25 Jan 2011, at 08:59, bolbita wrote:

> Dear GAP Forum,
> 
> I have little experience with GAP, and I wonder how can we control the execution time of a function.
> I dream about a function like
> UnderTimeControl := function (f, arg, maxtime)
> which would run the function f with arguments arg and a timer in parallel,
> and would return the result of f(arg) whenever its execution time is less than maxtime and would return fail 
> otherwise.

The current version of GAP is single-threaded so it's not directly possible to have exactly the scenario 
you've described.However, there are several possible workarounds of various difficulty and flexibility:

1) If you are able to use the GAP package IO by Max Neunhöffer 
( http://www.gap-system.org/Packages/io.html ), then the following 
function suggested by Laurent Bartholdi works exactly as you've 
described:

UnderTimeControl := function(f,args,maxtime)
   local pipe, pid, result;
   pipe := IO_pipe();
   pid := IO_fork();
   if pid=0 then
       pipe := IO_WrapFD(pipe.towrite,false,false);
       IO_Pickle(pipe,CallFuncList(f,args));
       IO_Close(pipe);
       IO_kill(IO_getpid(),9);
   fi;
   pipe := IO_WrapFD(pipe.toread,false,false);
   result := IO_Select([pipe],[],[],[],QuoInt(maxtime,1000000),RemInt(maxtime,1000000));
   if result=fail or result=0 then
       IO_kill(pid,9);
       return fail;
   fi;
   result := IO_Unpickle(pipe);
   IO_Close(pipe);
   return result;
end;

gap> UnderTimeControl(Factorial,[10],100);  
fail
gap> UnderTimeControl(Factorial,[10],10000);
3628800
gap> UnderTimeControl(NrConjugacyClasses,[SmallGroup(512,13)],100000);
fail
gap> UnderTimeControl(NrConjugacyClasses,[SmallGroup(512,13)],1000000);
92

However, note that:
- it forks another process, which may be potentially memory expensive;
- IO_Pickle/IO_Unpickle may not support arbitrary data to pass them as 
  arguments or results;
- if things go wrong, i.e. an Error occurs, the user interface will be
  completely broken and you will have to restart GAP.

Some other alternatives:

2) The function Runtime (see ?Runtime) returns the time spent by GAP in milliseconds as an integer. 
You may insert calls to it at some checkpoints in the code to analyse its result and take an action.
Of course, it will not help if GAP spends too much time in some function call prior to that. 

3) It's possible to call a remote procedure on the GAP server from the GAP client using the SCSCP package 
( http://www.cs.st-andrews.ac.uk/~alexk/scscp.htm ) and specify the timeout (i.e. maxtime). This can be
also done in parallel. 

4) It's also possible to submit GAP jobs using various job submission systems (pbs, Condor system) and 
specify the maximum duration of the job, but this is non-interactive. 

Please do not hesitate to ask more details about these scenarios. However, it might be rather worth to 
tell more details about the actual computation which you would like to do in this way - it may happen 
that there is a better way of doing it in GAP without caring about a runtime limit.

Best regards,
Alexander ( with the help of Laurent Bartholdi )


More information about the Forum mailing list