@@ -19,6 +19,7 @@ limitations under the License.
1919#define TENSORFLOW_C_EXPERIMENTAL_STREAM_EXECUTOR_STREAM_EXECUTOR_INTERNAL_H_
2020
2121#include " tensorflow/c/experimental/stream_executor/stream_executor.h"
22+ #include " tensorflow/c/tf_status_helper.h"
2223#include " tensorflow/stream_executor/executor_cache.h"
2324#include " tensorflow/stream_executor/lib/status.h"
2425#include " tensorflow/stream_executor/platform.h"
@@ -37,6 +38,13 @@ port::Status InitStreamExecutorPlugin(void* dso_handle);
3738// testing).
3839port::Status InitStreamExecutorPlugin (SEInitPluginFn init_fn);
3940
41+ struct TFStatusDeleter {
42+ void operator ()(TF_Status* s) const { TF_DeleteStatus (s); }
43+ };
44+
45+ // This file implements core stream executor base classes in terms of
46+ // the C API defined in stream_executor.h. A class "CSomething" represents a
47+ // "Something" that can be manipulated via calls in the C interface.
4048class CPlatform : public Platform {
4149 public:
4250 explicit CPlatform (SP_Platform platform,
@@ -83,5 +91,111 @@ class CPlatform : public Platform {
8391 stream_executor::ExecutorCache executor_cache_;
8492};
8593
94+ class CStream : public internal ::StreamInterface {
95+ public:
96+ CStream (SP_Device* device, SP_StreamExecutor* stream_executor)
97+ : device_(device),
98+ stream_executor_ (stream_executor),
99+ stream_handle_(nullptr ) {}
100+ ~CStream () override { Destroy (); }
101+
102+ port::Status Create () {
103+ std::unique_ptr<TF_Status, TFStatusDeleter> c_status (TF_NewStatus ());
104+ stream_executor_->create_stream (device_, &stream_handle_, c_status.get ());
105+ port::Status s = tensorflow::StatusFromTF_Status (c_status.get ());
106+ return s;
107+ }
108+
109+ void Destroy () {
110+ if (stream_handle_ != nullptr ) {
111+ stream_executor_->destroy_stream (device_, stream_handle_);
112+ stream_handle_ = nullptr ;
113+ }
114+ }
115+
116+ SP_Stream Handle () { return stream_handle_; }
117+
118+ private:
119+ SP_Device* device_;
120+ SP_StreamExecutor* stream_executor_;
121+ SP_Stream stream_handle_;
122+ };
123+
124+ class CEvent : public internal ::EventInterface {
125+ public:
126+ CEvent (SP_Device* device, SP_StreamExecutor* stream_executor)
127+ : device_(device),
128+ stream_executor_ (stream_executor),
129+ event_handle_(nullptr ) {}
130+ ~CEvent () override { Destroy (); }
131+
132+ port::Status Create () {
133+ std::unique_ptr<TF_Status, TFStatusDeleter> c_status (TF_NewStatus ());
134+ stream_executor_->create_event (device_, &event_handle_, c_status.get ());
135+ return tensorflow::StatusFromTF_Status (c_status.get ());
136+ }
137+
138+ port::Status Record (SP_Stream stream_handle) {
139+ std::unique_ptr<TF_Status, TFStatusDeleter> c_status (TF_NewStatus ());
140+ stream_executor_->record_event (device_, stream_handle, event_handle_,
141+ c_status.get ());
142+ return tensorflow::StatusFromTF_Status (c_status.get ());
143+ }
144+
145+ void Destroy () {
146+ if (event_handle_ != nullptr ) {
147+ stream_executor_->destroy_event (device_, event_handle_);
148+ event_handle_ = nullptr ;
149+ }
150+ }
151+
152+ SP_Event Handle () { return event_handle_; }
153+
154+ private:
155+ SP_Device* device_;
156+ SP_StreamExecutor* stream_executor_;
157+ SP_Event event_handle_;
158+ };
159+
160+ class CTimer : public internal ::TimerInterface {
161+ public:
162+ CTimer (SP_Device* device, SP_StreamExecutor* stream_executor,
163+ SP_TimerFns* timer_fns)
164+ : device_(device),
165+ stream_executor_ (stream_executor),
166+ timer_handle_(nullptr ),
167+ timer_fns_(timer_fns) {}
168+ ~CTimer () override { Destroy (); }
169+
170+ port::Status Create () {
171+ std::unique_ptr<TF_Status, TFStatusDeleter> c_status (TF_NewStatus ());
172+ stream_executor_->create_timer (device_, &timer_handle_, c_status.get ());
173+ return tensorflow::StatusFromTF_Status (c_status.get ());
174+ }
175+
176+ void Destroy () {
177+ if (timer_handle_ != nullptr ) {
178+ stream_executor_->destroy_timer (device_, timer_handle_);
179+ timer_handle_ = nullptr ;
180+ }
181+ }
182+
183+ SP_Timer Handle () { return timer_handle_; }
184+
185+ uint64 Microseconds () const override {
186+ return timer_fns_->nanoseconds (timer_handle_) / 1000 ;
187+ }
188+
189+ uint64 Nanoseconds () const override {
190+ return timer_fns_->nanoseconds (timer_handle_);
191+ }
192+
193+ private:
194+ SP_Device* device_;
195+ SP_StreamExecutor* stream_executor_;
196+ SP_Timer timer_handle_;
197+ SP_TimerFns* timer_fns_;
198+ };
199+
86200} // namespace stream_executor
87201#endif // TENSORFLOW_C_EXPERIMENTAL_STREAM_EXECUTOR_STREAM_EXECUTOR_INTERNAL_H_
0 commit comments