• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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/core/common_runtime/gpu/gpu_bfc_allocator.h"
17 
18 #include "tensorflow/core/common_runtime/gpu/gpu_id.h"
19 #include "tensorflow/core/common_runtime/gpu/gpu_id_utils.h"
20 #include "tensorflow/core/common_runtime/gpu/gpu_init.h"
21 #include "tensorflow/core/lib/strings/strcat.h"
22 
23 namespace tensorflow {
24 
GetAllowGrowthValue(const GPUOptions & gpu_options)25 bool GPUBFCAllocator::GetAllowGrowthValue(const GPUOptions& gpu_options) {
26   const char* force_allow_growth_string =
27       std::getenv("TF_FORCE_GPU_ALLOW_GROWTH");
28   if (force_allow_growth_string == nullptr) {
29     return gpu_options.allow_growth();
30   }
31 
32   if (strcmp("false", force_allow_growth_string) == 0) {
33     if (gpu_options.allow_growth()) {
34       LOG(WARNING)
35           << "Overriding allow_growth setting because the"
36           << " TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original"
37           << " config value was " << gpu_options.allow_growth() << ".";
38     }
39     return false;
40   } else if (strcmp("true", force_allow_growth_string) == 0) {
41     if (!gpu_options.allow_growth()) {
42       LOG(WARNING)
43           << "Overriding allow_growth setting because the"
44           << " TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original"
45           << " config value was " << gpu_options.allow_growth() << ".";
46     }
47     return true;
48   }
49 
50   LOG(ERROR)
51       << "The TF_FORCE_GPU_ALLOW_GROWTH environment variable is set but could"
52       << " not be parsed: \"" << force_allow_growth_string << "\". Valid"
53       << " values are \"true\" or \"false\". Using original config value"
54       << " of " << gpu_options.allow_growth() << ".";
55   return gpu_options.allow_growth();
56 }
57 
GPUBFCAllocator(GPUMemAllocator * sub_allocator,size_t total_memory,const string & name)58 GPUBFCAllocator::GPUBFCAllocator(GPUMemAllocator* sub_allocator,
59                                  size_t total_memory, const string& name)
60     : GPUBFCAllocator(sub_allocator, total_memory, GPUOptions(), name) {}
61 
GPUBFCAllocator(GPUMemAllocator * sub_allocator,size_t total_memory,const GPUOptions & gpu_options,const string & name)62 GPUBFCAllocator::GPUBFCAllocator(GPUMemAllocator* sub_allocator,
63                                  size_t total_memory,
64                                  const GPUOptions& gpu_options,
65                                  const string& name)
66     : BFCAllocator(sub_allocator, total_memory,
67                    GPUBFCAllocator::GetAllowGrowthValue(gpu_options), name) {}
68 
69 }  // namespace tensorflow
70