annotate Implab/Parallels/ArrayTraits.cs @ 98:4c945d94b9ab v2

fix MTQueue syntax error
author cin
date Tue, 04 Nov 2014 09:43:44 +0300
parents 4c0e5ef99986
children 279e226dffdd
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>();
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
15 readonly LogicalOperation m_logicalOperation;
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
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
26 m_logicalOperation = TraceContext.Instance.CurrentOperation;
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
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
32 m_promise.Anyway(Dispose);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34 InitPool();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35 }
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 public Promise<int> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39 return m_promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
40 }
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
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
43 protected override void Worker() {
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
44 TraceContext.Instance.EnterLogicalOperation(m_logicalOperation, false);
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
45 try {
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
46 base.Worker();
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
47 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
48 TraceContext.Instance.Leave();
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
49 }
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
50 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
51
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
52 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
53 unit = Interlocked.Increment(ref m_next) - 1;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
54 return unit < m_source.Length;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
55 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
56
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
57 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 m_action(m_source[unit]);
16
cin
parents: 15
diff changeset
60 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
62 m_promise.Resolve(m_source.Length);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
63 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
64 m_promise.Reject(e);
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
69 class ArrayMapper<TSrc, TDst>: DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 readonly Func<TSrc, TDst> m_transform;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72 readonly TDst[] m_dest;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73 readonly Promise<TDst[]> m_promise = new Promise<TDst[]>();
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
74 readonly LogicalOperation m_logicalOperation;
15
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 int m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
77 int m_next;
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 public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
80 : base(threads) {
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 Debug.Assert (source != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
83 Debug.Assert( transform != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
84
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
85 m_next = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
86 m_source = source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
87 m_dest = new TDst[source.Length];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
88 m_pending = source.Length;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
89 m_transform = transform;
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
90 m_logicalOperation = TraceContext.Instance.CurrentOperation;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
91
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
92 m_promise.Anyway(Dispose);
15
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 InitPool();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
95 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
96
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
97 public Promise<TDst[]> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
98 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
99 return m_promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
100 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
101 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
102
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
103 protected override void Worker() {
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
104 TraceContext.Instance.EnterLogicalOperation(m_logicalOperation,false);
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
105 try {
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
106 base.Worker();
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
107 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
108 TraceContext.Instance.Leave();
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
109 }
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
110 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
111
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
112 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
113 unit = Interlocked.Increment(ref m_next) - 1;
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
114 return unit < m_source.Length;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
115 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
116
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
117 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
118 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
119 m_dest[unit] = m_transform(m_source[unit]);
16
cin
parents: 15
diff changeset
120 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
121 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
122 m_promise.Resolve(m_dest);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
123 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
124 m_promise.Reject(e);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
125 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
126 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
127 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
128
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
129 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
130 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
131 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
132 if (transform == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133 throw new ArgumentNullException("transform");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
134
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
135 var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
136 return mapper.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
137 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
138
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
139 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
140 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
141 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
142 if (action == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
143 throw new ArgumentNullException("action");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
144
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
145 var iter = new ArrayIterator<TSrc>(source, action, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
146 return iter.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
147 }
16
cin
parents: 15
diff changeset
148
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
149 public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ResultMapper<TSrc, IPromise<TDst>> transform, int threads) {
16
cin
parents: 15
diff changeset
150 if (source == null)
cin
parents: 15
diff changeset
151 throw new ArgumentNullException("source");
cin
parents: 15
diff changeset
152 if (transform == null)
cin
parents: 15
diff changeset
153 throw new ArgumentNullException("transform");
cin
parents: 15
diff changeset
154 if (threads <= 0)
cin
parents: 15
diff changeset
155 throw new ArgumentOutOfRangeException("Threads number must be greater then zero");
cin
parents: 15
diff changeset
156
32
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
157 if (source.Length == 0)
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
158 return Promise<TDst[]>.ResultToPromise(new TDst[0]);
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
159
16
cin
parents: 15
diff changeset
160 var promise = new Promise<TDst[]>();
cin
parents: 15
diff changeset
161 var res = new TDst[source.Length];
cin
parents: 15
diff changeset
162 var pending = source.Length;
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
163
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
164 object locker = new object();
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
165 int slots = threads;
16
cin
parents: 15
diff changeset
166
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
167 // Analysis disable AccessToDisposedClosure
16
cin
parents: 15
diff changeset
168 AsyncPool.InvokeNewThread(() => {
cin
parents: 15
diff changeset
169 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
170 if(promise.IsResolved)
16
cin
parents: 15
diff changeset
171 break; // stop processing in case of error or cancellation
cin
parents: 15
diff changeset
172 var idx = i;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
173
89
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
174 if (Interlocked.Decrement(ref slots) < 0) {
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
175 lock(locker) {
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
176 while(slots < 0)
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
177 Monitor.Wait(locker);
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
178 }
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
179 }
89
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
180
16
cin
parents: 15
diff changeset
181 try {
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
182 transform(source[i])
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
183 .Anyway(() => {
89
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
184 Interlocked.Increment(ref slots);
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
185 lock (locker) {
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
186 Monitor.Pulse(locker);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
187 }
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
188 })
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
189 .Last(
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
190 x => {
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
191 res[idx] = x;
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
192 var left = Interlocked.Decrement(ref pending);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
193 if (left == 0)
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
194 promise.Resolve(res);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
195 },
89
ce0171cacec4 improved performance of a chained map operation
cin
parents: 80
diff changeset
196 promise.Reject
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
197 );
16
cin
parents: 15
diff changeset
198
cin
parents: 15
diff changeset
199 } catch (Exception e) {
cin
parents: 15
diff changeset
200 promise.Reject(e);
cin
parents: 15
diff changeset
201 }
cin
parents: 15
diff changeset
202 }
cin
parents: 15
diff changeset
203 return 0;
cin
parents: 15
diff changeset
204 });
cin
parents: 15
diff changeset
205
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
206 return promise;
16
cin
parents: 15
diff changeset
207 }
24
ee04e1fa78da fixed dispatch pool race condition
cin
parents: 19
diff changeset
208
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
209 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
210 }