tall array introduces significant overhead if I call gather() at every iteration in a loop

3 visualizzazioni (ultimi 30 giorni)
I want to figure out the spread of the invariants of a symmetric 3x3 matrix using the code below
%input
nPts = 2;
ub = 2.0;
%create combinations of the SIX independent components
comb = combinations(linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts)).Variables;
%results array
res = zeros(nPts^6, 2);
tic
for idx=1:nPts^6
C = [comb(idx,1), comb(idx,4), comb(idx,5);
comb(idx,4), comb(idx,2), comb(idx,6);
comb(idx,5), comb(idx,6), comb(idx,3)];
res(idx, :) = [trace(C), trace(inv(C))];
end
toc
%creater scatter plot
scatter(res(:,1), res(:,2));
This works, however, I want to set nPts=60 for instance. So I will definitely end up with array sizes that do not fit into memory of my local machine anymore.
As a workaround, I thought storing comb as tall array
comb = tall( combinations(linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts), ...
linspace(0.1, ub, nPts)).Variables );
and then
C = gather(C)
res(idx, :) = [trace(C), trace(inv(C))];
in the loop.
However, this code takes 170 seconds on my screen for just 2^6 = 64 combinations.
So is tall array not appropriate in my application or do I just use it not correctly?

Risposta accettata

Harald
Harald il 13 Lug 2023
Modificato: Harald il 13 Lug 2023
Hi,
tall arrays are more typically used when you have a single file or a set of files that is too large to be imported into memory at once.
One way I see to solve the challenge you are facing is to create only parts (e.g., leaving one of the variables constant for the time would "only" require 60^6 * 8 * 2 bytes = about 12 GB rather than 60^6 * 8 * 2 bytes = about 746 GB - sizes corrected compared to original post) of the data at a time and do the preprocessing you will need for the scatter plot. For example, you could use histcounts2 to bin the data, accumulate this over the parts, and then use heatmap for visualization.
That's just the idea - please let me know if you want to pursue this and need additional help with the implementation.
Another question will be computational efficiency. For this example, you can explicitly calculate the trace of the inverse, and it is a not overly complicated formula:
syms a b c d e f
M = [a d e; d b f; e f c];
simplify(trace(inv(M)))
That way, you can calculate the trace of a huge number of matrices and their inverses in a vectorized way.
This may also give you additional insights, such as: trace of inverse will be large, when denominator of that expression is close to 0.
Best wishes,
Harald
  6 Commenti
SA-W
SA-W il 14 Lug 2023
@Harald I think I found the mistake in your code:
I had to transpose the N matrix before passing it to scatter, i.e.,
N = N';
scatter(x(:), y(:), [], N(:))
colorbar
Does that make sense?
Harald
Harald il 14 Lug 2023
Good catch, that's it!
I hope that your question is then answered. If so, please consider marking the answer as "accepted".
Thanks and best wishes,
Harald

Accedi per commentare.

Più risposte (0)

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by