public static ParallelTaskResult StaticFor(int low, int high, Action<ParallelTaskOptions, int, int> work, int workerCount, int chunk) { if (low < 0 || high < low) return new ParallelTaskResult(); ModuleProc PROC = new ModuleProc("ParallelTasks", "StaticFor"); ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created); if (workerCount <= 0) workerCount = 1; if (chunk <= 0) chunk = 1; if (high < chunk) chunk = ((high - low) / workerCount); CountdownEvent cde = new CountdownEvent(workerCount); Thread[] threads = new Thread[workerCount]; int currentCount = 0; ParallelTaskOptions options = new ParallelTaskOptions(); try { for (int i = 0; i < workerCount; i++) { threads[i] = Extensions.CreateThreadAndStart((o) => { int k = (int)o; int start = low + (k * chunk); int end = ((k == (workerCount - 1)) ? high : (start + chunk)); for (int j = start; j < end; j++) { if (options.IsCancelled) break; try { work(options, j, currentCount); } catch (Exception ex) { Log.Exception(PROC, ex); result.Exceptions.Add(ex); } finally { Interlocked.Increment(ref currentCount); } } cde.Signal(); }, i, "StaticFor_" + i.ToString()); } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { cde.Wait(); result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed); } return result; }
public static ParallelTaskResult DynamicFor(int low, int high, Action <ParallelTaskOptions, int, int> work, int workerCount) { if (low < 0 || high < low) { return(new ParallelTaskResult()); } ModuleProc PROC = new ModuleProc("ParallelTasks", "For"); ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created); if (workerCount <= 0) { workerCount = 1; } const int chunk = 16; CountdownEvent cde = new CountdownEvent(workerCount); Thread[] threads = new Thread[workerCount]; int currentCount = 0; int currentValue = low; ParallelTaskOptions options = new ParallelTaskOptions(); try { for (int i = 0; i < workerCount; i++) { threads[i] = Extensions.CreateThreadAndStart((o) => { int j = 0; int currentChunk = 1; while (true) { if (options.IsCancelled) { break; } if ((currentValue + currentChunk) > Int32.MaxValue) { break; } j = Interlocked.Add(ref currentValue, currentChunk) - currentChunk; if (j >= high) { break; } for (int k = 0; (k < currentChunk) && ((j + k) < high); k++) { if (options.IsCancelled) { break; } try { work(options, j, currentCount); } catch (Exception ex) { Log.Exception(PROC, ex); } finally { Interlocked.Increment(ref currentCount); } } if (currentChunk < chunk) { currentChunk *= 2; } } cde.Signal(); }, i); } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { cde.Wait(); result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed); } return(result); }
public static ParallelTaskResult DynamicFor(int low, int high, Action<ParallelTaskOptions, int, int> work, int workerCount) { if (low < 0 || high < low) return new ParallelTaskResult(); ModuleProc PROC = new ModuleProc("ParallelTasks", "For"); ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created); if (workerCount <= 0) workerCount = 1; const int chunk = 16; CountdownEvent cde = new CountdownEvent(workerCount); Thread[] threads = new Thread[workerCount]; int currentCount = 0; int currentValue = low; ParallelTaskOptions options = new ParallelTaskOptions(); try { for (int i = 0; i < workerCount; i++) { threads[i] = Extensions.CreateThreadAndStart((o) => { int j = 0; int currentChunk = 1; while (true) { if (options.IsCancelled) break; if ((currentValue + currentChunk) > Int32.MaxValue) break; j = Interlocked.Add(ref currentValue, currentChunk) - currentChunk; if (j >= high) break; for (int k = 0; (k < currentChunk) && ((j + k) < high); k++) { if (options.IsCancelled) break; try { work(options, j, currentCount); } catch (Exception ex) { Log.Exception(PROC, ex); } finally { Interlocked.Increment(ref currentCount); } } if (currentChunk < chunk) currentChunk *= 2; } cde.Signal(); }, i); } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { cde.Wait(); result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed); } return result; }
public static ParallelTaskResult StaticFor(int low, int high, Action <ParallelTaskOptions, int, int> work, int workerCount, int chunk) { if (low < 0 || high < low) { return(new ParallelTaskResult()); } ModuleProc PROC = new ModuleProc("ParallelTasks", "StaticFor"); ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created); if (workerCount <= 0) { workerCount = 1; } if (chunk <= 0) { chunk = 1; } if (high < chunk) { chunk = ((high - low) / workerCount); } CountdownEvent cde = new CountdownEvent(workerCount); Thread[] threads = new Thread[workerCount]; int currentCount = 0; ParallelTaskOptions options = new ParallelTaskOptions(); try { for (int i = 0; i < workerCount; i++) { threads[i] = Extensions.CreateThreadAndStart((o) => { int k = (int)o; int start = low + (k * chunk); int end = ((k == (workerCount - 1)) ? high : (start + chunk)); for (int j = start; j < end; j++) { if (options.IsCancelled) { break; } try { work(options, j, currentCount); } catch (Exception ex) { Log.Exception(PROC, ex); result.Exceptions.Add(ex); } finally { Interlocked.Increment(ref currentCount); } } cde.Signal(); }, i, "StaticFor_" + i.ToString()); } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { cde.Wait(); result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed); } return(result); }