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.

MATLAB parfor and Comsol server

Please login with a confirmed email address before reporting spam

Hi,
I want to speed up my computations using the parfor loop in MATLAB (from Parallel Computing Toolbox). I run this on a Linux machine. My code so far:

system('killall -9 comsollauncher java'); % kill previous running Comsol server instances to prevent port assignment problems
pool = parpool('local',2); % open local pool with two Workers
parfor i=1:5 % set this to 2 iterations (=number of workers in pool) and it will work
comsolPort = 2036+i; % set unique port
system( ['comsol -np 1 server -port ',num2str(comsolPort), ' &'] ); % start a Comsol server
pause( 6 ) % give Comsol server time to start up
testfun(comsolPort) % function connects to the given Comsol server using mphstart(comsolPort), does some Comsol stuff and later disconnects
end
delete(pool); %close pool

Using 2 workers the code works if parfor makes only 2 iterations. But if there are more iterations mphstart() cannot connect to the given Comsol server throwing:

"Error using mphstart (line 152) A connection to Comsol could not be established. Please check that a Comsol Server is started prior to calling mphstart"

although a new server is definitely running on the given port. This might be related to how the MATLAB job scheduler works. But I have no idea why and how to fix this. Having only as many parfor iterations as workers in the pool is not optimal, because I have to run far more cases than there are CPUs/workers on my machine. Do you have any suggestions how to make this work?

Thank you and best regards,
Chris


7 Replies Last Post Aug 1, 2017, 5:03 a.m. EDT

Please login with a confirmed email address before reporting spam

Posted: 10 years ago Jun 29, 2014, 10:27 a.m. EDT
I think I figured out the root of the problem:
once a MATLAB instance has been connected to a Comsol server, it is no longer able to (re)connect to another Comsol server. So in my case the workers that already have been connected to a Comsol server cannot connect to a server again in the next parfor iteration. This reconnecting issue will be the topic of another thread here :)
I think I figured out the root of the problem: once a MATLAB instance has been connected to a Comsol server, it is no longer able to (re)connect to another Comsol server. So in my case the workers that already have been connected to a Comsol server cannot connect to a server again in the next parfor iteration. This reconnecting issue will be the topic of another thread here :)

Please login with a confirmed email address before reporting spam

Posted: 9 years ago Dec 22, 2014, 5:37 a.m. EST
Dear Chris,

Would you please let me know how to solve this problem? I also met the same problem when I run comsol matlab livelink on cluster. Thanks for your help in advance.

Best regards
Yanrui
Dear Chris, Would you please let me know how to solve this problem? I also met the same problem when I run comsol matlab livelink on cluster. Thanks for your help in advance. Best regards Yanrui

Please login with a confirmed email address before reporting spam

Posted: 9 years ago Dec 22, 2014, 6:09 a.m. EST
Dear Chris,

I think I also find a way. I delete the folder '.comsol', then reload comsol module and rebuild the connection of the server by type 'comsol server'. And then it works. Thanks for the hint.

Best regards
Yanrui
Dear Chris, I think I also find a way. I delete the folder '.comsol', then reload comsol module and rebuild the connection of the server by type 'comsol server'. And then it works. Thanks for the hint. Best regards Yanrui

Please login with a confirmed email address before reporting spam

Posted: 9 years ago Oct 8, 2015, 5:54 a.m. EDT
Hey,

I have the same problem. could you please show the code... which file you delete.
Hey, I have the same problem. could you please show the code... which file you delete.

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Nov 18, 2015, 1:51 p.m. EST

Dear Chris,

I think I also find a way. I delete the folder '.comsol', then reload comsol module and rebuild the connection of the server by type 'comsol server'. And then it works. Thanks for the hint.

Best regards
Yanrui


Do you mind telling me where I can find that folder .comsol?

Thank you
[QUOTE] Dear Chris, I think I also find a way. I delete the folder '.comsol', then reload comsol module and rebuild the connection of the server by type 'comsol server'. And then it works. Thanks for the hint. Best regards Yanrui [/QUOTE] Do you mind telling me where I can find that folder .comsol? Thank you

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Nov 22, 2015, 1:53 p.m. EST
Hi Camille,
I see you seem to have as much fun using Comsol Server + MATLAB as I used to have :)
Assuming that you are running Comsol on Linux machines the profile folder is located in the home directory of the account (~/.comsol). It is a hidden folder so you may check using 'la-al'.
Best,
Eli
Hi Camille, I see you seem to have as much fun using Comsol Server + MATLAB as I used to have :) Assuming that you are running Comsol on Linux machines the profile folder is located in the home directory of the account (~/.comsol). It is a hidden folder so you may check using 'la-al'. Best, Eli

Please login with a confirmed email address before reporting spam

Posted: 7 years ago Aug 1, 2017, 5:03 a.m. EDT
comsol server connection should be established only once for each worker.
that can be done for example by using
persistent var
if isempty(var)
comsol server connection should be established only once for each worker. that can be done for example by using persistent var if isempty(var)

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.