Vectorization or for loop (speedup)

1 回表示 (過去 30 日間)
Muna Tageldin
Muna Tageldin 2020 年 9 月 25 日
コメント済み: Matt J 2020 年 9 月 29 日
I have a function the do operations on 3 dimensional matrix like this: user_defined_function is a function that does the calculations
a=rand(100,100,100);
b=rand(100,100,100);
c=rand(1,1000);
l11=zeros(size(a))
for i=1:1:100
for j=1:1:100
for o=1:1:100
l11=user_defined_fnuction (a(o,j,i),b(o,j,i));
end
end
end
Basically the for-loop is slow so I tried to vectrize it. Using vectorization, I end up having a matrix of size (1e6,1e6) which exceeded maximum array size (requires 7320 GB). So I looked over the internet and I found this:
I tried the tall array but still I got the same problem. How I could solve this problem of maximum array size ( without the need to use spmd or for-drange since I am using parfor in the main program and the for loop is part of a function that does calculations).
Does deploying this code to MATALB server speedup the code
  4 件のコメント
Matt J
Matt J 2020 年 9 月 25 日
Fine, but it is still not clear how this results in a 1e6 x1e6 matrix. Your loops are still performing only 1e6 calculations.
Muna Tageldin
Muna Tageldin 2020 年 9 月 25 日
編集済み: Matt J 2020 年 9 月 26 日
so basically l1 has a size of 100*100*100. if I want to vectorize the above code the following is done:
a=reshape(a,[1,size(a,1)*size(a,2),size(a,3)];
b=reshape(b,[1,size(b,1)*size(a,2),size(b,3)];
sz=size(a,1)*size(a,2),size(a,3)-size(c,2);
c=[c zeros(1,sz)];
l1=(a.'.*c.').*exp(-b)
Eventually using the above code, l1 is a mtrix of size 1e6*1e6 (error maximum array size reaced: out of memory)

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2020 年 9 月 26 日
Your vectorized code doesn't resemble at all what your for-looped code is computing. The vectorized implementation of your for-loops would be,
a=a(:);
b=b(:);
c=c(:).';
l1=(a.*b).*exp(-c.*a);
This will be a 1e6 x 1e3 matrix, which will be 4GB in single floats. A large matrix to be sure, but possible on a 32GB RAM machine. Why though do you need so much data in memory at the same time?
  2 件のコメント
Muna Tageldin
Muna Tageldin 2020 年 9 月 28 日
yes you are correct. I was trying to vectorise the code by appending zeros to the array z to match the sizes of a, b and c and then do multiplication. But in some cases, I would need to operate on large data sets. For example, c could be of size 1*1e10. then resulting matrix would be of size 1e10*1e6. In this case, I get memory out of bounds error. How can I solve this problem ?
Matt J
Matt J 2020 年 9 月 29 日
I can't say what the solution would be because I don't know how l11 is being used. It's hard to image why a 1e10 x 1e6 matrix would need all of its data in RAM simultaneously.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by