简体   繁体   中英

Running n MATLAB instances using python in parallel

I would like to run some tests on MATLAB which usually takes 2 days and I have 3 such tests( so 3 x 2 = 6 days ). So, I run three MATLAB sessions on my windows machine and run my three tests (in parallel) which reduces my test time from 6 days to 2 days.

I would like to do similar stuff on python to invoke three MATLAB instances.(I can do that serially, but not parallely)

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

Does anyone know how I can do similar stuff in parallel?

Please let me know if you have any questions/suggestions/comments?

EDITED/Added the runTest1 skeleton

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

The MATLAB documentation for the start_matlab function here says:

Each time you call matlab.engine.start_matlab, it starts a new MATLAB process.

So, start a new MATLAB process for each test, and run them all. We also find from the documentation here that we need to use the background=True argument when running functions so that Python can call all 3 tests without waiting for them to finish.

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...

If you had a lot more than 3 it would probably be worth doing this with a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM