/// <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); } }
/// <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 < 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; } }
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); }
/// <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); } }
/// <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(); } } }
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( ); } }
/// <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(); } } }
// 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; }
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 (); } } }
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( ); } } }
/// <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; }
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); }
/// <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); }
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); }
/// <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); }