Problems in parallel computing of COMSOL model

Regarding the parallel computing of the COMSOL model, the method previously used
for j=1:1000
parfor i=1iter
port=mphstartcomsolmphserver
mphstart(port)
model = mphload(mph);
mphsetparam(model, 'P_load1', j);
However, during execution, it was found that loading the model with mphload takes a long time, which means that every time parfor is performed, the loading model needs to be called repeatedly. Therefore, the current idea is how to only call the model once and then update the parameter mphsetparam (model, 'P_load1', j) in the parfor loop; This shortens the running time. Encountered problem 1 during the process:
spmd
if spmdIndex<= numModels port=mphstartcomsolmphservermphstart(port)
model=mphload(modelFiles{spmdIndex});
else
model = [];
end
end
Unable to locate the model, there was an index localization exception when inputting model {i}, which means the corresponding working range of the model cannot be located. When saving the model in an array or other form, there was a problem with the model (mph) not being serialized

6 Comments

You should seek help from the authorities
One problem is that spmdIndex is from 1 to the number of labs, not from 1 to the number of model files.
You cannot start spmd with an arbitrary number of labs; the number of labs created is no more than the size of the parpool .
I guess the code is as follows,numModels ==2
spmd
if spmdIndex<= numModels
port=mphstartcomsolmphservermphstart(port)
model=mphload(modelFiles{spmdIndex});
else
model = [];
end
end
the number of labs created is no more than the size of the parpool .parpool is10 ,numodels=2。
The core of the problem lies in the inability to locate the model by placing it in an array or model {i}。
晶
on 11 Feb 2025
Edited: on 11 Feb 2025
My question is how to call the model only once and then update the parameter mphsetparam (model, 'P_load1', j) in a parfor loop; This can shorten the running time,the corresponding code is: (This code runs in the mlx environment)
modelFiles = {'CBK03_0120_newest1.mph', 'CBK03_0120_newest2.mph'};
numModels = length(modelFiles);
spmd
if spmdIndex <= numModels
port=mphstartcomsolmphserver
mphstart(port)
model = mphload(modelFiles{spmdIndex});
numModelssss={numModelssss,modell}
else
model = [];
end
end
Enter in the command window
model
The result is
model =
Worker 1: class = com.comsol.clientapi.impl.ModelClient, size = [1 1]
Worker 2: class = com.comsol.clientapi.impl.ModelClient, size = [1 1]
Worker 3: class = double, size = [0 0]
Worker 4: class = double, size = [0 0]
Worker 5: class = double, size = [0 0]
........
........
But when entering model {i}, it will appear
Incorrect use of index
An invalid indexing request was made.
reason:
Incorrect use
spmdlang.AbstractRemoteResourceSet/remoteRetrieval
An error occurred during remote data retrieval.
Incorrect use
spmdlang.AbstractRemoteResourceSet/handleRemoteSet/handleRemoteSetOrRetrievalFuture
Error during serialization
Enter numModelssss {1}
It will appear
Warning: The following warnings were encountered while retrieving a Composite value:
Warning: COMSOL Model Object
Name: CBK03_0120_newest1.mph
Tag: Model
Identifier: root is not serializable
Warning: COMSOL Model Object
Name: CBK03_0120_newest1.mph
Tag: Model
Identifier: root is not serializable
result:
ans=
{0×0 cell} {0×0 double}
Display not empty
Identify the issue: the model cannot be located (numModelssss {1} is empty), or there is a serialization problem (model {i} index is incorrect, unable to serialize)
Identify the issue: the model cannot be located (numModelssss {1} is empty), or there is a serialization problem (model {i} index is incorrect, unable to serialize)
主题复制
params = {1, 2};
results = cell(1, numModels);
parfor i = 1:numModels
currentModel = model{i}
currentParam = params{i};
mphsetparam(model, 'P_load1', currentParam );
end
The first step cannot execute the corresponding second parameterized parfor calculation, nor can it proceed
2.I have sought official help from Comsol regarding this issue. Reply:
The information previously found indicates that, MATLAB parallel pools are independent of each other and cannot use global variables or other functions to call external (out of loop) variables. It is recommended to address this issue MATLAB technical support consultation. Just consider the model as a structural variable.Is the long loading time of the model due to the fact that the model file to be loaded itself contains solutions and grids. Just as a template model for loading and calling, it can clear solutions, clear grids, compress history records, and then save.Thank you for contacting COMSOL technical support. We hope the above answer is helpful to you。
3.Questions similar to ours in the community
Question link:https://ww2.mathworks.cn/matlabcentral/answers/252604-parfor-with-comsol-object?s_tid=answers_rc1-1_p1_Topic
Similar issues can be seen as follows:
Dear Edric,
Thank you for the solution. I am able to access the model object this way inside the parfor loop
parfor i = 1:2
model{i} = mphload('LIB_9AH_1D.mph');
model{i}.variable('var8').set('D1_neg',D1_N{i}, 'Diffus');
end
However i am not able to access the model object outside parfor.
parfor i = 1:2
model{i} = mphload('LIB_9AH_1D.mph');
model{i}.variable('var8').set('D1_neg',D1_N{i}, 'Diffus');
end
check_model = model{1};
In the above case 'check_model' is a empty matrix. The reason for doing this is the above code segment is inside a objective function and when each time the function is called the model gets loaded. However i wish to load the model once in the main file and pass it as a parameter to the objective function (to reduce the execution time).
I have accepted the above answer as it partly solved my problem.
Thank a lot.
Kind regards,
Chris
dear Walter Roberson and jiangtao ,This problem has been bothering me for a long time, and I would greatly appreciate it if you could help me solve it。
Dear JIN
We are now trying to deconstruct your question. Your current concern is that you want to use model {i} to locate your COMSOL file (mph) and perform parameter operations. As far as I know, Parfor parallel computing data such as current and voltage time series data can be placed in the main workspace. That is to say, for unstructured data such as models (mph), there may be other methods to index and locate them. I suggest you go to communities like GT_Suit that also involve parallel computing with Maltba to see if there are any examples。
dear Walter Roberson
Do you have any suggestions or thoughts。

Sign in to comment.

Answers (1)

Categories

Products

Release

R2024b

Asked:

晶
on 9 Feb 2025

Answered:

on 18 Feb 2025

Community Treasure Hunt

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

Start Hunting!