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 2017年8月1日 GMT-4 05:03

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2014年6月29日 GMT-4 10:27
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: 10 years ago 2014年12月22日 GMT-5 05:37
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: 10 years ago 2014年12月22日 GMT-5 06:09
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 2015年10月8日 GMT-4 05:54
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: 9 years ago 2015年11月18日 GMT-5 13:51

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: 9 years ago 2015年11月22日 GMT-5 13:53
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 2017年8月1日 GMT-4 05:03
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.