使用c#

时间:2016-10-27 14:10:31

标签: c# multithreading tree stream twincat

我正在尝试优化我用于通过ADS接口在TwinCat 3中找到标记符号的搜索算法。问题不是TwinCat相关,所以不要害怕。

问题: 符号不会立即加载。我认为TwinCatAds库使用延迟加载。 符号具有非二元非平衡树的树状结构。

解决方案: 您可以向ADS打开多个流。并在多个线程中处理流。

问题是,我将第一级符号除以处理器内核的数量。因此,由于树是不平衡的,因此一些线程比其他线程完成得更快。因此,我需要一个更好的解决方案,如何划分线程之间的工作。

PS:我不能使用Parallel.ForEach()。由于流,它产生的时间量与单线程解决方案相同或更长。

我的测试代码看起来像这样,它只计算一个巨大项目的所有符号。

using TwinCAT.Ads;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Collections;


namespace MultipleStreamsTest
{
class Program
{
    static int numberOfThreads = Environment.ProcessorCount;
    static TcAdsClient client;
    static TcAdsSymbolInfoLoader symbolLoader;
    static TcAdsSymbolInfoCollection[] collection = new TcAdsSymbolInfoCollection[numberOfThreads];
    static int[] portionResult = new int[numberOfThreads];
    static int[] portionStart = new int[numberOfThreads];
    static int[] portionStop = new int[numberOfThreads];

    static void Connect()
    {
        client = new TcAdsClient();
        client.Connect(851);
        Console.WriteLine("Conected ");
    }
    static void Main(string[] args)
    {
        Connect();
        symbolLoader = client.CreateSymbolInfoLoader();
        CountAllOneThread();
        CountWithMultipleThreads();
        Console.ReadKey();
    }        
    static public void CountAllOneThread()
    {
        Stopwatch stopwatch = new Stopwatch();
        int index = 0;
        stopwatch.Start();
        Console.WriteLine("Counting with one thread...");
        //Count all symbols
        foreach (TcAdsSymbolInfo symbol in symbolLoader)
        {                
            index++;
        }
        stopwatch.Stop();
        //Output
        Console.WriteLine("Counted with one thred " + index + " symbols in " + stopwatch.Elapsed);
    }
    static public int countRecursive(TcAdsSymbolInfo symbol)
    {
        int i = 0;
        TcAdsSymbolInfo subSymbol = symbol.FirstSubSymbol;
        while (subSymbol != null)
        {
            i = i + countRecursive(subSymbol);
            subSymbol = subSymbol.NextSymbol;
            i++;
        }
        return i;
    }
    static public void countRecursiveMultiThread(object portionNum)
    {
        int portionNumAsInt = (int)portionNum;
        for (int i = portionStart[portionNumAsInt]; i <= portionStop[portionNumAsInt]; i++)
        {
                portionResult[portionNumAsInt] += countRecursive(collection[portionNumAsInt][i]);//Collection Teil 
        }
    }
    static public void CountWithMultipleThreads()
    {
        Stopwatch stopwatch = new Stopwatch();
        int sum = 0;
        stopwatch.Start();
        Console.WriteLine("Counting with multiple thread...");
        for (int i = 0; i < numberOfThreads; i++)
        {
            collection[i] = symbolLoader.GetSymbols(true);
        }
        int size = (int)(collection[0].Count / numberOfThreads);
        int rest = collection[0].Count % numberOfThreads;
        int m = 0;
        for (; m < numberOfThreads; m++)
        {
            portionStart[m] = m * size;
            portionStop[m] = portionStart[m] + size - 1;
        }
        portionStop[m - 1] += rest;

        Thread[] threads = new Thread[numberOfThreads];
        for (int i = 0; i < numberOfThreads; i++)
        {
            threads[i] = new Thread(countRecursiveMultiThread);
            threads[i].Start(i);
            Console.WriteLine("Thread #" + threads[i].ManagedThreadId + " started, fieldIndex: " + i);
        }
        //Check when threads finishing:
        int threadsFinished = 0;
        bool[] threadFinished = new bool[numberOfThreads];
        int x = 0;
        while (true)
        {
            if (threads[x].Join(10) && !threadFinished[x] )
            {
                Console.WriteLine("Thread #" + threads[x].ManagedThreadId + " finished ~ at: " + stopwatch.Elapsed);
                threadsFinished++;
                threadFinished[x] = true;                    
            }
            x++;
            x = x % numberOfThreads;
            if (threadsFinished == numberOfThreads) break;
            Thread.Sleep(50);
        }            
        foreach (int n in portionResult)
        {
            sum += n;
        }
        sum += collection[0].Count;
        stopwatch.Stop();
        //Output
        Console.WriteLine("Counted with multiple threds in Collection " + sum + " symbols " + " in " + stopwatch.Elapsed);
        for (int i = 0; i < numberOfThreads; i++)
        {
            Console.WriteLine("#" + i + ": " + portionResult[i]);
        }
    }
}
}

The console output:

如果您尝试运行代码,请使用TwinCat.Ads版本4.0.17.0(我正在使用)。他们打破了NuGet提供的新版本。

1 个答案:

答案 0 :(得分:1)

创建一个线程池并跟踪线程运行和空闲状态。在每个分支处检查是否有空闲线程,如果有分配线程到子分支。