% DISTKNAP distributes trials of knapsack problem to multiple workers.
%
%  Load the weights and target from the MAT-file.
%
  load knapweights

  job = createJob ( ...
    'configuration', 'ithaca_2009b', ...
    'FileDependencies', { 'knap.m', 'knapweights.mat' } );
%
%  Compute the number of combinations to check.
%
  combos = fix ( 2^length(weights) );

  endValue = -1;
%
%  Divide the range [0, COMBOS-1] among the 4 tasks.
%
  for task = 1 : 4

    startValue = endValue + 1;
    endValue = ( task / 4 ) * combos - 1;

    task_id = createTask ( job, @knap, 2, ...
                          { weights, target, [startValue, endValue] } );
  end
%
%  The job now consists of 4 tasks.  Submit the job.
%
  submit ( job );
%
%  Wait for results.
%
  waitForState ( job, 'finished' );
%
%  Retrieve the output from all tasks.
%
  results = getAllOutputArguments ( job );
%
%  Display the results (if any) from the four labs.
%
  for task = 1 : 4
    if ~isempty ( results{task,1} )
      disp ( 'weights');       disp(results{task,1});
      disp ( 'weight_values'); disp(results{task,2});
    end
  end
%
%  Clean up by freeing memory associated with the job.
%
  destroy ( job );

  clear sched job task task_id tasknum startValue endValue combos

