Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 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 #include "tensorflow/compiler/xla/service/device_memory_allocator.h"
     17 
     18 #include <string>
     19 
     20 #include "tensorflow/compiler/xla/status_macros.h"
     21 #include "tensorflow/compiler/xla/types.h"
     22 #include "tensorflow/compiler/xla/util.h"
     23 
     24 namespace xla {
     25 
     26 StreamExecutorMemoryAllocator::StreamExecutorMemoryAllocator(
     27     const perftools::gputools::Platform* platform,
     28     tensorflow::gtl::ArraySlice<perftools::gputools::StreamExecutor*>
     29         stream_executors)
     30     : DeviceMemoryAllocator(platform),
     31       stream_executors_(stream_executors.begin(), stream_executors.end()) {}
     32 
     33 StatusOr<perftools::gputools::DeviceMemoryBase>
     34 StreamExecutorMemoryAllocator::Allocate(int device_ordinal, uint64 size,
     35                                         bool retry_on_failure) {
     36   TF_ASSIGN_OR_RETURN(perftools::gputools::StreamExecutor * stream_executor,
     37                       GetStreamExecutor(device_ordinal));
     38   perftools::gputools::DeviceMemoryBase result =
     39       stream_executor->AllocateArray<uint8>(size);
     40   if (size > 0 && result == nullptr) {
     41     return ResourceExhausted(
     42         "Failed to allocate request for %s (%lluB) on device ordinal %d",
     43         tensorflow::strings::HumanReadableNumBytes(size).c_str(), size,
     44         device_ordinal);
     45   }
     46   return result;
     47 }
     48 
     49 tensorflow::Status StreamExecutorMemoryAllocator::Deallocate(
     50     int device_ordinal, perftools::gputools::DeviceMemoryBase* mem) {
     51   if (!mem->is_null()) {
     52     TF_ASSIGN_OR_RETURN(perftools::gputools::StreamExecutor * stream_executor,
     53                         GetStreamExecutor(device_ordinal));
     54     // We make a local copy of 'mem' so the original is not zeroed out by the
     55     // Deallocate() call below. This gives us a better chance of
     56     // catching double-free bugs, since Deallocate silently succeeds for null
     57     // values.
     58     perftools::gputools::DeviceMemoryBase mem_copy(*mem);
     59     stream_executor->Deallocate(&mem_copy);
     60   }
     61   return tensorflow::Status::OK();
     62 }
     63 
     64 StatusOr<perftools::gputools::StreamExecutor*>
     65 StreamExecutorMemoryAllocator::GetStreamExecutor(int device_ordinal) {
     66   if (device_ordinal < 0) {
     67     return InvalidArgument("device ordinal value (%d) must be non-negative",
     68                            device_ordinal);
     69   }
     70   if (device_ordinal >= stream_executors_.size()) {
     71     return InvalidArgument(
     72         "device ordinal value (%d) >= number of devices (%zu)", device_ordinal,
     73         stream_executors_.size());
     74   }
     75   if (stream_executors_[device_ordinal] == nullptr) {
     76     return NotFound("Device %s:%d present but not supported",
     77                     platform()->Name().c_str(), device_ordinal);
     78   }
     79   return stream_executors_[device_ordinal];
     80 }
     81 
     82 bool StreamExecutorMemoryAllocator::AllowsAsynchronousDeallocation() const {
     83   return false;
     84 }
     85 
     86 }  // namespace xla
     87