Unising Matlab with OpenSim API in High Performance Computing clusters

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Pavlos Silvestros » Tue Feb 13, 2018 3:19 pm

Dear all,

I am about to try running an optimisation process on Matlab that will be running OpenSim Forward Dynamic simulations in the cost function through the OpenSim 3.3 API on a HPC cluster. The Matlab code for the optimisation algorithm is suitable for HPC computing (parallelisation) but I am not sure how OpenSim can be set up for this use.

Has anyone got any experience or encountered any information about using OpenSim in such a way?

Thank you in advance for your help.

Best regards,
Pavlos

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Christopher Dembia » Tue Feb 13, 2018 3:45 pm

Make sure you are not using the same OpenSim Model on multiple threads.

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Pavlos Silvestros » Tue Feb 20, 2018 7:38 am

Hi Christopher

Thank you for your response.

How can I ensure that the model isn't used over multiple threads? Do you mean when handling the model through the API or more generally such as when a model is being used during ForwardDynamics simulations?

Best regards,
Pavlos

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Christopher Dembia » Tue Feb 20, 2018 10:14 am

This depends on how you are parallelizing your code. If you are simply invoking OpenSim's command-line tools in parallel, there is little to worry about (make sure you're not overwriting the same output files).

If you are using models in MATLAB through the OpenSim MATLAB interface, then you should not use these models in parallel in MATLAB (e.g., parfor). Also, I'm not sure you can even use OpenSim models with parfor.

User avatar
Tom Augenstein
Posts: 38
Joined: Thu May 03, 2018 8:19 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Tom Augenstein » Tue Jun 26, 2018 10:41 am

chrisdembia wrote:
Tue Feb 20, 2018 10:14 am
If you are using models in MATLAB through the OpenSim MATLAB interface, then you should not use these models in parallel in MATLAB (e.g., parfor). Also, I'm not sure you can even use OpenSim models with parfor.
Does this mean that if you wanted to use MATLAB's parfor, there would have to be a unique model file for each loop iteration?

User avatar
Adrian Lai
Posts: 46
Joined: Tue Mar 13, 2012 11:33 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Adrian Lai » Wed Jun 27, 2018 12:24 pm

Hi Tom and Chris,

I might be able to help here.

When I've run parfor in MATLAB with the OpenSim API, a new instant of the model was needed for each loop iteration (see the code below). It means that each thread must have a unique model in which to use rather than one that has been previously initialised outside of the parfor loop.

Code: Select all

parfor i=1:Ncolloc
    import org.opensim.modeling.*
    osimModel = Model(model_name);
    osimState = osimModel.initSystem();
    % Rest of your code
    end
Good luck!

Adrian

User avatar
Ross Miller
Posts: 371
Joined: Tue Sep 22, 2009 2:02 pm

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Ross Miller » Fri Jun 29, 2018 5:45 am

My brief experience in attempting this was the same as Adrian's: needed to load and initialize a new instance of the model within the loop.

Now I just run Direct Collocation simulations on a single computer =) But those calculations can (in theory) be parallelized too.

User avatar
Tom Augenstein
Posts: 38
Joined: Thu May 03, 2018 8:19 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Tom Augenstein » Tue Aug 14, 2018 3:30 pm

Thanks for the replys!

I'm attempting to run it now but I'm getting the following error:

"An UndefinedFunction error was thrown on the workers for 'Model'. This might
be because the file containing 'Model' is not accessible on the workers. Use
addAttachedFiles(pool, files) to specify the required files to be attached.
See the documentation for 'parallel.Pool/addAttachedFiles' for more details.

Caused by:
Undefined function 'Model' for input arguments of type 'char'."

The code works perfectly fine without parallel (or at least I think, it takes 12 hours so I haven't seen the output yet).

Has anyone experienced a similar error, and if so, what are the files I should attach?

Also, here is the code that I am running:

tic
poolobj = parpool(48);
numForce = 3;

% declare set-up file
setup = 'C:\Users\NeuRRo Lab\Desktop\armModel\CMC_Matlab\reaching_CMC_setup.xml';

parfor i = 1:numForce

import org.opensim.modeling.*

model = Model('scaled_upper_arm_CMC.osim');
cmc = CMCTool(setup);

% Set model
state = model.initSystem();

dir = ['CMC_results' i];
cmc.setResultsDir(dir);

cmc.run()

disp('ran')

end
delete(poolobj);
toc

User avatar
Thomas Uchida
Posts: 1778
Joined: Wed May 16, 2012 11:40 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Thomas Uchida » Tue Aug 14, 2018 4:06 pm

Can you confirm that each thread is using a different working directory? If not, you may be encountering a sharing violation as multiple threads attempt to write to the same out.log and err.log files.

User avatar
Tom Augenstein
Posts: 38
Joined: Thu May 03, 2018 8:19 am

Re: Unising Matlab with OpenSim API in High Performance Computing clusters

Post by Tom Augenstein » Wed Aug 15, 2018 9:20 am

Ok, I think that may be the issue. It seems like it is always writing these files to the .m file (the parallel file I'm trying to run) location for every thread. Is there a way to control where these files are output?

POST REPLY