Normalize line endings.
[mono.git] / mcs / class / System.Core / System.Linq / ParallelExecuter.cs
1 #if NET_4_0
2 //
3 // ParallelExecuter.cs
4 //
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
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
28 using System;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Concurrent;
34
35 namespace System.Linq
36 {
37         // TODO: Refactory each of the Process method into one big entity
38         // Check CancellationToken.Canceled parameter in the Task's action body too
39         internal static class ParallelExecuter
40         {
41                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode)
42                 {
43                         return CheckQuery<T> (startingNode, false);
44                 }
45
46                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, bool blocking)
47                 {
48                         return CheckQuery (startingNode, GetBestWorkerNumber (blocking));
49                 }
50
51                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, int partitionCount)
52                 {
53                         QueryCheckerVisitor visitor = new QueryCheckerVisitor (partitionCount);
54                         startingNode.Visit (visitor);
55
56                         return visitor.Options;
57                 }
58
59                 // QueryOptions.ImplementerToken = QueryOptions.ImplementerToken.Chain (myOperatorSource);
60                 internal static CancellationToken Chain (this CancellationToken self, CancellationTokenSource other)
61                 {
62                         CancellationTokenSource linked = CancellationTokenSource.CreateLinkedTokenSource (self, other.Token);
63                         return linked.Token;
64                 }
65
66                 internal static int GetBestWorkerNumber ()
67                 {
68                         return GetBestWorkerNumber (false);
69                 }
70
71                 internal static int GetBestWorkerNumber (bool blocking)
72                 {
73                         return blocking ? Environment.ProcessorCount + 1 : Environment.ProcessorCount;
74                 }
75
76                 internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement> call,
77                                                                    Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
78                                                                    QueryOptions options)
79                 {
80                         return Process<TSource, TElement> (node, call, acquisitionFunc, null, options);
81                 }
82                 
83                 internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement> call,
84                                                                    Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
85                                                                    Action endAction,
86                                                                    QueryOptions options)
87                 {
88                         return Process<TSource, TElement> (node,
89                                                            (e, i) => call (e),
90                                                            acquisitionFunc,
91                                                            endAction == null ? ((Action<int>)null) : (i) => endAction (),
92                                                            options);
93                 }
94
95                 internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node, Action<TElement, int> call,
96                                                                    Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
97                                                                    Action<int> endAction,
98                                                                    QueryOptions options)
99                 {
100                         IList<IEnumerable<TElement>> enumerables = acquisitionFunc (node, options);
101
102                         Task[] tasks = new Task[enumerables.Count];
103                         
104                         for (int i = 0; i < tasks.Length; i++) {
105                                 int index = i;
106                                 tasks[i] = Task.Factory.StartNew (() => {
107                                         foreach (TElement item in enumerables[index]) {
108                                                 // This is from specific operators
109                                                 if (options.ImplementerToken.IsCancellationRequested)
110                                                         break;
111                                                 if (options.Token.IsCancellationRequested)
112                                                         throw new OperationCanceledException (options.Token);
113
114                                                 call (item, index);
115                                         }
116                                         if (endAction != null)
117                                                 endAction (index);
118                                   }, options.Token);
119                         }
120
121                         return tasks;
122                 }
123
124                 internal static void ProcessAndBlock<T> (QueryBaseNode<T> node, Action<T> call)
125                 {
126                         QueryOptions options = CheckQuery (node, true);
127
128                         Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
129                         Task.WaitAll (tasks, options.Token);
130                 }
131
132                 internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<T> call,
133                                                               Action callback, QueryOptions options)
134                 {
135                         Task[] tasks = Process (node, call, (n, o) => n.GetEnumerables (o), options);
136                         if (callback != null)
137                                 Task.Factory.ContinueWhenAll (tasks,  (_) => callback ());
138
139                         return () => Task.WaitAll (tasks, options.Token);
140                 }
141
142                 internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, int> call,
143                                                               Action callback, QueryOptions options)
144                 {
145                         return ProcessAndCallback<T> (node, call, null, callback, options);
146                 }
147
148                 internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, int> call,
149                                                               Action<int> endAction,
150                                                               Action callback, QueryOptions options)
151                 {
152                         Task[] tasks = Process (node, call, (n, o) => n.GetOrderedEnumerables (o), endAction, options);
153                         if (callback != null)
154                                 Task.Factory.ContinueWhenAll (tasks,  (_) => callback ());
155
156                         return () => Task.WaitAll (tasks, options.Token);
157                 }
158
159                 internal static void ProcessAndAggregate<T, U> (QueryBaseNode<T> node,
160                                                     Func<U> seedFunc,
161                                                     Func<U, T, U> localCall,
162                                                     Action<IList<U>> call)
163                 {
164                         QueryOptions options = CheckQuery (node, true);
165
166                         IList<IEnumerable<T>> enumerables = node.GetEnumerables (options);
167                         U[] locals = new U[enumerables.Count];
168                         Task[] tasks = new Task[enumerables.Count];
169
170                         bool init = false;
171                         if (seedFunc != null) {
172                                 for (int i = 0; i < locals.Length; i++)
173                                         locals[i] = seedFunc ();
174                                 init = true;
175                         }
176
177                         for (int i = 0; i < tasks.Length; i++) {
178                                 int index = i;
179                                 tasks[i] = Task.Factory.StartNew (() => {
180                                         foreach (T item in enumerables[index]) {
181                                                 // This is from specific operators
182                                                 if (options.ImplementerToken.IsCancellationRequested)
183                                                         break;
184                                                 if (options.Token.IsCancellationRequested)
185                                                         throw new OperationCanceledException (options.Token);
186
187                                                 if (!init) {
188                                                         init = true;
189                                                         // HACK: TODO: omfwtfitsomuchsucks
190                                                         locals[index] = (U)(object)item;
191                                                         continue;
192                                                 }
193                                                 
194                                                 U acc = locals[index];
195                                                 locals[index] = localCall (acc, item);
196                                         }
197                                 }, options.Token);
198                         }
199
200                         Task.WaitAll (tasks, options.Token);
201
202                         if (call != null)
203                                 call (locals);
204                 }
205         }
206 }
207 #endif