| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('MandelIsolateTest'); | |
| 6 #import('dart:isolate'); | |
| 7 #import('../../../lib/unittest/unittest.dart'); | |
| 8 | |
| 9 final TERMINATION_MESSAGE = -1; | |
| 10 final N = 100; | |
| 11 final ISOLATES = 20; | |
| 12 | |
| 13 main() { | |
| 14 test("Render Mandelbrot in parallel", () { | |
| 15 final state = new MandelbrotState(); | |
| 16 state._validated.future.then(expectAsync1((result) { | |
| 17 expect(result).isTrue(); | |
| 18 })); | |
| 19 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); | |
| 20 }); | |
| 21 } | |
| 22 | |
| 23 | |
| 24 class MandelbrotState { | |
| 25 | |
| 26 MandelbrotState() { | |
| 27 _result = new List<List<int>>(N); | |
| 28 _lineProcessedBy = new List<LineProcessorClient>(N); | |
| 29 _sent = 0; | |
| 30 _missing = N; | |
| 31 _validated = new Completer<bool>(); | |
| 32 } | |
| 33 | |
| 34 void startClient(int id) { | |
| 35 assert(_sent < N); | |
| 36 final client = new LineProcessorClient(this, id); | |
| 37 client.processLine(_sent++); | |
| 38 } | |
| 39 | |
| 40 void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) { | |
| 41 assert(_result[y] === null); | |
| 42 _result[y] = line; | |
| 43 _lineProcessedBy[y] = client; | |
| 44 | |
| 45 if (_sent != N) { | |
| 46 client.processLine(_sent++); | |
| 47 } else { | |
| 48 client.shutdown(); | |
| 49 } | |
| 50 | |
| 51 // If all lines have been computed, validate the result. | |
| 52 if (--_missing == 0) { | |
| 53 _printResult(); | |
| 54 _validateResult(); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 void _validateResult() { | |
| 59 // TODO(ngeoffray): Implement this. | |
| 60 _validated.complete(true); | |
| 61 } | |
| 62 | |
| 63 void _printResult() { | |
| 64 var output = new StringBuffer(); | |
| 65 for (int i = 0; i < _result.length; i++) { | |
| 66 List<int> line = _result[i]; | |
| 67 for (int j = 0; j < line.length; j++) { | |
| 68 if (line[j] < 10) output.add("0"); | |
| 69 output.add(line[j]); | |
| 70 } | |
| 71 output.add("\n"); | |
| 72 } | |
| 73 // print(output); | |
| 74 } | |
| 75 | |
| 76 List<List<int>> _result; | |
| 77 List<LineProcessorClient> _lineProcessedBy; | |
| 78 int _sent; | |
| 79 int _missing; | |
| 80 Completer<bool> _validated; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 class LineProcessorClient { | |
| 85 | |
| 86 LineProcessorClient(MandelbrotState this._state, int this._id) { | |
| 87 _out = new LineProcessor().spawn(); | |
| 88 } | |
| 89 | |
| 90 void processLine(int y) { | |
| 91 _out.then((SendPort p) { | |
| 92 p.call(y).then((List<int> message) { | |
| 93 _state.notifyProcessedLine(this, y, message); | |
| 94 }); | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 void shutdown() { | |
| 99 _out.then((SendPort p) { | |
| 100 p.send(TERMINATION_MESSAGE, null); | |
| 101 }); | |
| 102 } | |
| 103 | |
| 104 MandelbrotState _state; | |
| 105 int _id; | |
| 106 Future<SendPort> _out; | |
| 107 | |
| 108 } | |
| 109 | |
| 110 | |
| 111 class LineProcessor extends Isolate { | |
| 112 | |
| 113 LineProcessor() : super() { } | |
| 114 | |
| 115 void main() { | |
| 116 this.port.receive((message, SendPort replyTo) { | |
| 117 if (message == TERMINATION_MESSAGE) { | |
| 118 assert(replyTo == null); | |
| 119 this.port.close(); | |
| 120 } else { | |
| 121 replyTo.send(_processLine(message), null); | |
| 122 } | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 static List<int> _processLine(int y) { | |
| 127 double inverseN = 2.0 / N; | |
| 128 double Civ = y * inverseN - 1.0; | |
| 129 List<int> result = new List<int>(N); | |
| 130 for (int x = 0; x < N; x++) { | |
| 131 double Crv = x * inverseN - 1.5; | |
| 132 | |
| 133 double Zrv = Crv; | |
| 134 double Ziv = Civ; | |
| 135 | |
| 136 double Trv = Crv * Crv; | |
| 137 double Tiv = Civ * Civ; | |
| 138 | |
| 139 int i = 49; | |
| 140 do { | |
| 141 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; | |
| 142 Zrv = Trv - Tiv + Crv; | |
| 143 | |
| 144 Trv = Zrv * Zrv; | |
| 145 Tiv = Ziv * Ziv; | |
| 146 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); | |
| 147 | |
| 148 result[x] = i; | |
| 149 } | |
| 150 return result; | |
| 151 } | |
| 152 } | |
| OLD | NEW |