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

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

Issue 1184373003: Add sk_parallel_for() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix Created 5 years, 6 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
11 #include "SkTypes.h" 11 #include "SkTypes.h"
12 #include "SkAtomics.h" 12 #include "SkAtomics.h"
13 #include "SkTemplates.h"
13 14
14 struct SkRunnable; 15 struct SkRunnable;
15 16
16 class SkTaskGroup : SkNoncopyable { 17 class SkTaskGroup : SkNoncopyable {
17 public: 18 public:
18 // Create one of these in main() to enable SkTaskGroups globally. 19 // Create one of these in main() to enable SkTaskGroups globally.
19 struct Enabler : SkNoncopyable { 20 struct Enabler : SkNoncopyable {
20 explicit Enabler(int threads = -1); // Default is system-reported core count. 21 explicit Enabler(int threads = -1); // Default is system-reported core count.
21 ~Enabler(); 22 ~Enabler();
22 }; 23 };
(...skipping 19 matching lines...) Expand all
42 43
43 private: 44 private:
44 typedef void(*void_fn)(void*); 45 typedef void(*void_fn)(void*);
45 46
46 void add (void_fn, void* arg); 47 void add (void_fn, void* arg);
47 void batch(void_fn, void* args, int N, size_t stride); 48 void batch(void_fn, void* args, int N, size_t stride);
48 49
49 SkAtomic<int32_t> fPending; 50 SkAtomic<int32_t> fPending;
50 }; 51 };
51 52
53 // Returns best estimate of number of CPU cores available to use.
54 int sk_num_cores();
55
56 // Call f(i) for i in [0, end).
57 template <typename Func>
58 void sk_parallel_for(int end, const Func& f) {
59 if (end <= 0) { return; }
60
61 struct Chunk {
62 const Func* f;
63 int start, end;
64 };
65
66 // TODO(mtklein): this chunking strategy could probably use some tuning.
67 int max_chunks = sk_num_cores() * 2,
68 stride = (end + max_chunks - 1 ) / max_chunks,
69 nchunks = (end + stride - 1 ) / stride;
70 SkASSERT(nchunks <= max_chunks);
71
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
75 for (int i = 0; i < nchunks; i++) {
76 Chunk& c = chunks[i];
77 c.f = &f;
78 c.start = i * stride;
79 c.end = SkTMin(c.start + stride, end);
80 SkASSERT(c.start < c.end); // Nothing will break if start >= end, but i t's a wasted chunk.
81 }
82
83 void(*run_chunk)(Chunk*) = [](Chunk* c) {
84 for (int i = c->start; i < c->end; i++) {
85 (*c->f)(i);
86 }
87 };
88 SkTaskGroup().batch(run_chunk, chunks.get(), nchunks);
89 }
90
52 #endif//SkTaskGroup_DEFINED 91 #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