示例#1
0
        public void ForeachExecution(IEnumerable <T> source, Action <T> action, ExecutionConstancyEnum executionConstancy)
        {
            if (executionConstancy == ExecutionConstancyEnum.Constant)
            {
                ForeachExecution(source, action);
            }
            else
            {
                ParallelQueue <ForeachPart <T> >[] parallelQueues = new ParallelQueue <ForeachPart <T> > [processorCount];
                int counter = 0;
                while (source.GetEnumerator().MoveNext())
                {
                    counter++;
                }
                chunk = counter / processorCount;
                int start = 0;
                int end   = chunk;

                for (int i = 1; i <= processorCount; i++)
                {
                    int x = i - 1;
                    waitHandles[x] = new ManualResetEvent(false);
                    ParallelQueueFiller parallelQueueFiller        = new ParallelQueueFiller();
                    ParallelQueue <ForeachPart <T> > parallelQueue = parallelQueueFiller.FillWithForeachPart <T>(start, end, action, source);
                    parallelQueues[x] = parallelQueue;
                    UnconstantForeachSynchronisationContainer <T> unconstantForeachSynchronisationContainer =
                        new UnconstantForeachSynchronisationContainer <T>((ManualResetEvent)waitHandles[x], parallelQueue, parallelQueues);
                    ThreadPool.QueueUserWorkItem(
                        delegate(object state)
                    {
                        UnconstantForeachSynchronisationContainer <T> localUnconstantForeachSynchronisationContainer =
                            (UnconstantForeachSynchronisationContainer <T>)state;
                        ParallelQueue <ForeachPart <T> > localparallelQueue    = localUnconstantForeachSynchronisationContainer.ParallelQueue;
                        ParallelQueue <ForeachPart <T> >[] localparallelQueues = localUnconstantForeachSynchronisationContainer.ParallelQueues;
                        try
                        {
                            bool localQueueIsEmpty = false;
                            while (!localQueueIsEmpty)
                            {
                                ForeachPart <T> foreachPart = localparallelQueue.Dequeue();
                                if (foreachPart != null)
                                {
                                    int ii = foreachPart.Start;
                                    foreachPart.ExecutionPart.Invoke(ref ii, foreachPart.SingleSource);
                                }
                                else
                                {
                                    localQueueIsEmpty = true;
                                }
                            }
                            foreach (ParallelQueue <ForeachPart <T> > localForegnParallelQueue in localparallelQueues)
                            {
                                if (localForegnParallelQueue != localparallelQueue)
                                {
                                    bool localForegnQueueIsEmpty = false;
                                    while (!localForegnQueueIsEmpty)
                                    {
                                        ForeachPart <T> foreachPart = localForegnParallelQueue.Steal();
                                        if (foreachPart != null)
                                        {
                                            int ii = foreachPart.Start;
                                            foreachPart.ExecutionPart.Invoke(ref ii, foreachPart.SingleSource);
                                        }
                                        else
                                        {
                                            localForegnQueueIsEmpty = true;
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
                            localUnconstantForeachSynchronisationContainer.ManualResetEvent_.Set();
                        }
                    }, unconstantForeachSynchronisationContainer);
                    start = end + 1;
                    end   = end + chunk;
                }
                WaitHandle.WaitAll(waitHandles);
            }
        }
示例#2
0
        public void ForExecution(int from, int to, int step, Action action, ExecutionConstancyEnum executionConstancy)
        {
            if (executionConstancy == ExecutionConstancyEnum.Constant)
            {
                ForExecution(from, to, step, action);
            }
            else
            {
                ParallelQueue <ForPart>[] parallelQueues = new ParallelQueue <ForPart> [processorCount];
                chunk = (to - from) / processorCount;
                int start = from;
                int end   = chunk;

                for (int i = 1; i <= processorCount; i++)
                {
                    int x = i - 1;
                    waitHandles[x] = new ManualResetEvent(false);
                    ParallelQueueFiller     parallelQueueFiller = new ParallelQueueFiller();
                    ParallelQueue <ForPart> parallelQueue       = parallelQueueFiller.FillWithForPart(start, end, step, action);
                    parallelQueues[x] = parallelQueue;
                    UnconstantForSynchronisationContainer unconstantForSynchronisationContainer =
                        new UnconstantForSynchronisationContainer((ManualResetEvent)waitHandles[x], parallelQueue, parallelQueues);
                    ThreadPool.QueueUserWorkItem(
                        delegate(object state)
                    {
                        UnconstantForSynchronisationContainer localUnconstantForSynchronisationContainer =
                            (UnconstantForSynchronisationContainer)state;
                        ParallelQueue <ForPart> localparallelQueue    = localUnconstantForSynchronisationContainer.ParallelQueue;
                        ParallelQueue <ForPart>[] localparallelQueues = localUnconstantForSynchronisationContainer.ParallelQueues;
                        try
                        {
                            bool localQueueIsEmpty = false;
                            while (!localQueueIsEmpty)
                            {
                                ForPart forPart = localparallelQueue.Dequeue();
                                if (forPart != null)
                                {
                                    int ii = forPart.From;
                                    forPart.ExecutionPart.Invoke(ref ii);
                                }
                                else
                                {
                                    localQueueIsEmpty = true;
                                }
                            }
                            foreach (ParallelQueue <ForPart> localForegnParallelQueue in localparallelQueues)
                            {
                                if (localForegnParallelQueue != localparallelQueue)
                                {
                                    bool localForegnQueueIsEmpty = false;
                                    while (!localForegnQueueIsEmpty)
                                    {
                                        ForPart forPart = localForegnParallelQueue.Steal();
                                        if (forPart != null)
                                        {
                                            int ii = forPart.From;
                                            forPart.ExecutionPart.Invoke(ref ii);
                                        }
                                        else
                                        {
                                            localForegnQueueIsEmpty = true;
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
                            localUnconstantForSynchronisationContainer.ManualResetEvent_.Set();
                        }
                    }, unconstantForSynchronisationContainer);
                    start = end + 1;
                    end   = end + chunk;
                }
                WaitHandle.WaitAll(waitHandles);
            }
        }
        public void InvokeExecution(ExecutionConstancyEnum executionConstancy, InvokeAction[] actions)
        {
            if (executionConstancy == ExecutionConstancyEnum.Constant)
            {
                InvokeExecution(actions);
            }
            else
            {
                ParallelQueue <InvokePart>[] parallelQueues = new ParallelQueue <InvokePart> [processorCount];
                chunk = (actions.Length) / processorCount;
                int start = 0;
                int end   = chunk;

                for (int i = 1; i <= processorCount; i++)
                {
                    int x = i - 1;
                    waitHandles[x] = new ManualResetEvent(false);
                    ParallelQueueFiller parallelQueueFiller       = new ParallelQueueFiller();
                    List <InvokeAction> perProcessorInvokeActions = new List <InvokeAction>();
                    for (int j = start; j <= end; j++)
                    {
                        perProcessorInvokeActions.Add(actions[j]);
                    }
                    ParallelQueue <InvokePart> parallelQueue = parallelQueueFiller.FillWithInvoke(perProcessorInvokeActions.ToArray());
                    parallelQueues[x] = parallelQueue;
                    UnconstantInvokeSynchronisationContainer unconstantInvokeSynchronisationContainer =
                        new UnconstantInvokeSynchronisationContainer((ManualResetEvent)waitHandles[x], parallelQueue, parallelQueues);
                    ThreadPool.QueueUserWorkItem(
                        delegate(object state)
                    {
                        UnconstantInvokeSynchronisationContainer localUnconstantInvokeSynchronisationContainer =
                            (UnconstantInvokeSynchronisationContainer)state;
                        ParallelQueue <InvokePart> localparallelQueue    = localUnconstantInvokeSynchronisationContainer.ParallelQueue;
                        ParallelQueue <InvokePart>[] localparallelQueues = localUnconstantInvokeSynchronisationContainer.ParallelQueues;
                        try
                        {
                            bool localQueueIsEmpty = false;
                            while (!localQueueIsEmpty)
                            {
                                InvokePart invokePart = localparallelQueue.Dequeue();
                                if (invokePart != null)
                                {
                                    foreach (InvokeAction localInvokeAction in invokePart.InvokationParts)
                                    {
                                        localInvokeAction.Invoke();
                                    }
                                }
                                else
                                {
                                    localQueueIsEmpty = true;
                                }
                            }
                            foreach (ParallelQueue <InvokePart> localForegnParallelQueue in localparallelQueues)
                            {
                                if (localForegnParallelQueue != localparallelQueue)
                                {
                                    bool localForegnQueueIsEmpty = false;
                                    while (!localForegnQueueIsEmpty)
                                    {
                                        InvokePart invokePart = localForegnParallelQueue.Steal();
                                        if (invokePart != null)
                                        {
                                            foreach (InvokeAction localInvokeAction in invokePart.InvokationParts)
                                            {
                                                localInvokeAction.Invoke();
                                            }
                                        }
                                        else
                                        {
                                            localForegnQueueIsEmpty = true;
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
                            localUnconstantInvokeSynchronisationContainer.ManualResetEvent_.Set();
                        }
                    }, unconstantInvokeSynchronisationContainer);
                    start = end + 1;
                    end   = end + chunk;
                }
                WaitHandle.WaitAll(waitHandles);
            }
        }