Calling matlab mex from parfeval

I have an existing matlab mex program that I want to alter to run from a thread. The matlab mex works properly when called from the main thread (command line) in MATLAB. I implemented the following
pool = backgroundPool();
q = parallel.pool.DataQueue();
for (ii=1:4000)
if (ii==1000)
parfeval(pool,@mex_thread,1,my_struct,q);
afterEach(q, @disp); % Need this to print messages
end
pause(0.001); % thread does not run until main script stops without this
end
function [retval] = mex_thread(my_struct,q)
send(q,sprintf('Got to point 1\n'));
for (idx=1:100)
send(q,sprintf('Got to point 2\n'));
my_mex_program(my_struct);
send(q,sprintf('Got to point 3\n'));
end
end
I simplified the mex program to this:
class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
return;
}
};
But I only get this on the console, indicating that the mex did not return
Got to point 1
Got to point 2
How do I go about determining what is wrong? Is it legal to call mex from a background thread?

Réponses (1)

Christopher Mirfin
Christopher Mirfin le 9 Sep 2025
Modifié(e) : Christopher Mirfin le 9 Sep 2025

1 vote

Unfortunately, calling MEX from a thread-based pool (in this case backgroundPool) is not currently supported. So for now, you must use parpool("Processes") to create a process-based pool if you have access to Parallel Computing Toolbox.
As a note, errors associated with parfeval are captured on the return output object, a parallel Future, i.e.
future = parfeval(pool,@mex_thread,1,my_struct,q);
Using fetchOutputs on this future will wait for the work to complete and throw any associated errors. Or, if you want to report the error asynchronously, use a continuation afterEach with the PassFuture=true option.

1 commentaire

Mike
Mike le 10 Sep 2025
Modifié(e) : Mike le 10 Sep 2025
Thanks. After some debugging I mostly have it working.
When called directly, printf can be used in matlab mex. printf does not work from matlab mex that is called from a matlab process.
What can be done? The code above shows how I implemented this in a matlab script, using 'q', 'afterEach', and 'send', but it is not clear how to translate that into cpp code in the mex source.
Also this update: I noticed that mex program writes to my_struct have no effect on the top level instance of my_struct. So I switched my_struct to be global. mex_thread declares the access to be global, but passes the structure to the mex code. mex_thread can alter my_struct and those modifications show up in the top level. However, any write to my_struct fields in the mex coded causes the code to hang.
My cpp code to write my_struct is
extern "C" void wr_field_mex(const char * regName, int wval)
{
ArrayFactory factory;
const TypedArray<double>lval = factory.createScalar<double>(wval);
matlabPtr->setProperty(*obj, regName, lval);
}
Which is invoked with this c code
wr_field_mex("my_field",wr_val)
In matlab my_struct is defined like this
classdef get_fields < handle
properties
my_field = 0;
... % many more fields
end
methods
function rval = get.my_field(obj)
rval = obj.my_field;
end
function set.my_field(obj, val)
obj.my_field = val;
end
... % many more access methods
end
end
Is it possible to get some debug information out of this or help with what is going wrong?

Connectez-vous pour commenter.

Catégories

Produits

Version

R2022b

Question posée :

le 8 Sep 2025

Modifié(e) :

le 10 Sep 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by