Problem

You have to integrate code that is already parallelized and you need to control how many instances of a task the agent runs.

Solution

Set the ExclusiveExecution property on a job to specify only one instance of a task should run concurrently.

Caution

This feature is not available on the embedded scheduler.

C#  Copy imageCopy
using System;
using System.Threading;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();

                Job job = scheduler.CreateJob();

                // Set ExclusiveExecution to true
                job.ExclusiveExecution = true;

                for (int i = 0; i < 3; ++i)
                {
                    Task task = new OnlyTaskThatIsRunning();
                    job.AddTask(task);
                }

                job.Submit();
                job.WaitUntilDone();

                for (int i = 0; i < 3; ++i)
                {
                    DateTime start = job.Tasks[i].GetProperty<DateTime>(TaskProperties.HostStartTime);
                    DateTime end = job.Tasks[i].GetProperty<DateTime>(TaskProperties.HostEndTime);
                    Console.WriteLine("Task #{0} ran on {1} from {2} to {3}", i + 1, job.Tasks[i].Result, start, end);
                }
            }

            /*
             * The output of the application should resemble:
             * Task #1 ran on MyComputerName from 2/28/2013 5:34:24 PM to 2/28/2013 5:34:29 PM
             * Task #2 ran on MyComputerName from 2/28/2013 5:34:29 PM to 2/28/2013 5:34:34 PM
             * Task #3 ran on MyComputerName from 2/28/2013 5:34:34 PM to 2/28/2013 5:34:39 PM
             */
        }

        [Serializable]
        public class OnlyTaskThatIsRunning : Task
        {
            public override void Execute()
            {
                Thread.Sleep(5000);
                this.Result = Environment.MachineName;
            }
        }
    }
}

Discussion

In cases where code has already been parallelized and will automatically use all the cores on a single machine, it would be bad for performance if the tasks were run in parallel. In these situations, execute only one instance of the task on each machine at a time. That is, if 8 cores are available on two machines, run only one host at a time on each of the two machines. To do this, set the ExclusiveExecution property to true.

Once the "exclusive" task completes, the agent goes back to the normal state and its cores are available to the cluster again.

See Also