How parallelize the solution of sparse matrices using mldivide

6 ビュー (過去 30 日間)
Emil Partsch
Emil Partsch 2019 年 2 月 28 日
編集済み: Emil Partsch 2019 年 3 月 5 日
I am trying to parallelize the solution of
x= A\B (mldivide.
My variables are: x = V(I*J*K), A = A(I*J*K x I*J*K) sparse matrix, vec = u (I,J,K) +V/constant +Bswitch (I*J*K x I*J*K)*V
To do this without parallelization, my code currently does this:
V_stacked = reshape(V,I*J*L,1);
vec = u_stacked + V_stacked/Delta + Bswitch*V_stacked;
V_stacked = A\vec;
To parallelize I have tried
u_stacked = reshape(u,I*J,L);
V_stacked = reshape(V,I*J,L);
BswitchTimesVstacked = Bswitch*reshape(V,I*J*L,1);
BswitchTimesVstacked = reshape(BswitchTimesVstacked,I*J,L);
vec = u_stacked + V_stacked/Delta + BswitchTimesVstacked;
tic
parfor l = 1:L
V_stacked(:,l) = A(:,:,l)\vec(:,l);
end
But as A is still I*J*L times I*J*L, it wont work. I am not sure if 1. what I am doing so far is correct and 2. how to reshape B appropriately.
Any help is highly appreciated :-)
  4 件のコメント
Matt J
Matt J 2019 年 3 月 1 日
V_stacked(:,l) = A(:,:,l)\vec(:,l);
If A is sparse, how in the above line did you reshape it into a 3D array so as to make it 3D indexable?
Emil Partsch
Emil Partsch 2019 年 3 月 1 日
編集済み: Emil Partsch 2019 年 3 月 1 日
I didn't - I made a mistake in the last sentence which I just edited: it should have said "But as A is still I*J*L times I*J*L, it wont work."
The reason I'm doing it as so is that I am trying to replicate how it's done here starting at line 120: https://github.com/ikarib/HANK/blob/master/mycode/HJBUpdate.m

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

採用された回答

Matt J
Matt J 2019 年 3 月 4 日
You can make a 3D stack of sparse matrices A(:,:,l) by converting them to ndSparse type. Then, the parfor construct will work. Alternatively, you can make a block diagonal matrix where all the A(:,:,l) form the diagonal blocks. Then you can solve all systems simultaneously, which would take advantage of Matlab's internal parallelization. I don't know which would be faster.
  5 件のコメント
Matt J
Matt J 2019 年 3 月 4 日
Was it faster when you used parfor? I would not expect so.
Emil Partsch
Emil Partsch 2019 年 3 月 5 日
編集済み: Emil Partsch 2019 年 3 月 5 日
It depends on the size of the I, J and K. For the sizes I originally used, it didn't really matter

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

その他の回答 (1 件)

Matt J
Matt J 2019 年 2 月 28 日
編集済み: Matt J 2019 年 2 月 28 日
I recommend using
V_stacked = pagefun(@mldivide,gpuArray(A),gpuArray(vec));
  1 件のコメント
Emil Partsch
Emil Partsch 2019 年 3 月 1 日
Doing that, I get the error that sparse gpuArrays are not supported for mldivide :-)

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by