フィルターのクリア

Parallel tool (parfor) and COM objects

7 ビュー (過去 30 日間)
Ahmadreza
Ahmadreza 2024 年 4 月 17 日
コメント済み: Ahmadreza 2024 年 4 月 18 日
Dear All,
I am trying to optimize the run time of my model by using the parallel tool. In my model, I am working with a COM object as follow:
iphreeqc = actxserver('IPhreeqcCOM.Object'); % calling the COM object
iphreeqc.LoadDatabase('database\phreeqc.dat'); % adding the prefered database to the COM object
n = 100;
Temp = [1:1:100];
R = 0.082057366080960;
OUTPUT = cell(100,1);
parfor i=1:n
IPCstringCell= {'SOLUTION 1', ...
['-temp ', num2str(Temp(i))], ...
'-pressure 10', ...
'-units mmol/L', ...
'Na 0',...
'Cl 0',...
'-water 1', ...
'pH 7 charge',...
'Gas_phase',...
'-pressure 10',...
['volume ',num2str(R*(Temp(i)+273.15)/10)],...
'fixed_volume',...
['-temp ',num2str(Temp(i))],...
'H2(g) 10',...
'SELECTED_OUTPUT', ...
'-saturation_indices H2(g)', ...
'sim true',...
'step true',...
'soln true', ...
'pH true', ...
'-equilibrium_phases H2(g)',...
'-molalities H2', ...
'state false', ...
'time false', ...
'distance false'};
IPCstring = sprintf('%s\n', IPCstringCell{:});
iphreeqc.RunString( IPCstring );
output = iphreeqc.GetSelectedOutputArray;
OUTPUT{i,1} = output;
end
However, I am facing with the below error:
It seems I cannot use dot (.) index to work with the iphreeqc object in the above code. In this case, what should I do?
Kind regards,
Ahmad
  2 件のコメント
Mario Malic
Mario Malic 2024 年 4 月 17 日
Try to open your COM application within parfor loop and see what happens.
Ahmadreza
Ahmadreza 2024 年 4 月 17 日
Great! It is working now!
I think this would be fine to me as openning the iphreeqc module is not time consuming using actxserver. If it takes much time, it would be another problen.
Thanks Mario.
Ahmad

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

採用された回答

Mario Malic
Mario Malic 2024 年 4 月 17 日
編集済み: Mario Malic 2024 年 4 月 17 日
It's not about time consumption.
Opening the application within parfor loop gives each worker its own instance of application so they can interact with it. This solved the question.
If you initialize COM instance outside of parfor, it can't be "copied" to workers' workspace (I am unable to explain why, there is a logical reason for sure). Regardless, it is not sensible to have one instance of program for workers to operate on asynchronously.

その他の回答 (1 件)

Edric Ellis
Edric Ellis 2024 年 4 月 18 日
The general approach to fixing cases where a variable is needed inside parfor, but cannot be directly sent there, is to use parallel.pool.Constant. You give the Constant constructor a function handle which gets run once on the workers to build the COM object, and then inside parfor, you access the .Value field of the Constant which has the value that got built.
  1 件のコメント
Ahmadreza
Ahmadreza 2024 年 4 月 18 日
Thanks for your response. Would you please provide an example of using actxserver inside a parfor by parallel.pool.Constant function?

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

カテゴリ

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