I converted a nested for loo into a unique parfor loop. The nested for loop looked like this:
for RIPETIZIONE = 1:N_RIPETIZIONI
for k=1:size(lambda_tol_vector)
%do things
end
end
Now, the parfor loop takes on the following values:
parfor n=1:N_RIPETIZIONI*K
[~,k]=ind2sub([N_RIPETIZIONI,K],n);
...
hence merging into a single loop the nested loops of before.
Now, inside the "outer" nested loop (for RIPETIZIONE = 1:N_RIPETIZIONI), but outside the inner loop (for k=1:size(lambda_tol_vector)), there was this part:
scv=size(columns_validation_test,2);
for counter_columns = 1:scv
COLONNA_SELEZIONATA=columns_validation_test(counter_columns);
Ctest=zeros(size(A.Value));
Ctest(:,COLONNA_SELEZIONATA)=C(:,COLONNA_SELEZIONATA);
Cvalidation=C-Ctest;
for j=1:size(lambdavector)
RMSE_initial_test{counter_columns}(j)=sqrt(sum2(Diff_sq_initial{j}.*Ctest)/sum(Ctest(:)));
RMSE_final_corrected_validation{counter_columns}(j)=sqrt(sum2(Diff_sq_corrected{j}.*Cvalidation)/sum(Cvalidation(:)));
RMSE_final_test{counter_columns}(j)=sqrt(sum2(Diff_sq{j}.*Ctest)/sum(Ctest(:)));
RMSE_final_corrected_test{counter_columns}(j)=sqrt(sum2(Diff_sq_corrected{j}.*Ctest)/sum(Ctest(:)));
end
[~,arg_min_temp_1]=min(RMSE_final_corrected_validation{counter_columns});
end
which I need now to include in the unique parfor loop with a pope indexing. Fo instance, Diff_sq_initial, Diff_sq_corrected and Diff_sq are all defined within the parfor loop with an index of n in the new loop (whereas previously they were indexed with k inside the for k=1:size(lambda_tol_vector) loop).
Is there a way to properly indexing those variables so that they fit the new parfor loop?
Related
I have a main Matlab code which I use in order to calculate ode.
The main code contains many lines but I will explain only the ones needed.
The code looks as following (short version):
global array
t0=0
x0=[5,5]
dt=0.01
tfinal=10
[tout,xout]=rk4('equation',t0,x0,dt,tfinal) % the functions use the function 'equation' and calculates its values at every time from 0 to 20
Where 'equation' is another function from the following type:
global array
Dydx = equation(t,x)
Dydx=[x(1)
x(2)+array(j)]
j=j+1
However, as can be seen, inside the function "equation" there is a variable j which I want to increase every t iteration of rk4.
Which means that while I calculate the ode for time t, the rk4 should use function 'equation' and increase the j by one and so on.
The problem I get is that if I try to make 'equation' gives output of j like [Dydx,j] =equation(t,x) I recieve an error because of rk4.
How can I increase j in the inner function while the outside functions also get the value?(tried global but it kept showing j as [] in the work space).
I have tried to make it global as following but it didnt work:
global array jcount
t0=0
x0=[5,5]
dt=0.01
tfinal=10
jcount = 1;
[tout,xout]=rk4('equation',t0,x0,dt,tfinal)
global array jcount
Dydx = equation(t,x)
Dydx=[x(1)
x(2)+array(jcount)]
jcount=jcount+1
Thank you.
.
I want to calculate count of outside points which are not in the circle. But I got this problem. My circle is unit circle.My Error is this : The temporary variable outside will be cleared at the beginning of each iteration
of the parfor loop.
function [ ] = girkoson( N,n )
%UNTÄ°TLED Summary of this function goes here
% Detailed explanation goes here
hold on
outside = 0;
parfor i=0:N
E=ones(N,n);
karekok = sqrt(n);
E = [E, eig(randn(n))/karekok];
a=real(E);
b= imag(E);
plot(a,b,'.r');
if (a>= -1) | (a<=1) | (b>=-1) | (b<=1)
outside = outside +1;
fprintf('%f',outside);
end
end
derece=0:0.01:2*pi;
xp=1*cos(derece);
yp=1*sin(derece);
x=0;y=0;
plot(x+xp,y+yp,'-b');
hold off
end
It looks like you're trying to treat outside as a parfor reduction variable. You cannot access the intermediate values of reduction variables during the loop - you can only perform the reductions. In other words, the line fprintf('%f', outside) is causing the problem, you must remove this for the parfor loop to work.
Also note that workers operating on the body of your parfor loop cannot display graphics to your desktop, so your plot calls will not show anything on-screen. (You can use print to emit graphics to a file if you wish).
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};
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);
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