簡體   English   中英

使用 python 並行運行 n 個 MATLAB 實例

[英]Running n MATLAB instances using python in parallel

我想在 MATLAB 上運行一些測試,這通常需要 2 天,我有 3 個這樣的測試(所以 3 x 2 = 6 天)。 因此,我在 windows 機器上運行了三個 MATLAB 會話並運行了三個測試(並行),這將我的測試時間從 6 天減少到 2 天。

我想在 python 上做類似的事情來調用三個 MATLAB 實例。(我可以串行執行,但不能並行執行)

import matlab.engine as MAT_E
eng=MAT_E.start_matlab()

test_id=1
isTestDone = eng.runTest1(test_id,nargout=1)   # runTest1 is a .m file which needs to be run

test_id=2
isTestDone = eng.runTest2(test_id,nargout=1)   # runTest2 is a .m file which needs to be run

test_id=3
isTestDone = eng.runTest3(test_id,nargout=1)   # runTest3 is a .m file which needs to be run

有誰知道我可以如何並行做類似的事情?

如果您有任何問題/建議/意見,請告訴我?

編輯/添加了 runTest1 骨架

function out1 = runTest1(test_id)

% some processing happens and variable 'x' is generated 

if x < 0.1
    % some warning
    warning('the samples are incosistent')
    keyboard;
end

if x > 99
    error('simulation encountered some out of bound values')
end


# some more processing 

end

start_matlab function 的 MATLAB 文檔在這里說:

每次調用 matlab.engine.start_matlab 時,都會啟動一個新的 MATLAB 進程。

因此,為每個測試啟動一個新的 MATLAB 進程,並運行它們。 我們還從此處的文檔中發現,我們需要在運行函數時使用background=True參數,以便 Python 可以調用所有 3 個測試而無需等待它們完成。

import matlab.engine as MAT_E
eng1 = MAT_E.start_matlab()
eng2 = MAT_E.start_matlab()
eng3 = MAT_E.start_matlab()

# start running the tests
test1_future = eng1.runTest1(1,nargout=1,background=True)
test2_future = eng2.runTest2(2,nargout=1,background=True)
test3_future = eng3.runTest3(3,nargout=1,background=True)

# get the results of all the tests (waits for tests to finish)
result1 = test1_future.result()
result2 = test2_future.result()
result3 = test3_future.result()

# do something with the results...

如果您的數量超過 3 個,則可能值得使用循環來執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM