Search code examples
c#debugginghpc

C# HPC Scheduler, Job not completes


I am trying to test a simply designed code piece on Microsoft HPC. It looks like the job is not completing the all code. My test method might be wrong though. I am just printing out some check points. Here is the code:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

class S
{
    static void Main()
    {
        pcount = Environment.ProcessorCount;
        Console.WriteLine("Proc count = " + pcount);
        ThreadPool.SetMinThreads(4, -1);
        ThreadPool.SetMaxThreads(4, -1);

      //  System.Threading.Thread.Sleep(20000);

        Console.WriteLine("check point 0 ");
        t1 = new Task(A, 1);
        t2 = new Task(A, 2);
        t3 = new Task(A, 3);
        t4 = new Task(A, 4);

        Console.WriteLine("Starting t1 " + t1.Id.ToString());
        t1.Start();
        Console.WriteLine("Starting t2 " + t2.Id.ToString());
        t2.Start();
        Console.WriteLine("Starting t3 " + t3.Id.ToString());
        t3.Start();
        Console.WriteLine("Starting t4 " + t4.Id.ToString());
        t4.Start();

        //  Console.ReadLine();
    }

    static void A(object o)
    {
        Console.WriteLine("check point A ");
        B(o);
    }
    static void B(object o)
    {
        int temp = (int)o;
        Console.WriteLine("check point B ");
        if (temp == 1)
        {
            C(o);
        }
        else
        {
            F(o);
        }
    }
    static void C(object o)
    {
        Console.WriteLine("check point C ");
        D(o);
    }
    static void D(object o)
    {
        int temp = (int)o;
        Console.WriteLine("check point D " + temp);

        if (temp == 2)
        {
            F(o);
        }
        else
        {
            E(o);
        }
    }
    static void E(object o)
    {
        Console.WriteLine("check point E ");

    }
    static void F(object o)
    {
        Console.WriteLine("check point F ");
        G(o);
    }
    static void G(object o)
    {
        Console.WriteLine("check point G ");

    }



    static Task t1, t2, t3, t4;

    static int pcount;
}

In the job output, it is not printing till to the end. Printout ends randomly at any point( in a random function). Ok, I understand that, It may not able to print everything since the execution is faster than printing (possible explanation). But, if I try to place breakpoints and try to debug by attaching to process, it only hits the breakpoints in the first parts of the code. Looks like hitting the same range as test print out has. How I can make sure that the code runs through the end, so that breakpoints at any point can be hit?


Solution

  • A Task is designed to run on a so-called background thread. That means: If the main thread terminates, so will the background thread. So as soon as your Main() method completes, all threads (and therefore all Tasks) get terminated, and your program ends.

    The solution is to wait for your Tasks to complete.

    static void Main()
    {
        pcount = Environment.ProcessorCount;
        Console.WriteLine("Proc count = " + pcount);
        ThreadPool.SetMinThreads(4, -1);
        ThreadPool.SetMaxThreads(4, -1);
    
      //  System.Threading.Thread.Sleep(20000);
    
        Console.WriteLine("check point 0 ");
        t1 = new Task(A, 1);
        t2 = new Task(A, 2);
        t3 = new Task(A, 3);
        t4 = new Task(A, 4);
    
        Console.WriteLine("Starting t1 " + t1.Id.ToString());
        t1.Start();
        Console.WriteLine("Starting t2 " + t2.Id.ToString());
        t2.Start();
        Console.WriteLine("Starting t3 " + t3.Id.ToString());
        t3.Start();
        Console.WriteLine("Starting t4 " + t4.Id.ToString());
        t4.Start();
    
        //  Console.ReadLine();
    
        t1.Wait();
        t2.Wait();
        t3.Wait();
        t4.Wait();
    }