Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "core/common/logging/logging.h"
#include "core/framework/device_stream_collection.h"
#include "core/framework/execution_frame.h"
#include "core/framework/ort_value.h"
#include "core/framework/iexecutor.h"
#include "core/framework/stream_handles.h"
#include "core/graph/basic_types.h"
#include "core/common/inlined_containers.h"
#include "core/framework/memory_info.h"
#ifdef ENABLE_TRAINING
#include "core/framework/partial_graph_execution_state.h"
#endif
 
namespace onnxruntime {
class SessionState;
 
class SessionScope;
typedef InlinedHashMap<std::string, OrtValue> OrtValueCache;
typedef std::shared_ptr<OrtValueCache> OrtValueCachePtr;
 
// Execution context that support to execute a command on stream.
// It is composed by following components:
// 1. a execution frame
// 2. a collection of device stream instances that kernels can launch to.
// 3. a set of notification instances needed to perform synchronization in current execution plan.
class StreamExecutionContext {
 public:
  /*
   * LIMITATION:
   * CountDownBarrier is only for scenario that the v is set
   * to the # of consumers and each consumer calls Dec() exactly once.
   */
  class CountDownBarrier {
   public:
    CountDownBarrier() : v_{0} {};
 
    void Set(int32_t v) {
      ORT_ENFORCE(v >= 0);
      v_.store(v, std::memory_order_relaxed);
    }
 
    bool Dec() {
      return v_.fetch_sub(1, std::memory_order_relaxed) == 1;
    }
 
    int32_t Get() {
      return gsl::narrow_cast<int32_t>(v_.load(std::memory_order_relaxed));
    }
 
    void Inc() {
      ++v_;
    }
 
   private:
    std::atomic_int_fast32_t v_;
  };
 
  StreamExecutionContext(const SessionState& sess_state,
                         int32_t num_streams,
#ifdef ORT_ENABLE_STREAM
                         gsl::span<const size_t> notification_owners,
                         size_t num_barriers,
                         const DeviceStreamCollection* device_stream_map,
#endif
                         gsl::span<const int> feed_mlvalue_idxs,
                         gsl::span<const OrtValue> feeds, gsl::span<const int> fetch_mlvalue_idxs,
                         std::vector<OrtValue>& fetches,
                         const std::unordered_map<size_t, IExecutor::CustomAllocator>& fetch_allocators,
                         const logging::Logger& sess_logger,
                         bool single_thread_mode);
 
  const SessionState& GetSessionState() const;
 
  const logging::Logger& GetLogger() const;
 
  ExecutionFrame& GetExecutionFrame();
 
  synchronize::Notification* GetNotification(size_t idx);
 
  void SetLogger(const logging::Logger& current_logger) {
    logger_ = &current_logger;
  }
 
  // Get status of the execution.
  // if one of the stream got non-OK status, the whole task status will be set as that non-OK status.
  const Status& TaskStatus() const;
 
  // Decrease the count of a given barrier.
  bool DecCountDownBarrier(size_t barrier_id);
 
  // The execution mode:
  // 1. single thread mode: all the streams will be launched using current host thread.
  // 2. multi-threads mode: use inter-op thread pool to schedule the N streams.
  bool SingleThreadMode() const { return single_thread_mode_; }
 
  // Get the Stream instance for a given logic sequence.
  // return nullptr if the device of given logic sequence doesn't register stream support.
  Stream* GetDeviceStream(size_t idx);
 
  // Decrease the count of remaining job by 1.
  void CompleteTask();
 
  // Increase the count of remaining job by 1.
  void AddTask();
 
  // This is only used under multi-threads mode.
  // blocked until all the jobs scheduled into inter-op thread pool complete.
  void WaitAll();
 
  // If one of the stream got non-OK status, update the status in the context.
  void SetStatus(Status& status);
 
  // Release the OrtValues after a step, based on the execution plan.
  void RecycleNodeInputs(onnxruntime::NodeIndex node_index);
 
#ifdef ENABLE_TRAINING
  void SetOrtValueCache(OrtValueCachePtr cache) {
    cache_ = std::move(cache);
  }
 
  OrtValueCachePtr& GetOrtValueCache() {
    return cache_;
  }
#endif
 
  ~StreamExecutionContext();
 
#ifdef ENABLE_TRAINING
  const ProgramRegion* GetCurrentRange() {
    return program_range_;
  }
 
  void SetCurrentRange(const ProgramRegion* range) {
    program_range_ = range;
  }
 
  const InlinedHashSet<NodeIndex>* GetNodeToExecute() {
    return node_to_execute_;
  }
 
  void SetNodeToExecute(const InlinedHashSet<NodeIndex>* node_to_execute) {
    node_to_execute_ = node_to_execute;
  }
 
#endif
 
 private:
  const SessionState* session_state_;
 
  ExecutionFrame frame_;
 
  const logging::Logger* logger_;
 
  std::unique_ptr<std::atomic_int[]> release_plan_;
 
  CountDownBarrier remain_tasks_;
 
  Status task_status_{Status::OK()};
 
#ifdef ENABLE_TRAINING
  const ProgramRegion* program_range_{nullptr};
 
  OrtValueCachePtr cache_{nullptr};
 
  // TODO: this is mainly for ort trainer
  // Should we deprecate it?
  const InlinedHashSet<NodeIndex>* node_to_execute_{nullptr};
#endif
  const bool single_thread_mode_;
 
#ifdef ORT_ENABLE_STREAM
  InlinedVector<std::unique_ptr<synchronize::Notification>> notifications_;
  // if it is nullptr, means current session doesn't have any EP using stream feature
  const DeviceStreamCollection* device_stream_map_;
 
  std::vector<CountDownBarrier> count_down_barriers_;
#endif
};
 
using NotificationIndex = size_t;
 
// Execute the stream at index 'stream_idx' with execution context 'ctx', from step 'since'.
void RunSince(size_t stream_idx,
              StreamExecutionContext& ctx,
              SessionScope& session_scope,
              const bool& terminate_flag,
              size_t since);
 
// Schedule the downstream jobs from other streams at 'trigger' step, based on the execution plan.
void ScheduleDownstream(StreamExecutionContext& ctx,
                        size_t trigger,
                        bool single_thread_mode,
                        const bool& terminate_flag,
                        SessionScope& session_scope);
}  // namespace onnxruntime