Beispiel #1
0
 /// <summary>
 ///   Parallel for mock-up. The provided
 ///   code will NOT be run in parallel.
 /// </summary>
 ///
 public static void For(int start, int stop, ForLoopBody loopBody)
 {
     for (int i = start; i < stop; i++)
     {
         loopBody(i);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Executes a for-loop in which iterations may run in parallel.
        /// </summary>
        ///
        /// <param name="start">Loop's start index.</param>
        /// <param name="stop">Loop's stop index.</param>
        /// <param name="loopBody">Loop's body.</param>
        ///
        /// <remarks><para>The method is used to parallel for-loop running its iterations in
        /// different threads. The <b>start</b> and <b>stop</b> parameters define loop's
        /// starting and ending loop's indexes. The number of iterations is equal to <b>stop - start</b>.
        /// </para>
        ///
        /// <para>Sample usage:</para>
        /// <code>
        /// Parallel.For( 0, 20, delegate( int i )
        /// // which is equivalent to
        /// // for ( int i = 0; i &lt; 20; i++ )
        /// {
        ///     System.Diagnostics.Debug.WriteLine( "Iteration: " + i );
        ///     // ...
        /// } );
        /// </code>
        /// </remarks>
        ///
        public static void For(int start, int stop, ForLoopBody loopBody)
        {
            lock ( sync )
            {
                // get instance of parallel computation manager
                Parallel instance = Instance;

                instance.currentIndex = start - 1;
                instance.stopIndex    = stop;
                instance.loopBody     = loopBody;

                // signal about available job for all threads and mark them busy
                for (int i = 0; i < threadsCount; i++)
                {
                    instance.threadIdle[i].Reset( );
                    instance.jobAvailable[i].Set( );
                }

                // wait until all threads become idle
                for (int i = 0; i < threadsCount; i++)
                {
                    instance.threadIdle[i].WaitOne( );
                }

                instance.loopBody = null;
            }
        }
Beispiel #3
0
		public static void For( int start, int stop, int stepLength, ForLoopBody loopBody, bool close  )
		{
			// get instance of parallel computation manager
			Parallel instance = new Parallel ();
			instance.Initialize ();
			instance.ForLoop (start,stop,stepLength,loopBody, close);
		}
Beispiel #4
0
 /// <summary>
 ///   Parallel for mock-up. The provided
 ///   code will NOT be run in parallel.
 /// </summary>
 ///
 public static void For(int start, int stop, ParallelOptions options, ForLoopBody loopBody)
 {
     for (int i = start; i < stop; i++)
     {
         loopBody(i);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Parallel for loop - we don't cate what order the itterations are run in
        /// </summary>
        /// <param name="start">start index.</param>
        /// <param name="stop">stop index.</param>
        /// <param name="loopBody">Loop's body</param>
        /// <example>
        /// Parallel.For( 0, 20, delegate( int i )
        /// {
        ///     // insert your itteration code here...
        /// } );
        /// </example>
        public static void For(int start, int stop, ForLoopBody loopBody)
        {
            lock (sync)
            {
                // get instance of parallel computation manager
                Parallel instance = Instance;

                instance.currentIndex = start - 1;
                instance.stopIndex = stop;
                instance.loopBody = loopBody;

                // signal about available job for all threads and mark them busy
                for (int i = 0; i < threadsCount; i++)
                {
                    instance.threadIdle[i].Reset();
                    instance.jobAvailable[i].Set();
                }

                // wait until all threads become idle
                for (int i = 0; i < threadsCount; i++)
                {
                    instance.threadIdle[i].WaitOne();
                }
            }
        }
Beispiel #6
0
		public void Close () {
			//Exit all threads
			loopBody = null;
			for ( int i = 0; i < threadsCount; i++ )
			{
				jobAvailable[i].Set( );
			}
		}
        public static void For(int start, int stop, int stepLength, ForLoopBody loopBody, bool close)
        {
            // get instance of parallel computation manager
            Parallel instance = new Parallel();

            instance.Initialize();
            instance.ForLoop(start, stop, stepLength, loopBody, close);
        }
 public void Close()
 {
     //Exit all threads
     loopBody = null;
     for (int i = 0; i < threadsCount; i++)
     {
         jobAvailable[i].Set( );
     }
 }
Beispiel #9
0
        /// <summary>
        ///   Parallel for mock-up. The provided
        ///   code will NOT be run in parallel.
        /// </summary>
        ///
        public static void For <T>(int start, int stop, ParallelOptions options,
                                   Func <T> initial, ForLoopBody <T> loopBody, Action <T> end)
        {
            T obj = initial();

            for (int i = start; i < stop; i++)
            {
                obj = loopBody(i, null, obj);
            }
            end(obj);
        }
 private void Terminate()
 {
     loopBody = null;
     for (int i = 0, threadsCount = threads.Length; i < threadsCount; i++)
     {
         jobAvailable[i].Set();
         threads[i].Join();
         jobAvailable[i].Close();
         threadIdle[i].Close();
     }
     jobAvailable = null;
     threadIdle   = null;
     threads      = null;
 }
        private void Terminate()
        {
            this.loopBody = null;
            int index  = 0;
            int length = this.threads.Length;

            while (index < length)
            {
                this.jobAvailable[index].Set();
                this.threads[index].Join();
                this.jobAvailable[index].Close();
                this.threadIdle[index].Close();
                index++;
            }
            this.jobAvailable = null;
            this.threadIdle   = null;
            this.threads      = null;
        }
 public static void For(int start, int stop, ForLoopBody loopBody)
 {
     lock (sync)
     {
         Parallel instance = Instance;
         instance.currentIndex = start - 1;
         instance.stopIndex    = stop;
         instance.loopBody     = loopBody;
         for (int i = 0; i < threadsCount; i++)
         {
             instance.threadIdle[i].Reset();
             instance.jobAvailable[i].Set();
         }
         for (int j = 0; j < threadsCount; j++)
         {
             instance.threadIdle[j].WaitOne();
         }
     }
 }
        public void ForLoop(int start, int stop, int stepLength, ForLoopBody loopBody, bool close)
        {
            lock ( sync )
            {
                //Parallel instance = new Parallel ();


                stepLength = stepLength < 1 ? 1 : stepLength;

                currentIndex  = start;
                stopIndex     = stop;
                this.loopBody = loopBody;
                step          = stepLength;
                //No point starting new threads for a single core computer, just run it on this thread
                if (threadsCount <= 1)
                {
                    for (int i = start; i < stop; i += stepLength)
                    {
                        loopBody(i);
                    }
                    return;
                }

                // signal about available job for all threads and mark them busy
                for (int i = 0; i < threadsCount; i++)
                {
                    threadIdle[i].Reset( );
                    jobAvailable[i].Set( );
                }

                // wait until all threads become idle
                for (int i = 0; i < threadsCount; i++)
                {
                    threadIdle[i].WaitOne( );
                }

                if (close)
                {
                    Close();
                }
            }
        }
Beispiel #14
0
        // Terminate all worker threads used for parallel computations and close all
        // synchronization objects
        private void Terminate( )
        {
            // finish thread by setting null loop body and signaling about available work
            loopBody = null;
            for (int i = 0, threadsCount = threads.Length; i < threadsCount; i++)
            {
                jobAvailable[i].Set( );
                // wait for thread termination
                threads[i].Join( );

                // close events
                jobAvailable[i].Close( );
                threadIdle[i].Close( );
            }

            // clean all array references
            jobAvailable = null;
            threadIdle   = null;
            threads      = null;
        }
Beispiel #15
0
		public void ForLoop ( int start, int stop, int stepLength, ForLoopBody loopBody, bool close  )
		{
			lock ( sync )
			{
				
				//Parallel instance = new Parallel ();
				
				
				stepLength = stepLength < 1 ? 1 : stepLength;
				
				currentIndex	= start;
				stopIndex		= stop;
				this.loopBody	= loopBody;
				step			= stepLength;
				//No point starting new threads for a single core computer, just run it on this thread
				if (threadsCount <= 1) {
					for (int i=start;i<stop;i+= stepLength) {
						loopBody( i );
					}
					return;
				}
				
				// signal about available job for all threads and mark them busy
				for ( int i = 0; i < threadsCount; i++ )
				{
					threadIdle[i].Reset( );
					jobAvailable[i].Set( );
				}
				
				// wait until all threads become idle
				for ( int i = 0; i < threadsCount; i++ )
				{
					threadIdle[i].WaitOne( );
				}
				
				if (close) {
					Close ();
				}
			}
		}
Beispiel #16
0
        public static void For( int start, int stop, ForLoopBody loopBody  )
        {
            lock ( sync )
            {
                var parallelInstance = Instance;

                parallelInstance.currentIndex   = start - 1;
                parallelInstance.stopIndex      = stop;
                parallelInstance.loopBody       = loopBody;

                for ( var i = 0; i < threadsCount; i++ )
                {
                    parallelInstance.threadIdle[i].Reset( );
                    parallelInstance.jobAvailable[i].Set( );
                }

                for ( var i = 0; i < threadsCount; i++ )
                {
                    parallelInstance.threadIdle[i].WaitOne( );
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Terminate all worker threads used for parallel computations and close all synchronization objects
        /// </summary>
        private void Terminate()
        {
            // finish thread by setting null loop body and signaling about available work
            loopBody = null;
            for (int i = 0, threadsCount = threads.Length; i < threadsCount; i++)
            {
                jobAvailable[i].Set();

                // wait for thread termination
                threads[i].Join();

                // close events
                jobAvailable[i].Close();
                threadIdle[i].Close();
            }

            // clean all array references
            jobAvailable = null;
            threadIdle = null;
            threads = null;
        }
Beispiel #18
0
        private void Terminate( )
        {
            loopBody = null;
            for ( int i = 0, count = threads.Length ; i < count; i++ )
            {
                jobAvailable[i].Set( );
                threads[i].Join( );

                jobAvailable[i].Close( );
                threadIdle[i].Close( );
            }

            jobAvailable    = null;
            threadIdle      = null;
            threads         = null;
        }
 public static void For(int start, int stop, int stepLength, ForLoopBody loopBody)
 {
     For(start, stop, stepLength, loopBody, true);
 }
 public static void For(int start, int stop, ForLoopBody loopBody)
 {
     For(start, stop, 1, loopBody);
 }
Beispiel #21
0
 /// <summary>
 ///   Parallel for mock-up. The provided
 ///   code will NOT be run in parallel.
 /// </summary>
 ///
 public static void For <T>(int start, int stop,
                            Func <T> initial, ForLoopBody <T> loopBody, Action <T> end)
 {
     For(start, stop, null, initial, loopBody, end);
 }
Beispiel #22
0
		public static void For( int start, int stop, int stepLength, ForLoopBody loopBody  )
		{
			For (start,stop,stepLength,loopBody,true);
		}
Beispiel #23
0
		public static void For( int start, int stop, ForLoopBody loopBody  )
		{
			For (start,stop,1,loopBody);
		}
 /// <summary>
 ///   Parallel for mock-up. The provided
 ///   code will NOT be run in parallel.
 /// </summary>
 /// 
 public static void For(int start, int stop, ForLoopBody loopBody)
 {
     for (int i = start; i < stop; i++) loopBody(i);
 }