MATLAB parfor error - matlab

I have a Matlab function in which I use parfor and it goes like this:
sbestposition=zeros(8,38);
sbestvalue=zeros(8,1);
matlabpool open 8
parfor i=1:8
[ sbestvalue(i) sbestposition(i,1:i+30)] = pso(lb, ub, s2, i+31, 30,1,2,2 );
end
matlabpool close
The sizeof sbestposition depends of the value of i and it is (1,31) when i=1 and (1,38) when i=8; and I intend to keep their values in one variable.
The function works when it is [ sbestvalue(i) sbestposition(i,:)] = pso(lb, ub, s2, i+31, 30,1,2,2 ); But when I use it, the function will not work again because of the dimension mismatch error.
Instead, here it gives an error
The variable sbestposition in a parfor cannot be classified
How do I overcome this?
Thank you.

Why not use a cell array for sbestposition
parfor i = 1:8
[sbestvalue(i), sbestposition{i}] = ...
end

Matlab does not "know" how to slice a variable in such a complex pattern. Every iteration uses at most the full row, most less that the row. Make it obvious that the variable should be sliced row wise:
parfor i=1:8
p=zeros(1,38);
[sbestvalue(i),p(1:i+30)]=pso(lb, ub, s2, i+31, 30,1,2,2 );
sbestposition(i,:)=p;
end
In this code, the toolbox can push each row to one worker, having obviously no interference.

You are changing the dimension of the variable inside the parloop.

Related

Undefined function or variable "new_m"

I am new to Matlab. I have three functions. When I call co.m and pol_det.m they both work fine. However, when I call minor.m which in itself calls pol_det which in its turn calls co.m I get an error referring to the co.m: Undefined function or variable "new_m".
I am working with R2007b version. The three functions are below. Originally, they are written each in a separate .m document.
function [ k ] = pol_det(a)
%calculates the determinant of a general matrix (not just consisting of
%numbers)
dim=size(a); %dimensions of a matrix
if dim(1)~= dim(2)
disp('Only Square Matrices, please')
end
m=length(a);
k=0;
if(m==2)
k=sum_p(conv(a(1,1),a(2,2)),- conv(a(2,1),a(1,2))); %calc. the determinant of a 2x2 m.
else
for i=1:m
k=k+((-1)^(1+i))*conv(a(1,i),co(a,1,i)); %calc. the determinant using cofactor expansion
end
end
if (k==0)
disp('Matrix non-invertible')
end
end
function [ out ] = co( a,i,j )
%cofactor expansion,
%http://people.math.carleton.ca/~kcheung /math/notes/MATH1107/wk07/07_cofactor_expansion.html
[m,n]=size(a);
%create a new matrix by eliminating the row and column in which the %element is present
%new_m=zeros(m,n)
row=1;
col=1;
for i1=1:m
for j1=1:n
if(i1~=i && j1~=j)
new_m(row,col)=a(i1,j1);
col=col+1;
end
end
if(col~=1)
row=row+1;
end
col=1;
end
%new_m
out=pol_det(new_m);
end
function [ m ] = minor(a)
dim=size(a); %dimensions of a matrix
if dim(1)~= dim(2)
disp('Only Square Matrices, please')
end
a=a.';
for i=1:dim(1)
for j=1:dim(1)
a(i,:)=[];
a(:,j)=[];
m(i,j)= pol_det(a);
end
end
end
Your problem is that, given certain values of a, i, and j, you may never enter the conditional statement inside your loops that initializes new_m. In such a case, the variable won't exist when you then get to the following line out=pol_det(new_m);.
You should set a default value for newm before your loops and conditional statements, such as [], so that the variable will always have a value. You should also make sure pol_det can appropriately handle this default value. Best practice is to make use of preallocation, both to improve performance and avoid conditional existence of necessary variables.

Looping a Function in Matlab

total newbie here. I'm having problems looping a function that I've created. I'm having some problems copying the code over but I'll give a general idea of it:
function[X]=Test(A,B,C,D)
other parts of the code
.
.
.
X = linsolve(K,L)
end
where K,L are other matrices I derived from the 4 variables A,B,C,D
The problem is whenever I execute the function Test(1,2,3,4), I can only get one answer out. I'm trying to loop this process for one variable, keep the other 3 variables constant.
For example, I want to get answers for A = 1:10, while B = 2, C = 3, D = 4
I've tried the following method and they did not work:
Function[X] = Test(A,B,C,D)
for A = 1:10
other parts of the code...
X=linsolve(K,L)
end
Whenever I keyed in the command Test(1,2,3,4), it only gave me the output of Test(10,2,3,4)
Then I read somewhere that you have to call the function from somewhere else, so I edited the Test function to be Function[X] = Test(B,C,D) and left A out where it can be assigned in another script eg:
global A
for A = 1:10
Test(A,2,3,4)
end
But this gives an error as well, as Test function requires A to be defined. As such I'm a little lost and can't seem to find any information on how can this be done. Would appreciate all the help I can get.
Cheers guys
I think this is what you're looking for:
A=1:10; B=2; C=3; D=4;
%Do pre-allocation for X according to the dimensions of your output
for iter = 1:length(A)
X(:,:,iter)= Test(A(iter),B,C,D);
end
X
where
function [X]=Test(A,B,C,D)
%other parts of the code
X = linsolve(K,L)
end
Try this:
function X = Test(A,B,C,D)
% allocate output (it is faster than changing the size in every loop)
X = {};
% loop for each position in A
for i = 1:numel(A);
%in the other parts you have to use A(i) instead of just A
... other parts of code
%overwrite the value in X at position i
X{i} = linsolve(K,L);
end
end
and run it with Test(1:10,2,3,4)
To answer what went wrong before:
When you loop with 'for A=1:10' you overwrite the A that was passed to the function (so the function will ignore the A that you passed it) and in each loop you overwrite the X calculated in the previous loop (that is why you can only see the answer for A=10).
The second try should work if you have created a file named Test.m with the function X = (A,B,C,D) as the first code in the file. Although the global assignment is unnecessary. In fact I would strongly recommend you not to use global variables as it gets very messy very fast.

How to use a variable outside a PARFOR loop in MATLAB?

In MATLAB, I have a variable proba and I have a parfor loop as showing below:
parfor f = 1:N
proba = (1/M)*ones(1, M);
% rest of the code
end
pi_proba = proba;
MATLAB said that: "The temporary variable 'proba' is used after the PARFOR loop, but its value is nondeterministic"
I do not understand how to correct this error. I need to use a parallel loop and I need proba after the loop. How to do this?
When using parfor the classes are classified according to these categories. Make sure every variable matches one of these categories. For non-writing access to proba a Broadcast-Variable would be the best choice:
proba = (1/M)*ones(1, M);
parfor f = 1:N
% rest of the code
end
pi_proba = proba;
In case of writing access within the loop, a sliced variable is nessecary:
proba=cell(1,N)
parfor f = 1:N
%now use proba{f} inside the loop
proba{f}=(1/M)*ones(1, M);
% rest of the code
end
%get proba from whatever iteration you want
pi_proba = proba{N};

Using Matlab Parfor with 'Eval'

I am trying to execute a parfor loop within a parent script for Matlab.
I want to calculate the implied volatility of an option price, and then create a new column within a preexisting dataset with the results.
load('/home/arreat/Casino/names.mat')
name = char(names(i))
%Loop over n rows to populate columns in dataset named using variable 'name(i)'
rows = eval(['length(',name,')'])
parfor n=[1:rows]
%Calculate implied volatility using blsimpv(Price, Strike, Rate, Time, Value, Limit,Yield, Tolerance, Class)
BidIV = blsimpv(eval([name,'.UnderlyingPrice(n)']),...
eval([name,'.Strike(n)']),...
RiskFree/100,...
eval([name,'.Lifespan(n)'])/252,...
eval([name,'.Bid(n)'])+.01,...
10,...
0,...
1e-15,...
eval([name,'.Type(n)'])...
)
eval([name,'.BidIV(n,1) = double(BidIV);']);
%Loop and add implied volatility (BidIV) to a column with n number of
%rows.
end
The problem arises with the 'eval()' calculation in the parfor loop. Mathworks suggested that I should turn the whole script into a function, and then call the function within the parfor loop.
While I work on this, any ideas?
Instead of calling eval all the time, you can call it once outside the loop, e.g. data = eval(name), and then use data.Strike etc inside the parfor loop.
To avoid calling eval at all, do the following:
%# load mat-file contents into structure allData, where
%# each variable becomes a field
allData = load('/home/arreat/Casino/names.mat');
data = allData.(name);

Using struct arrays in parfor

I am having trouble using struct arrays in Matlab's parfor loop. The following code has 2 problems I do not understand:
s=struct('a',{},'b',{});
if matlabpool('size')==0
matlabpool open local 2
end
for j = 1:2
parfor k=1:4
fprintf('[%d,%d]\n',k,j)
s(j,k).a = k;
s(j,k).b = j;
end
end
matlabpool close
It fails with an error Error using parallel_function (line 589)
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
On output, variable s is a vector, not an array (as it should be, even if the code breaks before finishing).
EDIT the problem is solved if I initialize the struct arrays to the correct size, by:
s=struct('a',cell(2,4),'b',cell(2,4));
However, I would still be happy to get insights about the problem (e.g is it rally a bug, as suggested by Oleg Komarov)
It was originally working fine for me but then I don't know what happens. In general you need to be careful with parfor loops and there are ample documentation on how to align everything. Two different words of advice.
First and more importantly, the parfor loop is on the outside loop:
function s = foo
s=struct('a',{},'b',{});
parfor j = 1:2
for k=1:4
fprintf('[%d,%d]\n',k,j)
s(j,k).a = k;
s(j,k).b = j;
end
end
Two, Matlab gets very picky about writing the main exit variable (i.e. the variable contained in the parfor loop which is indexed to the loop, in your case, s). You first want to create a dummy variable that holds all the innerloop information, and then writes to it once at the end of the loops. Example:
function s = khal
s=struct('a',{},'b',{});
parfor j = 1:2
dummy=struct('a',{},'b',{});
for k=1:4
fprintf('[%d,%d]\n',k,j)
dummy(k).a = k;
dummy(k).b = j;
end
s(j,:) = dummy;
end
You don't have a problem here, but it can get complicated in other instances