"Gabri Gwala" <email@example.com> wrote in message <firstname.lastname@example.org>... > "Oliver Woodford" wrote in message <email@example.com>... > > "Gabri Gwala" wrote: > > > Many thanks to Oliver Woodford and all the other contributors of this effective strategy. Anyway I would like to submit a potential issue when using Matlab Parallel Computation Toolbox. In particular i'm trying to use this scheme inside a parfor, obtaining a segmentation fault while trying to convert a matlab handle in C++ pointer. > > > Since this is not a pure Matlab topic, I posted the detailed question on stackoverflow. > > > > > > http://stackoverflow.com/questions/14607109/matlab-c-segmentation-fault-on-parallel-computing-with-c-mex-persistent-obj > > > > > > Regards, > > > Gabriele Gualandi > > > > If all your workers are threads on the same computer then you might be able to solve the problem by making the class instance and all the memory allocated by it in shared memory, e.g. by using the approach described here: > > http://www.boost.org/doc/libs/1_51_0/doc/html/interprocess/sharedmemorybetweenprocesses.html > > > > If the workers are on different computers, then, as Steve points out, this won't work. > > As Edric suggested on StackOverflow, the solution to my previous problem was simply that I had to perform a deep copy at C++ side and transfer all the data thru numeric arrays at Matlab side. Adopting a deep copy, this framework really works good also in parallel computing. > > However there is something unclear to me: > > 1) > I see that at every invocation of a function on the interface, a new instance of class CLASS_HANDLE is created (since is called convertPtr2Mat). This class offers also a destroyer, but I cannot see where it is called. > > 2) > Every time an instance of CLASS_HANDLE is created, its constructor calls mexLock(): Is this really necessary, since the whole code is compiled as a single MEX-function? > > 3) > The usage of the wrapping class CLASS_HANDLE is necessary for the interface to work, or it only provides usefull auxilary methods, for example the check !ptr->isValid()? > > Thank you!!
Sorry, I have to correct my previous sentence #2: mexLock() is not called from the constructor of CLASS_HANDLE, but just before creating a new instance of CLASS_HANDLE. However what I want to highlight is that this happens every time a function is invoked on the interface.