Submitting sub or child jobs |
You want to submit sub-jobs within a task.
Get a reference to the IJobSchedulerContext interface by calling GetProperty and passing in TaskPropertiesJobSchedulerContext.
IJobSchedulerContext schedulerContext = this.GetProperty<IJobSchedulerContext>(TaskProperties.JobSchedulerContext);
With a reference to the IJobSchedulerContext interface, call CreateChildJob to create a job.
IJob job = schedulerContext.CreateChildJob();
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(); job.AddTask(new ParentTask()); job.Submit(); job.WaitUntilDone(); Console.WriteLine(job.Tasks[0].Result); } /* * The output of the application should resemble: * From Parent -> From Child */ } [Serializable] public class ParentTask : Task { public override void Execute() { // Get the instance of the job scheduler context IJobSchedulerContext schedulerContext = this.GetProperty<IJobSchedulerContext>(TaskProperties.JobSchedulerContext); // Create a child job by calling CreateChildJob. IJob job = schedulerContext.CreateChildJob(); Task task = new ChildTask(); job.AddTask(task); job.Submit(); job.WaitUntilDone(); this.Result = "From Parent -> " + task.Result; } } [Serializable] public class ChildTask : Task { public override void Execute() { this.Result = "From Child"; } } } }
Along with being submitted by a client, jobs can also be submitted in a task. Submitting sub-jobs can be a powerful tool used to handle dynamic workloads such as divide and conquer computations. Sub-jobs have all the same fields and properties as a standard job, e.g. events, monitoring, status reporting, etc. When sub-jobs are submitted they are submitted back to the original client's job scheduler and tracked the same way a task submitted from a regular client is tracked. Sub-jobs can also have sub-jobs of their own, which enables tasks to recursively spawn work to child jobs.
Note |
---|
CreateChildJob returns an IJob. This is just a Job object and can be cast to the Job class if needed. |
There are some advanced details about sub-jobs that are useful to know when debugging an issue. The most important concept to understand is how sub-jobs deal with allocated resources. Internally, a sub-job is treated just like a regular job; before a job can run, it needs available resources to consume. For both jobs and sub-jobs, cores are the most common of these resources.
To explain how they are different is best done through an example. If a regular job is submitted to a job scheduler that has no cores available, the job gets queued and waits until a core becomes available. When a sub-job is submitted to a job scheduler that has no cores available, the child job gets immediately executed. This happens because internally the parent task yields all its resources to the child job. This works transparently without the user doing anything. If this is not the desired behavior, there is an overload to CreateChildJob(Boolean) that allows a yieldAllResourcesToChildren parameter. If the yieldAllResourcesToChildren parameter is false, resource yielding will not be automatic. The sub-job waits in the queue like a regular job.
Note |
---|
When submitting sub-jobs, the most common use case is that the child job handles the workload of its parent while the parent waits for the work to finish. By default, the reason all resources are provided to the children is to try to make this use case as easy as possible. |
STK Parallel Computing Server 2.9 API for .NET