multithreading 如何在 MATLAB 中进行线程处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2713218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do threading in MATLAB?
提问by hai
How to do threading in MATLAB? I want to run one function on two variables simultaneously. How do I do it?
如何在 MATLAB 中进行线程处理?我想同时在两个变量上运行一个函数。我该怎么做?
回答by Adrien
The parallel toolbox has some tools that might help you. Find below some example pasted from the Matlab help
并行工具箱中有一些可能对您有所帮助的工具。在下面找到一些从 Matlab 帮助中粘贴的示例
matlabpool % Use default parallel configuration
spmd % By default uses all labs in the pool
INP = load(['somedatafile' num2str(labindex) '.mat']);
RES = somefun(INP);
end
Then the values of RES
on the labs are accessible from the client as RES{1}
from lab 1, RES{2}
from lab 2, etc.
然后RES
可以从客户端访问实验室上的值,如RES{1}
实验室 1、RES{2}
实验室 2 等。
You might also look at parfor
as a simple parallel replacement of for
. Hope this helps even if it's not exactly what you're looking for.
您也可以将其parfor
视为for
. 希望这会有所帮助,即使它不是您正在寻找的内容。
回答by CrimsonX
I do not believe there is any built in multithreading support from MATLAB. This comes from both a conversation I had with a coworker recently and a quick google search
我不相信 MATLAB 有任何内置的多线程支持。这来自我最近与一位同事的一次谈话和一个快速的谷歌搜索
Hope this helps.
希望这可以帮助。
回答by deraltefritz
You may do it with MEX files and std::thread
(see here).
您可以使用 MEX 文件和std::thread
(请参阅此处)来执行此操作。
I have not tried to call mexEvalString
from the MEX file, and quite possibly it will result in a runtime error or freeze MATLAB for the time of execution. But if you can write that specific piece of code in C++, it may be what you're looking for.
我没有尝试mexEvalString
从 MEX 文件中调用,很可能会导致运行时错误或在执行时冻结 MATLAB。但是,如果您可以用 C++ 编写那段特定的代码,那么它可能就是您要找的。