Asked by Giorgia
on 20 Jun 2017

I've been getting the common "Error: The variable ph_samples_volume in a parfor cannot be classified." and I cannot understand how to fix it. I have been reading the answers to similar questions but I haven't figured out how to apply it for my case. I think it has to do with the way I am slicing my variable but I am not sure how to improve the current slicing. Thank you in advance for your help, Giorgia

x_dim=96;

y_dim=112;

z_dim=96;

nsamples=50;

for i = 1:max_fib

eval(strcat('ph_samples_volume(:,:,:,:,i) = zeros(dimension(1),dimension(2),dimension(3),nsamples);'));

eval(strcat('th_samples_volume(:,:,:,:,i) = zeros(dimension(1),dimension(2),dimension(3),nsamples);'));

end

parfor z=1:z_dim

for x=1:x_dim

for y=1:y_dim

if odf_full(x,y,z) ~= 0 && mask(x,y,z)~=0

%get odf at x,y,z

odf = odfs(:,odf_full(x,y,z)); %odf at [x y z] location

p_idx=index(x,y,z,:)+1;

[ph, th] = find_samples( odf, nsamples, odf_vertices, odf_faces, p_idx); %in deg

curr_fibers=length(p_idx(p_idx~=0)); %number between 1 and 3

for i=1:curr_fibers

peak_number=strcat('peak',num2str(i));

ph_samples=[ph.(peak_number)];

th_samples=[th.(peak_number)];

ph_samples_volume(x,y,z,:,i) = ph_samples; %in deg

th_samples_volume(x,y,z,:,i) = th_samples; %in deg

end

end

end

end

end

Answer by Eric
on 20 Jun 2017

Try replacing the for loop with the eval() statements with

[ph_samples_volume, th_samples_volume] = deal(zeros(dimension(1),dimension(2),dimension(3),nsamples,max_fib));

Giorgia
on 21 Jun 2017

Hello Eric,

Thank you for you suggestion but I still get the same error even with your correction. Any other ideas?

Giorgia

Eric
on 21 Jun 2017

It's hard to debug this without the code, but here are a few thoughts:

1. Can you re-write find_samples() such that it is compatible with parfeval? It seems you would have to pass it x, y, z, odf_full, and mask in addition to the existing inputs. It could then create odf and p_idx itself. It would also need to do the checking of the if statement and return zeros when that case fails.

2. The outer three for loops could be replaced by a single for loop. You could calculate z, x, and y from that single index. My guess is that won't help, though.

3. Is there a way to re-write find_samples such that it returns a matrix rather than a structure? It would allow considerably simpler indexing. I wonder if the anonymous indexing into a structure is causing Matlab grief in classifying variables.

4. Before parallelizing, it's usually worthwhile figuring out where the bottleneck is in the first place. Presumably the bottleneck is the find_samples() function. Is there anything in that code that could be improved? Often times people come to me for help parallelizing code or running it on a cluster when they haven't used the profiler. Once we see what's causing the slowdown, we can often find a solution to the problem that makes parallelizing the code unnecessary. If you haven't profiled your code, I would recommend giving it a try. You can start the profiler by running

profile viewer

at the command prompt.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Jan (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345580-error-the-variable-in-a-parfor-cannot-be-classified#comment_463147

## Giorgia (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345580-error-the-variable-in-a-parfor-cannot-be-classified#comment_463327

Sign in to comment.