-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
333 lines (292 loc) · 8.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using CoCoL;
using System.Drawing;
using CoCoL.Network;
using System.Collections.Generic;
using System.Linq;
using System.Drawing.Imaging;
namespace Mandelbrot
{
/// <summary>
/// The overall task being sent
/// </summary>
struct RenderTask
{
/// <summary>
/// The top offset
/// </summary>
public int top;
/// <summary>
/// The left offset
/// </summary>
public int left;
/// <summary>
/// The result width of the image
/// </summary>
public int width;
/// <summary>
/// The results height of the image
/// </summary>
public int height;
/// <summary>
/// The number of iterations pr. pixel
/// </summary>
public int iterations;
/// <summary>
/// Initializes a new instance of the <see cref="Mandelbrot.RenderTask"/> struct.
/// </summary>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="iterations">The number of iterations</param>
public RenderTask(int width, int height, int iterations)
{
this.top = height / -2;
this.left = width / -2;
this.width = width;
this.height = height;
this.iterations = iterations;
}
}
/// <summary>
/// The pixel input and output
/// </summary>
struct Pixel
{
/// <summary>
/// The pixel x coordinate
/// </summary>
public int x;
/// <summary>
/// The pixel y coordinate
/// </summary>
public int y;
/// <summary>
/// The iteration count
/// </summary>
public int value;
}
/// <summary>
/// The farmer process is responsible for spawning the workers
/// </summary>
class Farmer : IProcess
{
public const string FARMER_CHANNEL = "farmer";
public async void Run()
{
//Set up channels
var farmer_channel = ChannelManager.GetChannel<RenderTask>(Farmer.FARMER_CHANNEL).AsRead();
var worker_channel = ChannelManager.GetChannel<Pixel>(Worker.WORKER_INPUT_CHANNEL).AsWrite();
var harvester_channel = ChannelManager.GetChannel<RenderTask>(Harvester.HARVESTER_CHANNEL).AsWrite();
try
{
while (true)
{
// Grab a job
var task = await farmer_channel.ReadAsync();
// Prepare and wait for the harvester
await harvester_channel.WriteAsync(task);
// Emit the individual pixels
for (var x = 0; x < task.width; x++)
for (var y = 0; y < task.height; y++)
await worker_channel.WriteAsync(new Pixel() { x = task.left + x, y = task.top + y, value = task.iterations });
}
}
catch (RetiredException)
{
farmer_channel.Retire();
worker_channel.Retire();
harvester_channel.Retire();
}
}
}
/// <summary>
/// The harvester process is responsible for collecting pixel results and rendering the image
/// </summary>
class Harvester : IProcess
{
public const string HARVESTER_CHANNEL = "harvester";
public const string SHUTDOWN_CHANNEL = "shutdown";
public async void Run()
{
// Set up channels
var worker_channel = ChannelManager.GetChannel<Pixel>(Worker.WORKER_OUTPUT_CHANNEL).AsRead();
var harvester_channel = ChannelManager.GetChannel<RenderTask>(Harvester.HARVESTER_CHANNEL).AsRead();
var shutdown_channel = ChannelManager.GetChannel<bool>(Harvester.SHUTDOWN_CHANNEL).AsWrite();
var imagesSupported = OperatingSystem.IsWindowsVersionAtLeast(6, 1);
try
{
while (true)
{
// Wait for a new task
var task = await harvester_channel.ReadAsync();
var starttime = DateTime.Now;
// Set up an image buffer
var pixels = task.width * task.height;
using (var img = imagesSupported && !Config.DisableImages ? new Bitmap(task.width, task.height) : null)
{
// Collect all pixels
for (var i = 0; i < pixels; i++)
{
var px = await worker_channel.ReadAsync();
if (imagesSupported && img != null)
img.SetPixel(px.x - task.left, px.y - task.top, ColorMap(px.value, task.iterations));
}
if (imagesSupported && img != null)
img.Save(string.Format("{0}-{1}x{2}-{3}.png", DateTime.Now.Ticks, task.width, task.height, task.iterations), ImageFormat.Png);
Console.WriteLine("Rendered a {0}x{1}:{2} image in {3}", task.width, task.height, task.iterations, DateTime.Now - starttime);
}
}
}
catch (RetiredException)
{
worker_channel.Retire();
harvester_channel.Retire();
shutdown_channel.Retire();
}
}
public static Color ColorMap(int value, int max)
{
var v = Math.Max(0, Math.Min(255, (int)(255.0 / max * value)));
return Color.FromArgb(255, v, v, v);
}
}
/// <summary>
/// The worker processes are responsible for rendering each pixel
/// </summary>
[Process(WORKER_COUNT)]
class Worker : IProcess
{
public const string WORKER_INPUT_CHANNEL = "worker_input";
public const string WORKER_OUTPUT_CHANNEL = "worker_output";
private const int WORKER_COUNT = 32;
public async void Run()
{
// Set up the channels
var input_channel = ChannelManager.GetChannel<Pixel>(Worker.WORKER_INPUT_CHANNEL).AsRead();
var output_channel = ChannelManager.GetChannel<Pixel>(Worker.WORKER_OUTPUT_CHANNEL).AsWrite();
try
{
while (true)
{
// Grab work
var px = await input_channel.ReadAsync();
// Compute the value
var n = Compute(px.x / 100.0, px.y / 100.0, px.value);
if (n == px.value)
n = 0;
// Write the result
await output_channel.WriteAsync(new Pixel() { x = px.x, y = px.y, value = n });
}
}
catch (RetiredException)
{
input_channel.Retire();
output_channel.Retire();
}
}
private const double RADIUS_SQUARED = 4.0;
private static int Compute(double a, double b, int maxIterations)
{
int n = 0;
var x = a;
var y = b;
var xSquared = x * x;
var ySquared = y * y;
while ((n < maxIterations) && ((xSquared + ySquared) < RADIUS_SQUARED))
{
double tmp = (xSquared - ySquared) + a;
y = ((2 * x) * y) + b;
x = tmp;
xSquared = x * x;
ySquared = y * y;
n++;
}
return n;
}
}
public class Config
{
/// <summary>
/// The width of the image
/// </summary>
[CommandlineOption("The width of the image", "width", "w", "500")]
public static int Width = -1;
/// <summary>
/// The height of the image
/// </summary>
[CommandlineOption("The height of the image", "height", "h")]
public static int Height = 500;
/// <summary>
/// The number of iterations on each pixel
/// </summary>
[CommandlineOption("The number of iterations on each pixel", "iterations", "i")]
public static int Iterations = 100;
/// <summary>
/// The number of iterations on each pixel
/// </summary>
[CommandlineOption("The number of repeated runs", "repeats", "r")]
public static int Repeats = 1;
/// <summary>
/// Disables all image operations
/// </summary>
[CommandlineOption("Disable writing images, as the drawing only works on Windows", longname: "noimages")]
public static bool DisableImages = false;
/// <summary>
/// Parses the commandline args
/// </summary>
/// <param name="args">The commandline arguments.</param>
public static bool Parse(List<string> args)
{
return SettingsHelper.Parse<Config>(args, null);
}
/// <summary>
/// Returns the config object as a human readable string
/// </summary>
/// <returns>The string.</returns>
public static string AsString()
{
return string.Join(", ", typeof(Config).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Select(x => string.Format("{0}={1}", x.Name, x.GetValue(null))));
}
}
/// <summary>
/// The main class provides the driver for starting jobs
/// </summary>
public class MainClass
{
public static void Main(string[] _args)
{
var args = new List<string>(_args);
// Send jobs into the network
if (!Config.Parse(args))
return;
Console.WriteLine("Config is: {0}", Config.AsString());
var farmer_channel = ChannelManager.GetChannel<RenderTask>(Farmer.FARMER_CHANNEL).AsWrite();
var shutdown_channel = ChannelManager.GetChannel<bool>(Harvester.SHUTDOWN_CHANNEL).AsRead();
// Auto-start all defined processes in this assembly
CoCoL.Loader.StartFromAssembly(typeof(MainClass).Assembly);
if (Config.Width != -1)
{
for (var i = 0; i < Config.Repeats; i++)
farmer_channel.Write(new RenderTask(Config.Width, Config.Height, Config.Iterations));
}
else
{
// Send jobs into the network
farmer_channel.Write(new RenderTask(500, 500, 10));
farmer_channel.Write(new RenderTask(500, 500, 100));
farmer_channel.Write(new RenderTask(500, 500, 256));
farmer_channel.Write(new RenderTask(500, 500, 1000));
}
// Signal completion
farmer_channel.Retire();
try
{
// Block main thread until all jobs complete
shutdown_channel.Read();
}
catch (RetiredException)
{
}
}
}
}