Analysis Tool: Bad Allocation Error (MATLAB API)

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Elizabeth Schmida
Posts: 5
Joined: Fri May 22, 2020 12:09 pm

Analysis Tool: Bad Allocation Error (MATLAB API)

Post by Elizabeth Schmida » Mon Oct 03, 2022 10:26 am

Hi All,

I am using the OpenSim (V4.3) API with MATLAB (R2021a) to batch process subject data through the Scaling, IK, ID, and Analysis Tools. I am currently able to scale a base model and run IK and ID on multiple trials for multiple subjects without issue. However, when I incorporate the Analysis Tool (to obtain model COM position) into the workflow I receive the following, seemingly random, bad allocation error (on some occasions I will instead get a fatal error, forcing MATLAB to close).
BadAllocationMatlab.png
Bad Allocation Error (MATLAB)
BadAllocationMatlab.png (118.73 KiB) Viewed 726 times
The error generally seems to become an issue when I attempt to process >7 subjects (all with roughly five 15-second running trials). Stepping through my Analysis Tool code (attached in the zip file), the error occurs when I attempt to run the tool (analysis.run()). Listed below are the steps I've taken to resolve the issue, but so far none have worked. I've also attached sample input my analysis function uses to generate COM position, while this won't replicate the error, it hopefully provides a bit more context, or hopefully someone notices something I missed. Any and all suggestions would be immensely appreciated. Thank you!!

Bad Allocation Error Troubleshooting:
1) When the error was thrown for a particular subject, reprocessed that subject individually with no issues.
2) Tried different datasets (and a dataset that only included subjects that threw no error during batch processing) and still ran into the error.
3) Upgraded my machine's memory + storage (now 4 processors, 16GB Ram, 134GB Hard drive) and still get the error.
4) Clear and close everything at the end of the analysis function--still get the error.
5) A previous forum post mentioned this issue was resolved by clearing out old setup files and results from folders: this unfortunately did not resolve the issue either.
Attachments
AnalysisToolCode&Inputs.zip
Analysis Code and Example Inputs (trimmed to 5 second trials for forum upload purposes)
(422.68 KiB) Downloaded 55 times

Tags:

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

Re: Analysis Tool: Bad Allocation Error (MATLAB API)

Post by Ayman Habib » Tue Oct 04, 2022 10:50 am

Hi Elizabeth,

While nothing in the code snippet you sent looks suspect, the behavior you describe is likely related to memory usage and/or leaks that cause Matlab to run out of heap space. We fixed a few issues related to that in version 4.4 that has been posted last July. I'd suggest you give that a try and let us know if the problem persists. If it does then please file a bug or send along and attach a minimal example to reproduce (e.g. running the same model/data file in a loop) and we'll investigate.

Best regards,
-Ayman

User avatar
Elizabeth Schmida
Posts: 5
Joined: Fri May 22, 2020 12:09 pm

Re: Analysis Tool: Bad Allocation Error (MATLAB API)

Post by Elizabeth Schmida » Fri Oct 07, 2022 10:19 am

Thank you for the suggestions! While updating to V4.4 did not resolve the issue, I believe I found a work around. Interestingly, running my original code using V4.4 resulted in a fatal matlab error after processing only 1 trial instead of after ~20 or so as it did in V4.3. I reworked my code and am now able to process multiple subjects and trials without receiving a bad allocation error. Below is an explanation of my process and my attachments in case they are in anyway useful to others.

Explanation/Notes:
1) Updating to V4.4 and running my original code resulted in a fatal MATLAB error occurring rather than a bad allocation error. In V4.4 this error occurred after just one trial was processed, and instead of occurring during Analyze it occurred when the ID tool was run.
2) In creating the abridged, looped code (attached) for the OS forum, I noticed that if I only pass one speed into the ID (batchID.m) and Analyze Tools(AnalyzeTool.m) at a time, no error is thrown after the first loop. (My original code fed the batchID function a list of trials to process and looped through ID within the function.)
3) My original code instantiated OpenSim ID objects(Model, InverseDynamicsTool) prior to looping through the processed data. In V4.4 this seems to result in a fatal MATLAB error. When I adjusted the code such that these objects are instantiated at the beginning of each loop, no error occurred after the first loop. (I also re-specified the model (idTool.setModel(model)) at the beginning of each loop).
4) After my updates, my matlab code is now able to iterate through subjects and trials without any issues. Using this restructured code with V4.3 also seemed to resolve the issue.
5) Also of possible interest is that if you're working on a virtual desktop, a bad allocation error is typically thrown after a certain number of subjects/trials are processed. On an "actual" laptop/desktop this error does not occur. I think this is more of a machine issue than an OpenSim problem.

Attached files/MATLAB code Explanation:
Folders
  • matlab: use wrapper code to run the 2 scripts used to process data through ID and Analyze Tools
  • generic_setupfiles: includes the generic XML files loaded into the tools
  • DataCollections: shows the data file structure used with 1 subject folder with data included to run the code
Code
  • TroubleshootingWrapper.m = specifies filepaths, and loops through the processes
  • batchID.m = runs inverse dynamics, calls AnalyzeTool function
  • AnalyzeTool.m = runs analysis tool to output model COM location
Notes:
  • The loop in line #19 of TroubleshootingWrapper.m is currently set to 50 iterations to simulate a number of subjects/trials.
    ID and Analyze tool outputs are overwritten each iteration.
  • IK and ID inputs have been trimmed (to 5 sec data collection) to accommodate the forum's attachment size limits.
AnalyzeToolTroubleshooting.zip
Sample Code and Data for ID and Analyze Tool Processing
(405.76 KiB) Downloaded 57 times

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

Re: Analysis Tool: Bad Allocation Error (MATLAB API)

Post by Ayman Habib » Mon Oct 10, 2022 4:51 pm

Hi Elizabeth,

Thanks for sending this out. I ran your latest code and indeed there were no issues. Since there was no driver for the older/earlier code I couldn't test but I noted one thing that may not be clear in the documentation. When you call

Code: Select all

AnalyzeTool.setLoadModelAndInput(true);
the code assumes that the tool owns the model ragardless of whether you called setModel or if model_file is specified in XML. The side-effect is that when the tool is deleted (or goes out of scope because the function returns) the model is also deleted. So, my recommendation for future reference is to exclusively use one of these two approaches:
1. Your code is responsible for creating the model and updating it, adding External loads etc., call tool.setModel(), tool.setLoadModelAndInput(false), and you decide when to delete/clear the model.
2. Write the model to a file, specify the model file to the tool in XML, call tool.setLoadModelAndInput(true), then the tool decides when to load the model, add External loads or make other changes then it will delete it on exit.

Mixing the two workflows is problematic as you found out.
Hope this helps,
-Ayman

POST REPLY