Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Parallel computing MatLab Comsol

Please login with a confirmed email address before reporting spam

Hi,

I am using Comsol 4.3 with Livelink for Matlab. I run an optimization code in Matlab where i call Comsol to perform frequency response simulations. Now i would like to start two parallel frequency response simulations that are independent of each other from my Matlab code.
My approach so far has been to open another Matlab worker by using matlabpool and parfor. However, only one of the Matlab workers are then connected to Comsol. Is it possible to perform parallel computing in Comsol when using the Matlab livelink?

//Anders


12 Replies Last Post 23 mai 2017, 04:37 UTC−4
COMSOL Moderator

Hello Anders Gudmarsson

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 31 janv. 2013, 10:19 UTC−5
Hi Anders,

Were you able to solve this problem?

Hi,

I am trying to do the following

parfor k=1:20
model2 = mphload('micromodel');
end

This should initiate several parallel comsol instances in matlab. However, I get the following error

Error using parallel function
Undefined variable "ModelUtil" or "ModelUtil.load"

However if I do this in serial:

parfor k=1:20
model2 = mphload('micromodel');
end

it works fine.

Thanks,
Hi Anders, Were you able to solve this problem? Hi, I am trying to do the following parfor k=1:20 model2 = mphload('micromodel'); end This should initiate several parallel comsol instances in matlab. However, I get the following error Error using parallel function Undefined variable "ModelUtil" or "ModelUtil.load" However if I do this in serial: parfor k=1:20 model2 = mphload('micromodel'); end it works fine. Thanks,

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 5 févr. 2013, 10:14 UTC−5
Hi Hamsa,

Yes I managed to find a solution to this problem.

To connect the matlab workers to comsol i use the system command below to start a server for each worker. Then i use the mphstart command to connect my parallel functions to the comsol servers by specifying the ports.

comsolPort = 2036;
system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] );

mphstart( comsolPort )
import com.comsol.model.*
import com.comsol.model.util.*

I hope this will help you.

Regards,
Anders
Hi Hamsa, Yes I managed to find a solution to this problem. To connect the matlab workers to comsol i use the system command below to start a server for each worker. Then i use the mphstart command to connect my parallel functions to the comsol servers by specifying the ports. comsolPort = 2036; system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] ); mphstart( comsolPort ) import com.comsol.model.* import com.comsol.model.util.* I hope this will help you. Regards, Anders

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 5 févr. 2013, 12:03 UTC−5
Hi Anders,

Thanks for your reply. I am able to launch comsol on the workers, however, they are not able to finish off normally. Somhow they end up crashing.

Can you tell me if you use labBarriers, LabSend and LabReceive commands to communicate from the parent process to the worker processes?

Also, would I be right in making the parent do the writing to the files etc. and just get the data from the child processes?

Thanks,

Hi Anders, Thanks for your reply. I am able to launch comsol on the workers, however, they are not able to finish off normally. Somhow they end up crashing. Can you tell me if you use labBarriers, LabSend and LabReceive commands to communicate from the parent process to the worker processes? Also, would I be right in making the parent do the writing to the files etc. and just get the data from the child processes? Thanks,

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 8 févr. 2013, 12:33 UTC−5
Hi Anders,

I'm having the same problem you started with, but I'm not quite following how you solved it.

Could you please upload some sample code that show how you fixed the problem? I'd really appreciate it.

Thanks,
-jfb
Hi Anders, I'm having the same problem you started with, but I'm not quite following how you solved it. Could you please upload some sample code that show how you fixed the problem? I'd really appreciate it. Thanks, -jfb

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 13 févr. 2013, 04:50 UTC−5
Hi,

Here is a sample code of my solution to this problem.
I use the system command to start comsol and then i call my comsol functions in the parfor loop.


matlabpool open 2
comsolPort = 2036;
system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] );
pause( 6 )

comsolPort = 2037;
system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] );
pause( 6 )

parfor i = 1:2
if i == 1
y_acc_amp_final{i}=Torsion(dataT,f_range);
else
x_acc_amp_final{i}=Long(dataT,f_range);
end
end

matlabpool close


In my comsol functions i use the mphstart command to connect the functions to the opened comsol ports.


cd('C:\Program Files\COMSOL43\mli')
mphstart(2036)


I hope this may be helpful.

Regards,
Anders
Hi, Here is a sample code of my solution to this problem. I use the system command to start comsol and then i call my comsol functions in the parfor loop. matlabpool open 2 comsolPort = 2036; system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] ); pause( 6 ) comsolPort = 2037; system( ['comsol -np 1 server -silent -port ' num2str(comsolPort) ' &'] ); pause( 6 ) parfor i = 1:2 if i == 1 y_acc_amp_final{i}=Torsion(dataT,f_range); else x_acc_amp_final{i}=Long(dataT,f_range); end end matlabpool close In my comsol functions i use the mphstart command to connect the functions to the opened comsol ports. cd('C:\Program Files\COMSOL43\mli') mphstart(2036) I hope this may be helpful. Regards, Anders

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 13 févr. 2013, 13:00 UTC−5
Hi,

Thanks so much for your help! I got it working.

Thanks again,
-jfb
Hi, Thanks so much for your help! I got it working. Thanks again, -jfb

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 13 mars 2014, 02:56 UTC−4
Hi,

I'm trying to implement the solution suggested here.
I'm able to get the different servers up and connect them to the workers but I'm now stuck when I try to use mphload (in order load my model).
Should I load my model in the parent process or should I load it for each worker?
if so how can I specify to which server mphload should load the model?
Your help is appreciated in advance!

Thanks

Michael
Hi, I'm trying to implement the solution suggested here. I'm able to get the different servers up and connect them to the workers but I'm now stuck when I try to use mphload (in order load my model). Should I load my model in the parent process or should I load it for each worker? if so how can I specify to which server mphload should load the model? Your help is appreciated in advance! Thanks Michael

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 3 juil. 2014, 14:22 UTC−4
Hi

I had problems inside MATLAB's parfor loop.

parfor idx = 1:2
import com.comsol.model.*
import com.comsol.model.util.*
model = ModelUtil.create('Model');
end

Error: MATLAB cannot determine whether "ModelUtil" refers to a function or variable.
See Parallel for Loops in MATLAB, "Unambiguous Variable Names".

Could you please kindly advise how you solved this?

Best regards, and thanks in advance!
Alvin
Hi I had problems inside MATLAB's parfor loop. parfor idx = 1:2 import com.comsol.model.* import com.comsol.model.util.* model = ModelUtil.create('Model'); end Error: MATLAB cannot determine whether "ModelUtil" refers to a function or variable. See Parallel for Loops in MATLAB, "Unambiguous Variable Names". Could you please kindly advise how you solved this? Best regards, and thanks in advance! Alvin

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 3 juil. 2014, 14:38 UTC−4
Hi Hamsa,

Did you solve the problem? I have exactly the same problem.

Error: MATLAB cannot determine whether "ModelUtil" refers to a function or variable.
See Parallel for Loops in MATLAB, "Unambiguous Variable Names".

Could you please kindly help?? Really desperate.
Thanks a lot

Alvin
Hi Hamsa, Did you solve the problem? I have exactly the same problem. Error: MATLAB cannot determine whether "ModelUtil" refers to a function or variable. See Parallel for Loops in MATLAB, "Unambiguous Variable Names". Could you please kindly help?? Really desperate. Thanks a lot Alvin

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18 juil. 2014, 11:43 UTC−4
Hi Anders,

I'm trying to use Comsol with the optimization tool from Matlab to estimate some parameters of my Comsol model using genetic algorithms. Could you please, give me some tips how to do it? I'm not trying to do parallel processing, it's just the first part of your text, which I think you are dealing well with that, right?

Could you please help me with that?

Thank you.

Marco
Hi Anders, I'm trying to use Comsol with the optimization tool from Matlab to estimate some parameters of my Comsol model using genetic algorithms. Could you please, give me some tips how to do it? I'm not trying to do parallel processing, it's just the first part of your text, which I think you are dealing well with that, right? Could you please help me with that? Thank you. Marco

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 3 déc. 2016, 01:31 UTC−5
Hi Anders

Is your problem solved? I am in exactly the same situation as you.

Regards
Jia
Hi Anders Is your problem solved? I am in exactly the same situation as you. Regards Jia

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 23 mai 2017, 04:37 UTC−4
You could start multiple comsol servers. but then you need multiple licenses, do you have that? In that case, you could start multiple servers. The effect is this is rather small however if is it is all on the same node (computer) because comsol on itself is already nicely parallelized. If you have multiple nodes, however, having multiple server instances might greatly improve performance. However, I don't think Matlab supports distributed memory parallelism out of the box, so you have to try something with MPI
You could start multiple comsol servers. but then you need multiple licenses, do you have that? In that case, you could start multiple servers. The effect is this is rather small however if is it is all on the same node (computer) because comsol on itself is already nicely parallelized. If you have multiple nodes, however, having multiple server instances might greatly improve performance. However, I don't think Matlab supports distributed memory parallelism out of the box, so you have to try something with MPI

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.