internal ParallelQueue <ForPart <T, T1> > FillWithForPart <T, T1>(int from, int to, int step, Action <T, T1> action)
        {
            ParallelQueue <ForPart <T, T1> > parallelQueue = new ParallelQueue <ForPart <T, T1> >();

            for (int i = from; i < to; i = i + step)
            {
                ForPart <T, T1> forPart = new ForPart <T, T1>(i, i);
                forPart.ExecutionPart = action;
                parallelQueue.Enqueue(forPart);
            }
            return(parallelQueue);
        }
        internal ParallelQueue <ForPart <T1, T2> > FillWithForPart <T1, T2>(int from, int to, int step, Action <T1, T2> action, T1 value1, T2 value2)
        {
            ParallelQueue <ForPart <T1, T2> > parallelQueue = new ParallelQueue <ForPart <T1, T2> >();

            for (int i = from; i < to; i = i + step)
            {
                ForPart <T1, T2> forPart = new ForPart <T1, T2>(i, i);
                forPart.Value1        = value1;
                forPart.Value2        = value2;
                forPart.ExecutionPart = action;
                parallelQueue.Enqueue(forPart);
            }
            return(parallelQueue);
        }
Exemple #3
0
        public void ForExecution <T1, T2>(int from, int to, int step, Action <T1, T2> action, T1 value1, T2 value2)
        {
            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);
                ForPart <T1, T2> forPart = new ForPart <T1, T2>(start, end, step);
                forPart.ExecutionPart = action;
                forPart.Value1        = value1;
                forPart.Value2        = value2;
                ConstantForSynchronisationContainer forSynchronisationContainer =
                    new ConstantForSynchronisationContainer((ManualResetEvent)waitHandles[x], forPart);
                ThreadPool.QueueUserWorkItem(
                    delegate(object state)
                {
                    ConstantForSynchronisationContainer <T1, T2> localforSynchronisationContainer = (ConstantForSynchronisationContainer <T1, T2>)state;
                    ForPart <T1, T2> localforPart = localforSynchronisationContainer.ForPart_;
                    try
                    {
                        for (int ii = localforPart.From; ii <= localforPart.To; ii = ii + localforPart.Step)
                        {
                            localforPart.ExecutionPart.Invoke(ref ii, localforPart.Value1, localforPart.Value2);
                        }
                    }
                    finally
                    {
                        localforSynchronisationContainer.ManualResetEvent_.Set();
                    }
                }, forSynchronisationContainer);
                start = end + 1;
                end   = end + chunk;
            }
            WaitHandle.WaitAll(waitHandles);
        }
Exemple #4
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);
            }
        }
Exemple #5
0
 public ConstantForSynchronisationContainer(ManualResetEvent manualResetEvent, ForPart forPart) : base(manualResetEvent)
 {
     this.forPart_ = forPart;
 }