Sharing large structure with nested functions with function handles increase main function overhead?

9 visualizzazioni (ultimi 30 giorni)
Good day!
I am building a large simulation model using matlab. I have a quite complitcated structure with many sub structures and data containing model states and I have found that much of the computational power is spent on shuffeling this structure to and from functions where I edit the model states. I decided to try out some nested functions that share this complicated structure, so no copying is necessary. However, the nested functions seem to incur a massive overhead charge, using just one nested function increases the self execution time ('end' in profiler) to 60% of the main function, this is compeared to using a .m file function insted of nested.
Some relevant information:
I'm calling the function from a for loop a couple of thousand of times 4000-6000.
I'm using function handles in a cell array and calling them like so: Output = cell_array{i}(Input);
The shared structure is modified several times inside the for loop by different functions that are specified in m-files.
The nested function seems to execute faster than the .m function, but the total execution time is around 100% slower.
Is there something that can explain this increase in overhead? It would be a massive improvement not having to copy data between functions so I would very much appricieta some input!
Thanks!
  3 Commenti
Stephen23
Stephen23 il 17 Nov 2023
"I have a quite complitcated structure with many sub structures and data containing model states and I have found that much of the computational power is spent on shuffeling this structure to and from functions where I edit the model states."
Flatter data is often much more efficient to process than deeply nested data. It usually makes code simpler too.
"It would be a massive improvement not having to copy data between functions so I would very much appricieta some input!"
MATLAB does not copy data every time you pass something to a function. Search this forum for "copy on write" to know how it really works, as well as reading this documentation:
Edvin W
Edvin W il 21 Nov 2023
Thank you for your input!
@Ted: I tried the handle class previously but it did not seem to help the situation. So maybe it is not the copying of data that is the problem like Matt J is suggesting but the editing. As for the structure... there are around 5 substructures that contain read-only static information. However, there are a few "dynamic" containing events that are read and written to, they have pre-allocated memory and burried four levels deep in the main structure. Im suspecting that this is the main problem reading your anwers.
I am editing theese as follows:
Read:
event = A.b.c.d(idx);
Write/add =
A.b.c.d(idx + 1: end_idx + 1) = A.b.c.d(idx: end_idx);
A.b.c.d(idx) = event;
---
The model is slowed down considerably as struct "d" is filled with data, I thought it was due it having to be copied but now I am not so sure. The structure "d" has around 20 fields.
@Stephen23: Thank you for the input, I could consider flattening the data, do you have any idea of what kind of performance impovement I could expect from that? I nested the structrue because of code complexity and readability so there is a balance there i guess. I'm afraid I have some try catch blocks around the code as well that I would be reluctant to remove. Am I correct in my understanding that "file functions (.m)" do not get the memory optimization on "copy on write" that local functions do?
Thank you for your answers!

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 17 Nov 2023
where I edit the model states
It is the editing of the variable contents that incurs time, not the passing of them to functions. Note that if one of the struct variables is a large numeric matrix, changing even one element will allocate memory for a complete new matrix. Similarly, if you have lots of small matrices that you are making changes to, that would consume a lot of time as well.
  10 Commenti
Stephen23
Stephen23 il 22 Nov 2023
Modificato: Stephen23 il 22 Nov 2023
I second Matt J's advice, particularly if you have situations where particular access is repeated many times:
temp = A.b.c.d;
for k = lots
..temp..
end
A.b.c.d = temp;

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by