Parfor-loop not working, how to fix? - matlab

I am trying to parallize two of my for-loops and run it on a remote cluster.
I am using matlabpool open local 12 at the beginning with matlabpool close at the end. The problem I am running into is that my parfor-loop cannot use my matric properly and I am not sure how I would rewrite it so that it works.
H = hadamard(n);
H = [H;-H];
P = setdiff(P,H,'rows');
[r,c] = size(P);
A = zeros(n,r);
parfor i=1:r
for j=1:n
d = P(i,:) + H(j,:);
A(j,i) = sum(d(:) ~= 0);
end
end
and:
u2Had = cell(2,r);
parfor i =1:r
u2Had{1,i} = min(A(:,i));
MinHadIndex = find(A(:,i) == u2Had{1,i});
u2Had{2,i} = MinHadIndex;
end
Those are the two segments of the code I am trying to parallize. Any help is much appreciated and if I need to add anymore information please ask.

I don't know what your problem is in the first part as it works fine (perhaps if you defined P better)
regarding the second part, you can only send information to and from parloops in narrow cases.
Here change your code to the following:
u2HadT = cell(1,r);
parfor i =1:r
temp = min(A(:,i));
MinHadIndex = find(A(:,i) == temp);
u2HadT{i} = {temp;MinHadIndex};
end
u2Had = cell(2,r);
for i =1:r
u2Had{1,i} = u2HadT{i}(1);
u2Had{2,i} = u2HadT{i}(2);
end

Related

for cycle in copulapdf

I have to perform a copulapdf analysis and i need to create 8 different figures starting from varying nu (in this code called nuvar) from 1 to 8. I'm new in Matlab. I tried to write this for cycle but it doesn't works. Can someone help me please?
for nuvar= 1:1:8
for numvar1= 1:1:8
r=0.5;
nu=1;
u = linspace(0,1,20);
[u1,u2] = meshgrid(u,u);
rho1 = copulaparam('t',r,nu);
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
end
I also tried to correct the script in this way:
r=0.5;
nu=1;
u = linspace(0,1,20);
rho1 = copulaparam('t',r,nu);
[u1,u2] = meshgrid(u,u);
for numvar1= 1:1:8
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
I have the same error "Subscripted assignment dimension mismatch".

Parallelizing MATLAB code

Hello I want to use parallelize my MATLAB code to run High computing server. It is code to make image database for Deep learning. To parallelize the code I found the I have to for parfor loop. But I used that with the first loop or with the second loop it shows me error parfor cannot be run due to the variable imdb and image_counter. Anyone please help me to change the code to work with parfor
for i = 1:length(cur_images)
X = sprintf('image Numb: %d ',i);
disp(X)
cur_image = load(cur_images{i,:});
cur_image=(cur_image.Image.crop);
%----------------------------------------------
cur_image = imresize(cur_image, image_size);
if(rgb < 1)
imdb.images.data(:,:,1,image_counter) = cur_image;
else
imdb.images.data(:,:,1,image_counter) = cur_image(:,:,1);
imdb.images.data(:,:,2,image_counter) = cur_image(:,:,2);
imdb.images.data(:,:,3,image_counter) = cur_image(:,:,3);
imdb.images.data(:,:,4,image_counter) = cur_image(:,:,4);
imdb.images.data(:,:,5,image_counter) = cur_image(:,:,5);
imdb.images.data(:,:,6,image_counter) = cur_image(:,:,6);
imdb.images.data(:,:,7,image_counter) = cur_image(:,:,7);
imdb.images.data(:,:,8,image_counter) = cur_image(:,:,8);
imdb.images.data(:,:,9,image_counter) = cur_image(:,:,9);
imdb.images.data(:,:,10,image_counter) = cur_image(:,:,10);
end
imdb.images.set( 1,image_counter) = set;
image_counter = image_counter + 1;
end
The main problem here is that you can't assign to fields of a structure inside parfor in the way that you're trying to do. Also, your outputs need to be indexed by the loop variable to qualify as "sliced" - i.e. don't use image_counter. Putting this together, you need something more like:
% Make a numeric array to store the output.
data_out = zeros([image_size, 10, length(cur_images)]);
parfor i = 1:length(cur_images)
cur_image = load(cur_images{i, :});
cur_image=(cur_image.Image.crop);
cur_image = imresize(cur_image, image_size);
% Now, assign into 'data_out'. A little care needed
% here.
if rgb < 1
data_tmp = zeros([image_size, 10]);
data_tmp(:, :, 1) = cur_image;
else
data_tmp = cur_image;
end
data_out(:, :, :, i) = data_tmp;
end
imdb.images.data = data_out;

Not enough input arguments when using For Loop over an Integral

When I run the following code in matlab, I get the "Not enough input arguments."
It is not clear to me what I am doing wrong. I have checked it over an over again, but obviously what I intend and what the computer says I intend do not match up.
tau = 50e-3;
b= 1;
dt = .2;
time=1:dt:100;
w = 1; W= w;
inte = zeros(1, length(time));
inte(1) = 0;
Ds = #(s) (w*s-s+b)*(1/tau);
for i=1:length(time)
inte(i) = integral(Ds,0,time(i));
end
My hope was that the loop would integrate with different values of time from the time array. Instead, it appears to be looping through and only integrating once.
Any and all help is appreciated.
Update to include full error message:
% Validate the first three inputs.
narginchk(3,inf);
if ~isa(fun,'function_handle')
error(message('MATLAB:integral:funArgNotHandle'));
end
if ~(isscalar(a) && isfloat(a) && isscalar(b) && isfloat(b))
error(message('MATLAB:integral:invalidEndpoint'));
end
opstruct = integralParseArgs(varargin{:});
Q = integralCalc(fun,a,b,opstruct);

parallel for loop in Matlab

I am using a parfor in my code, I read all parfor limitations and I think I am doing everything right:
for it = 1:maxiter
rep_it = it;
parfor Fo = allFo
rep_Fo = Fo;
Fmax = 2*Fo;
find_rep = find(rep_Fo==allFo) ;
[Fac, c, F_est,loss] = AutoTen(info.Data,Fmax,2);
[Fac, F_est_baseline3] = AutoTenBaseline(info.Data,Fmax,1);
[Fac, F_est_baseline3] = AutoTenBaseline(info.Data,Fmax,2);
est_rank(find_rep,rep_it) = F_est;
est_rank_baseline1(find_rep,rep_it) = F_est_baseline3;
est_rank_baseline2(find_rep,rep_it) = F_est_baseline3;
end
end
But I get the error:
Error: The variable est_rank in a parfor cannot be classified
Any help is appreciated how to solve this.
Your problem comes from the find_rep and rep_it variables that are used for indexing. If you want est_rank to be sliceable, you have to use the Fo index for it like:
est_rank(Fo, rep_it) = F_est;
otherwise you (i.e. MATLAB) cannot guarantee that you won't try to access the same cell in the variable from two parallel processes.
If I got your intention correctly, then this fix should do it:
for it = 1:maxiter
parfor Fo = 1:numel(allFo)
Fmax = 2*allFo(Fo);
[Fac, c, F_est,loss] = AutoTen(info.Data,Fmax,2);
[Fac, F_est_baseline3] = AutoTenBaseline(info.Data,Fmax,1);
[Fac, F_est_baseline3] = AutoTenBaseline(info.Data,Fmax,2);
est_rank(Fo,it) = F_est;
est_rank_baseline1(Fo,it) = F_est_baseline3;
est_rank_baseline2(Fo,it) = F_est_baseline3;
end
end
Let me know if it did the trick ;)

Undefined function or variable using parfor in matlab

I'm unable to execute the following code:
parallel_mode = true; %To process multiple images at the same time
parallel_degree = 4; %Number of threads that will be created
if parallel_mode
if matlabpool('size') == 0
matlabpool(parallel_degree);
elseif matlabpool('size') ~= parallel_degree
matlabpool close;
matlabpool(parallel_degree);
end
end
%Loading dictionary
try
load(dictionary_path);
catch
error(['Was impossible to load the dictionary in path: ' dictionary_path ', Please, check the path. ' ...
'Maybe you should use dictionary_training function to create it.'])
end
%% Processing test images
test_images = dir([test_im_path, pattern]);
num_test_images = size(test_images,1);
%Pre-allocating memory to speed-up
estimated_count = zeros(1,num_test_images);
true_count = zeros(1,num_test_images);
estimated_upper_count = zeros(1,num_test_images);
estimated_lower_count = zeros(1,num_test_images);
true_upper_count = zeros(1,num_test_images);
true_lower_count = zeros(1,num_test_images);
%Calculating dimensions of the image subregion where we can count
im_test = imread([test_im_path test_images(1).name]);
dis = round(patch_size/2);
dim_x = dis:size(im_test,2)-dis+1;
dim_y = dis:size(im_test,1)-dis+1;
toGaussian = fspecial('gaussian', hsize, sigma);
parfor a=1:num_test_images
disp(['Processing image #' num2str(a) ' of ' num2str(num_test_images) '...']);
im_test = imread([test_im_path test_images(a).name]);
[~, name, extension] = fileparts(test_images(a).name);
im_ground_truth = imread([ground_truth_path name 'dots' extension]);
disp('Extracting features...');
features = extract_features(im_test, features_type, dic_signal, sparsity, patch_size, mean_rem_flag);
features = full(features); %ND-sparse arrays are not supported.
%Re-arranging features
features = reshape(features', size(dim_y,2), size(dim_x,2), dic_size);
%Normalizing features
max_factors_3D = repmat(max_factors_depth, [size(features,1), size(features,2)]);
max_offset_3D = repmat(max_offset_depth, [size(features,1), size(features,2)]);
features = (features-max_offset_3D)./max_factors_3D;
%%Some stuff
....
end
When i execute it i get:
Undefined function or variable 'dic_signal'.
when it arrives to the extract_features function. However, in the single thread version (with for instead of parfor) it works correctly.
Someone can give me any hint?.
Thank you.
EDIT:
dic_signal is defined and correctly loaded in load(dictionary_path);
I suspect the load command doesn't load the variables in the workers workspace, only on your MATLAB instance workspace, which is why it works in a normal for loop, but not with a parfor loop. You might want to try instead:
pctRunOnAll load(dictionary_path)
to ensure the correct data is loaded into the workspace of each of the workers.