• 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 // Contains device-level options that can be specified at a platform level.
17 // Example usage:
18 //    auto device_options = DeviceOptions::Default();
19 
20 #ifndef TENSORFLOW_STREAM_EXECUTOR_DEVICE_OPTIONS_H_
21 #define TENSORFLOW_STREAM_EXECUTOR_DEVICE_OPTIONS_H_
22 
23 #include <map>
24 
25 #include "tensorflow/stream_executor/platform/port.h"
26 #include "tensorflow/stream_executor/platform/logging.h"
27 
28 namespace stream_executor {
29 
30 // Indicates a set of options for a device's usage, which generally must be
31 // provided at StreamExecutor device-initialization time.
32 //
33 // These are intended to be useful-but-not-mandatorily-supported options for
34 // using devices on the underlying platform. Presently, if the option requested
35 // is not available on the target platform, a warning will be emitted.
36 struct DeviceOptions {
37  public:
38   // When it is observed that more memory has to be allocated for thread stacks,
39   // this flag prevents it from ever being deallocated. Potentially saves
40   // thrashing the thread stack memory allocation, but at the potential cost of
41   // some memory space.
42   static const unsigned kDoNotReclaimStackAllocation = 0x1;
43 
44   // The following options refer to synchronization options when
45   // using SynchronizeStream or SynchronizeContext.
46 
47   // Synchronize with spinlocks.
48   static const unsigned kScheduleSpin = 0x02;
49   // Synchronize with spinlocks that also call CPU yield instructions.
50   static const unsigned kScheduleYield = 0x04;
51   // Synchronize with a "synchronization primitive" (e.g. mutex).
52   static const unsigned kScheduleBlockingSync = 0x08;
53 
54   static const unsigned kMask = 0xf;  // Mask of all available flags.
55 
56   // Constructs an or-d together set of device options.
DeviceOptionsDeviceOptions57   explicit DeviceOptions(unsigned flags) : flags_(flags) {
58     CHECK((flags & kMask) == flags);
59   }
60 
61   // Factory for the default set of device options.
DefaultDeviceOptions62   static DeviceOptions Default() { return DeviceOptions(0); }
63 
flagsDeviceOptions64   unsigned flags() const { return flags_; }
65 
66   bool operator==(const DeviceOptions& other) const {
67     return flags_ == other.flags_;
68   }
69 
70   bool operator!=(const DeviceOptions& other) const {
71     return !(*this == other);
72   }
73 
ToStringDeviceOptions74   string ToString() {
75     return flags_ == 0 ? "none" : "kDoNotReclaimStackAllocation";
76   }
77 
78   // Platform-specific device options. Expressed as key-value pairs to avoid
79   // DeviceOptions subclass proliferation.
80   std::map<string, string> non_portable_tags;
81 
82  private:
83   unsigned flags_;
84 };
85 
86 }  // namespace stream_executor
87 
88 #endif  // TENSORFLOW_STREAM_EXECUTOR_DEVICE_OPTIONS_H_
89