PARFOR Transparency violation error
이전 댓글 표시
Why am I getting the transperancy error for the following code?
Error using syms (line 216)
Transparency violation error.
See Parallel Computing Toolbox documentation about Transparency
clc
clear
alpha =4; % Path loss exponent
del = 2/alpha;
R =1;
N = 4;
p_k = [0.15 0.05 0.45 0.35];
epsi = 1;
N_set = 1:N;
lambda_dash = 0.05:0.05:0.5;
spec_eff1 = zeros(length(lambda_dash),N);
parfor vv = 1:length(lambda_dash)
vv
comm_term = lambda_dash(vv)*pi*(epsi+R^alpha).*gamma(1+del)*gamma(1-del);
syms tt
fun_int1 = zeros(1,N);
for k = 1:N
term = 0;
for ii = 1:N
m =max(ii-N+k,0):1:min(k,ii);
p_intf = (factorial(k)./(factorial(m).*factorial(k-m))).*(factorial(N-k)./(factorial(ii-m).*factorial(N-k-ii+m)))./(factorial(N)./(factorial(ii).*factorial(N-ii)));
term =term + (sum(p_k(ii).*p_intf.*((2^(tt)-1).*m./k).*((epsi+R^alpha).*(2^(tt)-1).*(m./k) + epsi).^(del-1)));
end
f = exp(-comm_term.*term);
fun_int1(k) = vpa(int(f,[0 inf]));
end
spec_eff1(vv,:) = fun_int1;
end
spec_eff = sum(p_k.*spec_eff1,2);
semilogy(lambda_dash,spec_eff,'k-')
hold on
grid on
채택된 답변
추가 답변 (1개)
gonzalo Mier
2019년 6월 17일
0 개 추천
Parfor loop don't work in GPU, it just multithread your CPU code, so you have to be careful with the variables you use because all of them can change the values of the variables that are accessed by other threads, so the behavior is not predefined. For that, matlab create this error if more than one thread can access to the same memory space. I recommend to use cells for each variable so you don't have any problems with the threats. Also, defining a symbolic variable inside the parfor is a bad idea for the same reason.
댓글 수: 3
Andrea Picciau
2019년 6월 18일
Hi Gonzalo,
I would like to clarify two points in your answer. Let me know if this helps you!
parfor and GPUs
parfor and gpuArrays can actually be used together (see this answer) and that allows you to use multiple GPUs.
In a nutshell, parfor works by starting a number communicating MATLAB instances called workers inside a common environment called parallel pool (you can find more details about how parallel pools work on this doc page). Each worker is automatically associated with a GPU when it starts.
If you want to parallelize MATLAB code on a single GPU, you can do one of (or a combination of) these things:
- convert your data to gpuArrays at the beginning of your code, and take advantage of the fact that many functions already accept gpuArray inputs and parallelise your code automatically.
- use functions like arrayfun or pagefun, which also accept gpuArray inputs and allow you to apply the same custom function to all the elements of the gpuArray.
- write a custom CUDA kernel and call mexcuda to use it within MATLAB.
If you do any of these things inside a parfor, each worker will use its GPU to do the computations.
symbolic variables inside parfor
I don't think there's anything wrong about defining a symbolic variable inside a parfor. See my answer to this question for how to do it safely...
gonzalo Mier
2019년 6월 20일
thank you, that was really helpful :)
Andrea Picciau
2019년 6월 20일
You're welcome!
카테고리
도움말 센터 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!