function s = myApp(n, nprocs)
% function s = myApp(n, nprocs)
% Purpose: runs basic MATLAB commands to simulate user code for a standalone demo
%       n: size of simple arithmetic sequence [1+2+3+ . . . +n]
%  nprocs: number of processors in parallel computing

% Create a bar chart . . .
fprintf(1,'\n First, a graphics test . . . . .\n');
bar(magic(4));  % bar chart
title('This demonstrates graphics usage.');

% Compute with the Parallel Computing Toolbox
fprintf(1,'\n Next, test the parallel computing toolbox . . . . .\n');
fprintf(1,'Parfor will be used to compute the sum of the arithmetic sequence\n');
fprintf(1,'s = 1 + 2 + 3 + . . . + n = n(n+1)/2\n\n');
fprintf(1,'Will use %d threads for parfor operations.\n', nprocs);
% Request thread recsources

matlabpool('local', nprocs);  
spmd   % invoke spmd paradigm
  fprintf(1,'This is lab %d of %d\n', labindex, numlabs);
end
% testing parfor
s = 0;    % initialize solution 
parfor i=1:n
    s = s + i;       % s = 1 + 2 + 3 + . . . + n
end
matlabpool close;  % return thread resources
fprintf(1,'\n\n Iteration count n = %d\n', n);
fprintf(1,'\nComputed s = %d   ;  Expected s =  %d \n\n', s, n*(n+1)/2);

end   % end of function 

