annotate Implab/Parallels/ArrayTraits.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents d9d794b61bb9
children 4439140706d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
1 using Implab.Diagnostics;
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
2 using System;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
3 using System.Collections.Generic;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 using System.Diagnostics;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 using System.Linq;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
6 using System.Text;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
7 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
8
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
9 namespace Implab.Parallels {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
10 public static class ArrayTraits {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
11 class ArrayIterator<TSrc> : DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
12 readonly Action<TSrc> m_action;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
13 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
14 readonly Promise<int> m_promise = new Promise<int>();
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
15 readonly TraceContext m_traceContext;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
17 int m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
18 int m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20 public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21 : base(threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23 Debug.Assert(source != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
24 Debug.Assert(action != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
26 m_traceContext = TraceContext.Snapshot();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 m_next = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28 m_source = source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29 m_pending = source.Length;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 m_action = action;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
31
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
32 m_promise.Anyway(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33 m_promise.Cancelled(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35 InitPool();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
37
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 public Promise<int> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
40 return m_promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
41 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
44 protected override void Worker() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 41
diff changeset
45 TraceContext.Fork(m_traceContext);
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
46 base.Worker();
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
47 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
48
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
49 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
50 unit = Interlocked.Increment(ref m_next) - 1;
cin
parents: 15
diff changeset
51 return unit >= m_source.Length ? false : true;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
52 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
53
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
54 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
55 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
56 m_action(m_source[unit]);
16
cin
parents: 15
diff changeset
57 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 m_promise.Resolve(m_source.Length);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 m_promise.Reject(e);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
62 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
63 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
64 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
65
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
66 class ArrayMapper<TSrc, TDst>: DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 readonly Func<TSrc, TDst> m_transform;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
69 readonly TDst[] m_dest;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 readonly Promise<TDst[]> m_promise = new Promise<TDst[]>();
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
71 readonly TraceContext m_traceContext;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73 int m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
74 int m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
75
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
76 public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
77 : base(threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
78
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
79 Debug.Assert (source != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
80 Debug.Assert( transform != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
81
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
82 m_next = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
83 m_source = source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
84 m_dest = new TDst[source.Length];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
85 m_pending = source.Length;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
86 m_transform = transform;
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
87 m_traceContext = TraceContext.Snapshot();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
88
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
89 m_promise.Anyway(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
90 m_promise.Cancelled(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
91
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
92 InitPool();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
93 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
94
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
95 public Promise<TDst[]> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
96 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
97 return m_promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
98 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
99 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
100
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
101 protected override void Worker() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 41
diff changeset
102 TraceContext.Fork(m_traceContext);
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
103 base.Worker();
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
104 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
105
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
106 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
107 unit = Interlocked.Increment(ref m_next) - 1;
cin
parents: 15
diff changeset
108 return unit >= m_source.Length ? false : true;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
109 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
110
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
111 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
112 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
113 m_dest[unit] = m_transform(m_source[unit]);
16
cin
parents: 15
diff changeset
114 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
115 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
116 m_promise.Resolve(m_dest);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
117 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
118 m_promise.Reject(e);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
119 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
120 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
121 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
122
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
123 public static IPromise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
124 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
125 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
126 if (transform == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
127 throw new ArgumentNullException("transform");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
128
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
129 var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
130 return mapper.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
131 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
132
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
133 public static IPromise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
134 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
135 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
136 if (action == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
137 throw new ArgumentNullException("action");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
138
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
139 var iter = new ArrayIterator<TSrc>(source, action, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
140 return iter.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
141 }
16
cin
parents: 15
diff changeset
142
26
f0bf98e4d22c refactoring
cin
parents: 25
diff changeset
143 public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) {
16
cin
parents: 15
diff changeset
144 if (source == null)
cin
parents: 15
diff changeset
145 throw new ArgumentNullException("source");
cin
parents: 15
diff changeset
146 if (transform == null)
cin
parents: 15
diff changeset
147 throw new ArgumentNullException("transform");
cin
parents: 15
diff changeset
148 if (threads <= 0)
cin
parents: 15
diff changeset
149 throw new ArgumentOutOfRangeException("Threads number must be greater then zero");
cin
parents: 15
diff changeset
150
32
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
151 if (source.Length == 0)
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
152 return Promise<TDst[]>.ResultToPromise(new TDst[0]);
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
153
16
cin
parents: 15
diff changeset
154 var promise = new Promise<TDst[]>();
cin
parents: 15
diff changeset
155 var res = new TDst[source.Length];
cin
parents: 15
diff changeset
156 var pending = source.Length;
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
157
16
cin
parents: 15
diff changeset
158 var semaphore = new Semaphore(threads, threads);
cin
parents: 15
diff changeset
159
cin
parents: 15
diff changeset
160 AsyncPool.InvokeNewThread(() => {
cin
parents: 15
diff changeset
161 for (int i = 0; i < source.Length; i++) {
19
e3935fdf59a2 Promise is rewritten to use interlocked operations instead of locks
cin
parents: 16
diff changeset
162 if(promise.IsResolved)
16
cin
parents: 15
diff changeset
163 break; // stop processing in case of error or cancellation
cin
parents: 15
diff changeset
164 var idx = i;
cin
parents: 15
diff changeset
165 semaphore.WaitOne();
cin
parents: 15
diff changeset
166 try {
cin
parents: 15
diff changeset
167 var p1 = transform(source[i]);
cin
parents: 15
diff changeset
168 p1.Anyway(() => semaphore.Release());
cin
parents: 15
diff changeset
169 p1.Cancelled(() => semaphore.Release());
cin
parents: 15
diff changeset
170 p1.Then(
cin
parents: 15
diff changeset
171 x => {
cin
parents: 15
diff changeset
172 res[idx] = x;
cin
parents: 15
diff changeset
173 var left = Interlocked.Decrement(ref pending);
cin
parents: 15
diff changeset
174 if (left == 0)
cin
parents: 15
diff changeset
175 promise.Resolve(res);
cin
parents: 15
diff changeset
176 },
72
d67b95eddaf4 promises refactoring
cin
parents: 48
diff changeset
177 e => {
d67b95eddaf4 promises refactoring
cin
parents: 48
diff changeset
178 promise.Reject(e);
d67b95eddaf4 promises refactoring
cin
parents: 48
diff changeset
179 throw new TransientPromiseException(e);
d67b95eddaf4 promises refactoring
cin
parents: 48
diff changeset
180 }
16
cin
parents: 15
diff changeset
181 );
cin
parents: 15
diff changeset
182
cin
parents: 15
diff changeset
183 } catch (Exception e) {
cin
parents: 15
diff changeset
184 promise.Reject(e);
cin
parents: 15
diff changeset
185 }
cin
parents: 15
diff changeset
186 }
cin
parents: 15
diff changeset
187 return 0;
cin
parents: 15
diff changeset
188 });
cin
parents: 15
diff changeset
189
cin
parents: 15
diff changeset
190 return promise.Anyway(() => semaphore.Dispose());
cin
parents: 15
diff changeset
191 }
24
ee04e1fa78da fixed dispatch pool race condition
cin
parents: 19
diff changeset
192
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
193 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
194 }