system command in parfor loop

I am invoking a C++ executable from the Matlab command prompt.
Interactively, it goes like this:
>> [status,cmdout] = system('multi.exe','-echo')
Input unique file identifier. Ex: 2~04-13-013939 - 2~11-30-091444
And this works fine.
However, I need to invoke the C++ executable multiple times. So I created a parfor loop, like so:
parpool
tic
parfor i=1:nfiles
[status,cmdout] = system('multi.exe','-echo')
str3{i,1}
end
where str3 is a series of inputs:
str3 =
14×1 cell array
{'1~11-24-054646'}
{'1~11-24-095017'}
{'1~11-29-060908'}
{'1~11-29-101743'}
{'1~11-30-091225'}
{'2~11-24-055554'}
{'2~11-24-100903'}
{'2~11-29-060541'}
{'2~11-29-101814'}
{'2~11-30-091444'}
{'3~11-24-055933'}
{'3~11-29-101847'}
{'4~11-24-060505'}
{'4~11-29-101935'}
This ran for over 54 hours, but the task never completed, and I eventually hit Ctrl-C.
Any thoughts/ feedback?

댓글 수: 2

Mario Malic
Mario Malic 2022년 1월 17일
Check this out. Parfor worked fine for me using Sysem.Diagnostics.Process.
In order to relay the messages from workers in parpool, there are functions which I don't remember the name exactly. Check out this example https://www.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html
jessupj
jessupj 2022년 1월 17일
편집: jessupj 2022년 1월 17일
just to play devil's advocate, i've approached a similar problem in the past by calling parallel instances of matlab from the shell. it was by no means the most I/O-efficient method and wasted a lot of time opening and closing matlab & pooling. the individual system jobs were geophysical models that took O(days) so the data importing and pool opening overhead was comparatively insignificant.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 1월 17일

0 개 추천

parpool
tic
parfor i=1:nfiles
cmd = sprintf('echo "%s" | multi.exe', str3{i,1});
[status,cmdout] = system(cmd, '-echo')
end

댓글 수: 12

Sujatha
Sujatha 2022년 1월 18일
Sorry that didn't work. Please see attachment.
Sujatha
Sujatha 2022년 1월 18일
This may be pertinent (please see enclosure). Thanks!
I think I know what the issue is: In interactive mode, I type:
>> [status,cmdout] = system('multi.exe','-echo')
Matlab responds with:
Input unique file identifier. Ex: 2~04-13-013939 -
I then type:
2~11-30-091444 <Return> (Keyboard Return)
In the parfor loop, there is no <Return> (Keyboard Return) after the str3{i,1} input to the parallel processes!
Perhaps you need
parpool
tic
parfor i=1:nfiles
cmd = sprintf('(echo "%s" & echo.) | multi.exe', str3{i,1});
[status,cmdout] = system(cmd, '-echo')
end
The period after the echo is important, and must be immediately after the echo with no space.
Sujatha
Sujatha 2022년 1월 19일
No joy!
>> parfor i=1:4
cmd = sprintf('(echo "%s" & echo.) | multi.exe', str3{i,1});
[status,cmdout] = system(cmd, '-echo')
end
'multi.exe' is not recognized as an internal or external command,
operable program or batch file.
'multi.exe' is not recognized as an internal or external command,
operable program or batch file.
'multi.exe' is not recognized as an internal or external command,
operable program or batch file.
'multi.exe' is not recognized as an internal or external command,
operable program or batch file.
What does:
(echo "4~11-24-060505" & echo.)
do?
At the windows COMMAND level,
(echo "4~11-24-060505" & echo.) |
should create an output stream that contains
4~11-24-060505<NEWLINE><NEWLINE>
and make it available as input to the next program in sequence (which here would be multi.exe)
Could you confirm for me that if you hard-code
system('multi.exe', '-echo')
that the program multi runs, but that if you hard-code
system('(echo "4~11-24-060505" & echo.) | multi.exe', '-echo')
that it fails saying it does not know what multi.exe is ?
Sujatha
Sujatha 2022년 1월 20일
Yes, that is true.
The program runs with:
system('multi.exe', '-echo')
The program fails with:
system('(echo "4~11-24-060505" & echo.) | multi.exe', '-echo')
and does not recognize "multi.exe".
Well, there is another possibility.
parpool
tic
parfor i=1:nfiles
tname{i} = tempfile();
[fid, msg] = fopen(tname{i});
if fid < 0
error('iteration %d failed to create temporary file "%s" because "%s"', i, tname{i}, msg);
end
fprintf(fid, '%s\n\n', str3{i,1});
fclose(fid)
cmd = sprintf('multi.exe < "%s"', tname{i});
[status,cmdout] = system(cmd, '-echo')
end
Then once you are sure that all of the executables have finished, you can delete all of the files named in tname .
Sujatha
Sujatha 2022년 1월 26일
An UndefinedFunction error was thrown on the workers for 'tempfile'. This might be because the
file containing 'tempfile' is not accessible on the workers. Use addAttachedFiles(pool, files)
to specify the required files to be attached. For more information, see the documentation for
'parallel.Pool/addAttachedFiles'.
Caused by:
Unrecognized function or variable 'tempfile'.
Sujatha
Sujatha 2022년 1월 26일
addAttachedFiles(ans, tempfile), didn't help.
parpool
tic
parfor i=1:nfiles
tname{i} = tempname();
[fid, msg] = fopen(tname{i});
if fid < 0
error('iteration %d failed to create temporary file "%s" because "%s"', i, tname{i}, msg);
end
fprintf(fid, '%s\n\n', str3{i,1});
fclose(fid)
cmd = sprintf('multi.exe < "%s"', tname{i});
[status,cmdout] = system(cmd, '-echo')
end

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2022년 1월 17일

댓글:

2022년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by