Killing a MEX function with Ctrl+C

221 views
Skip to first unread message

Ramón Casero Cañas

unread,
Sep 12, 2012, 6:56:03 PM9/12/12
to Gerardus Users Mailing List, wota...@rice.edu

Hi all,

In Matlab, if a function is running and you press the keys Ctrl+C, the
function stops its execution and exits with an error. This is perfectly
fine, desirable behaviour.

With C++ MEX functions, this doesn't work by default, which is a real
pain when you have e.g. a loop with lots of iterations that will take
for a very long time to complete. If one launches the MEX function, and
realises a parameter is wrong, the only way to stop execution is by
forcibly killing Matlab itself and restarting everything again. (Or you
have to wait until the function finishes).

Prof. Wotao Yin has kindly written a short tutorial where he explains
that there's an undocumented library in Matlab with a function
utIsInterruptPending() that captures the Ctrl+C signal

http://www.caam.rice.edu/~wy1/links/mex_ctrl_c_trick/

I have added a function to Gerardus that takes advantage of this
feature. The function is called ctrlcCheckPoint()

http://code.google.com/p/gerardus/source/browse/trunk/matlab/GerardusCommon.hpp#67?r=953

and can be included in your C++ code like this:

for (size_t i=0; i<1e6; ++i) {

// exit if user pressed Ctrl+C
ctrlcCheckPoint(__FILE__, __LINE__);

... all your loop code here ...

}

This way, at the beginning of each loop iteration, your program will
check whether the user has pressed Ctrl+C. If so, it produces an error
message like this in Matlab, and exits:

>> Operation terminated by user during cgal_insurftri (line 336)

In addition, the error message contains two hyperlinks:

* Clicking on "cgal_insurftri" will display the help for that function.

* Clicking on "line 336" will open the source file
CgalInSurfaceTriangulation.cpp at line 336 (where ctrlcCheckPoint() was
called from)

A practical example can be found in the code for the MEX function
cgal_insurftri(), where I have added check points in three loops:

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#230?r=953

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#335?r=953

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#369?r=953


Cheers,

Ramon.

--
Dr. Ram�n Casero Ca�as

Oxford e-Research Centre (OeRC)
University of Oxford
7 Keble Rd
Oxford OX1 3QG

tlf +44 (0) 1865 610739
web http://www.cs.ox.ac.uk/people/Ramon.CaseroCanas
photos http://www.flickr.com/photos/rcasero/

Ramón Casero Cañas

unread,
Sep 12, 2012, 7:02:18 PM9/12/12
to Gerardus Users Mailing List, wota...@rice.edu
On 12/09/12 23:56, Ram�n Casero Ca�as wrote:
>
> A practical example can be found in the code for the MEX function
> cgal_insurftri(), where I have added check points in three loops:

Sorry, the links were wrong. These ones should work:

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#230

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#335

http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#369

Likewise for the ctrlcCheckPoint() function:

http://code.google.com/p/gerardus/source/browse/trunk/matlab/GerardusCommon.hpp?r=953#67

Wotao Yin

unread,
Sep 13, 2012, 2:08:05 AM9/13/12
to Ramón Casero Cañas, Gerardus Users Mailing List
Dear Ramon,

Thanks a lot for your generous contribution. I have added your post to
my Ctrl-C page.

Wotao

On Wed, Sep 12, 2012 at 6:02 PM, Ramón Casero Cañas <rca...@gmail.com> wrote:
> On 12/09/12 23:56, Ramón Casero Cañas wrote:
>>
>>
>> A practical example can be found in the code for the MEX function
>> cgal_insurftri(), where I have added check points in three loops:
>
>
> Sorry, the links were wrong. These ones should work:
>
> http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#230
>
> http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#335
>
> http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp?r=953#369
>
> Likewise for the ctrlcCheckPoint() function:
>
> http://code.google.com/p/gerardus/source/browse/trunk/matlab/GerardusCommon.hpp?r=953#67
>
>
> Cheers,
>
> Ramon.
>
> --
> Dr. Ramón Casero Cañas

Ramón Casero Cañas

unread,
Sep 13, 2012, 6:22:22 AM9/13/12
to Wotao Yin, Gerardus Users Mailing List
On 13/09/12 07:08, Wotao Yin wrote:
> Dear Ramon,
>
> Thanks a lot for your generous contribution. I have added your post to
> my Ctrl-C page.

Dear Wotao,

Thanks for adding a link, although I think you used the wrong one I sent
first. It should be:
Dr. Ram�n Casero Ca�as

Ramón Casero Cañas

unread,
Sep 29, 2012, 2:28:56 PM9/29/12
to Gerardus Users Mailing List
I have made some subtle changes to ctrlcCheckPoint(), the checkpoint
that allowed to kill MEX functions with CTRL+C .

http://code.google.com/p/gerardus/source/browse/trunk/matlab/GerardusCommon.h?r=983

Now, instead of being a function part of a gerardusCommon library,
it's a C++ macro that gets directly included in the MEX file.

This speeds things up considerably. For instance, bwregiongrow() got
>5 times slower after adding the checkpoints the old way, while it's
only 15% slower with the new checkpoint implementation).

The reason seems that if the checkpoint has to make a function call to
then check the CTRL+C flag at every iteration of a loop, it's much
slower than sklipping the function call, and directly checking the
flag.

Cheers,

Ramon.

--
Dr. Ramón Casero Cañas

gideon....@gmail.com

unread,
Apr 29, 2013, 9:43:11 PM4/29/13
to gerardu...@googlegroups.com, wota...@rice.edu
Question on this.  If, upon detecting an interrupt, I throw mexErrMsgTxt("mesg"), which terminates the mex function, do I have to manually de-allocate any memory I have manually allocated in the mex function, or is that automatically handled?
Dr. Ram�n Casero Ca�as

Ramón Casero Cañas

unread,
Apr 30, 2013, 5:43:03 AM4/30/13
to Gerardus Users Mailing List
Hi Gideon,

I'm pretty sure all memory you have allocated with mex functions will be deallocated. If you use something like malloc, I don't know. But it's easy to test, simply start your system monitor to see how much memory you have available, run your MEX function that allocates a large chunck of memory and then throws a mexErrMsgTxt("mesg"), and see what happens to the memory.

If you can report it back to the list, that'd be great, thanks!

Best regards,

Ramon.



--
--
To post: gerardu...@googlegroups.com
To unsubscribe: gerardus-user...@googlegroups.com
More options: http://groups.google.com/group/gerardus-users?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "gerardus-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gerardus-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Dr. Ramón Casero Cañas

Reply all
Reply to author
Forward
0 new messages