HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
environment.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <atomic>
7 #include <memory>
8 #include "core/common/common.h"
9 #include "core/common/status.h"
13 
14 struct OrtThreadingOptions;
15 namespace onnxruntime {
16 /** TODO: remove this class
17  Provides the runtime environment for onnxruntime.
18  Create one instance for the duration of execution.
19 */
20 class Environment {
21  public:
22  /**
23  Create and initialize the runtime environment.
24  @param logging manager instance that will enable per session logger output using
25  session_options.session_logid as the logger id in messages.
26  If nullptr, the default LoggingManager MUST have been created previously as it will be used
27  for logging. This will use the default logger id in messages.
28  See core/common/logging/logging.h for details, and how LoggingManager::DefaultLogger works.
29  @param tp_options optional set of parameters controlling the number of intra and inter op threads for the global
30  threadpools.
31  @param create_global_thread_pools determine if this function will create the global threadpools or not.
32  */
33  static Status Create(std::unique_ptr<logging::LoggingManager> logging_manager,
34  std::unique_ptr<Environment>& environment,
35  const OrtThreadingOptions* tp_options = nullptr,
36  bool create_global_thread_pools = false);
37 
39  return logging_manager_.get();
40  }
41 
42  void SetLoggingManager(std::unique_ptr<onnxruntime::logging::LoggingManager> logging_manager) {
43  logging_manager_ = std::move(logging_manager);
44  }
45 
47  return intra_op_thread_pool_.get();
48  }
49 
51  return inter_op_thread_pool_.get();
52  }
53 
55  return create_global_thread_pools_;
56  }
57 
58  /**
59  * Registers an allocator for sharing between multiple sessions.
60  * Return an error if an allocator with the same OrtMemoryInfo is already registered.
61  */
63 
64  /**
65  * Creates and registers an allocator for sharing between multiple sessions.
66  * Return an error if an allocator with the same OrtMemoryInfo is already registered.
67  */
68  Status CreateAndRegisterAllocator(const OrtMemoryInfo& mem_info, const OrtArenaCfg* arena_cfg = nullptr);
69 
70  /**
71  * Returns the list of registered allocators in this env.
72  */
73  const std::vector<AllocatorPtr>& GetRegisteredSharedAllocators() const {
74  return shared_allocators_;
75  }
76 
77  /**
78  * Removes registered allocator that was previously registered for sharing between multiple sessions.
79  */
80  Status UnregisterAllocator(const OrtMemoryInfo& mem_info);
81 
82  Environment() = default;
83 
84  private:
85  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Environment);
86  Status Initialize(std::unique_ptr<logging::LoggingManager> logging_manager,
87  const OrtThreadingOptions* tp_options = nullptr,
88  bool create_global_thread_pools = false);
89 
90  std::unique_ptr<logging::LoggingManager> logging_manager_;
91  std::unique_ptr<onnxruntime::concurrency::ThreadPool> intra_op_thread_pool_;
92  std::unique_ptr<onnxruntime::concurrency::ThreadPool> inter_op_thread_pool_;
93  bool create_global_thread_pools_{false};
94  std::vector<AllocatorPtr> shared_allocators_;
95 };
96 } // namespace onnxruntime
const std::vector< AllocatorPtr > & GetRegisteredSharedAllocators() const
Definition: environment.h:73
Status CreateAndRegisterAllocator(const OrtMemoryInfo &mem_info, const OrtArenaCfg *arena_cfg=nullptr)
void SetLoggingManager(std::unique_ptr< onnxruntime::logging::LoggingManager > logging_manager)
Definition: environment.h:42
The logging manager. Owns the log sink and potentially provides a default Logger instance. Provides filtering based on a minimum LogSeverity level, and of messages with DataType::User if enabled.
Definition: logging.h:87
Status UnregisterAllocator(const OrtMemoryInfo &mem_info)
std::shared_ptr< IAllocator > AllocatorPtr
Definition: allocator.h:190
onnxruntime::concurrency::ThreadPool * GetIntraOpThreadPool() const
Definition: environment.h:46
logging::LoggingManager * GetLoggingManager() const
Definition: environment.h:38
Status RegisterAllocator(AllocatorPtr allocator)
bool EnvCreatedWithGlobalThreadPools() const
Definition: environment.h:54
onnxruntime::concurrency::ThreadPool * GetInterOpThreadPool() const
Definition: environment.h:50
static Status Create(std::unique_ptr< logging::LoggingManager > logging_manager, std::unique_ptr< Environment > &environment, const OrtThreadingOptions *tp_options=nullptr, bool create_global_thread_pools=false)