Hi, I have problem parallelizing the following code:
parfor T_f=1:Nt,
disp(T_f);
me = obj.trian(1:3,T_f);
ind_me = find(me~=0);
me = me( ind_me );
sign_me = sign( me ).';
me = abs( me ) ; % indices that correspond to rows of matrix elements
vert_me = obj.vertex( : , obj.topol(ind_me,T_f) ); % defines coordinates of each vertex in triangle
[r_field,wght_f] = get_r_field(r1,r2,r3,T_f,n_f); % integracijske tocke svakog trokuta field points
tmp_end = zeros( 3 , Ne );
for n=1:n_f,
rf=r_field(:,n); % svaka pojedina integracijska tocka ( x, y ,z ) koordinate
%%%Surface analytical integration inside the triangles: terms 1/R, (rho'-rho)/R
[ tmp_rhodivR , tmp_1divR ] = int_divR( rf , r1 , r2 , r3 , obj );
% Low order term; integrated numerically
tmp_A_f0 = zeros(3,Ne);
tmp_phi_f0 = zeros(1,Ne);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%NUMERICAL INTEGRATION %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for p=1:n_s,
rs = r_source(3*(p-1)+1:1:3*p,:); % source points defined on all triangles ( jedna tocka, koordinate x,y,z)
rho_plus = 0.5 * ( rs(:,Tp_s) - obj.vertex(:,vp_s) );
rho_less = 0.5 * ( rs(:,Tm_s) - obj.vertex(:,vm_s) ); % vectors of RWG defined
Rvec = rf*ones(1,Nt) - rs;
R = sqrt(sum(Rvec.^2));
i_zro = find(R<eps);
if length(i_zro), R(i_zro)=1;
end
term_pot = ( exp(-j*k*R) - 1 )./R;
if length(i_zro), term_pot(i_zro)=-j*k;
end
tmp_A_f0 = tmp_A_f0 + (-j*k*eta*wght_s(p))*...
( ( ones(3,1)*term_pot( Tp_s ) ) .* rho_plus - ...
( ones(3,1)*term_pot( Tm_s ) ) .* rho_less );
tmp_phi_f0 = tmp_phi_f0 + j*(eta/k)*wght_s(p)*...
( term_pot( Tp_s ) - ...
term_pot( Tm_s ) );
end %%n_s
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%ANALYTICAL INTEGRATION %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Vector potential analytical update
rf_rv_pl = rf * ones(1,Ne) - obj.vertex(:,vp_s);
d_pl = sum( rf_rv_pl .* obj.un(:,Tp_s) );
d_vec_pl = ( ones(3,1) * d_pl ) .* obj.un(:,Tp_s); % udaljenost od vrhova source trokuta do projekcijske tocke field tocke u ravnini tog trokuta T+
rhof_rhov_pl = rf_rv_pl - d_vec_pl;
A_anal_plus = ( ones(3,1) * sc_pl ) .* ( tmp_rhodivR(:,Tp_s) + rhof_rhov_pl .* ( ones(3,1) * tmp_1divR(Tp_s) ) );
rf_rv_mn = rf * ones(1,Ne) - obj.vertex(:,vm_s);
d_mn = sum( rf_rv_mn .* obj.un(:,Tm_s) );
d_vec_min = ( ones(3,1) * d_mn ) .* obj.un(:,Tm_s); % udaljenost od vrhova source trokuta do projekcijske tocke field tocke u ravnini tog trokuta T-
rhof_rhov_mn = rf_rv_mn - d_vec_min;
A_anal_min = ( ones(3,1) * sc_min ) .* ( tmp_rhodivR(:,Tm_s) + rhof_rhov_mn .* ( ones(3,1) * tmp_1divR(Tm_s) ) );
tmp_A_f0 = tmp_A_f0 + (-j*k*eta)*( A_anal_plus - A_anal_min );
%%%Scalar potential analytical update
phi_anal_plus = ( sc_div_pl .* tmp_1divR(Tp_s) );
phi_anal_min = ( sc_div_min .* tmp_1divR(Tm_s) );
tmp_phi_f0 = tmp_phi_f0 + j*(eta/k)*( phi_anal_plus - phi_anal_min );
%%Potential vector term
for q=1:3,
tmp_end(q,:) = tmp_end(q,:) + 0.5 * wght_f(n) * ... % testing with RWG function
sum( ( ( rf - vert_me(:,q) ) * ...
ones(1,Ne) ) .* tmp_A_f0 );
end %%for q=1:length(ind_me),
%%Potential scalar term
tmp_end = tmp_end + wght_f(n)*( ones(length(ind_me),1) * tmp_phi_f0);
end %%n_f
aux_end = (1/(4*pi))*( (sign_me.')*ones(1,Ne) ).*tmp_end;
Z(me,:) = Z(me,:) + aux_end;
end %% for T_f=1:Nt
as you can see, matrix Z is indexed with me, which is a column. me is formed inside parfor loop (at the beginning), I know this is wrong and this is the reason why it does not work, but I think I can not avoid that.. Is there any way to parallelize this in Matlab?

 Réponse acceptée

Jan
Jan le 23 Mai 2017

0 votes

You can collect the outputs aux_end and me of every parfor loop in a cell string and perform the addition afterwards. Most likely the sum Z(me,:) + aux_end is not the bottleneck of the code, such that running this sequentially instead of a parallelized version will not degrade the runtime remarkably.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by