|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // Shared utility functions for and macros parallel operations in `Array.js` |
|
6 // and `TypedObject.js`. |
|
7 |
|
8 #ifdef ENABLE_PARALLEL_JS |
|
9 |
|
10 /* The mode asserts options object. */ |
|
11 #define TRY_PARALLEL(MODE) \ |
|
12 ((!MODE || MODE.mode !== "seq")) |
|
13 #define ASSERT_SEQUENTIAL_IS_OK(MODE) \ |
|
14 do { if (MODE) AssertSequentialIsOK(MODE) } while(false) |
|
15 |
|
16 /** |
|
17 * The ParallelSpew intrinsic is only defined in debug mode, so define a dummy |
|
18 * if debug is not on. |
|
19 */ |
|
20 #ifndef DEBUG |
|
21 #define ParallelSpew(args) |
|
22 #endif |
|
23 |
|
24 #define MAX_SLICE_SHIFT 6 |
|
25 #define MAX_SLICE_SIZE 64 |
|
26 #define MAX_SLICES_PER_WORKER 8 |
|
27 |
|
28 /** |
|
29 * Macros to help compute the start and end indices of slices based on id. Use |
|
30 * with the object returned by ComputeSliceInfo. |
|
31 */ |
|
32 #define SLICE_START_INDEX(shift, id) \ |
|
33 (id << shift) |
|
34 #define SLICE_END_INDEX(shift, start, length) \ |
|
35 std_Math_min(start + (1 << shift), length) |
|
36 |
|
37 /** |
|
38 * ForkJoinGetSlice acts as identity when we are not in a parallel section, so |
|
39 * pass in the next sequential value when we are in sequential mode. The |
|
40 * reason for this odd API is because intrinsics *need* to be called during |
|
41 * ForkJoin's warmup to fill the TI info. |
|
42 */ |
|
43 #define GET_SLICE(sliceStart, sliceEnd, id) \ |
|
44 ((id = ForkJoinGetSlice((InParallelSection() ? -1 : sliceStart++) | 0)) < sliceEnd) |
|
45 |
|
46 /** |
|
47 * Determine the number and size of slices. The info object has the following |
|
48 * properties: |
|
49 * |
|
50 * - shift: amount to shift by to compute indices |
|
51 * - count: number of slices |
|
52 */ |
|
53 function ComputeSlicesInfo(length) { |
|
54 var count = length >>> MAX_SLICE_SHIFT; |
|
55 var numWorkers = ForkJoinNumWorkers(); |
|
56 if (count < numWorkers) |
|
57 count = numWorkers; |
|
58 else if (count >= numWorkers * MAX_SLICES_PER_WORKER) |
|
59 count = numWorkers * MAX_SLICES_PER_WORKER; |
|
60 |
|
61 // Round the slice size to be a power of 2. |
|
62 var shift = std_Math_max(std_Math_log2(length / count) | 0, 1); |
|
63 |
|
64 // Recompute count with the rounded size. |
|
65 count = length >>> shift; |
|
66 if (count << shift !== length) |
|
67 count += 1; |
|
68 |
|
69 return { shift: shift, count: count }; |
|
70 } |
|
71 |
|
72 #endif // ENABLE_PARALLEL_JS |