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

Parallel computing MatLab Comsol

Anders Gudmarsson
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 May 23, 2017, 4:37 AM EDT
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.

Posted: 5 years ago Jan 31, 2013, 10:19 AM EST
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,

Anders Gudmarsson
Posted: 5 years ago Feb 5, 2013, 10:14 AM EST
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

Posted: 5 years ago Feb 5, 2013, 12:03 PM EST
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,

Julian Freed-Brown
Posted: 5 years ago Feb 8, 2013, 12:33 PM EST
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

Anders Gudmarsson
Posted: 5 years ago Feb 13, 2013, 4:50 AM EST
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

Julian Freed-Brown
Posted: 5 years ago Feb 13, 2013, 1:00 PM EST
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

Michael Mrejen
Posted: 4 years ago Mar 13, 2014, 2:56 AM EDT
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

Posted: 3 years ago Jul 3, 2014, 2:22 PM EDT
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

Posted: 3 years ago Jul 3, 2014, 2:38 PM EDT
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

Posted: 3 years ago Jul 18, 2014, 11:43 AM EDT
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

Posted: 10 months ago Dec 3, 2016, 1:31 AM EST
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

Julian van Velzen
Posted: 4 months ago May 23, 2017, 4:37 AM EDT
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

Reply

Please read the discussion forum rules before posting.

Please log in to post a reply.

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.