ParallelExecuter.cs 7.6 KB

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