annotate Implab/Parallels/ArrayTraits.cs @ 80:4f20870d0816 v2

added memory barriers
author cin
date Fri, 26 Sep 2014 03:32:34 +0400
parents c761fc982e1d
children ce0171cacec4
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
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() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 41
diff changeset
44 TraceContext.Fork(m_traceContext);
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
45 base.Worker();
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
46 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
47
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
49 unit = Interlocked.Increment(ref m_next) - 1;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
50 return unit < m_source.Length;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
51 }
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 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
54 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
55 m_action(m_source[unit]);
16
cin
parents: 15
diff changeset
56 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
57 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58 m_promise.Resolve(m_source.Length);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60 m_promise.Reject(e);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 }
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 class ArrayMapper<TSrc, TDst>: DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
66 readonly Func<TSrc, TDst> m_transform;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68 readonly TDst[] m_dest;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
69 readonly Promise<TDst[]> m_promise = new Promise<TDst[]>();
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
70 readonly TraceContext m_traceContext;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72 int m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73 int m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
74
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
75 public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
76 : base(threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
77
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
78 Debug.Assert (source != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
79 Debug.Assert( transform != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
80
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
81 m_next = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
82 m_source = source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
83 m_dest = new TDst[source.Length];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
84 m_pending = source.Length;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
85 m_transform = transform;
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
86 m_traceContext = TraceContext.Snapshot();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
87
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
88 m_promise.Anyway(Dispose);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
89
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
90 InitPool();
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
93 public Promise<TDst[]> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
94 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
95 return m_promise;
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
98
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
99 protected override void Worker() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 41
diff changeset
100 TraceContext.Fork(m_traceContext);
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
101 base.Worker();
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
102 }
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 32
diff changeset
103
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
104 protected override bool TryDequeue(out int unit) {
16
cin
parents: 15
diff changeset
105 unit = Interlocked.Increment(ref m_next) - 1;
cin
parents: 15
diff changeset
106 return unit >= m_source.Length ? false : true;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
107 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
108
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
109 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
110 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
111 m_dest[unit] = m_transform(m_source[unit]);
16
cin
parents: 15
diff changeset
112 var pending = Interlocked.Decrement(ref m_pending);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
113 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
114 m_promise.Resolve(m_dest);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
115 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
116 m_promise.Reject(e);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
117 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
118 }
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
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
121 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
122 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
123 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
124 if (transform == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
125 throw new ArgumentNullException("transform");
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 var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
128 return mapper.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
129 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
130
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
131 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
132 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
134 if (action == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
135 throw new ArgumentNullException("action");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
136
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
137 var iter = new ArrayIterator<TSrc>(source, action, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
138 return iter.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
139 }
16
cin
parents: 15
diff changeset
140
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
141 public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ResultMapper<TSrc, IPromise<TDst>> transform, int threads) {
16
cin
parents: 15
diff changeset
142 if (source == null)
cin
parents: 15
diff changeset
143 throw new ArgumentNullException("source");
cin
parents: 15
diff changeset
144 if (transform == null)
cin
parents: 15
diff changeset
145 throw new ArgumentNullException("transform");
cin
parents: 15
diff changeset
146 if (threads <= 0)
cin
parents: 15
diff changeset
147 throw new ArgumentOutOfRangeException("Threads number must be greater then zero");
cin
parents: 15
diff changeset
148
32
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
149 if (source.Length == 0)
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
150 return Promise<TDst[]>.ResultToPromise(new TDst[0]);
8eca2652d2ff fixed: StackOverflow in IPromiseBase.Then(handler)
cin
parents: 30
diff changeset
151
16
cin
parents: 15
diff changeset
152 var promise = new Promise<TDst[]>();
cin
parents: 15
diff changeset
153 var res = new TDst[source.Length];
cin
parents: 15
diff changeset
154 var pending = source.Length;
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 26
diff changeset
155
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
156 object locker = new object();
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
157 int slots = threads;
16
cin
parents: 15
diff changeset
158
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
159 // Analysis disable AccessToDisposedClosure
16
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;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
165
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
166 lock(locker) {
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
167 while(slots == 0)
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
168 Monitor.Wait(locker);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
169 slots--;
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
170 }
16
cin
parents: 15
diff changeset
171 try {
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
172 transform(source[i])
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
173 .Anyway(() => {
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
174 lock(locker) {
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
175 slots ++;
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
176 Monitor.Pulse(locker);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
177 }
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
178 })
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
179 .Last(
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
180 x => {
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
181 res[idx] = x;
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
182 var left = Interlocked.Decrement(ref pending);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
183 if (left == 0)
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
184 promise.Resolve(res);
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
185 },
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
186 e => promise.Reject(e)
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
187 );
16
cin
parents: 15
diff changeset
188
cin
parents: 15
diff changeset
189 } catch (Exception e) {
cin
parents: 15
diff changeset
190 promise.Reject(e);
cin
parents: 15
diff changeset
191 }
cin
parents: 15
diff changeset
192 }
cin
parents: 15
diff changeset
193 return 0;
cin
parents: 15
diff changeset
194 });
cin
parents: 15
diff changeset
195
80
4f20870d0816 added memory barriers
cin
parents: 76
diff changeset
196 return promise;
16
cin
parents: 15
diff changeset
197 }
24
ee04e1fa78da fixed dispatch pool race condition
cin
parents: 19
diff changeset
198
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
199 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
200 }