locked
Submitting and tracking job status using v2 C# API: A Step-by-Step guide RRS feed

  • General discussion

  • Disclaimer: this code only works in the upcoming CTP.  In v2, a key functionality we added to the API is the ability to track job status through callbacks.  Here's a step-by-step guide on how to submit and track job status using the C# API

     

    Prerequisite

    Install Microsoft HPC Pack 2008 SDK. 

     

    Step 1

    Launch Visual Studio, create a console project and reference to "Microsoft.Hpc.Scheduler.dll" and "Microsoft.Hpc.Scheduler.Properties.dll" from the bin directory of the Microsoft HPC Pack 2008 SDK.  By default, it is in c:\program files\Microsoft HPC Pack 2008 SDK\bin

     

    Step 2

    Add the HPC Scheduler Name spaces

     


    Code Snippet
    using Microsoft.Hpc.Scheduler;
    using Microsoft.Hpc.Scheduler.Properties;

     

     

     

    Step 3

    Connect to a cluster

     

    Code Snippet


                Scheduler scheduler = new Scheduler();
                scheduler.Connect("localhost");

     

     

    Step 4

    Create Job and Task

     

    Code Snippet

     

                ISchedulerJob job = scheduler.CreateJob();

                job.MinimumNumberOfNodes = 1;
                job.MaximumNumberOfNodes = 1;
              

                ISchedulerTask task = job.CreateTask();

                task.CommandLine = "ComputeTask 5";
                task.WorkDirectory = @"c:\tests";

                job.AddTask(task);

     

     

    Step 5

    Add callback to job state change and submit job


     

    Code Snippet
                job.OnJobState += new EventHandler(job_OnJobState);
              
                scheduler.SubmitJob(job, null, null);

     

     


    Step 6

    Use a ManualResetEvent object to synchronize the main thread and other callback threads.          

      

    Code Snippet

               // Main thread blocks here after submission

               // waiting for a thread processing the callback function "job_OnJobState" to set the event
                jobFinishEvt.WaitOne();
            }

     

            static void job_OnJobState(object sender, JobStateEventArg e)
            {
                Console.WriteLine("Job <{0}> has changed from state <{1}> to <{2}>",
                                  e.JobId, e.PreviousState.ToString(), e.NewState.ToString());
                if (e.NewState.Equals(JobState.Finished))
                {
                    Console.WriteLine("Job <{0}> has finished", e.JobId);

                    // Set the jobFinishEvt so that the main thread will be unblocked
                    jobFinishEvt.Set();
                }
            }

     

     


    Here's the complete code:

     

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Hpc.Scheduler;
    using Microsoft.Hpc.Scheduler.Properties;
    using System.Threading;

    namespace JobSubV2
    {
        class Program
        {
            private static ManualResetEvent jobFinishEvt = new ManualResetEvent(false);
            static void Main(string[] args)
            {
                Scheduler scheduler = new Scheduler();


                scheduler.Connect("localhost");

                ISchedulerJob job = scheduler.CreateJob();

                job.MinimumNumberOfNodes = 1;
                job.MaximumNumberOfNodes = 1;
              

                ISchedulerTask task = job.CreateTask();

                task.CommandLine = "ComputeTask 5";
                task.WorkDirectory = @"c:\tests";

                job.AddTask(task);
     
                job.OnJobState += new EventHandler<JobStateEventArg>(job_OnJobState);
              
                scheduler.SubmitJob(job, null, null);
             
                jobFinishEvt.WaitOne();
            }

            static void job_OnJobState(object sender, JobStateEventArg e)
            {
                Console.WriteLine("Job <{0}> has changed from state <{1}> to <{2}>",
                                  e.JobId, e.PreviousState.ToString(), e.NewState.ToString());
                if (e.NewState.Equals(JobState.Finished))
                {
                    Console.WriteLine("Job <{0}> has finished", e.JobId);
                    jobFinishEvt.Set();
                }
            }
        }
    }

     

     


    Friday, April 18, 2008 7:37 PM

All replies