Home | History | Annotate | Download | only in sycl
      1 /* Copyright 2016 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 #ifdef TENSORFLOW_USE_SYCL
     17 
     18 #include "tensorflow/core/common_runtime/sycl/sycl_allocator.h"
     19 
     20 namespace tensorflow {
     21 
     22 SYCLAllocator::SYCLAllocator(Eigen::QueueInterface* queue)
     23     : sycl_device_(new Eigen::SyclDevice(queue)) {
     24   cl::sycl::queue& sycl_queue = sycl_device_->sycl_queue();
     25   const cl::sycl::device& device = sycl_queue.get_device();
     26   stats_.bytes_limit =
     27       device.get_info<cl::sycl::info::device::max_mem_alloc_size>();
     28 }
     29 
     30 SYCLAllocator::~SYCLAllocator() {
     31   if (sycl_device_) {
     32     delete sycl_device_;
     33   }
     34 }
     35 
     36 string SYCLAllocator::Name() { return "device:SYCL"; }
     37 
     38 void* SYCLAllocator::AllocateRaw(size_t alignment, size_t num_bytes) {
     39   mutex_lock lock(mu_);
     40   assert(sycl_device_);
     41   if (num_bytes == 0) {
     42     // Cannot allocate no bytes in SYCL, so instead allocate a single byte
     43     num_bytes = 1;
     44   }
     45   auto p = sycl_device_->allocate(num_bytes);
     46   const auto& allocated_buffer = sycl_device_->get_sycl_buffer(p);
     47   const std::size_t bytes_allocated = allocated_buffer.get_range().size();
     48 
     49   ++stats_.num_allocs;
     50   stats_.bytes_in_use += bytes_allocated;
     51   stats_.max_bytes_in_use =
     52       std::max<int64>(stats_.max_bytes_in_use, stats_.bytes_in_use);
     53   stats_.max_alloc_size =
     54       std::max<int64>(stats_.max_alloc_size, bytes_allocated);
     55 
     56   return p;
     57 }
     58 
     59 void SYCLAllocator::DeallocateRaw(void* ptr) {
     60   mutex_lock lock(mu_);
     61   if (sycl_device_) {
     62     const auto& buffer_to_delete = sycl_device_->get_sycl_buffer(ptr);
     63     const std::size_t dealloc_size = buffer_to_delete.get_range().size();
     64     stats_.bytes_in_use -= dealloc_size;
     65     sycl_device_->deallocate(ptr);
     66   }
     67 }
     68 
     69 void SYCLAllocator::GetStats(AllocatorStats* stats) {
     70   mutex_lock lock(mu_);
     71   *stats = stats_;
     72 }
     73 
     74 void SYCLAllocator::ClearStats() override {
     75   mutex_lock l(mu_);
     76   stats_.num_allocs = 0;
     77   stats_.max_bytes_in_use = stats_.bytes_in_use;
     78   stats_.max_alloc_size = 0;
     79 }
     80 
     81 size_t SYCLAllocator::RequestedSize(void* ptr) {
     82   mutex_lock lock(mu_);
     83   if (!sycl_device_) {
     84     return 0;
     85   }
     86   const auto& buffer = sycl_device_->get_sycl_buffer(ptr);
     87   return buffer.get_size();
     88 }
     89 
     90 }  // namespace tensorflow
     91 
     92 #endif  // TENSORFLOW_USE_SYCL
     93