Halide 22.0.0 Halide compiler and libraries

test_sharding.h
Go to the documentation of this file.
1#ifndef TEST_SHARDING_H
2#define TEST_SHARDING_H
3
4// This file may be used by AOT tests, so it deliberately does not
5// include Halide.h
6
7#include <cassert>
8#include <fstream>
9#include <iostream>
10#include <string>
11#include <utility>
12
13 namespace Halide {
14 namespace Internal {
15 namespace Test {
16
17// Support the environment variables are used by the GoogleTest framework
18// to allow a large test to be 'sharded' into smaller pieces:
19//
20// - If TEST_SHARD_STATUS_FILE is not empty, we should create a file at that path
21// to indicate to the test framework that we support sharding. (Note that this
22// must be done even if the test does a [SKIP] and executes no tests.)
23// - If TEST_TOTAL_SHARDS and TEST_SHARD_INDEX are defined, we should
24// split our work into TEST_TOTAL_SHARDS chunks, and only do the TEST_SHARD_INDEX-th
25// chunk on this run.
26//
27// The Halide buildbots don't (yet) make use of these, but some downstream consumers do.
28
29 class Sharder {
30 // returns empty string (not null) if env var not found
31 static std::string get_env(const char *v) {
32 const char *r = getenv(v);
33 if (!r) r = "";
34 return r;
35 }
36
37 // returns 0 if env var not found
38 static int32_t get_env_i32(const char *v) {
39 return std::atoi(get_env(v).c_str()); // 0 if not found
40 }
41
42 const size_t total_shards, shard_index;
43
44 public:
45 // Available publicly in case the test is skipped via [SKIP] --
46 // even if the test runs nothing, we still need to write to this file
47 // (if requested) to avoid making the external test framework unhappy.
48 // (We don't need to call it when actually instantiating a Sharder.)
49 static void accept_sharded_status() {
50 std::string shard_status_file = get_env("TEST_SHARD_STATUS_FILE");
51 if (!shard_status_file.empty()) {
52 std::ofstream f(shard_status_file, std::ios::out | std::ios::binary);
53 f << "sharder\n";
54 f.flush();
55 f.close();
56 }
57 }
58
59 explicit Sharder()
60 : total_shards(get_env_i32("TEST_TOTAL_SHARDS")),
61 shard_index(get_env_i32("TEST_SHARD_INDEX")) {
62
63 accept_sharded_status();
64 if (total_shards != 0) {
65 if (total_shards < 0 || shard_index < 0 || shard_index >= total_shards) {
66 std::cerr << "Illegal values for sharding: total " << total_shards << " current " << shard_index << "\n";
67 exit(1);
68 }
69 }
70 }
71
72 bool should_run(size_t task_index) const {
73 if (total_shards > 0) {
74 return (task_index % total_shards) == shard_index;
75 } else {
76 return true;
77 }
78 }
79
80 bool is_sharded() const {
81 return total_shards > 0;
82 }
83};
84
85} // namespace Test
86} // namespace Internal
87} // namespace Halide
88
89#endif // TEST_SHARDING_H
Halide::Internal::Test::Sharder
Definition: test_sharding.h:29
Halide::Internal::Test::Sharder::accept_sharded_status
static void accept_sharded_status()
Definition: test_sharding.h:49
Halide::Internal::Test::Sharder::Sharder
Sharder()
Definition: test_sharding.h:59
Halide::Internal::Test::Sharder::is_sharded
bool is_sharded() const
Definition: test_sharding.h:80
Halide::Internal::Test::Sharder::should_run
bool should_run(size_t task_index) const
Definition: test_sharding.h:72
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
atoi
int atoi(const char *)
int32_t
signed __INT32_TYPE__ int32_t
Definition: runtime_internal.h:24
getenv
char * getenv(const char *)