• 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/lib/strings/strcat.h"
19 
20 namespace tensorflow {
21 
GetAllowGrowthValue(const GPUOptions & gpu_options)22 bool GPUBFCAllocator::GetAllowGrowthValue(const GPUOptions& gpu_options) {
23   const char* force_allow_growth_string =
24       std::getenv("TF_FORCE_GPU_ALLOW_GROWTH");
25   if (force_allow_growth_string == nullptr) {
26     return gpu_options.allow_growth();
27   }
28 
29   if (strcmp("false", force_allow_growth_string) == 0) {
30     if (gpu_options.allow_growth()) {
31       LOG(WARNING)
32           << "Overriding allow_growth setting because the"
33           << " TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original"
34           << " config value was " << gpu_options.allow_growth() << ".";
35     }
36     return false;
37   } else if (strcmp("true", force_allow_growth_string) == 0) {
38     if (!gpu_options.allow_growth()) {
39       LOG(WARNING)
40           << "Overriding allow_growth setting because the"
41           << " TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original"
42           << " config value was " << gpu_options.allow_growth() << ".";
43     }
44     return true;
45   }
46 
47   LOG(ERROR)
48       << "The TF_FORCE_GPU_ALLOW_GROWTH environment variable is set but could"
49       << " not be parsed: \"" << force_allow_growth_string << "\". Valid"
50       << " values are \"true\" or \"false\". Using original config value"
51       << " of " << gpu_options.allow_growth() << ".";
52   return gpu_options.allow_growth();
53 }
54 
GetGarbageCollectionValue()55 bool GPUBFCAllocator::GetGarbageCollectionValue() {
56   const char* enable_gpu_garbage_collection =
57       std::getenv("TF_ENABLE_GPU_GARBAGE_COLLECTION");
58   if (enable_gpu_garbage_collection == nullptr) {
59     // By default, turn on the memory garbage collection.
60     return true;
61   }
62   if (strcmp("false", enable_gpu_garbage_collection) == 0) {
63     return false;
64   } else if (strcmp("true", enable_gpu_garbage_collection) == 0) {
65     return true;
66   }
67 
68   LOG(ERROR)
69       << "The TF_ENABLE_GPU_GARBAGE_COLLECTION environment variable is set but"
70       << " could not be parsed: \"" << enable_gpu_garbage_collection << "\"."
71       << " Valid values are \"true\" or \"false\"."
72       << " Using the default value \"true\".";
73   return true;
74 }
75 
GPUBFCAllocator(SubAllocator * sub_allocator,size_t total_memory,const string & name)76 GPUBFCAllocator::GPUBFCAllocator(SubAllocator* sub_allocator,
77                                  size_t total_memory, const string& name)
78     : GPUBFCAllocator(sub_allocator, total_memory, GPUOptions(), name) {}
79 
GPUBFCAllocator(SubAllocator * sub_allocator,size_t total_memory,const GPUOptions & gpu_options,const string & name)80 GPUBFCAllocator::GPUBFCAllocator(SubAllocator* sub_allocator,
81                                  size_t total_memory,
82                                  const GPUOptions& gpu_options,
83                                  const string& name)
84     : BFCAllocator(sub_allocator, total_memory,
85                    GPUBFCAllocator::GetAllowGrowthValue(gpu_options), name,
86                    GPUBFCAllocator::GetGarbageCollectionValue()) {}
87 
88 }  // namespace tensorflow
89