annotate Implab/Parallels/ArrayTraits.cs @ 15:0f982f9b7d4d promises

implemented parallel map and foreach for arrays rewritten WorkerPool with MTQueue for more efficiency
author cin
date Thu, 07 Nov 2013 03:41:32 +0400
parents
children 5a4b735ba669
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
1 using System;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
2 using System.Collections.Generic;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
3 using System.Diagnostics;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 using System.Linq;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 using System.Text;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
6 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
7
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
8 namespace Implab.Parallels {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
9 public static class ArrayTraits {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
10 class ArrayIterator<TSrc> : DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
11 readonly Action<TSrc> m_action;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
12 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
13 readonly Promise<int> m_promise = new Promise<int>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
14
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
15 int m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16 int m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
17
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
18 public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19 : base(threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21 Debug.Assert(source != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22 Debug.Assert(action != null);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
24 m_next = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25 m_source = source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 m_pending = source.Length;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 m_action = action;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29 m_promise.Anyway(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 m_promise.Cancelled(() => Dispose());
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 InitPool();
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35 public Promise<int> Promise {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
37 return m_promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39 }
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 protected override bool TryDequeue(out int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 int index;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43 unit = -1;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
44 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
45 index = m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46 if (index >= m_source.Length)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
47 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48 } while (index != Interlocked.CompareExchange(ref m_next, index + 1, index));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
49
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
50 unit = index;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
51 return true;
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]);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
57 int pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 pending = m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60 } while (pending != Interlocked.CompareExchange(ref m_pending, pending - 1, pending));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 pending--;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
62 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
63 m_promise.Resolve(m_source.Length);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
64 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
65 m_promise.Reject(e);
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 class ArrayMapper<TSrc, TDst>: DispatchPool<int> {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71 readonly Func<TSrc, TDst> m_transform;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72 readonly TSrc[] m_source;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73 readonly TDst[] m_dest;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
74 readonly Promise<TDst[]> m_promise = new Promise<TDst[]>();
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;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
90
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
91 m_promise.Anyway(() => Dispose());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
92 m_promise.Cancelled(() => Dispose());
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
103 protected override bool TryDequeue(out int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
104 int index;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
105 unit = -1;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
106 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
107 index = m_next;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
108 if (index >= m_source.Length)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
109 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
110 } while (index != Interlocked.CompareExchange(ref m_next, index + 1, index));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
111
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
112 unit = index;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
113 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
114 }
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 protected override void InvokeUnit(int unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
117 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
118 m_dest[unit] = m_transform(m_source[unit]);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
119 int pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
120 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
121 pending = m_pending;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
122 } while ( pending != Interlocked.CompareExchange(ref m_pending,pending -1, pending));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
123 pending --;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
124 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
125 m_promise.Resolve(m_dest);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
126 } catch (Exception e) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
127 m_promise.Reject(e);
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
130 }
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 public static Promise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
134 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
135 if (transform == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
136 throw new ArgumentNullException("transform");
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 var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
139 return mapper.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
140 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
141
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
142 public static Promise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
143 if (source == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
144 throw new ArgumentNullException("source");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
145 if (action == null)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
146 throw new ArgumentNullException("action");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
147
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
148 var iter = new ArrayIterator<TSrc>(source, action, threads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
149 return iter.Promise;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
150 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
151 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
152 }