Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: src/core/SkTaskGroup.h

Issue 1552093002: If we swap its arguments, SkTaskGroup::batch() _is_ sk_parallel_for. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkMultiPictureDraw.cpp ('k') | src/core/SkTaskGroup.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « src/core/SkMultiPictureDraw.cpp ('k') | src/core/SkTaskGroup.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698