| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library MandelIsolateTest; | 5 library MandelIsolateTest; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import 'dart:math'; | 8 import 'dart:math'; |
| 9 import '../../pkg/unittest/lib/unittest.dart'; | 9 import '../../pkg/unittest/lib/unittest.dart'; |
| 10 | 10 |
| 11 const TERMINATION_MESSAGE = -1; | 11 const TERMINATION_MESSAGE = -1; |
| 12 const N = 100; | 12 const N = 100; |
| 13 const ISOLATES = 20; | 13 const ISOLATES = 20; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 test("Render Mandelbrot in parallel", () { | 16 test("Render Mandelbrot in parallel", () { |
| 17 final state = new MandelbrotState(); | 17 final state = new MandelbrotState(); |
| 18 state._validated.future.then(expectAsync1((result) { | 18 state._validated.future.then(expectAsync1((result) { |
| 19 expect(result, isTrue); | 19 expect(result, isTrue); |
| 20 })); | 20 })); |
| 21 for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i); | 21 for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i); |
| 22 }); | 22 }); |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 class MandelbrotState { | 26 class MandelbrotState { |
| 27 | 27 |
| 28 MandelbrotState() { | 28 MandelbrotState() { |
| 29 _result = new List<List<int>>(N); | 29 _result = new List<List<int>>(N); |
| 30 _lineProcessedBy = new List<LineProcessorClient>.fixedLength(N); | 30 _lineProcessedBy = new List<LineProcessorClient>(N); |
| 31 _sent = 0; | 31 _sent = 0; |
| 32 _missing = N; | 32 _missing = N; |
| 33 _validated = new Completer<bool>(); | 33 _validated = new Completer<bool>(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void startClient(int id) { | 36 void startClient(int id) { |
| 37 assert(_sent < N); | 37 assert(_sent < N); |
| 38 final client = new LineProcessorClient(this, id); | 38 final client = new LineProcessorClient(this, id); |
| 39 client.processLine(_sent++); | 39 client.processLine(_sent++); |
| 40 } | 40 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 100 } |
| 101 | 101 |
| 102 MandelbrotState _state; | 102 MandelbrotState _state; |
| 103 int _id; | 103 int _id; |
| 104 SendPort _port; | 104 SendPort _port; |
| 105 } | 105 } |
| 106 | 106 |
| 107 List<int> processLine(int y) { | 107 List<int> processLine(int y) { |
| 108 double inverseN = 2.0 / N; | 108 double inverseN = 2.0 / N; |
| 109 double Civ = y * inverseN - 1.0; | 109 double Civ = y * inverseN - 1.0; |
| 110 List<int> result = new List<int>.fixedLength(N); | 110 List<int> result = new List<int>(N); |
| 111 for (int x = 0; x < N; x++) { | 111 for (int x = 0; x < N; x++) { |
| 112 double Crv = x * inverseN - 1.5; | 112 double Crv = x * inverseN - 1.5; |
| 113 | 113 |
| 114 double Zrv = Crv; | 114 double Zrv = Crv; |
| 115 double Ziv = Civ; | 115 double Ziv = Civ; |
| 116 | 116 |
| 117 double Trv = Crv * Crv; | 117 double Trv = Crv * Crv; |
| 118 double Tiv = Civ * Civ; | 118 double Tiv = Civ * Civ; |
| 119 | 119 |
| 120 int i = 49; | 120 int i = 49; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 134 void processLines() { | 134 void processLines() { |
| 135 port.receive((message, SendPort replyTo) { | 135 port.receive((message, SendPort replyTo) { |
| 136 if (message == TERMINATION_MESSAGE) { | 136 if (message == TERMINATION_MESSAGE) { |
| 137 assert(replyTo == null); | 137 assert(replyTo == null); |
| 138 port.close(); | 138 port.close(); |
| 139 } else { | 139 } else { |
| 140 replyTo.send(processLine(message), null); | 140 replyTo.send(processLine(message), null); |
| 141 } | 141 } |
| 142 }); | 142 }); |
| 143 } | 143 } |
| OLD | NEW |