📄 distm_demo_1.m
字号:
disp(' 5. IMPLEMENTING A SPMD PARALLEL MODEL ')disp(' ')disp('PARALLELIZE is the most important function of this toolbox, it ')disp('is used to automatic create parallel instances and mannage them ')disp('with any quantity of workers available when the data is ')disp('regularly ordered. The same function is applied to ')disp('sub-hyperblocks of data wich are selected with control indexes. ')disp(' ')disp('The function keeps track of the tasks assigned to every worker, ')disp('finds out if it can re-use data, and order the output variables ')disp(' ')disp('The remaining of this demos show how to use this function to ')disp('solve typical SPMD parallel problems ')disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('FIRST EXAMPLE...')%if (exist('imread')==2) imshow(imread('para1.tif')); enddisp(' ')disp('Ax=b is solved for 15 different b''s')pss=num2str(5*ps);a = rand(5*ps,5*ps); disp(['A = rand(' pss ',' pss '); <-- A is a constant matrix'])b = rand(5*ps,15); disp(['B = rand(' pss ',15); <-- for every b column in B'])disp(' ')disp('We want to compute: ')disp(' ')disp([' mldivide( A(1:' pss ',1:' pss '), B(1:' pss ',1:1) ) '])disp([' mldivide( A(1:' pss ',1:' pss '), B(1:' pss ',2:2) ) '])disp(' ...')disp(' ')disp([' mldivide( A(1:' pss ',1:' pss '), B(1:' pss ',14:14) ) '])disp([' mldivide( A(1:' pss ',1:' pss '), B(1:' pss ',15:15) ) '])disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('To control the indexes for A we use:')disp(' ')disp(' [Dim #elem startat inc blocksize]')aindexes=[1 1 1 0 5*ps; 2 15 1 0 5*ps]disp(' ')disp(' observe that for A we need 15 parallel elements, but it happens')disp(' that they are all the same, therefore increment in both dimesions')disp(' is always 0, we also see that every element extracted from A starts')disp([' at (1,1) and has a size of (' pss ',' pss ') resulting in a constant '])disp(' matrix fed to every parallel instance')disp(' ')disp(' the 15 elements could have also been generated using the first dimension')disp(' control indexes (first row) instead of the second, but since the output of ')disp([' every mldivide will be a column ' pss 'x1 we use the second dimension to']) disp(' concatenate the result along the columns and not rows. i.e. the order of ')disp(' every output variable is controlled by the dimensions of the first')disp(' set of indexes, in this case the indexes of A (aindexes)')disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('Now, to control the indexes for B we use:')disp(' ')disp(' [Dim #elem startat inc blocksize]')bindexes=[2 15 1 1 1; 1 1 1 0 5*ps]disp(' ')disp(' to transverse B, we travel first among the second dimension generating')disp(' the 15 parallel elements, the outer loop is processed only once.')disp(' Observe that in the second dimension we DO have increments of 1 and')disp(' that we start at the first column and proces 1 column at the time,')disp(' while in the first dimension we always extrat the entire row from')disp([' 1 to ' pss ' (increment=0). Since the indexes of the data were constant'])disp(' in one dimension we could switch the order of the rows in bindexes ')disp(' without changing the result, (remember that output is controlled')disp(' by the first set of control indexes)')disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('To launch the parallel instances we use:')disp(' ')disp('x=parallelize(ss,rs,inf,1,''mldivide'',1,a,aindexes,b,bindexes);')disp(' \ / | | | | | | | |')disp(' \ / | | | | | |____|_____|___ Control indexes for ')disp(' | | | | | | | every input variable')disp(' | | | | | | |')disp(' port _______| | | | | |__________|__________Input variables') disp(' handlers | | | |')disp(' | | | |___ Expected output variables')disp(' how to ____________| | |')disp(' handle | |_________ Function to parallelize')disp(' errors Verbose ctrl')disp(' ')disp(' ')disp(' PAUSE')pauseclcdisp(' ')x=parallelize(ss,rs,inf,1,'mldivide',1,a,aindexes,b,bindexes);disp(' ')disp(['Results are in ''x'' which have size of ' num2str(size(x))])disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('SECOND EXAMPLE...')disp(' ')disp(' Now a real time consuming computation to see the advantages...')disp(' Linear least squares with nonnegativity constraints for 50 ')disp(' different ''d'', will search for vectors that minimizes NORM(C*y - d)')disp(' subject to y >= 0. C and d must be real. Here we will use four')disp(' output variables, all ordered as the first index control commands')disp(' ')c = rand(2*ps,2*ps);disp(['C = rand(' pss ',' pss ');'])d = rand(2*ps,50);disp(['D = rand(' pss ',50);'])disp(' ')disp(' Indexes are controlled as follows (very similar to the first')disp(' example), output for every different variable (in this case 4)')disp(' will be concatenated in the second dimension')disp(' [Dim #elem startat inc blocksize]')cindexes=[1 1 1 0 2*ps; 2 50 1 0 2*ps]dindexes=[2 50 1 1 1; 1 1 1 0 2*ps]disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('To launch the parallel instances we use:')disp(' ')disp(' ')disp('[y,RESNORM,RESIDUAL,EXITFLAG] =parallelize(ss,rs,inf,1,''LSQNONNEG'',4,c,cindexes,d,dindexes);')disp(' | | \ /')disp(' Function to parallelize ___| | \ /')disp(' | \ /')disp(' Expecting 4 output variables ______| \ /')disp(' \ /')disp(' 2 input variables and their control indexes _____________\_________/')disp(' ')disp(' ')disp(' Observe that some workers may be faster than others,')disp(' but the correct ordering of the results is preserved...')disp(' Also note that output parameters has the same ideology as regular')disp(' matlab functions, we could have had omitted the last three if we')disp(' do not need them') disp(' ')disp(' You better launch some other workers, but do not worry, you can')disp(' launch them on the fly as you feel you will need them !! :>)')disp(' ')disp(' PAUSE')pauseclcdisp(' ')[y,RESNORM,RESIDUAL,EXITFLAG]=parallelize(ss,rs,inf,1,'lsqnonneg',4,c,cindexes,d,dindexes);disp(' ')disp(['Results are in y,RESNORM,RESIDUAL,EXITFLAG...'])disp(' ')disp(' PAUSE')pauseclcdisp(' ')disp('A FINAL EASY EXAMPLE...')disp(' ')disp(' Lets take the average over the 50 ''y'' solutions of the last problem,')disp(' this example demostrates that input dimensions do not need to be the same')disp(' as output dimensions... observe now that the first dimension to be')disp(' processed is the one that actually generates the parallel elements,')disp(' therefore the output (1x1) elements will be concatenated rowise')disp(' ')disp('Indexes are controlled as follows')disp(' [Dim #elem startat inc blocksize]')yindexes=[1 2*ps 1 1 1; 2 1 1 0 50]disp('parallelize(ss,rs,inf,1,''mean'',1,y,yindexes)')disp(' ')disp(' PAUSE')pauseclcdisp(' ')parallelize(ss,rs,inf,1,'mean',1,y,yindexes)disp(' ')disp(' ')disp('Now I will release all workers and close connections')disp('closemajordomo(ss,rs)')disp(' ')disp(' ')disp(' PAUSE')pauseclcclosemajordomo(ss,rs)disp('End of demo, if you have the image toolbox please go to distm_demo_2')disp('For further questions/comments please e-mail landrade@ece.neu.edu')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -