samer@0
|
1 % ugather - Run a processing unit and collect output
|
samer@0
|
2 %
|
samer@0
|
3 % ugather ::
|
samer@0
|
4 % unit({}, {[[N,1]]}, _) ~'live processing unit',
|
samer@0
|
5 % T:natural ~'number of iterations to run (can be inf)',
|
samer@0
|
6 % options {
|
samer@0
|
7 % draw :: boolean/false ~'whether or not to call drawnow after each iteration';
|
samer@0
|
8 % quiet :: boolean/false ~'whether or not to suppress progress messages';
|
samer@0
|
9 % chunk :: natural/1 ~'print progress every chunk interations';
|
samer@0
|
10 % expand :: boolean/false~'do binary expansion of results array'
|
samer@0
|
11 % }
|
samer@0
|
12 % -> [[N,T]] ~'collected output'.
|
samer@0
|
13 %
|
samer@0
|
14 % This function accepts the live processing unit associated
|
samer@0
|
15 % with an arrow (as created by with_arrow). The arrow must
|
samer@0
|
16 % have zero inputs and one output. The requested number of outputs
|
samer@0
|
17 % are collected into an array and returned. If inf outputs are
|
samer@0
|
18 % requested, the system is run until an EOF exception is caught.
|
samer@0
|
19
|
samer@0
|
20 function X=ugather(u,its,varargin)
|
samer@37
|
21 opts=options('expand',1,varargin{:});
|
samer@42
|
22 szout = u.sizes_out{1};
|
samer@42
|
23
|
samer@42
|
24 if szout(2)==1
|
samer@42
|
25 if opts.expand && isinf(its)
|
samer@42
|
26 X=zeros(szout(1),32); % initial size is arbitrarily 32
|
samer@42
|
27 done=uiter(u,its,@gatherxx,[],'label','ugather',opts);
|
samer@42
|
28 if done<size(X,2), X=X(:,1:done); end
|
samer@42
|
29 else
|
samer@42
|
30 if ~isinf(its), X=zeros(szout(1),its); end
|
samer@42
|
31
|
samer@42
|
32 done=uiter(u,its,@gatherx,[],'label','ugather',opts);
|
samer@42
|
33 if done<its, X=X(:,1:done); end
|
samer@42
|
34 end
|
samer@0
|
35 else
|
samer@42
|
36 if opts.expand && isinf(its)
|
samer@42
|
37 X=zeros(szout(1),32); % initial size is arbitrarily 32
|
samer@42
|
38 done=szout(2)*uiter(u,its,@gatherxxw,[],'label','ugather',opts);
|
samer@42
|
39 if done<size(X,2), X=X(:,1:done); end
|
samer@42
|
40 else
|
samer@42
|
41 if ~isinf(its), X=zeros(szout(1),its*szout(2)); end
|
samer@0
|
42
|
samer@42
|
43 done=szout(2)*uiter(u,its,@gatherxw,[],'label','ugather',opts);
|
samer@42
|
44 if done<its, X=X(:,1:done); end
|
samer@42
|
45 end
|
samer@0
|
46 end
|
samer@0
|
47
|
samer@0
|
48 function s=gatherx(i,s), X(:,i)=u.process(); end
|
samer@0
|
49 function s=gatherxx(i,s),
|
samer@0
|
50 if i>size(X,2),
|
samer@0
|
51 %fprintf('ugather: expanding at %d.\n',size(X,2));
|
samer@0
|
52 X=[X,zeros(size(X))]; % double size
|
samer@0
|
53 end
|
samer@0
|
54 X(:,i)=u.process();
|
samer@0
|
55 end
|
samer@42
|
56
|
samer@42
|
57 function s=gatherxw(i,s),
|
samer@42
|
58 i1=1+(i-1)*szout(2);
|
samer@42
|
59 i2=i*szout(2);
|
samer@42
|
60 X(:,i1:i2)=u.process();
|
samer@42
|
61 end
|
samer@42
|
62 function s=gatherxxw(i,s),
|
samer@42
|
63 i1=1+(i-1)*szout(2);
|
samer@42
|
64 i2=i*szout(2);
|
samer@42
|
65 if i2>size(X,2),
|
samer@42
|
66 %fprintf('ugather: expanding at %d.\n',size(X,2));
|
samer@42
|
67 X=[X,zeros(size(X))]; % double size
|
samer@42
|
68 end
|
samer@42
|
69 X(:,i1:i2)=u.process();
|
samer@42
|
70 end
|
samer@0
|
71 end
|
samer@0
|
72
|
samer@0
|
73
|