Parallel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. #if NET_4_0
  2. // Parallel.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Collections.Concurrent;
  28. using System.Threading;
  29. namespace System.Threading.Tasks
  30. {
  31. public static class Parallel
  32. {
  33. internal static int GetBestWorkerNumber ()
  34. {
  35. return GetBestWorkerNumber (TaskScheduler.Current);
  36. }
  37. internal static int GetBestWorkerNumber (TaskScheduler scheduler)
  38. {
  39. return scheduler.MaximumConcurrencyLevel;
  40. }
  41. static void HandleExceptions (IEnumerable<Task> tasks)
  42. {
  43. HandleExceptions (tasks, null);
  44. }
  45. static void HandleExceptions (IEnumerable<Task> tasks, ParallelLoopState.ExternalInfos infos)
  46. {
  47. List<Exception> exs = new List<Exception> ();
  48. foreach (Task t in tasks) {
  49. if (t.Exception != null && !(t.Exception is TaskCanceledException))
  50. exs.Add (t.Exception);
  51. }
  52. if (exs.Count > 0) {
  53. if (infos != null)
  54. infos.IsExceptional = true;
  55. throw new AggregateException (exs);
  56. }
  57. }
  58. static void InitTasks (Task[] tasks, Action action, int count)
  59. {
  60. InitTasks (tasks, count, () => Task.Factory.StartNew (action, TaskCreationOptions.DetachedFromParent));
  61. }
  62. static void InitTasks (Task[] tasks, Action action, int count, TaskScheduler scheduler)
  63. {
  64. InitTasks (tasks, count, () => Task.Factory.StartNew (action, TaskCreationOptions.DetachedFromParent, scheduler));
  65. }
  66. static void InitTasks (Task[] tasks, int count, Func<Task> taskCreator)
  67. {
  68. for (int i = 0; i < count; i++) {
  69. tasks [i] = taskCreator ();
  70. }
  71. }
  72. #region For
  73. public static ParallelLoopResult For (int from, int to, Action<int> action)
  74. {
  75. return For (from, to, null, action);
  76. }
  77. public static ParallelLoopResult For (int from, int to, Action<int, ParallelLoopState> action)
  78. {
  79. return For (from, to, null, action);
  80. }
  81. public static ParallelLoopResult For (int from, int to, ParallelOptions options, Action<int> action)
  82. {
  83. return For (from, to, options, (index, state) => action (index));
  84. }
  85. public static ParallelLoopResult For (int from, int to, ParallelOptions options, Action<int, ParallelLoopState> action)
  86. {
  87. return For<object> (from, to, options, null, (i, s, l) => { action (i, s); return null; }, null);
  88. }
  89. public static ParallelLoopResult For<TLocal> (int from, int to, Func<TLocal> init,
  90. Func<int, ParallelLoopState, TLocal, TLocal> action, Action<TLocal> destruct)
  91. {
  92. return For<TLocal> (from, to, null, init, action, destruct);
  93. }
  94. [MonoTODO]
  95. public static ParallelLoopResult For<TLocal> (int from, int to, ParallelOptions options,
  96. Func<TLocal> init,
  97. Func<int, ParallelLoopState, TLocal, TLocal> action,
  98. Action<TLocal> destruct)
  99. {
  100. if (action == null)
  101. throw new ArgumentNullException ("action");
  102. // Number of task to be launched (normally == Env.ProcessorCount)
  103. int num = Math.Min (GetBestWorkerNumber (),
  104. options != null && options.MaxDegreeOfParallelism != -1 ? options.MaxDegreeOfParallelism : int.MaxValue);
  105. // Integer range that each task process
  106. int step = Math.Min (5, (to - from) / num);
  107. if (step <= 0)
  108. step = 1;
  109. throw new NotImplementedException ();
  110. /*
  111. // Each worker put the indexes it's responsible for here
  112. // so that other worker may steal if they starve.
  113. ConcurrentBag<int> bag = new ConcurrentBag<int> ();
  114. Task[] tasks = new Task [num];
  115. ParallelLoopState.ExternalInfos infos = new ParallelLoopState.ExternalInfos ();
  116. int currentIndex = from;
  117. Action workerMethod = delegate {
  118. int index, actual;
  119. TLocal local = (init == null) ? default (TLocal) : init ();
  120. ParallelLoopState state = new ParallelLoopState (tasks, infos);
  121. try {
  122. while ((index = Interlocked.Add (ref currentIndex, step) - step) < to) {
  123. if (infos.IsStopped.Value)
  124. return;
  125. if (options != null && options.CancellationToken.IsCancellationRequested) {
  126. state.Stop ();
  127. return;
  128. }
  129. for (int i = index; i < to && i < index + step; i++)
  130. bag.Add (i);
  131. for (int i = index; i < to && i < index + step && bag.TryTake (out actual); i++) {
  132. if (infos.IsStopped.Value)
  133. return;
  134. if (options != null && options.CancellationToken.IsCancellationRequested) {
  135. state.Stop ();
  136. return;
  137. }
  138. if (infos.LowestBreakIteration != null && infos.LowestBreakIteration > actual)
  139. return;
  140. state.CurrentIteration = actual;
  141. local = action (actual, state, local);
  142. }
  143. }
  144. while (bag.TryTake (out actual)) {
  145. if (infos.IsStopped.Value)
  146. return;
  147. if (options != null && options.CancellationToken.IsCancellationRequested) {
  148. state.Stop ();
  149. return;
  150. }
  151. if (infos.LowestBreakIteration != null && infos.LowestBreakIteration > actual)
  152. continue;
  153. state.CurrentIteration = actual;
  154. local = action (actual, state, local);
  155. }
  156. } finally {
  157. if (destruct != null)
  158. destruct (local);
  159. }
  160. };
  161. if (options != null && options.TaskScheduler != null)
  162. InitTasks (tasks, workerMethod, num, options.TaskScheduler);
  163. else
  164. InitTasks (tasks, workerMethod, num);
  165. try {
  166. Task.WaitAll (tasks);
  167. } catch {
  168. HandleExceptions (tasks, infos);
  169. }
  170. return new ParallelLoopResult (infos.LowestBreakIteration, !(infos.IsStopped.Value || infos.IsExceptional));
  171. */
  172. }
  173. #endregion
  174. #region Foreach
  175. [MonoTODO]
  176. static ParallelLoopResult ForEach<TSource, TLocal> (Func<int, IList<IEnumerator<TSource>>> enumerable, ParallelOptions options,
  177. Func<TLocal> init, Func<TSource, ParallelLoopState, TLocal, TLocal> action,
  178. Action<TLocal> destruct)
  179. {
  180. int num = Math.Min (GetBestWorkerNumber (),
  181. options != null && options.MaxDegreeOfParallelism != -1 ? options.MaxDegreeOfParallelism : int.MaxValue);
  182. Task[] tasks = new Task[num];
  183. ParallelLoopState.ExternalInfos infos = new ParallelLoopState.ExternalInfos ();
  184. throw new NotImplementedException ();
  185. /*
  186. ConcurrentBag<TSource> bag = new ConcurrentBag<TSource> ();
  187. const int bagCount = 5;
  188. IList<IEnumerator<TSource>> slices = enumerable (num);
  189. int sliceIndex = 0;
  190. Action workerMethod = delegate {
  191. IEnumerator<TSource> slice = slices[Interlocked.Increment (ref sliceIndex) - 1];
  192. TLocal local = (init != null) ? init () : default (TLocal);
  193. ParallelLoopState state = new ParallelLoopState (tasks, infos);
  194. try {
  195. bool cont = true;
  196. TSource element;
  197. while (cont) {
  198. if (infos.IsStopped.Value)
  199. return;
  200. if (options != null && options.CancellationToken.IsCancellationRequested) {
  201. state.Stop ();
  202. return;
  203. }
  204. for (int i = 0; i < bagCount && (cont = slice.MoveNext ()); i++) {
  205. bag.Add (slice.Current);
  206. }
  207. for (int i = 0; i < bagCount && bag.TryTake (out element); i++) {
  208. if (infos.IsStopped.Value)
  209. return;
  210. if (options != null && options.CancellationToken.IsCancellationRequested) {
  211. state.Stop ();
  212. return;
  213. }
  214. local = action (element, state, local);
  215. }
  216. }
  217. while (bag.TryTake (out element)) {
  218. if (infos.IsStopped.Value)
  219. return;
  220. if (options != null && options.CancellationToken.IsCancellationRequested) {
  221. state.Stop ();
  222. return;
  223. }
  224. local = action (element, state, local);
  225. }
  226. } finally {
  227. if (destruct != null)
  228. destruct (local);
  229. }
  230. };
  231. if (options != null && options.TaskScheduler != null)
  232. InitTasks (tasks, workerMethod, num, options.TaskScheduler);
  233. else
  234. InitTasks (tasks, workerMethod, num);
  235. try {
  236. Task.WaitAll (tasks);
  237. } catch {
  238. HandleExceptions (tasks, infos);
  239. }
  240. return new ParallelLoopResult (infos.LowestBreakIteration, !(infos.IsStopped.Value || infos.IsExceptional));
  241. */
  242. }
  243. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable, Action<TSource> action)
  244. {
  245. return ForEach<TSource, object> (Partitioner.Create (enumerable), ParallelOptions.Default, null,
  246. (e, s, l) => { action (e); return null; }, null);
  247. }
  248. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable, Action<TSource, ParallelLoopState> action)
  249. {
  250. return ForEach<TSource, object> (Partitioner.Create (enumerable), ParallelOptions.Default, null,
  251. (e, s, l) => { action (e, s); return null; }, null);
  252. }
  253. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable,
  254. Action<TSource, ParallelLoopState, long> action)
  255. {
  256. return ForEach<TSource, object> (Partitioner.Create (enumerable), ParallelOptions.Default, null,
  257. (e, s, l) => { action (e, s, -1); return null; }, null);
  258. }
  259. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source,
  260. Action<TSource, ParallelLoopState> body)
  261. {
  262. return ForEach<TSource, object> (source, ParallelOptions.Default, null, (e, s, l) => { body (e, s); return null; }, null);
  263. }
  264. public static ParallelLoopResult ForEach<TSource> (OrderablePartitioner<TSource> source,
  265. Action<TSource, ParallelLoopState, long> body)
  266. {
  267. return ForEach<TSource, object> (source, ParallelOptions.Default, null, (e, s, i, l) => { body (e, s, i); return null; }, null);
  268. }
  269. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source,
  270. Action<TSource> body)
  271. {
  272. return ForEach<TSource, object> (source, ParallelOptions.Default, null, (e, s, l) => { body (e); return null; }, null);
  273. }
  274. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  275. Action<TSource> body)
  276. {
  277. return ForEach<TSource, object> (Partitioner.Create (source), parallelOptions, null,
  278. (e, s, l) => { body (e); return null; }, null);
  279. }
  280. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  281. Action<TSource, ParallelLoopState> body)
  282. {
  283. return ForEach<TSource, object> (Partitioner.Create (source), parallelOptions, null,
  284. (e, s, l) => { body (e, s); return null; }, null);
  285. }
  286. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  287. Action<TSource, ParallelLoopState, long> body)
  288. {
  289. return ForEach<TSource, object> (Partitioner.Create (source), parallelOptions,
  290. null, (e, s, i, l) => { body (e, s, i); return null; }, null);
  291. }
  292. public static ParallelLoopResult ForEach<TSource> (OrderablePartitioner<TSource> source, ParallelOptions parallelOptions,
  293. Action<TSource, ParallelLoopState, long> body)
  294. {
  295. return ForEach<TSource, object> (source, parallelOptions, null, (e, s, i, l) => { body (e, s, i); return null; }, null);
  296. }
  297. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source, ParallelOptions parallelOptions,
  298. Action<TSource> body)
  299. {
  300. return ForEach<TSource, object> (source, parallelOptions, null, (e, s, l) => {body (e); return null; }, null);
  301. }
  302. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source, ParallelOptions parallelOptions,
  303. Action<TSource, ParallelLoopState> body)
  304. {
  305. return ForEach<TSource, object> (source, parallelOptions, null, (e, s, l) => { body (e, s); return null; }, null);
  306. }
  307. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, Func<TLocal> localInit,
  308. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  309. Action<TLocal> localFinally)
  310. {
  311. return ForEach<TSource, TLocal> ((Partitioner<TSource>)Partitioner.Create (source), null, localInit, body, localFinally);
  312. }
  313. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, Func<TLocal> localInit,
  314. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  315. Action<TLocal> localFinally)
  316. {
  317. return ForEach<TSource, TLocal> (Partitioner.Create (source), null, localInit, body, localFinally);
  318. }
  319. public static ParallelLoopResult ForEach<TSource, TLocal> (OrderablePartitioner<TSource> source, Func<TLocal> localInit,
  320. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  321. Action<TLocal> localFinally)
  322. {
  323. return ForEach<TSource, TLocal> (source, ParallelOptions.Default, localInit, body, localFinally);
  324. }
  325. public static ParallelLoopResult ForEach<TSource, TLocal> (Partitioner<TSource> source, Func<TLocal> localInit,
  326. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  327. Action<TLocal> localFinally)
  328. {
  329. return ForEach<TSource, TLocal> (source, ParallelOptions.Default, localInit, body, localFinally);
  330. }
  331. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  332. Func<TLocal> localInit,
  333. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  334. Action<TLocal> localFinally)
  335. {
  336. return ForEach<TSource, TLocal> (Partitioner.Create (source), parallelOptions, localInit, body, localFinally);
  337. }
  338. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  339. Func<TLocal> localInit,
  340. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  341. Action<TLocal> localFinally)
  342. {
  343. return ForEach<TSource, TLocal> (Partitioner.Create (source), parallelOptions, localInit, body, localFinally);
  344. }
  345. public static ParallelLoopResult ForEach<TSource, TLocal> (Partitioner<TSource> enumerable, ParallelOptions options,
  346. Func<TLocal> init,
  347. Func<TSource, ParallelLoopState, TLocal, TLocal> action,
  348. Action<TLocal> destruct)
  349. {
  350. return ForEach<TSource, TLocal> (enumerable.GetPartitions, options, init, action, destruct);
  351. }
  352. public static ParallelLoopResult ForEach<TSource, TLocal> (OrderablePartitioner<TSource> enumerable, ParallelOptions options,
  353. Func<TLocal> init,
  354. Func<TSource, ParallelLoopState, long, TLocal, TLocal> action,
  355. Action<TLocal> destruct)
  356. {
  357. return ForEach<KeyValuePair<long, TSource>, TLocal> (enumerable.GetOrderablePartitions, options,
  358. init, (e, s, l) => action (e.Value, s, e.Key, l), destruct);
  359. }
  360. #endregion
  361. /* Disabled as this is an API addition
  362. #region While
  363. public static void While (Func<bool> predicate, Action body)
  364. {
  365. if (body == null)
  366. throw new ArgumentNullException ("body");
  367. if (predicate == null)
  368. throw new ArgumentNullException ("predicate");
  369. int num = GetBestWorkerNumber ();
  370. Task[] tasks = new Task [num];
  371. Action action = delegate {
  372. while (predicate ())
  373. body ();
  374. };
  375. InitTasks (tasks, action, num);
  376. Task.WaitAll (tasks);
  377. HandleExceptions (tasks);
  378. }
  379. #endregion
  380. */
  381. #region Invoke
  382. public static void Invoke (params Action[] actions)
  383. {
  384. if (actions == null)
  385. throw new ArgumentNullException ("actions");
  386. Invoke (actions, (Action a) => Task.Factory.StartNew (a));
  387. }
  388. public static void Invoke (ParallelOptions parallelOptions, params Action[] actions)
  389. {
  390. if (parallelOptions == null)
  391. throw new ArgumentNullException ("parallelOptions");
  392. if (actions == null)
  393. throw new ArgumentNullException ("actions");
  394. Invoke (actions, (Action a) => Task.Factory.StartNew (a, TaskCreationOptions.None, parallelOptions.TaskScheduler));
  395. }
  396. static void Invoke (Action[] actions, Func<Action, Task> taskCreator)
  397. {
  398. if (actions.Length == 0)
  399. throw new ArgumentException ("actions is empty");
  400. // Execute it directly
  401. if (actions.Length == 1 && actions[0] != null)
  402. actions[0] ();
  403. bool shouldThrow = false;
  404. Task[] ts = Array.ConvertAll (actions, delegate (Action a) {
  405. if (a == null) {
  406. shouldThrow = true;
  407. return null;
  408. }
  409. return taskCreator (a);
  410. });
  411. if (shouldThrow)
  412. throw new ArgumentException ("One action in actions is null", "actions");
  413. try {
  414. Task.WaitAll (ts);
  415. } catch {
  416. HandleExceptions (ts);
  417. }
  418. }
  419. #endregion
  420. #region SpawnBestNumber, used by PLinq
  421. internal static Task[] SpawnBestNumber (Action action, Action callback)
  422. {
  423. return SpawnBestNumber (action, -1, callback);
  424. }
  425. internal static Task[] SpawnBestNumber (Action action, int dop, Action callback)
  426. {
  427. return SpawnBestNumber (action, dop, false, callback);
  428. }
  429. internal static Task[] SpawnBestNumber (Action action, int dop, bool wait, Action callback)
  430. {
  431. // Get the optimum amount of worker to create
  432. int num = dop == -1 ? (wait ? GetBestWorkerNumber () + 1 : GetBestWorkerNumber ()) : dop;
  433. // Initialize worker
  434. CountdownEvent evt = new CountdownEvent (num);
  435. Task[] tasks = new Task [num];
  436. for (int i = 0; i < num; i++) {
  437. tasks [i] = Task.Factory.StartNew (() => {
  438. action ();
  439. evt.Signal ();
  440. if (callback != null && evt.IsSet)
  441. callback ();
  442. });
  443. }
  444. // If explicitely told, wait for all workers to complete
  445. // and thus let main thread participate in the processing
  446. if (wait)
  447. Task.WaitAll (tasks);
  448. return tasks;
  449. }
  450. #endregion
  451. }
  452. }
  453. #endif