Efficienty in accessing memory

3 vues (au cours des 30 derniers jours)
Nicola Donelli
Nicola Donelli le 15 Sep 2015
Commenté : Walter Roberson le 15 Sep 2015
I am writing an interative algorithm that essentially consits of a big for loop with some sub-loops. At every iteration of the outer loop I have to save some variables in a cell array called MC (I have pre-allocated the memory for MC). The variables saved in one iteration are not used in subsequent iterations. Is it more efficient to create a "temporary variable" to be used inside every iteration, saved in the MC cell-array and then overwritten the nex iteration or to directly create the variable in the cell-array and use it for the operations inside the iteration?
For example, is it more efficient a thing like:
for t=2:1e5
N = floor(log(min(u * c)) / log((c / (kappa * (1 + c)))));
MC{t}.N = N;
% vector of nj values
n_vec = sum(repmat(d, 1, N) == repmat(1:N, T, 1))';
n_vec2 = sum(repmat(d, 1, N) > repmat(1:N, T, 1))';
...
end
or something like:
for t = 2 : 1e5
MC{t}.N = floor(log(min(u * c)) / log((c / (kappa * (1 + c)))));
% vector of nj values
n_vec = sum(repmat(d, 1, MC{t}.N) == repmat(1:MC{t}.N, T, 1))';
n_vec2 = sum(repmat(d, 1, MC{t}.N) > repmat(1:MC{t}.N, T, 1))';
....
end
Keep in mind that this is only an example: there are 12 variables that behaves like N in the example and some of them are quite big tridimensional arrays.
P.S. I am running the algorithm using version 2015a on a Server with a 64bit OS, a RAM of 128GB and a 3.5GHz CPU
Thanks
  2 commentaires
Cedric
Cedric le 15 Sep 2015
Modifié(e) : Cedric le 15 Sep 2015
It is difficult to say a priori, because it is difficult to anticipate what JIT will do exactly. You should build a small benchmark and profile all relevant approaches (using both the profiler [type profile viewer in the command window and run your script there] and tic/toc). You may realize that REPMAT is the/a bottleneck and try to implement some alternate approach based on matrix multiplication with a precomputed matrix and/or some call to BSXFUN.
James Tursa
James Tursa le 15 Sep 2015
Modifié(e) : James Tursa le 15 Sep 2015
How are you pre-allocating MC? Just the MC, or all of the MC{etc}.N? Can you use a simple double vector for these values instead of a field of a struct of a cell array? Seems like you are re-doing some calculations when forming n_vec and n_vec2.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 15 Sep 2015
If you were using the Parallel Computing Toolbox, you would need to compute everything into a temporary variable and then write it in to the output indexed by the parfor loop index.
The JIT compiler is able to detect variables that are completely overwritten each time, and handle them more efficiently -- though it would not hurt to clear the variable at the end of the loop to hint even more strongly.
  2 commentaires
Nicola Donelli
Nicola Donelli le 15 Sep 2015
Sorry, I forgot to specify that I am not using the Parallel Toolbox since the algorithm cannot be parallelized.
Walter Roberson
Walter Roberson le 15 Sep 2015
None the less, I would expect better optimization by using loop-local variables to build the value to be saved as an aggregate.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by