// // Math.NET Numerics, part of the Math.NET Project // http://mathnet.opensourcedotnet.info // // Copyright (c) 2009 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics.Threading { using System; using System.Collections.Generic; using System.Threading; using Properties; /// /// Provides support for parallel loops. /// internal static class Parallel { /// /// Executes a for loop in which iterations may run in parallel. /// /// The start index, inclusive. /// The end index, exclusive. /// The body to be invoked for each iteration. /// The argument is null. /// At least one invocation of the body threw an exception. internal static void For(int fromInclusive, int toExclusive, Action body) { if (body == null) { throw new ArgumentNullException("body"); } var actions = new Action[ThreadQueue.ThreadCount]; var count = toExclusive - fromInclusive; var size = count / actions.Length; if (count < 1) { return; } // partition the jobs into separate sets for each but the last worked thread for (var i = 0; i < actions.Length - 1; i++) { var start = fromInclusive + (i * size); var stop = fromInclusive + ((i + 1) * size); actions[i] = () => { for (var j = start; j < stop; j++) { body(j); } }; } // add another set for last worker thread actions[actions.Length - 1] = () => { for (var i = fromInclusive + ((actions.Length - 1) * size); i < toExclusive; i++) { body(i); } }; Invoke(actions); } /// /// Executes each of the provided actions inside a discrete, asynchronous task. /// /// An array of actions to execute. /// The argument is null. /// The actions array contains a null element. /// An action threw an exception. internal static void Invoke(params Action[] actions) { if (actions == null) { throw new ArgumentNullException("actions"); } // create a job for each action var tasks = new Task[actions.Length]; for (int i = 0; i < tasks.Length; i++) { Action action = actions[i]; if (action == null) { throw new ArgumentException(String.Format(Resources.ArgumentItemNull, "actions"), "actions"); } tasks[i] = new Task(action); } // run the jobs ThreadQueue.Enqueue(tasks); // wait until all jobs have completed if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) { // not sure if this the best approach for STA for (int i = 0; i < tasks.Length; i++) { tasks[i].WaitOne(); } } else { WaitHandle.WaitAll(tasks); } // collect all thrown exceptions and dispose the jobs var exceptions = new List(); foreach (var task in tasks) { if (task.ThrewException) { exceptions.Add(task.Exception); } // this calls dispose task.Close(); } // throw the aggregated exceptions, if any if (exceptions.Count > 0) { throw new AggregateException(exceptions); } } } }