ParallelExecuter.cs 7.0 KB

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