Skip to content

Commit 5a4fe13

Browse files
committed
Merged PR 1590: Make Env::Default() returns a const ref
This change makes the Env::Default() to return a const reference to the environment. Related work items: #153
1 parent b14b82a commit 5a4fe13

File tree

7 files changed

+45
-40
lines changed

7 files changed

+45
-40
lines changed

lotus/core/lib/threadpool.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ limitations under the License.
2525
namespace Lotus {
2626
namespace thread {
2727

28-
struct EigenEnvironment {
28+
class EigenEnvironment {
29+
public:
2930
typedef Thread EnvThread;
3031

3132
struct TaskImpl {
@@ -38,16 +39,12 @@ struct EigenEnvironment {
3839
std::unique_ptr<TaskImpl> f;
3940
};
4041

41-
Env* const env_;
42-
const ThreadOptions thread_options_;
43-
const std::string name_;
44-
45-
EigenEnvironment(Env* env, const ThreadOptions& thread_options,
42+
EigenEnvironment(const Env& env, const ThreadOptions& thread_options,
4643
const std::string& name)
4744
: env_(env), thread_options_(thread_options), name_(name) {}
4845

4946
EnvThread* CreateThread(std::function<void()> f) {
50-
return env_->StartThread(thread_options_, name_, [=]() {
47+
return env_.StartThread(thread_options_, name_, [=]() {
5148
// TODO
5249
// Set the processor flag to flush denormals to zero.
5350
// port::ScopedFlushDenormal flush;
@@ -85,24 +82,29 @@ struct EigenEnvironment {
8582
t.f->f();
8683
}
8784
}
85+
86+
private:
87+
const Env& env_;
88+
const ThreadOptions thread_options_;
89+
const std::string name_;
8890
};
8991

9092
struct ThreadPool::Impl : Eigen::ThreadPoolTempl<EigenEnvironment> {
91-
Impl(Env* env, const ThreadOptions& thread_options, const std::string& name,
93+
Impl(const Env& env, const ThreadOptions& thread_options, const std::string& name,
9294
int num_threads, bool low_latency_hint)
9395
: Eigen::ThreadPoolTempl<EigenEnvironment>(
9496
num_threads, low_latency_hint,
9597
EigenEnvironment(env, thread_options, name)) {}
9698
};
9799

98-
ThreadPool::ThreadPool(Env* env, const std::string& name, int num_threads)
100+
ThreadPool::ThreadPool(const Env& env, const std::string& name, int num_threads)
99101
: ThreadPool(env, ThreadOptions(), name, num_threads, true) {}
100102

101-
ThreadPool::ThreadPool(Env* env, const ThreadOptions& thread_options,
103+
ThreadPool::ThreadPool(const Env& env, const ThreadOptions& thread_options,
102104
const std::string& name, int num_threads)
103105
: ThreadPool(env, thread_options, name, num_threads, true) {}
104106

105-
ThreadPool::ThreadPool(Env* env, const ThreadOptions& thread_options,
107+
ThreadPool::ThreadPool(const Env& env, const ThreadOptions& thread_options,
106108
const std::string& name, int num_threads,
107109
bool low_latency_hint) {
108110
LOTUS_ENFORCE(num_threads >= 1);

lotus/core/lib/threadpool.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ class ThreadPool {
3636
// operations like I/O the hint should be set to false.
3737
//
3838
// REQUIRES: num_threads > 0
39-
ThreadPool(Env* env, const ThreadOptions& thread_options, const std::string& name,
39+
ThreadPool(const Env& env, const ThreadOptions& thread_options, const std::string& name,
4040
int num_threads, bool low_latency_hint);
4141

4242
// Constructs a pool for low-latency ops that contains "num_threads" threads
4343
// with specified "name". env->StartThread() is used to create individual
4444
// threads.
4545
// REQUIRES: num_threads > 0
46-
ThreadPool(Env* env, const std::string& name, int num_threads);
46+
ThreadPool(const Env& env, const std::string& name, int num_threads);
4747

4848
// Constructs a pool for low-latency ops that contains "num_threads" threads
4949
// with specified "name". env->StartThread() is used to create individual
5050
// threads with the given ThreadOptions.
5151
// REQUIRES: num_threads > 0
52-
ThreadPool(Env* env, const ThreadOptions& thread_options, const std::string& name,
52+
ThreadPool(const Env& env, const ThreadOptions& thread_options, const std::string& name,
5353
int num_threads);
5454

5555
// Waits until all scheduled work has finished and then destroy the

lotus/core/platform/env.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct ThreadOptions;
4040
/// multiple threads without any external synchronization.
4141
class Env {
4242
public:
43-
Env();
4443
virtual ~Env() = default;
4544

4645
/// \brief Returns a default environment suitable for the current operating
@@ -51,18 +50,18 @@ class Env {
5150
///
5251
/// The result of Default() belongs to this library and must never be deleted.
5352
// TODO(Task:153) Lotus::Env::Default() should return const reference
54-
static Env* Default();
53+
static const Env& Default();
5554

56-
virtual int GetNumCpuCores() = 0;
55+
virtual int GetNumCpuCores() const = 0;
5756

5857
/// \brief Returns the number of micro-seconds since the Unix epoch.
59-
virtual uint64 NowMicros() { return env_time_->NowMicros(); }
58+
virtual uint64 NowMicros() const { return env_time_->NowMicros(); }
6059

6160
/// \brief Returns the number of seconds since the Unix epoch.
62-
virtual uint64 NowSeconds() { return env_time_->NowSeconds(); }
61+
virtual uint64 NowSeconds() const { return env_time_->NowSeconds(); }
6362

6463
/// Sleeps/delays the thread for the prescribed number of micro-seconds.
65-
virtual void SleepForMicroseconds(int64 micros) = 0;
64+
virtual void SleepForMicroseconds(int64 micros) const = 0;
6665

6766
/// \brief Returns a new thread that is running fn() and is identified
6867
/// (for debugging/performance-analysis) by "name".
@@ -71,10 +70,13 @@ class Env {
7170
/// (the deletion will block until fn() stops running).
7271
virtual Thread* StartThread(const ThreadOptions& thread_options,
7372
const std::string& name,
74-
std::function<void()> fn) = 0;
73+
std::function<void()> fn) const = 0;
7574

7675
// TODO add filesystem related functions
7776

77+
protected:
78+
Env();
79+
7880
private:
7981
LOTUS_DISALLOW_COPY_ASSIGN_AND_MOVE(Env);
8082
EnvTime* env_time_ = EnvTime::Default();

lotus/core/platform/posix/env.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ class PosixEnv : public Env {
4343
return default_env;
4444
}
4545

46-
virtual int GetNumCpuCores() override {
46+
virtual int GetNumCpuCores() const override {
4747
// TODO if you need the number of physical cores you'll need to parse
4848
// /proc/cpuinfo and grep for "cpu cores".
4949
return std::thread::hardware_concurrency();
5050
}
5151

52-
void SleepForMicroseconds(int64 micros) override {
52+
void SleepForMicroseconds(int64 micros) const override {
5353
while (micros > 0) {
5454
timespec sleep_time;
5555
sleep_time.tv_sec = 0;
@@ -71,7 +71,7 @@ class PosixEnv : public Env {
7171
}
7272

7373
Thread* StartThread(const ThreadOptions& thread_options, const std::string& name,
74-
std::function<void()> fn) override {
74+
std::function<void()> fn) const override {
7575
return new StdThread(thread_options, name, fn);
7676
}
7777

@@ -84,8 +84,8 @@ class PosixEnv : public Env {
8484
#if defined(PLATFORM_POSIX) || defined(__ANDROID__)
8585
// REGISTER_FILE_SYSTEM("", PosixFileSystem);
8686
// REGISTER_FILE_SYSTEM("file", LocalPosixFileSystem);
87-
Env* Env::Default() {
88-
return &PosixEnv::Instance();
87+
const Env& Env::Default() {
88+
return PosixEnv::Instance();
8989
}
9090
#endif
9191

lotus/core/platform/windows/env.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class StdThread : public Thread {
4343

4444
class WindowsEnv : public Env {
4545
public:
46-
void SleepForMicroseconds(int64 micros) override { Sleep(static_cast<DWORD>(micros) / 1000); }
46+
void SleepForMicroseconds(int64 micros) const override { Sleep(static_cast<DWORD>(micros) / 1000); }
4747

4848
Thread* StartThread(const ThreadOptions& thread_options, const std::string& name,
49-
std::function<void()> fn) override {
49+
std::function<void()> fn) const override {
5050
return new StdThread(thread_options, name, fn);
5151
}
5252

53-
virtual int GetNumCpuCores() override {
53+
virtual int GetNumCpuCores() const override {
5454
SYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer[256];
5555
DWORD returnLength = sizeof(buffer);
5656
if (GetLogicalProcessorInformation(buffer, &returnLength) == FALSE) {
@@ -101,8 +101,8 @@ class WindowsEnv : public Env {
101101
} // namespace
102102

103103
#if defined(PLATFORM_WINDOWS)
104-
Env* Env::Default() {
105-
return &WindowsEnv::Instance();
104+
const Env& Env::Default() {
105+
return WindowsEnv::Instance();
106106
}
107107
#endif
108108

lotus/test/onnx/main.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ int main(int argc, char* argv[]) {
6262
std::string provider = kCpuExecutionProvider;
6363
//if this var is not empty, only run the tests with name in this list
6464
std::vector<std::string> whitelisted_test_cases;
65-
int concurrent_session_runs = Env::Default()->GetNumCpuCores();
66-
int p_models = Env::Default()->GetNumCpuCores();
65+
int concurrent_session_runs = Env::Default().GetNumCpuCores();
66+
int p_models = Env::Default().GetNumCpuCores();
6767
{
6868
int ch;
6969
while ((ch = getopt(argc, argv, "c:hj:m:n:p:e:")) != -1) {

lotus/test/onnx/vstest_main.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ static void run(const std::string& provider) {
5959

6060
TEST_CLASS(ONNX_TEST){
6161
public :
62-
TEST_METHOD(test_sequential_planner){
63-
run<Lotus::AllocationPlannerType::SEQUENTIAL_PLANNER>(LotusIR::kCpuExecutionProvider);
64-
}
62+
TEST_METHOD(test_sequential_planner){
63+
run<Lotus::AllocationPlannerType::SEQUENTIAL_PLANNER>(LotusIR::kCpuExecutionProvider);
64+
}
6565

66-
TEST_METHOD(test_simple_sequential_planner) {
67-
run<Lotus::AllocationPlannerType::SIMPLE_SEQUENTIAL_PLANNER>(LotusIR::kCpuExecutionProvider);
68-
}
69-
};
66+
TEST_METHOD(test_simple_sequential_planner) {
67+
run<Lotus::AllocationPlannerType::SIMPLE_SEQUENTIAL_PLANNER>(LotusIR::kCpuExecutionProvider);
68+
}
69+
}
70+
;

0 commit comments

Comments
 (0)