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