Commit ff6c45d9 authored by Enrico Glerean's avatar Enrico Glerean

adding

parent 99f62907
function a = MyRand(A)
tic
parfor i = 1:200
a(i) = max(abs(eig(rand(A))));
end
toc
%{
ans =
Pool with properties:
Connected: true
NumWorkers: 1
Cluster: local
AttachedFiles: {}
AutoAddClientPath: true
IdleTimeout: 30 minutes (30 minutes remaining)
SpmdEnabled: true
>> a = MyRand(1000);
Elapsed time is 144.843540 seconds.
%}
\ No newline at end of file
%% gopest
spmd
if labindex==1
a=1:6
else a=3*ones(1,6)
end
m=gop(@max,a,1)
end
%%
spmd
x=linspace(labindex-1,labindex,10)
y=sin(x)
m=gop(@max,y)
end
X=[x{:}]
Y=[y{:}]
M=[m{:}]
hold off
plot(X,Y)
hold on
plot(X,M)
shg
hold off
%%% myGSexample.m (2019spring)
%{
Example:
Run global search on the optimization problem
minimize peaks(x, y); subject to
(x+3)^2 + (y+3)^2 <= 36,
-3 <= x <= 3 and
-3 <= y <= 3.
%}
% Constraints:
type mycon
%% Set up problem:
problem = createOptimProblem('fmincon','objective', ...
@(x) peaks(x(1), x(2)), 'x0', [1 2], 'lb', [-3 -3], ...
'ub', [3 3], 'nonlcon', @mycon)
%%
gs = GlobalSearch
%% Run |GlobalSearch| on 1-D Problem
% Consider a function with several local minima.
%
%%
format compact
%%
fun = @(x) x.^2 + 4*sin(5*x);
fplot(fun,[-5,5])
%%
% To search for the global minimum, run |GlobalSearch| using the |fmincon|
% |'sqp'| algorithm.
rng default % For reproducibility
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
fun,'x0',3,'lb',-5,'ub',5,'options',opts);
% x0=3, lb=-5, ub=5
%%
gs = GlobalSearch;
[x,f] = run(gs,problem)
%% Timings
%
% 1) Use parallel off
opts.UseParallel=0
Tstart=tic;
[x,f] = run(gs,problem);
Tserial=toc(Tstart)
%%
parpool
%%
% 2) Use parallel on
opts.UseParallel=1
Tstart=tic;
[x,f] = run(gs,problem);
Tparallel=toc(Tstart)
%%
Tserial
Tparallel
%{
Tserial =
0.3525
Tparallel =
0.2614
%}
%% Run |GlobalSearch| on Multidimensional Problem
% Create an optimization problem that has several local minima, and try to find
% the global minimum using |GlobalSearch|. The objective is the six-hump camel
% back problem (see <docid:gads.bsc599n Run the Solver>).
rng default % For reproducibility
gs = GlobalSearch;
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
+ x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
problem = createOptimProblem('fmincon','x0',[-1,2],...
'objective',sixmin,'lb',[-3,-3],'ub',[3,3]);
x = run(gs,problem)
%%
% You can request the objective function value at |x| when you call |run|
% by using the following syntax:
%
% |[x,fval] = run(gs,problem)|
%
% However, if you neglected to request |fval|, you can still compute the
% objective function value at |x|.
fval = sixmin(x)
%%
[X,Y]=meshgrid(-2:.2:2,-1:.05:1);
Z=4*X.^2-2.1*X.^4+X.^6/3+X.*Y-4*Y.^2+4*Y.^4;
surf(X,Y,Z);view(-44,14)
xlabel('x');ylabel('y')
title('sixmin(x,y)')
% Six-hump camel back.
%%
% _Copyright 2012 The MathWorks, Inc._
\ No newline at end of file
function [c,ceq] = mycon(x)
c = (x(1)+3)^2 + (x(2)+3)^2 - 36;
ceq = [];
end
function [f,gf]=myonehump(x)
r=x(1)^2+x(2)^2;
s=exp(-r);
f=x(1)*s+r/20;
if nargout > 1
gf = [x(1)/10 + exp(- x(1)^2 - x(2)^2) - 2*x(1)^2*exp(- x(1)^2 - x(2)^2);...
x(2)/10 - 2*x(1)*x(2)*exp(- x(1)^2 - x(2)^2)];
end
%% Writing Task Functions
%
addpath /usr/local/MATLAB/R2018b/examples/distcomp/main/ % My observation,
% This is not included in 2019a. HA
%
% In this example, we look at two common cases when we might want to write a
% wrapper function for the Parallel Computing Toolbox(TM). Those wrapper
% functions will be our task functions and will allow us to use the toolbox in
% an efficient manner. The particular cases are:
%
% * We want one task to consist of calling a nonvectorized function
% multiple times.
% * We want to reduce the amount of data returned by a task.
%
% Prerequisites:
%
% * <docid:distcomp_examples.example-ex53988799
% Customizing the Settings for the Examples in the Parallel Computing Toolbox>
% * <docid:distcomp_examples.example-ex17307408
% Dividing MATLAB(R) Computations into Tasks>
%
% For further reading, see:
%
% * <docid:distcomp_examples.example-ex74813814 Minimizing Network Traffic>
% * <docid:distcomp_examples.example-ex21012732 Using Callback Functions>
% Copyright 2007-2017 The MathWorks, Inc.
%% Calling a Nonvectorized Function Multiple Times in a Task
% The <docid:distcomp_examples.example-ex17307408
% Dividing MATLAB Computations into Tasks>
% example discusses how inefficient it would be to construct a large number of
% tasks where each task performs only a small amount of work. Instead, each
% task should perform a reasonable amount of work, so that the overhead of a
% task does not dwarf its run time. Consequently, we often find ourselves in
% the situation where each task should do the following:
%
% Given a vector |x|, return a vector |y| such that |y(i) = f(x(i))|. If the
% function |f| is vectorized, the MATLAB statement |y = f(x)| does exactly
% that, so we let |f| be our task function. However, if |f| is not
% vectorized, we have to write a task function that calls |f| inside a
% for-loop. That is, we want the task function to look like the
% following:
%
% function y = mytaskfnc(x)
% len = numel(x);
% y = zeros(1, len);
% for i = 1:len
% y(i) = f(x(i));
% end
%%
% As an example, let's look at the problem of minimizing the Rosenbrock test
% function from multiple starting points. Suppose that we want the starting
% point to be of the form |[-d, d]| and that we want to use the |fminsearch|
% method to perform the minimization. We easily arrive at the following task
% function:
type pctdemo_task_tutorial_taskfunction
%%
% We can create a job that is composed of several tasks, where each task can
% handle as many different starting points as we need.
%% Reducing the Data Returned by a Task
% Our tasks might invoke some MATLAB functions that generate more data than we
% are interested in. Since there is considerable overhead in transmitting the
% return data over the network, we would like to minimize such data transfers.
% Thus, the task function might look something like the following:
%
% function d = mytaskfnc(x)
% % Only return the last output argument from f. Drop the rest.
% [a, b, c, d] = f(x);
%
% There are of course numerous other possibilities: We might want to return only
% the sum of a vector instead of the entire vector, the last point of a time
% series instead of the entire time series, etc.
function [c,ceq,gc,gceq] = mytiltedellipse(x)
c=x(1)*x(2)/2 + (x(1)+2)^2+(x(2)-2)^2/2 -2;
ceq=[];
if nargout > 2
gc=[2*x(1) + x(2)/2 + 4;
x(1)/2 + x(2) - 2];
gceq=[];
end
%%% scaleup_parforMathwork.m
%{
In this example, you start on your local multicore desktop and measure the
time required to run a calculation, as a function of increasing numbers of
workers. The test is called a strong scaling test. It enables you to measure
the decrease in time required for the calculation if you add more workers.
This dependence is known as speedup, and allows you to estimate the parallel
scalability of your code. You can then decide whether it is useful to increase
the number of workers in your parallel pool, and scale up to cluster and cloud
computing.
%}
%%
parpool(1);
a = MyRand(1000);
%delete(gcp);
%{
ans =
Pool with properties:
Connected: true
NumWorkers: 1
Cluster: local
AttachedFiles: {}
AutoAddClientPath: true
IdleTimeout: 30 minutes (30 minutes remaining)
SpmdEnabled: true
>> a = MyRand(1000);
Elapsed time is 144.843540 seconds.
%}
\ No newline at end of file
%% Problems: 10.12.2017, spmd_minmaxproblems2.m
%
%% (1) Max and min-points
%
% Find max and min-values and locations by decomposing domain
% into labs and working in parallel with all labs available.
%
%% (2) Bisection ?, maybe not, n-section, yes!
%
%% First example:
clear
close all
format compact
%% Define function f
%
f=@(x) x.*sin(x);
%% Find local min and max on I=[-pi N*pi], take N=nlabs+1
%
% In this case we know zeros of f: n*pi .
%% Open pool:
nlabs=2; % Nr. of workers on laptop
nlabs=20; % Triton max
I=[0, (nlabs+1)*pi];
ezplot(f,I);grid on;shg
hold on
%
% parpool(nlabs) % Remove comment if no pool is open
%% Some rules of pools: parpool(nlabs), spmd ... end, delete(gcp)
%
% parpool(4) % This is just to show the "labintervals"
spmd
a=-pi+(labindex-1)*2*pi;
b=pi+(labindex-1)*2*pi;
fprintf('Subinterval: [%-4g, %-4g]\n',a,b) % Remove comment for
% nlabs=4 (eg.)
end
% Note1: If you started pool for nlabs=24 (eg.), you have to close it before
% changing to parpool(4) (eg.)
% To close pool:
% delete(gcp)
%
% Note 2: If you have a pool open, you can do several spmd-blocks one after
% another. So the end stetement od yourcspmd-block doesn't close the pool.
%
%% Composite to ordinary variables (workers -> client)
A=[a{:}];B=[b{:}];
plot(A,f(A),'*k',B,f(B),'ok')
% Lab-boundaries a,b are zeros of f this time (zeros of sin).
%% Find min and max on each lab.
n=1000;
disp([num2str(n*nlabs) ' calculations, each lab doing ' ...
num2str(n) ' simultaneously' ])
spmd
x=linspace(a,b,n);
y=f(x);
% [ymax,maxI]=max(y); % Max on each lab and index of the 1'st
% [ymin,minI]=min(y); % Min on each lab and its index of the 1'st
ymax=max(y); % Max values on each lab
maxI=ymax==y; % Logical indexvector of (all) max-values in each lab
minI=ymin==y; % Logical indexvector of (all) min-values in each lab
xmax=x(maxI); % x-values of max's
xmin=x(minI); % x-values of min's
end
%% Error estimate
L=2*pi % "Labinterval's" length
abserr=L/n % upper bound for abs error
%% Global max and min points
% gmaxx=[xmax{1} xmax{2}]
% gmaxy=[ymax{1} ymax{2}]
% gminx=[xmin{1} xmin{2}]
% gminy=[ymin{1} ymin{2}]
%% Composite to ordinary variables (results from workers to client):
Xmax=[xmax{:}];
Ymax=[ymax{:}];
Xmin=[xmin{:}];
Ymin=[ymin{:}];
%% Check lab-borders
sum(Xmax==A)
sum(Xmin==A)
% Both return 0, as they should, because the labintervals are [k-1,k+1]*pi.
plot(Xmin,Ymin,'*r',Xmax,Ymax,'*k')
%% Increase accuracy!
%
% Repeat the same procedure using labs with small radii around min/max-points.
nlabs=24; % Triton max %as before
n=1000; % as before
% parpool(nlabs)
spmd
am=xmin-abserr;bm=xmin+abserr;
aM=xmax-abserr;bM=xmax+abserr;
xm=linspace(am,bm,n);
xM=linspace(aM,bM,n);
ym=f(xm);yM=f(xM);
[ymax,maxI]=max(yM); % Max on each lab and its index
[ymin,minI]=min(ym); % Min on each lab and its index
% In this case the shorter form using max/min is ok, as we know
% uniqueness on the small intertevals. (But this is hardly more
% efficient though.)
xmax2=xM(maxI); % x-values of max's
ymax2=f(xmax2);
xmin2=xm(minI); % x-values of min's
ymin2=f(xmin2);
end
Xmax2=[xmax2{:}];
Ymax2=[ymax2{:}];
Xmin2=[xmin2{:}];
Ymin2=[ymin2{:}];
%% Error:
abserr2=abserr/n
max(abs(Xmax2-Xmax))
max(abs(Ymax2-Ymax))
%
%% In some cases need to remove extra points
% Here's one attempt:
%{
aa=[a{:}];bb=[b{:}];
Axmax=[aa' gmaxx']
pick=~(Axmax(:,1)==Axmax(:,2));
xmaxtrue=gmaxx(pick);
ymaxtrue=gmaxy(pick);
%}
%% Removal of a single value is easy:
t=-2:2; T=[t t t t]
% Remove 0's
T(T~=0)
% or
T(T==0)=[]
%% Zeros of f using brackets we just found
Br=[Ymax' Ymin']
spmd
ab=[Xmax(labindex) Xmin(labindex)];
z=fzero(f,ab);
end
Z=[z{:}]
% Here of course we know the result.
Z-(1:nlabs)*pi
% YES !!!
%%
%% Problems: 11.12.2017, spmd_minmaxproblems3.m
% Modified 19.03.2019
% Practically identical with spmd_minmaxproblems2.m
%% (1) Max and min-points
%
% Find max and min-values and locations by decomposing domain
% into labs and working in parallel with all labs available.
%% Start with the version 2 problems
%% First example:
clear
close all
format compact
%% Define function f
%
f=@(x) x.*sin(x);
%fplot(f,[-pi 10*pi])
%% Find local min and max on I=[-pi N*pi], take N=nlabs+1
%
% In this case we know zeros of f: n*pi .
%% Open pool:
p=parpool
nlabs=p.NumWorkers % Triton default: 20
%% Some rules of pools: parpool(nlabs), spmd ... end, delete(gcp)
%
%
spmd
a=2*(labindex-1)*pi;
b=2*labindex*pi;
fprintf('Subinterval: [%-4g, %-4g]\n',a,b)
end
% Note1: If you started pool for nlabs=20 (eg.), you have to close it before
% changing to parpool(4) (eg.)
% To close pool:
% delete(gcp)
%
% Note 2: If you have a pool open, you can do several spmd-blocks one after
% another. So the end statement of your spmd-block doesn't close the pool.
%
Interval.a=a;
Interval.b=b;
%% Composite to ordinary variables (workers -> client)
A=[a{:}];B=[b{:}];
plot(A,f(A),'*k',B,f(B),'ok')
% Lab-boundaries a,b are zeros of f this time (zeros of sin).
%% Find min and max on each lab.
n=1000;
disp([num2str(n*nlabs) ' calculations, each lab doing ' ...
num2str(n) ' simultaneously' ])
spmd
x=linspace(a,b,n);
y=f(x);
% [ymax,maxI]=max(y); % Max on each lab and index of the 1'st
% [ymin,minI]=min(y); % Min on each lab and its index of the 1'st
ymax=max(y); % Max values on each lab
ymin=min(y); % Min values on each lab
maxI=ymax==y; % Logical indexvector of (all) max-values in each lab
minI=ymin==y; % Logical indexvector of (all) min-values in each lab
xmax=x(maxI); % x-values of max's
xmin=x(minI); % x-values of min's
end
%% Error estimate (Not quite ...)
L=2*pi % "Labinterval's" length
abserr=L/n % upper bound for abs error
%% Global max and min points
% gmaxx=[xmax{1} xmax{2}]
% gmaxy=[ymax{1} ymax{2}]
% gminx=[xmin{1} xmin{2}]
% gminy=[ymin{1} ymin{2}]
%% Composite to ordinary variables (results from workers to client):
Xmax=[xmax{:}];
Ymax=[ymax{:}];
Xmin=[xmin{:}];
Ymin=[ymin{:}];
%% Check lab-borders
sum(Xmax==A)
sum(Xmin==A)
% Both return 0, as they should, because the labintervals are [k-1,k+1]*pi.
plot(Xmin,Ymin,'*r',Xmax,Ymax,'*k')
%% Increase accuracy!
%
% Repeat the same procedure using labs with small radii around min/max-points.
nlabs=20; % Triton max %as before
n=1000; % as before
% parpool(nlabs)
spmd
am=xmin-abserr;bm=xmin+abserr;
aM=xmax-abserr;bM=xmax+abserr;
xm=linspace(am,bm,n);
xM=linspace(aM,bM,n);
ym=f(xm);yM=f(xM);
[ymax,maxI]=max(yM); % Max on each lab and its index
[ymin,minI]=min(ym); % Min on each lab and its index
% In this case the shorter form using max/min is ok, as we know
% uniqueness on the small intervals. (But this is hardly more
% efficient though.)
xmax2=xM(maxI); % x-values of max's
ymax2=f(xmax2);
xmin2=xm(minI); % x-values of min's
ymin2=f(xmin2);
end
Xmax2=[xmax2{:}];
Ymax2=[ymax2{:}];
Xmin2=[xmin2{:}];
Ymin2=[ymin2{:}];
%% Error:
abserr2=abserr/n
max(abs(Xmax2-Xmax))
max(abs(Ymax2-Ymax))
%
%% In some cases need to remove extra points
% Here's one attempt:
%{
aa=[a{:}];bb=[b{:}];
Axmax=[aa' gmaxx']
pick=~(Axmax(:,1)==Axmax(:,2));
xmaxtrue=gmaxx(pick);
ymaxtrue=gmaxy(pick);
%}
%% Removal of a single value is easy:
t=-2:2; T=[t t t t]
% Remove 0's
T(T~=0)
% or
T(T==0)=[]
%% Zeros of f using brackets we just found
Br=[Ymax' Ymin']
spmd
ab=[Xmax(labindex) Xmin(labindex)];
z=fzero(f,ab);
end
Z=[z{:}]
% Here of course we know the result.
Z-(1:nlabs)*pi
% YES !!!
%%
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment