Skip to content

Commit 895abbb

Browse files
committed
Make parallel_for iterate backwards in debug builds
Catches users who assume it goes forwards in order (like my attempt to use parallel_for in reductions, whoops). ghstack-source-id: 4fa2978 ghstack-comment-id: 2707052203 Pull Request resolved: #9044
1 parent 2889483 commit 895abbb

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

runtime/kernel/targets.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def define_common_targets():
5656
exported_headers = ["thread_parallel_interface.h"],
5757
exported_deps = [
5858
"//executorch/runtime/core:core",
59+
"//executorch/runtime/core/portable_type/c10/c10:c10",
5960
"//executorch/runtime/platform:platform",
6061
],
6162
visibility = [

runtime/kernel/thread_parallel_interface.h

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstdint>
1212
#include <functional>
1313

14+
#include <c10/util/irange.h>
1415
#include <executorch/runtime/core/error.h>
1516
#include <executorch/runtime/platform/assert.h>
1617

@@ -29,7 +30,17 @@ inline bool parallel_for_no_threadpool(
2930
begin,
3031
end);
3132
ET_CHECK_OR_RETURN_FALSE(grain_size > 0, "grain_size = %" PRId64, grain_size);
33+
#ifndef NDEBUG
34+
// Go backwards through the range elementwise to catch code that
35+
// assumes parallel_for is in order like a regular for loop.
36+
for (const auto i : c10::irange(begin, end)) {
37+
const auto offset = i - begin;
38+
const auto idx = end - offset - 1;
39+
f(idx, idx + 1);
40+
}
41+
#else // NDEBUG
3242
f(begin, end);
43+
#endif
3344
return true;
3445
}
3546

0 commit comments

Comments
 (0)