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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkMultiPictureDraw.cpp ('k') | src/core/SkTaskGroup.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkTaskGroup.h
diff --git a/src/core/SkTaskGroup.h b/src/core/SkTaskGroup.h
index 8c7369da25caa9b124bc3486f74ce701c086095e..3af64d775312f999c495302fd332343ad91c8b01 100644
--- a/src/core/SkTaskGroup.h
+++ b/src/core/SkTaskGroup.h
@@ -10,6 +10,7 @@
#include "SkTypes.h"
#include "SkAtomics.h"
+#include "SkTemplates.h"
struct SkRunnable;
@@ -49,4 +50,42 @@ private:
SkAtomic<int32_t> fPending;
};
+// Returns best estimate of number of CPU cores available to use.
+int sk_num_cores();
+
+// Call f(i) for i in [0, end).
+template <typename Func>
+void sk_parallel_for(int end, const Func& f) {
+ if (end <= 0) { return; }
+
+ struct Chunk {
+ const Func* f;
+ int start, end;
+ };
+
+ // TODO(mtklein): this chunking strategy could probably use some tuning.
+ int max_chunks = sk_num_cores() * 2,
+ stride = (end + max_chunks - 1 ) / max_chunks,
+ nchunks = (end + stride - 1 ) / stride;
+ SkASSERT(nchunks <= max_chunks);
+
+ // With the chunking strategy above this won't malloc until we have a machine with >512 cores.
+ SkAutoSTMalloc<1024, Chunk> chunks(nchunks);
+
+ for (int i = 0; i < nchunks; i++) {
+ Chunk& c = chunks[i];
+ c.f = &f;
+ c.start = i * stride;
+ c.end = SkTMin(c.start + stride, end);
+ SkASSERT(c.start < c.end); // Nothing will break if start >= end, but it's a wasted chunk.
+ }
+
+ void(*run_chunk)(Chunk*) = [](Chunk* c) {
+ for (int i = c->start; i < c->end; i++) {
+ (*c->f)(i);
+ }
+ };
+ SkTaskGroup().batch(run_chunk, chunks.get(), nchunks);
+}
+
#endif//SkTaskGroup_DEFINED
« 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