1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_STREAM_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_STREAM_H_ 18 19 #include "tensorflow/core/tpu/tpu_api.h" 20 #include "tensorflow/stream_executor/stream_executor_internal.h" 21 #include "tensorflow/stream_executor/tpu/c_api_conversions.h" 22 #include "tensorflow/stream_executor/tpu/status_helper.h" 23 #include "tensorflow/stream_executor/tpu/tpu_executor_c_api.h" 24 #include "tensorflow/stream_executor/tpu/tpu_stream_interface.h" 25 26 namespace tensorflow { 27 namespace tpu { 28 29 class TpuStream : public tensorflow::tpu::TpuStreamInterface { 30 public: 31 using Status = stream_executor::port::Status; 32 TpuStream(SE_Stream * stream)33 explicit TpuStream(SE_Stream* stream) : stream_(stream) {} ~TpuStream()34 ~TpuStream() override { 35 tensorflow::tpu::ExecutorApiFn()->TpuStream_FreeFn(stream_); 36 } 37 IsSameSharedMemoryLocation(tensorflow::tpu::TpuStreamInterface * other)38 bool IsSameSharedMemoryLocation( 39 tensorflow::tpu::TpuStreamInterface* other) override { 40 return tensorflow::tpu::ExecutorApiFn() 41 ->TpuStream_IsSameSharedMemoryLocationFn( 42 stream_, static_cast<TpuStream*>(other)->stream_); 43 } 44 EnqueueTransferHostToDevice(stream_executor::DeviceMemoryBase device_dst,const void * host_src,uint64 size)45 Status EnqueueTransferHostToDevice( 46 stream_executor::DeviceMemoryBase device_dst, const void* host_src, 47 uint64 size) { 48 StatusHelper status; 49 tensorflow::tpu::ExecutorApiFn()->TpuStream_EnqueueTransferHostToDeviceFn( 50 stream_, ApiConverter::ToC(device_dst), const_cast<void*>(host_src), 51 size, status.c_status); 52 return status.status(); 53 } 54 EnqueueTransferDeviceToHost(stream_executor::DeviceMemoryBase device_src,void * host_dst,uint64 size)55 Status EnqueueTransferDeviceToHost( 56 stream_executor::DeviceMemoryBase device_src, void* host_dst, 57 uint64 size) { 58 StatusHelper status; 59 tensorflow::tpu::ExecutorApiFn()->TpuStream_EnqueueTransferDeviceToHostFn( 60 stream_, ApiConverter::ToC(device_src), host_dst, size, 61 status.c_status); 62 return status.status(); 63 } 64 EnqueueOnTpuDeviceSendRecvLocal(stream_executor::DeviceMemoryBase send_buffer,stream_executor::DeviceMemoryBase recv_buffer)65 Status EnqueueOnTpuDeviceSendRecvLocal( 66 stream_executor::DeviceMemoryBase send_buffer, 67 stream_executor::DeviceMemoryBase recv_buffer) override { 68 StatusHelper status; 69 tensorflow::tpu::ExecutorApiFn() 70 ->TpuStream_TpuEnqueueOnDeviceSendRecvLocalFn( 71 stream_, ApiConverter::ToC(send_buffer), 72 ApiConverter::ToC(recv_buffer), status.c_status); 73 return status.status(); 74 } 75 se_stream()76 SE_Stream* se_stream() const { return stream_; } 77 78 private: 79 mutable SE_Stream* stream_; 80 }; 81 82 } // namespace tpu 83 } // namespace tensorflow 84 85 #endif // TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_STREAM_H_ 86