1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#if !TENSORFLOW_USE_SYCL 17#error This file must only be included when building TensorFlow with SYCL support 18#endif 19 20#ifndef TENSORFLOW_COMMON_RUNTIME_SYCL_SYCL_ALLOCATOR_H_ 21#define TENSORFLOW_COMMON_RUNTIME_SYCL_SYCL_ALLOCATOR_H_ 22 23#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 24#include "tensorflow/core/framework/allocator.h" 25#include "tensorflow/core/platform/mutex.h" 26#include "tensorflow/core/platform/types.h" 27 28namespace tensorflow { 29 30class SYCLAllocator : public Allocator { 31 public: 32 SYCLAllocator(Eigen::QueueInterface* queue); 33 virtual ~SYCLAllocator() override; 34 string Name() override; 35 void* AllocateRaw(size_t alignment, size_t num_bytes) override; 36 void DeallocateRaw(void* ptr) override; 37 38 virtual bool ShouldAllocateEmptyTensors() override final { return true; } 39 void Synchronize() { 40 mutex_lock lock(mu_); 41 if (sycl_device_) { 42 sycl_device_->synchronize(); 43 } 44 } 45 bool Ok() { return sycl_device_ && sycl_device_->ok(); } 46 void GetStats(AllocatorStats* stats) override; 47 void ClearStats() override; 48 49 // The SYCL buffers keep track of their size, so we already have tracking. 50 bool TracksAllocationSizes() override { return true; } 51 // Get the size of the corresponding SYCL buffer. 52 // Implementing this also provides an implementation of 53 // AllocatedSize(void* ptr) by default. 54 size_t RequestedSize(void* ptr) override; 55 Eigen::SyclDevice* getSyclDevice() { return sycl_device_; } 56 // Clear the SYCL device used by the Allocator 57 void ClearSYCLDevice() { 58 mutex_lock lock(mu_); 59 if (sycl_device_) { 60 delete sycl_device_; 61 sycl_device_ = nullptr; 62 } 63 } 64 65 private: 66 mutable mutex mu_; 67 Eigen::SyclDevice* sycl_device_ GUARDED_BY(mu_); // owned 68 AllocatorStats stats_ GUARDED_BY(mu_); 69 70 TF_DISALLOW_COPY_AND_ASSIGN(SYCLAllocator); 71}; 72 73} // namespace tensorflow 74 75#endif // TENSORFLOW_COMMON_RUNTIME_SYCL_SYCL_ALLOCATOR_H_ 76