The Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Making C++ objects persistent between mex calls, and robust.
Replies: 38   Last Post: Aug 5, 2017 8:06 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Raunak Borker

Posts: 1
Registered: 8/5/17
Re: Making C++ objects persistent between mex calls, and robust.
Posted: Aug 5, 2017 8:06 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Oliver Woodford" wrote in message <hul1u8$s3c$1@fred.mathworks.com>...
> "Anthony Halley" wrote:
> > Did you ever find a nice solution for this issue? If so, I'd be interested in hearing about it.
>
> Yes. Walter Roberson pointed out the properties of the handle class to me, including it's ability to call a clean up function, here:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/283309
>
> So the solution is to define a class which inherits the handle class, something like this:
>
> classdef myclass < handle
> properties (Hidden = true, SetAccess = private)
> cpp_handle;
> end
> methods
> % Constructor
> function this = myclass()
> this.cpp_handle = init_mex();
> end
> % Destructor
> function delete(this)
> clear_mex(this.cpp_handle);
> end
> % Example method
> function output = action(this, data)
> output = action_mex(this.cpp_handle, data);
> end
> end
> end
>
> I was going to post this when I'd used and tested it, but your question prompted me to post it earlier. As a result, it may not be exactly right, but hopefully you get the gist.
>
> Oliver


I have found this approach useful. Thanks for the code.

Below is the issue I am facing.

I have created a structure in my mex code and I am outputting it to MATLAB by creating a pointer to it and then casting it as a uint64_t. I am able to pass this into another mex function and decode the data perfectly. All this is as suggested in this thread and it works.

When I try to do the same thing but now within a parfor loop MATLAB crashes with a message saying "a worker aborted during execution ... ". The psuedo code is written below:

---------------------
outputhandle = create_object_handle(...);

parfor 1:n
[] = some_other_mex_function(outputhandle,...);
end
---------------------

I know I am passing the same pointer to multiple cores which share the memory, and all of them might be trying to access the data at the same time. Could this be the issue? If so any suggestions to overcome this would be helpful.

Thanks


Date Subject Author
4/1/10
Read Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/7/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Walter Roberson
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/22/11
Read Re: Making C++ objects persistent between mex calls, and robust.
newuser asdf
1/22/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Bruno Luong
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Rune Allnor
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Bruno Luong
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
newuser asdf
1/24/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/15/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
7/6/11
Read Re: Making C++ objects persistent between mex calls, and robust.
mathias
7/6/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
5/24/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Ajay
5/25/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
10/2/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
11/7/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Francesco Montorsi
11/8/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
11/9/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/21/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/30/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
1/30/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Steven Lord
1/31/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
9/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
9/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
9/27/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
8/5/17
Read Re: Making C++ objects persistent between mex calls, and robust.
Raunak Borker

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.