Calling matlab::en​gine::MATL​ABEngine::​feval from multiple threads

2 次查看(过去 30 天)
I just discovered the new mex C++ API and immediately fell in love with it! But what's about thread safety of the matlab::engine::MATLABEngine::feval call in particular and the whole API in general? The docs don't say anything regarding this topic.
I have an algorithm that filters a lot of signals, which could gain a lot from parallelization. So ideally I'd like to create as much std::threads as there are independent signals and process them all in parallel with the MATLAB engine. Is that possible? I assume that the interpreter invoked by feval will be probably single-threaded, however maybe the execution of the function after parsing is capable of multithreading? Or should I go on and perform multithreaded filtering completely in the C++ domain using my own filter classes and go back to the MATLAB interface after having joined all threads?
What about the Data API? Is it safe to access (readonly) a matlab::data::Array from multiple threads in parallel?
I'm looking forwards to your answers!

回答(1 个)

Sangeetha Jayaprakash
The matlab::engine::MATLABEngine::fevalAsync function is thread safe i.e. if there are multiple threads in the C++ application, all of them can access the "fevalAsync" function.
Please note that this still means that only one of those requests(from any one of the threads) will be actually run on the MATLAB engine instance at any given time.
  2 个评论
Janos Buttgereit
Janos Buttgereit 2018-5-28
Thank you for that answer. I suspected that the Matlab engine executes request sequentially, so I see that although it will be safe to let multiple threads access the engine, it won't give any performance gain from parallelization.
However, you mentioned fevalAsync while I asked for feval. Does this mean only fevalAsync will be thread-safe? Is there an overview of which C++ Api calls are thread safe or should I just assume none of them to be thread safe?
Arwel
Arwel 2021-4-8
Hi Sangeetha - is it in principle therefore possible to have multiple Matlab engine instances, each of which is being accessed from a different threads in the c++ application? So if there are 4 threads, have 4 engines, and each of those dealing with feval from the different threads? If so, do you have an example of this?
Arwel

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Enterprise Deployment with MATLAB Production Server 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by