tall array introduces significant overhead if I call gather() at every iteration in a loop
10 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Harald
2023-7-13
编辑:Harald
2023-7-13
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 个评论
Harald
2023-7-14
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!