OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTaskGroup_DEFINED | 8 #ifndef SkTaskGroup_DEFINED |
9 #define SkTaskGroup_DEFINED | 9 #define SkTaskGroup_DEFINED |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 SkTaskGroup(); | 27 SkTaskGroup(); |
28 ~SkTaskGroup() { this->wait(); } | 28 ~SkTaskGroup() { this->wait(); } |
29 | 29 |
30 // Add a task to this SkTaskGroup. It will likely run on another thread. | 30 // Add a task to this SkTaskGroup. It will likely run on another thread. |
31 // Neither add() method takes owership of any of its parameters. | 31 // Neither add() method takes owership of any of its parameters. |
32 void add(SkRunnable*); | 32 void add(SkRunnable*); |
33 | 33 |
34 void add(std::function<void(void)> fn); | 34 void add(std::function<void(void)> fn); |
35 | 35 |
36 // Add a batch of N tasks, all calling fn with different arguments. | 36 // Add a batch of N tasks, all calling fn with different arguments. |
37 void batch(std::function<void(int)> fn, int N); | 37 void batch(int N, std::function<void(int)> fn); |
38 | 38 |
39 // Block until all Tasks previously add()ed to this SkTaskGroup have run. | 39 // Block until all Tasks previously add()ed to this SkTaskGroup have run. |
40 // You may safely reuse this SkTaskGroup after wait() returns. | 40 // You may safely reuse this SkTaskGroup after wait() returns. |
41 void wait(); | 41 void wait(); |
42 | 42 |
43 private: | 43 private: |
44 SkAtomic<int32_t> fPending; | 44 SkAtomic<int32_t> fPending; |
45 }; | 45 }; |
46 | 46 |
47 // Returns best estimate of number of CPU cores available to use. | 47 // Returns best estimate of number of CPU cores available to use. |
48 int sk_num_cores(); | 48 int sk_num_cores(); |
49 | 49 |
50 int sk_parallel_for_thread_count(); | |
51 | |
52 // Call f(i) for i in [0, end). | |
53 template <typename Func> | |
54 void sk_parallel_for(int end, const Func& f) { | |
55 if (end <= 0) { return; } | |
56 | |
57 struct Chunk { | |
58 const Func* f; | |
59 int start, end; | |
60 }; | |
61 | |
62 // TODO(mtklein): this chunking strategy could probably use some tuning. | |
63 int max_chunks = sk_num_cores() * 2, | |
64 stride = (end + max_chunks - 1 ) / max_chunks, | |
65 nchunks = (end + stride - 1 ) / stride; | |
66 SkASSERT(nchunks <= max_chunks); | |
67 | |
68 #if defined(GOOGLE3) | |
69 // Stack frame size is limited in GOOGLE3. | |
70 SkAutoSTMalloc<512, Chunk> chunks(nchunks); | |
71 #else | |
72 // With the chunking strategy above this won't malloc until we have a machin
e with >512 cores. | |
73 SkAutoSTMalloc<1024, Chunk> chunks(nchunks); | |
74 #endif | |
75 | |
76 for (int i = 0; i < nchunks; i++) { | |
77 Chunk& c = chunks[i]; | |
78 c.f = &f; | |
79 c.start = i * stride; | |
80 c.end = SkTMin(c.start + stride, end); | |
81 SkASSERT(c.start < c.end); // Nothing will break if start >= end, but i
t's a wasted chunk. | |
82 } | |
83 | |
84 Chunk* chunkBase = chunks.get(); | |
85 auto run_chunk = [chunkBase](int i) { | |
86 Chunk& c = chunkBase[i]; | |
87 for (int i = c.start; i < c.end; i++) { | |
88 (*c.f)(i); | |
89 } | |
90 }; | |
91 SkTaskGroup().batch(run_chunk, nchunks); | |
92 } | |
93 | |
94 #endif//SkTaskGroup_DEFINED | 50 #endif//SkTaskGroup_DEFINED |
OLD | NEW |