Click or drag to resize

Constrain the amount of resources required for a task to be scheduled

Problem

You need fine grain control over the amount of available resources necessary for a task to execute.

Solution

On the task, call SetProperty and pass the resource to be configured as the first parameter and the amount of the resource as the second parameter. There are currently three resources that can be configured for consumption: Cores, Memory, and Estimated Memory Usage. These can be set by specifying TaskPropertiesMinCoresForTask and TaskPropertiesMaxCoresForTask for Cores, TaskPropertiesMinMemoryForTask and TaskPropertiesMaxMemoryForTask for Memory, and TaskPropertiesEstimatedMemoryUsageForTask for Estimated Memory Usage.

C#
task.SetProperty(TaskProperties.MinCoresForTask, 2);
task.SetProperty(TaskProperties.MaxCoresForTask, 4);

A task can inspect the following properties when it executes to retrieve the resource amounts allocated to it: TaskPropertiesAllocatedCoresForTask, TaskPropertiesAllocatedMemoryForTask and TaskPropertiesEstimatedMemoryUsageForTask.

C#
long coresAllocated = this.GetProperty<long>(TaskProperties.AllocatedCoresForTask);

Below is a code example that demonstrates how to configure a task to consume between 2 and 4 cores.

C#
using System;
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();
                Task task = new ReturnsNumberOfAllocatedCores();

                // Configure the task to use at least 2 cores, but no more than 4.
                task.SetProperty(TaskProperties.MinCoresForTask, 2);
                task.SetProperty(TaskProperties.MaxCoresForTask, 4);

                job.AddTask(task);
                job.Submit();
                job.WaitUntilDone();

                Console.WriteLine("Cores allocated: " + task.Result);
            }

            /*
             * The output of the application should resemble:
             * Cores allocated: 4
             */
        }

        [Serializable]
        public class ReturnsNumberOfAllocatedCores : Task
        {
            public override void Execute()
            {
                // You can get the number of cores allocated by inspecting the TaskProperties.AllocatedCoresForTask property
                // You can use this value to decide how many threads you can spawn in your task.
                long coresAllocated = this.GetProperty<long>(TaskProperties.AllocatedCoresForTask);
                this.Result = coresAllocated;
            }
        }
    }
}

Below is another code example that demonstrates how to configure a task to consume a specific amount of the estimated memory budget.

C#
using System;
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();
                Task task;
                for (int i = 1; i < 3; i++) 
                {
                    task = new EstimatedMemoryUsageExample();
                    // Configure the task to use i*3 number of GB of the estimated memory budget
                    task.SetProperty(TaskProperties.EstimatedMemoryUsageForTask, i * 3072);
                    job.AddTask(task);
                }

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

            /*
             * If the machine has 6GB of memory and the two tasks are 
             * estimated to require 6GB and 3GB of memory, the task estimated 
             * to require 6GB memory won't run until the task estimated to 
             * require 3GB memory completes.
             */
        }

        [Serializable]
        public class EstimatedMemoryUsageExample : Task
        {
            public override void Execute()
            {
                /*
                 * Even though the body of the Execute() function is empty, the agent's 
                 * available estimated memory budget must be large enough to schedule a
                 * task with a non-zero value of the EstimatedMemoryUsageForTask.
                 */
            }
        }
    }
}
Discussion

When a task executes, what is happening internally is that the task is consuming a resource. The most common of these resources is "Cores". By default a task consumes one resource of type "Cores" on an agent. On a machine with eight cores, an agent can run eight concurrent tasks at a time until its "Cores" resource is exhausted. The amount of resources a task can consume can be optionally specified. For instance, if each task is configured to consume at least two cores, the eight core machine can only run four of these tasks concurrently. When a task is finished, then it will release the resources that it was allocated back into the pool of available resources.

The resource "Memory" has a limited number of useful applications. This resource will check the current available memory of the current machine at the time a task is attempting to be executed. Imagine a machine with 8GB of memory and 8 cores. If you attempt to submit four tasks each with a minimum memory set at 4GB, even if two tasks are already executing, as long as there is 4GB of available memory, the other tasks can begin execution. In this case, the number of tasks running is not being constrained by the memory resource properly. An example of a practical use for this resource is if you have a cluster of machines and the ExclusiveExecution flag is true for the job so that only one task may run at a time per machine. After finishing executing a task, a check will be made to ensure there is enough free memory on the machine to execute the next task.

The third type of resource is the "Estimated Memory Usage". This resource will simulate allocation of memory for tasks, without actually checking how much physical memory each task consumes as it executes. The total memory budget is set for the agent with the default value equal to the number of MB of physical memory available on the machine. The estimated memory usage is then set for each task and subtracted from the agent's total memory budget as the task executes. If a task's estimated memory usage is less than the agent's available budget, it will run. If a task's estimated memory usage is greater than the available budget, it will wait to be executed until there is enough memory available. The default estimated memory usage for a task is zero. Imagine a task believed to consume 2GB of memory. On an agent with a estimated memory budget of 6GB and eight cores, the agent would only execute three of these tasks concurrently to prevent the agent from swapping massive amounts of memory. In this case, configure the task's estimated memory usage to 2GB.

Note that tasks are not restricted from only using the resources allocated to them. That is, a task with two resources of type "Cores" can spawn as many threads as it wants. The task will not be stopped from over consuming its declared resources. Efficient use of resource consumption requires that tasks are coded to be honest and only use the resources they are allocated.

See Also

STK Parallel Computing Server 2.9 API for .NET