ParallelExecuter.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, (e, i) => call (e), acquisitionFunc, (i) => endAction (), options);
  79. }
  80. internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement, int> call,
  81. Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
  82. Action<int> endAction,
  83. QueryOptions options)
  84. {
  85. IList<IEnumerable<TElement>> enumerables = acquisitionFunc (node, options);
  86. Task[] tasks = new Task[enumerables.Count];
  87. for (int i = 0; i < tasks.Length; i++) {
  88. int index = i;
  89. tasks[i] = Task.Factory.StartNew (() => {
  90. foreach (TElement item in enumerables[index]) {
  91. // This is from specific operators
  92. if (options.ImplementerToken.IsCancellationRequested)
  93. break;
  94. if (options.Token.IsCancellationRequested)
  95. throw new OperationCanceledException (options.Token);
  96. call (item, index);
  97. }
  98. if (endAction != null)
  99. endAction (index);
  100. }, options.Token);
  101. }
  102. return tasks;
  103. }
  104. internal static void ProcessAndBlock<T> (QueryBaseNode<T> node, Action<T> call)
  105. {
  106. QueryOptions options = CheckQuery (node, true);
  107. Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
  108. Task.WaitAll (tasks, options.Token);
  109. }
  110. internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<T> call,
  111. Action callback, QueryOptions options)
  112. {
  113. Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
  114. if (callback != null)
  115. Task.Factory.ContinueWhenAll (tasks, (_) => callback ());
  116. return () => Task.WaitAll (tasks, options.Token);
  117. }
  118. internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, int> call,
  119. Action callback, QueryOptions options)
  120. {
  121. return ProcessAndCallback<T> (node, call, null, callback, options);
  122. }
  123. internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, int> call,
  124. Action<int> endAction,
  125. Action callback, QueryOptions options)
  126. {
  127. Task[] tasks = Process (node, call, (n, o) => n.GetOrderedEnumerables (o), endAction, options);
  128. if (callback != null)
  129. Task.Factory.ContinueWhenAll (tasks, (_) => callback ());
  130. return () => Task.WaitAll (tasks, options.Token);
  131. }
  132. internal static void ProcessAndAggregate<T, U> (QueryBaseNode<T> node,
  133. Func<U> seedFunc,
  134. Func<U, T, U> localCall,
  135. Action<IList<U>> call)
  136. {
  137. QueryOptions options = CheckQuery (node, true);
  138. IList<IEnumerable<T>> enumerables = node.GetEnumerables (options);
  139. U[] locals = new U[enumerables.Count];
  140. Task[] tasks = new Task[enumerables.Count];
  141. bool init = false;
  142. if (seedFunc != null) {
  143. for (int i = 0; i < locals.Length; i++)
  144. locals[i] = seedFunc ();
  145. init = true;
  146. }
  147. for (int i = 0; i < tasks.Length; i++) {
  148. int index = i;
  149. tasks[i] = Task.Factory.StartNew (() => {
  150. foreach (T item in enumerables[index]) {
  151. // This is from specific operators
  152. if (options.ImplementerToken.IsCancellationRequested)
  153. break;
  154. if (options.Token.IsCancellationRequested)
  155. throw new OperationCanceledException (options.Token);
  156. if (!init) {
  157. init = true;
  158. // HACK: TODO: omfwtfitsomuchsucks
  159. locals[index] = (U)(object)item;
  160. continue;
  161. }
  162. U acc = locals[index];
  163. locals[index] = localCall (acc, item);
  164. }
  165. }, options.Token);
  166. }
  167. Task.WaitAll (tasks, options.Token);
  168. if (call != null)
  169. call (locals);
  170. }
  171. }
  172. }
  173. #endif