MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to interrupt a running evaluation in MathLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17015] Re: How to interrupt a running evaluation in MathLink
  • From: tgayley at linkobjects.com (Todd Gayley)
  • Date: Sat, 10 Apr 1999 02:13:43 -0400
  • Organization: LinkObjects
  • References: <7e9olb$3ku@smc.vnet.net> <7ek3gp$n58$2@dragonfly.wolfram.com>
  • Sender: owner-wri-mathgroup at wolfram.com

This is a repost, as my original message was somehow truncated.


On 5 Apr 1999 03:27:39 -0400, rmille1 at aol.com (RMille1) wrote:

>I'm using Mathlink in my C program to start a Mathlink kernel and send this
>expression to be evaluated:
>
>N[ReplaceAll["Some expression",x->"some variable"]]
>
>Everything is working otay. However, occasionally an expression I send causes
>the kernel to evaluate for longer than I would like it to. So what I want to do
>is interrupt the evaluation and abort just as it would from a command line with
>Ctr-C and abort, but through MathLink. Here's the snippet of code that will
>call it:
>
>pkt = MLFlush(Link);
>interval = 0;
>while((!MLReady(Link))&&(interval<=10))
>{
>	interval++;
>	Sleep(500);
>}
>
>if(interval > 10)
>{
>dump = fopen("dump.txt","a");//test
>fprintf(dump,"\n/////////////////////Restarting
>Kernel///////////////////\n\n");//test
>fclose(dump);  //test
>
>***** Interrupt and Abort here. ************
>	
>	return 0;
>}
>
>I know there's a way to do this with MLAbort from the kernel, but not the other
>way. Also I didn't use Loopback or mprep, 

You can just do this:

     MLPutMessage(Link, MLAbortMessage);
     MLNextPacket(Link);
     // Now read the contents of this packet, which will be a
     // RETURNPKT. If the computation finished just before
     // the abort was recevied, it will be the result, otherwise
     // it will be $Aborted
     MLNewPacket(Link);

You should be aware that in some versions of Mathematica if the kernel
received an MLAbortMessage when it wasn't busy with a computation, it
would quit. This created a nasty race condition wherein if the kernel
finished right before you sent the abort, it would quit on you. This
appears to have been fixed in more recent versions (apparently
starting with 3.0), but I haven't yet taken the time to investigate
fully. As a result of this problem, the method that I got into the
habit of using is to send an MLInterruptMessage and handle the MENUPKT
that comes back. I know this works fine in all versions of
Mathematica.

     MLPutMessage(Link, MLInterruptMessage);
     pkt = MLNextPacket(Link);
     // The packet just received will be a MENUPKT if it results from
     // the attempt to interrupt. If the interrupt was received after
     // the computation finished, then the interrupt will be ignored,
     // and this is a RETURNPKT containing the result.
     if (pkt == MENUPKT) {
          // Send an "a" to abort the computation.
          MLPutString(Link, "a");
          // Now open the RETURNPKT containing $Aborted
          pkt = MLNextPacket(Link);
     }
     // Here, read the contents of the RETURNPKT, which will be either
     // the answer or $Aborted. 
     MLNewPacket(Link);

>One other thing. I found the MathLink Tutorial by Todd Gayley to be the best
>resource for learning how to use MathLink to start and run a Math kernel from
>C.

Now there's a good way to inspire me to answer your question.

>However, it seems a little dated. I'm using 
>
>char *argv[5] = {"-linkname",
>                 "c:\\progra~1\\wolfra~1\\mathem~1\\3.0\\math -mathlink",
>                      "-linkmode",
>                      "launch",
>                      NULL};
>with MLOpen(), and that doesn't jive with the documentation in Mathematica 3.0
>Is there a more up to date version available?

The MathLink Tutorial is 5 years old now, and yes it is becoming
dated, although it is still useful.

The old style you are using (MLOpen instead of MLOpenArgv, etc.) still
works, although you might want to update. A good example is the
factor.c MathLink sample program.

There is not a more up-to-date version of the MathLink Tutorial,
although I am writing a much more extensive treatment of MathLink
programming in the manual for my MathLink-Java project.


Todd Gayley
LinkObjects


  • Prev by Date: Re: Need help about how to bring image to clipboard?
  • Next by Date: Notebooks
  • Previous by thread: Re: How to interrupt a running evaluation in MathLink
  • Next by thread: Re: How to interrupt a running evaluation in MathLink