How parallelize the solution of sparse matrices using mldivide
이전 댓글 표시
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.
Mathematical info is here: http://www.princeton.edu/~moll/HACTproject/two_asset_kinked.pdf (section 4)
Any help is highly appreciated :-)
댓글 수: 4
Heiko Weichelt
2019년 3월 1일
Hi Emil
In order to give you the most helpful answer, let me ask you a few clarifying questions:
- What is your motivation behind trying to paralellize the sparse MLDIVIDE step?
- What is your expected benefit?
- What sizes will you use (smallest and largest)?
- Do you have a GPU or a cluster available?
More specific to the code you provided:
- Is x = V(I*J*K x I*J*K) and Bswitch (I*J*K x I*J*K) dense or sparse?
- If V is I*J*K x I*J*K and you use V_stacked = reshape(V,I*J*L,1), I assume you meant V_stacked = reshape(V,(I*J*L)^2,1);
In general, notice that sparse MLDIVIDE already uses multi-threading, which means the algorithm itself performs some steps parallel. Usually, as long as the matrix fits in memory and MLDIVIDE's additional memory usage (which can be large compared to the original sparse matrix due to fill-in) can be handled by the avaliavle RAM, it is hard to write a faster algorithm yourself.
Also see https://www.mathworks.com/matlabcentral/answers/95958-which-matlab-functions-benefit-from-multithreaded-computation for more information about multi-threading.
Best,
Heiko
Emil Partsch
2019년 3월 1일
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
2019년 3월 1일
편집: Emil Partsch
2019년 3월 1일
채택된 답변
추가 답변 (1개)
I recommend using
V_stacked = pagefun(@mldivide,gpuArray(A),gpuArray(vec));
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!