Running Opensim parallel

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Paulien Roos
Posts: 5
Joined: Thu Aug 12, 2010 3:02 am

Running Opensim parallel

Post by Paulien Roos » Thu Jun 02, 2011 8:02 am

Hi,

Could anyone tell me if OpenSim can run parallel using threads (i.e. OpenMP or MPI), or if this will be possible in the near future?

Thanks,
Paulien

User avatar
Ayman Habib
Posts: 2252
Joined: Fri Apr 01, 2005 12:24 pm

RE: Running Opensim parallel

Post by Ayman Habib » Thu Jun 02, 2011 9:18 am

Hi Paulien,

There's currently nothing in the works in this regard primarily because the nature of our time consuming calculations is not parallelizable (e.g. CMC which integrates forward in time). What's your specific use case?

Best regards,
-Ayman

User avatar
Paulien Roos
Posts: 5
Joined: Thu Aug 12, 2010 3:02 am

RE: Running Opensim parallel

Post by Paulien Roos » Fri Jun 03, 2011 1:09 am

Hi Ayman,

Thank you for your reply.
It's mainly a question out of interest. I'm planning a study where we'll optimize an Opensim model together with a different model, and was wondering if this could be done faster by running opensim parallel, as we can run our other model parallel. However I do understand now that the CMC cannot be run parallel.

Thank you,
Paulien

User avatar
Sina Porsa
Posts: 99
Joined: Thu Feb 03, 2011 7:21 pm

Re: Running Opensim parallel

Post by Sina Porsa » Sun Jul 22, 2012 6:42 pm

Hi Ayman,
I have the same question. I was wondering if I can use OpenMP to perform independent forward dynamics in parallel.

I want to find the optimum excitation pattern in muscles which minimizes/maximizes my cost function.
here is a brief description of my algorithm:

// load osim model
// while (flag = false)
// generate random excitation pattern
// perform FD using the generated excitation pattern
// calculate the cost and evaluate the flag
// end of while

the iterations in the while loop are completely independent. But when I want to use OpenMP, it ends up with errors. I was wondering if sth is wrong in my code or maybe opensim cannot run on parallel threads.

Thanks,
Sina

User avatar
Michael Sherman
Posts: 812
Joined: Fri Apr 01, 2005 6:05 pm

Re: Running Opensim parallel

Post by Michael Sherman » Sun Jul 22, 2012 8:28 pm

Hi, Sina.

For your application you might be able to use Simbody's SimTK::ParallelExecutor class, which is available when using the OpenSim API (if that's what you're doing). Have a look at the doxygen description here:
https://simtk.org/api_docs/simbody/api_ ... cutor.html

Regards,
Sherm

User avatar
Sina Porsa
Posts: 99
Joined: Thu Feb 03, 2011 7:21 pm

Re: Running Opensim parallel

Post by Sina Porsa » Mon Jul 23, 2012 9:01 pm

Hi Sherm and thanks for your reply;
It seems that this ParallelExecuter can help me. But I do have some problems, maybe because I am not an expert of C++ and API.
It is not clear for me how can I define a new Task class. To be more clear, if I let's consider this:

i know I should have sth like this:

Code: Select all

ParallelExecutor executor;
SimTK::ParallelExecutor::Task & calculateTheCost();
executor.execute(calculateTheCost, times);
But it is not clear for me how can I define the initialize, finish and execute functions of the task class (calculateTheCost). It would be great if you could help me to solve this problem.

Thanks,
Sina

User avatar
Michael Sherman
Posts: 812
Joined: Fri Apr 01, 2005 6:05 pm

Re: Running Opensim parallel

Post by Michael Sherman » Mon Jul 23, 2012 10:30 pm

Hi, Sina.

You have to make a concrete class that derives from the abstract base class ParallelExecutor::Task. Your concrete class then implements the virtual methods defined by the base class.

Below is an example I grabbed from a Simbody unit test for ParallelExecutor. Your class would have the same three methods, but everything else you see here is just specific to "SetFlagTask".

Sherm

Code: Select all

class SetFlagTask : public ParallelExecutor::Task {
public:
    SetFlagTask(Array_<int>& flags, int& count) : flags(flags), count(count) {
    }
    void execute(int index) {
        flags[index]++;
        localCount.upd()++;
        ASSERT(ParallelExecutor::isWorkerThread() == isParallel);
    }
    void initialize() {
        localCount.upd() = 0;
        ASSERT(ParallelExecutor::isWorkerThread() == isParallel);
    }
    void finish() {
        count += localCount.get();
        ASSERT(ParallelExecutor::isWorkerThread() == isParallel);
    }
private:
    Array_<int>& flags;
    int& count;
    ThreadLocal<int> localCount;
};

User avatar
Sina Porsa
Posts: 99
Joined: Thu Feb 03, 2011 7:21 pm

Re: Running Opensim parallel

Post by Sina Porsa » Wed Jul 25, 2012 4:40 pm

Thanks.
:)

User avatar
Sina Porsa
Posts: 99
Joined: Thu Feb 03, 2011 7:21 pm

Re: Running Opensim parallel

Post by Sina Porsa » Tue Jul 31, 2012 6:35 pm

Hi Sherm,
I have another problem with this parallel executor. Memory leakage!
Here is what I do in the execute function:

Code: Select all

void execute(int index) {
		Model myModel = loadedModel; // loaded model is a global variable. I want to load the model from an *.OSIM file just once.
               //creat a controller
               PrescribedController *muscleController= new PrescribedController();
	       muscleController->setActuators(osimModel.updActuators());
	       try {
                         double muscle1ExcVec[numOfNodes];
                         double muscle2ExcVec[numOfNodes];
                         for (int n=0; n<numOfNodes; n++){
                                muscle1ExcVec[n] = input[whichPage][0][n];//  excitation[0][n];
                                muscle2ExcVec[n] = input[whichPage][1][n];//  excitation[1][n];
                         }	
                         // Set the indiviudal muscle control functions for the prescribed muscle controller
                         muscleController->prescribeControlForActuator("muscle1", new PiecewiseLinearFunction(numOfNodes, timeVec, muscle1ExcVec));
                         muscleController->prescribeControlForActuator("muscle2", new PiecewiseLinearFunction(numOfNodes, timeVec, muscle2ExcVec));
                         //add the controller to myModel 
                         ...
                         // Perform forward dynamics (define system, integrator and manager and integrate the equations)
                         ...
                         }//end of try
                         } // end of execute
any idea how should I solve this problem?
Thanks,
Sina

User avatar
Michael Sherman
Posts: 812
Joined: Fri Apr 01, 2005 6:05 pm

Re: Running Opensim parallel

Post by Michael Sherman » Tue Jul 31, 2012 7:29 pm

Hi, Sina. You're making a copy of the Model and then a bunch of changes to it every time you execute. I believe OpenSim does have a Model memory leak that we noticed recently. Can you arrange to build the model only once and then treat it as read-only during execution?

Regards,
Sherm

POST REPLY