Asked by IIT Pan
on 17 Jun 2019

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

Answer by Andrea Picciau
on 18 Jun 2019

Accepted Answer

The problem occurs in this line:

syms tt

Although the syms function is very handy for hacking symbolic math on your client machine, it modifies the global state of the workspace in a way that is not suitable for parallel computations (and parfor in particular). For parallel computations, use the sym object constructor and change that line to:

tt = sym('tt');

In general, with parfor it's better to use functions that construct and return objects (like the function sym), which are transparent in most cases.

saddam alqasimi
on 20 Sep 2019

I'm working on parfor, I'm getting the same error which I think due to a similar mistake.

would you mind taking a look, please

%% CEDAS parameters

radius = 0.1; %0.005; % CEDAS microC radius

decay = 100; % Number of data samples to consider 'recent data'

fade = 1/decay;

minThreshold = 4; % Min microC threshold

mergeTimeInterval = 150; %150

minimumNumberOfLinks = 25;

gridGranularity =0.01 ; %1

plotSpeed = inf;

datalogSpeed =1;

vali = 0;

parfor n= 1:2

%% Initialization

clustersRelations = struct('relation',[],'connectedCMCs',[],'timer',[]);

numberOfClusters = 0;

temp = dataset1(:,1:end-1);

d=size(temp,1);

maxVal = ceil(max(temp(:)));

minVal = floor(min(temp(:)));

if minVal <0

gridDimensions = repmat(ceil(maxVal-minVal/gridGranularity),size(dataset1,2)-1,1);

else

gridDimensions = repmat(ceil(maxVal/gridGranularity),size(dataset1,2)-1,1);

end

cmcs = struct('centre',[],'cluster',[],'edge',[],'gridCoordinates',[],'count',[],'life',[],...

'pointsIndices',[]);

outliers = struct('centre',[],'cluster',[],'edge',[],'gridCoordinates',[],'count',[],'life',[],...

'pointsIndices',[]);

if (n==1)

for counter=0:size(dataset1,1)/sampleSpeed

time= mod(counter,3006)+1; % just because time= time+1 not acceptable

for i = 1:sampleSpeed

c1= counter*sampleSpeed+i;

if c1<size(dataset1,1)

dataPoint = dataset1(c1,end-1);

[cmcs,outliers,clustersRelations,numberOfClusters] = Assign(cmcs,outliers,dataPoint,radius,c1,...

gridGranularity,gridDimensions,minThreshold,numberOfClusters,clustersRelations,mergeTimeInterval);

end

end

[cmcs,outliers,clustersRelations,numberOfClusters] = Kill(cmcs,outliers,fade,clustersRelations,numberOfClusters);

[cmcs,clustersRelations,numberOfClusters] = UpdateClustersRelationTable(cmcs,clustersRelations,...

minimumNumberOfLinks,numberOfClusters);

if mod(time,datalogSpeed)==0

%dataLogged(time/datalogSpeed).graph = clustersGraph;

dataLogged(time/datalogSpeed).cmcs = cmcs;

dataLogged(time/datalogSpeed).outliers = outliers;

[targets,outputs,inds,oinds] = ExtractPointOutputsTargets(dataLogged,time/datalogSpeed,dataset);

ind = [1:size(dataset1,1)]';

ninds = setdiff(ind,[inds;oinds]);

dataLogged(time/datalogSpeed).targets = targets;

dataLogged(time/datalogSpeed).outputs = outputs;

dataLogged(time/datalogSpeed).inds = inds;

dataLogged(time/datalogSpeed).oinds = oinds; % outliers cmcs

end

end

else

%another code

end

end

Andrea Picciau
on 20 Sep 2019

Hi Saddam,

I can't see any usage of syms in your code.

Take a look at your error. It should be something like this:

Error using XXXXX (line NNN)

Transparency violation error.

it should give you a starting point to debug your code. Read this to see how to do it!

Sign in to comment.

Answer by gonzalo Mier
on 17 Jun 2019

Andrea Picciau
on 18 Jun 2019

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
on 20 Jun 2019

thank you, that was really helpful :)

Andrea Picciau
on 20 Jun 2019

You're welcome!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.