ParallelExecuter.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // ParallelExecuter.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0
  27. using System;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.Concurrent;
  33. using System.Linq.Parallel.QueryNodes;
  34. namespace System.Linq.Parallel
  35. {
  36. internal static class ParallelExecuter
  37. {
  38. internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode)
  39. {
  40. return CheckQuery<T> (startingNode, false);
  41. }
  42. internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, bool blocking)
  43. {
  44. return CheckQuery (startingNode, GetBestWorkerNumber (blocking));
  45. }
  46. internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, int partitionCount)
  47. {
  48. QueryCheckerVisitor visitor = new QueryCheckerVisitor (partitionCount);
  49. startingNode.Visit (visitor);
  50. return visitor.Options;
  51. }
  52. internal static CancellationToken Chain (this CancellationToken self, CancellationTokenSource other)
  53. {
  54. CancellationTokenSource linked = CancellationTokenSource.CreateLinkedTokenSource (self, other.Token);
  55. return linked.Token;
  56. }
  57. // TODO: replace the CheckQuery call with a custom visitor that stops after the
  58. // first encountered order guard
  59. internal static bool IsOrdered<TSource> (this QueryBaseNode<TSource> source)
  60. {
  61. return CheckQuery (source).BehindOrderGuard.Value;
  62. }
  63. internal static int GetBestWorkerNumber ()
  64. {
  65. return GetBestWorkerNumber (false);
  66. }
  67. internal static int GetBestWorkerNumber (bool blocking)
  68. {
  69. return blocking ? Environment.ProcessorCount + 1 : Environment.ProcessorCount;
  70. }
  71. internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement> call,
  72. Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
  73. QueryOptions options)
  74. {
  75. return Process<TSource, TElement> (node, call, acquisitionFunc, null, options);
  76. }
  77. internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement> call,
  78. Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
  79. Action endAction,
  80. QueryOptions options)
  81. {
  82. return Process<TSource, TElement> (node,
  83. (e, i) => call (e),
  84. acquisitionFunc,
  85. endAction == null ? ((Action<int>)null) : (i) => endAction (),
  86. options);
  87. }
  88. internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement, int> call,
  89. Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
  90. Action<int> endAction,
  91. QueryOptions options)
  92. {
  93. IList<IEnumerable<TElement>> enumerables = acquisitionFunc (node, options);
  94. Task[] tasks = new Task[enumerables.Count];
  95. for (int i = 0; i < tasks.Length; i++) {
  96. int index = i;
  97. tasks[i] = Task.Factory.StartNew (() => {
  98. foreach (TElement item in enumerables[index]) {
  99. // This is from specific operators
  100. if (options.ImplementerToken.IsCancellationRequested)
  101. break;
  102. if (options.Token.IsCancellationRequested)
  103. throw new OperationCanceledException (options.Token);
  104. call (item, index);
  105. }
  106. if (endAction != null)
  107. endAction (index);
  108. }, options.Token);
  109. }
  110. return tasks;
  111. }
  112. internal static void ProcessAndBlock<T> (QueryBaseNode<T> node, Action<T> call)
  113. {
  114. QueryOptions options = CheckQuery (node, true);
  115. Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
  116. Task.WaitAll (tasks, options.Token);
  117. }
  118. internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<T> call,
  119. Action callback, QueryOptions options)
  120. {
  121. Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
  122. if (callback != null)
  123. Task.Factory.ContinueWhenAll (tasks, (_) => callback ());
  124. return () => Task.WaitAll (tasks, options.Token);
  125. }
  126. internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, int> call,
  127. Action<int> endAction,
  128. Action callback, QueryOptions options)
  129. {
  130. Task[] tasks = Process (node, call, (n, o) => n.GetOrderedEnumerables (o), endAction, options);
  131. if (callback != null)
  132. Task.Factory.ContinueWhenAll (tasks, (_) => callback ());
  133. return () => Task.WaitAll (tasks, options.Token);
  134. }
  135. internal static void ProcessAndAggregate<T, U> (QueryBaseNode<T> node,
  136. Func<U> seedFunc,
  137. Func<U, T, U> localCall,
  138. Action<IList<U>> call)
  139. {
  140. QueryOptions options = CheckQuery (node, true);
  141. IList<IEnumerable<T>> enumerables = node.GetEnumerables (options);
  142. U[] locals = new U[enumerables.Count];
  143. Task[] tasks = new Task[enumerables.Count];
  144. if (seedFunc != null) {
  145. for (int i = 0; i < locals.Length; i++)
  146. locals[i] = seedFunc ();
  147. }
  148. for (int i = 0; i < tasks.Length; i++) {
  149. int index = i;
  150. bool firstRun = true;
  151. tasks[i] = Task.Factory.StartNew (() => {
  152. foreach (T item in enumerables[index]) {
  153. // This is from specific operators
  154. if (options.ImplementerToken.IsCancellationRequested)
  155. break;
  156. if (options.Token.IsCancellationRequested)
  157. throw new OperationCanceledException (options.Token);
  158. if (firstRun && seedFunc == null) {
  159. firstRun = false;
  160. // HACK: TODO: omgwtfitsuckssomuch
  161. locals[index] = (U)(object)item;
  162. continue;
  163. }
  164. U acc = locals[index];
  165. locals[index] = localCall (acc, item);
  166. }
  167. }, options.Token);
  168. }
  169. Task.WaitAll (tasks, options.Token);
  170. if (call != null)
  171. call (locals);
  172. }
  173. }
  174. }
  175. #endif