view defical-sharp/defical-cli/threader-backtrack.cs @ 0:ebed2bd0d300

Initial import from svn. History be damned.
author Edho P. Arief <me@myconan.net>
date Fri, 02 Apr 2010 23:11:57 +0700
parents
children
line wrap: on
line source

using System;
using System.Threading;
using System.IO;
using libbacktrack;

namespace defical_cli
{
    public partial class Program
    {
        private partial class main
        {
            private void goBT()
            {
                this.isProcessing = true;
                this.result = "";
                Thread[] threads = new Thread[this.numThreads];
                int start;
                int end = 0;
                for (int i = 0; i < this.numThreads; i++)
                {
                    threads[i] = new Thread(goThreadBT);
                    threads[i].IsBackground = true;
                    threads[i].Priority = ThreadPriority.Lowest;
                    start = end + 1;
                    end = start + ((this.numVer + this.numDef + 1) / 2) / this.numThreads;
                    if (i == this.numThreads - 1)
                        end = (this.numVer + this.numDef + 1) / 2;
                    threads[i].Start(new int[] { start, end, i });
                }
                while (this.isProcessing)
                {
                    Thread.Sleep(100);
                    bool threadsIsProcessing = false;
                    for (int i = 0; i < this.numThreads; i++)
                    {
                        if (threads[i].IsAlive)
                        {
                            threadsIsProcessing = true;
                        }
                    }
                    this.isProcessing = threadsIsProcessing;
                }
                if (this.result == "") { this.result = "SEMT labeling can't be constructed."; }
                Console.WriteLine(this.result);
                this.isProcessing = false;
                for (int i = 0; i < this.numThreads; i++)
                {
                    if (threads[i].IsAlive)
                    {
                        threads[i].Abort();
                    }
                }

            }
            private void goThreadBT(object o)
            {
                int[] args = o as int[];
                int start = args[0];
                int end = args[1];
                int threadID = args[2];
                for (int i = start; i <= end; i++)
                {
                    //if (this.isProcessing)
                    {
                        Backtrack todo = new Backtrack(this.graphType, this.numVer, this.numDef, i, true);
                        todo.Walk();
                        if (this.isProcessing)
                        {
                            if (todo.IsSemt)
                            {
                                //this.isProcessing = false;
                                Console.Write("{0}",todo.Result);
                                this.result = todo.Result;
                                /*string filename = "log-" +
                                    this.graphType + "-" +
                                    "numver=" + this.numVer.ToString() + "-" +
                                    "numdef=" + this.numDef.ToString() + "-" +
                                    DateTime.UtcNow.ToString("yyyyMMdd_hhmmss");
                                string filepath = getHomeDir() + filename;
                                int n = 1;
                                while (File.Exists(filepath + ".txt"))
                                {
                                    if (!File.Exists(filepath + "-" + n.ToString() + ".txt"))
                                    {
                                        filepath += "-" + n.ToString();
                                        break;
                                    }
                                    n++;
                                }
                                WriteToFile(filepath + ".txt", this.result);*/
                                //break;
                            }
                            else
                            {
                                Console.WriteLine("[404] SEMT labeling not found for first label of {0}", i);
                            }
                        }
                    }
                }
            }
        }
    }
}