Home | History | Annotate | Download | only in gpu
      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 #ifdef GOOGLE_CUDA
     17 #include "cuda/include/cuda.h"
     18 #include "tensorflow/stream_executor/cuda/cuda_activation.h"
     19 #endif  // GOOGLE_CUDA
     20 
     21 #include "tensorflow/core/common_runtime/gpu/gpu_cudamalloc_allocator.h"
     22 
     23 #include "tensorflow/core/common_runtime/gpu/gpu_id.h"
     24 #include "tensorflow/core/common_runtime/gpu/gpu_id_utils.h"
     25 #include "tensorflow/core/common_runtime/gpu/gpu_init.h"
     26 #include "tensorflow/core/platform/stream_executor.h"
     27 
     28 namespace tensorflow {
     29 
     30 GPUcudaMallocAllocator::GPUcudaMallocAllocator(VisitableAllocator* allocator,
     31                                                CudaGpuId cuda_gpu_id)
     32     : base_allocator_(allocator) {
     33   stream_exec_ = GpuIdUtil::ExecutorForCudaGpuId(cuda_gpu_id).ValueOrDie();
     34 }
     35 
     36 GPUcudaMallocAllocator::~GPUcudaMallocAllocator() { delete base_allocator_; }
     37 
     38 void* GPUcudaMallocAllocator::AllocateRaw(size_t alignment, size_t num_bytes) {
     39 #ifdef GOOGLE_CUDA
     40   // allocate with cudaMalloc
     41   gpu::cuda::ScopedActivateExecutorContext scoped_activation{stream_exec_};
     42   CUdeviceptr rv = 0;
     43   CUresult res = cuMemAlloc(&rv, num_bytes);
     44   if (res != CUDA_SUCCESS) {
     45     LOG(ERROR) << "cuMemAlloc failed to allocate " << num_bytes;
     46     return nullptr;
     47   }
     48   return reinterpret_cast<void*>(rv);
     49 #else
     50   return nullptr;
     51 #endif  // GOOGLE_CUDA
     52 }
     53 void GPUcudaMallocAllocator::DeallocateRaw(void* ptr) {
     54 #ifdef GOOGLE_CUDA
     55   // free with cudaFree
     56   CUresult res = cuMemFree(reinterpret_cast<CUdeviceptr>(ptr));
     57   if (res != CUDA_SUCCESS) {
     58     LOG(ERROR) << "cuMemFree failed to free " << ptr;
     59   }
     60 #endif  // GOOGLE_CUDA
     61 }
     62 
     63 void GPUcudaMallocAllocator::AddAllocVisitor(Visitor visitor) {
     64   return base_allocator_->AddAllocVisitor(visitor);
     65 }
     66 
     67 void GPUcudaMallocAllocator::AddFreeVisitor(Visitor visitor) {
     68   return base_allocator_->AddFreeVisitor(visitor);
     69 }
     70 
     71 bool GPUcudaMallocAllocator::TracksAllocationSizes() { return false; }
     72 
     73 }  // namespace tensorflow
     74