annotate Implab/Parallels/DispatchPool.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.Linq;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 using System.Text;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
6 using System.Diagnostics;
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 abstract class DispatchPool<TUnit> : IDisposable {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
10 readonly int m_minThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
11 readonly int m_maxThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
12 int m_runningThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
13 int m_maxRunningThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
14 int m_suspended = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
15 int m_exitRequired = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16 AutoResetEvent m_hasTasks = new AutoResetEvent(false);
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 protected DispatchPool(int min, int max) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19 if (min < 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20 throw new ArgumentOutOfRangeException("min");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21 if (max <= 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22 throw new ArgumentOutOfRangeException("max");
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 if (min > max)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25 min = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 m_minThreads = min;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 m_maxThreads = max;
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 protected DispatchPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
31 : this(threads, threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
32 }
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 protected DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35 int maxThreads, maxCP;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
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 m_minThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39 m_maxThreads = maxThreads;
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 protected void InitPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43 for (int i = 0; i < m_minThreads; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
44 StartWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
45 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
47 public int ThreadCount {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
49 return m_runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
50 }
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 public int MaxRunningThreads {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
54 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
55 return m_maxRunningThreads;
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 protected bool IsDisposed {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 return m_exitRequired != 0;
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 bool StartWorker() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
66 var current = m_runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 // use spins to allocate slot for the new thread
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
69 if (current >= m_maxThreads || m_exitRequired != 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 // no more slots left or the pool has been disposed
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current + 1, current));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
74 m_maxRunningThreads = Math.Max(m_maxRunningThreads, current + 1);
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 // slot successfully allocated
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 var worker = new Thread(this.Worker);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
79 worker.IsBackground = true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
80 worker.Start();
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 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
83 }
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 protected abstract bool TryDequeue(out TUnit unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
86
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
87 protected virtual void WakeNewWorker() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
88 if (m_suspended > 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
89 m_hasTasks.Set();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
90 else
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
91 StartWorker();
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
94 bool FetchTask(out TUnit unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
95 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
96 // exit if requested
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
97 if (m_exitRequired != 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
98 // release the thread slot
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
99 int running;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
100 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
101 running = m_runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
102 } while (running != Interlocked.CompareExchange(ref m_runningThreads, running - 1, running));
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
103 running--;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
104
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
105 if (running == 0) // it was the last worker
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
106 m_hasTasks.Dispose();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
107 else
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
108 m_hasTasks.Set(); // release next worker
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
109 unit = default(TUnit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
110 return false;
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
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
113 // fetch task
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
114 if (TryDequeue(out unit)) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
115 WakeNewWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
116 return true;
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 //no tasks left, exit if the thread is no longer needed
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
120 int runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
121 bool exit = true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
122 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
123 runningThreads = m_runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
124 if (runningThreads <= m_minThreads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
125 exit = false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
126 break;
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 } while (runningThreads != Interlocked.CompareExchange(ref m_runningThreads, runningThreads - 1, runningThreads));
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 if (exit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
131 Interlocked.Decrement(ref m_runningThreads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
132 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133 }
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 // keep this thread and wait
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
136 Interlocked.Increment(ref m_suspended);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
137 m_hasTasks.WaitOne();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
138 Interlocked.Decrement(ref m_suspended);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
139 } while (true);
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 protected abstract void InvokeUnit(TUnit unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
143
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
144 void Worker() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
145 TUnit unit;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
146 while (FetchTask(out unit))
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
147 InvokeUnit(unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
148 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
149
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
150 protected virtual void Dispose(bool disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
151 if (disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
152 if (m_exitRequired == 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
153 if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
154 return;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
155
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
156 // wake sleeping threads
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
157 m_hasTasks.Set();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
158 GC.SuppressFinalize(this);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
159 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
160 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
161 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
162
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
163 public void Dispose() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
164 Dispose(true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
165 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
166
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
167 ~DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
168 Dispose(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
169 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
170 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
171 }