view arrows/ugather.m @ 2:7357e1dc2ad6

Simplified scheduler library with new schedule representation.
author samer
date Sat, 22 Dec 2012 16:17:51 +0000
parents 672052bd81f8
children beb8a3f4a345
line wrap: on
line source
% ugather - Run a processing unit and collect output
%
% ugather :: 
%    unit({}, {[[N,1]]}, _) ~'live processing unit',
%    T:natural              ~'number of iterations to run (can be inf)',
%    options {
%       draw  :: boolean/false ~'whether or not to call drawnow after each iteration';
%       quiet :: boolean/false ~'whether or not to suppress progress messages';
%       chunk :: natural/1     ~'print progress every chunk interations';
%       expand :: boolean/false~'do binary expansion of results array'
%    } 
% -> [[N,T]] ~'collected output'.
%
% This function accepts the live processing unit associated
% with an arrow (as created by with_arrow). The arrow must
% have zero inputs and one output. The requested number of outputs
% are collected into an array and returned. If inf outputs are
% requested, the system is run until an EOF exception is caught.

function X=ugather(u,its,varargin)
	opts=prefs('expand',1,varargin{:});
	if opts.expand && isinf(its)
		X=zeros(u.sizes_out{1}(1),32); % initial size is arbitrarily 32
		done=uiter(u,its,@gatherxx,[],'label','ugather',opts);
		if done<size(X,2), X=X(:,1:done); end
	else
		if ~isinf(its), X=zeros(u.sizes_out{1}(1),its); end

		done=uiter(u,its,@gatherx,[],'label','ugather',opts);
		if done<its, X=X(:,1:done); end
	end

	function s=gatherx(i,s), X(:,i)=u.process(); end
	function s=gatherxx(i,s), 
		if i>size(X,2),
			%fprintf('ugather: expanding at %d.\n',size(X,2));
			X=[X,zeros(size(X))]; % double size
		end
		X(:,i)=u.process(); 
	end
end