Use system function with parfor

3 ビュー (過去 30 日間)
Meddi Tharun
Meddi Tharun 2020 年 5 月 20 日
コメント済み: Edric Ellis 2020 年 5 月 28 日
I'm generating input files to C code from matlab and collecting output from C code into matlab for further processing through .txt files. In between writing & reading "system", matlab builtin
function
will be called to run C binary file as shown below. I'm unable to use "parfor" in place of "for" loop when I use system function?. What can I do to enable "parfor"?
itrMax = 10000;
StoreResult = zeros(1, itrMax );
for i = 1:itrMax
CINP = randi([0 1], 1, 1000);
fp = fopen("input.txt","w");
fprintf(fp, "%d", CINP);
fclose(fp);
cd cCode
system("./a.out");
cd ../
fp = fopen("output.txt","r");
COUT = fscanf(fp,"%d\n");
fclose(fp)
//FUNCTION Call to process COUT, whose output will be a constant
// OutFlag
StoreResult(1, i) = OutFlag;
end
itrMax = 10000;
StoreResult = zeros(1, itrMax );
for i = 1:itrMax
CINP = randi([0 1], 1, 1000);
fp = fopen("input.txt","w");
fprintf(fp, "%d", CINP);
fclose(fp);
cd cCode
system("./a.out");
cd ../
fp = fopen("output.txt","r");
COUT = fscanf(fp,"%d\n");
fclose(fp);
//FUNCTION Call to process COUT, whose output will be a constant
// OutFlag
StoreResult(1, i) = OutFlag;
end

回答 (1 件)

Edric Ellis
Edric Ellis 2020 年 5 月 20 日
It looks like your executable expects to read from input.txt and write to output.txt. This will not work in parallel because the different workers will clash when the all try to write input.txt. What I suggest is that you modify your program so that it takes the input and output files as command-line arguments, and then do something like this:
parfor i = 1:itrMax
input_fname = sprintf('input_%d.txt', i);
output_fname = sprintf('output_%d.txt', i);
fh = fopen(input_fname, 'w');
% write into fh
fclose(fh);
cmd = sprintf('./a.out %s %s', input_fname, output_fname);
system(cmd);
fh = fopen(output_fname, 'r');
% read from fh
fclose(fh);
% Optional - clean up these files
delete(input_fname);
delete(output_fname);
end
  2 件のコメント
Meddi Tharun
Meddi Tharun 2020 年 5 月 21 日
Hi Edric,
It's showing same error. Let me explain what I did, so that you can comment whether I did exactly what you said or not.
In simulations folder there will be two folders. One is "MATLAB" and another is "C".
%% C script
void main()
{
int cinp[100];
int cout[100];
FILE *fp = freopen("../MATLAB/input.txt","r", stdin);
for(int i = 0;i<100;i++)
{
scanf("%d\n", cinp+i);
}
fclose(fp);
//C Function Call
FILE *fp1 = freopen("../MATLAB/output.txt","w", stdout);
for(int i2 = 0;i2<100;i2++)
{
printf("%d\n", cout[i2]);
}
fclose(fp1);
}
%% EVEN TRIED READING FILES WITH "argc", "argv" ARGUMENTS.
%% MATLAB SCRIPT
parfor i= 1:itrMax
fp = fopen("input.txt","w");
%file operation
fclose(fp);
cd ../C
system("./a.out input.txt output.txt");
cd ../MATLAB
pf = fopen("output.txt","r");
%file reading
fclose(pf);
%% FURTHER Processing on "output.txt"
end
Edric Ellis
Edric Ellis 2020 年 5 月 28 日
You need to change your C code to read from a file named on the command line, and write to a file named on the command line to match the MATLAB code I posted. These days it's probably easier to write C++ than plain C, start with a tutorial like e.g. https://www.w3schools.com/cpp/default.asp . Basically, you need to modify your C(++) code to do the following:
  • Make your main function take argc (number of command-line arguments) and argv (the values of those arguments)
  • Use argv[1] as the input file name, argv[2] as the output file name
  • Probably simpler to read from the file directly rather than using freopen.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by