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 constexpr 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 constexpr unsigned kScheduleSpin = 0x02;
49   // Synchronize with spinlocks that also call CPU yield instructions.
50   static constexpr unsigned kScheduleYield = 0x04;
51   // Synchronize with a "synchronization primitive" (e.g. mutex).
52   static constexpr unsigned kScheduleBlockingSync = 0x08;
53 
54   static constexpr 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            non_portable_tags == other.non_portable_tags;
69   }
70 
71   bool operator!=(const DeviceOptions& other) const {
72     return !(*this == other);
73   }
74 
ToStringDeviceOptions75   std::string ToString() {
76     return flags_ == 0 ? "none" : "kDoNotReclaimStackAllocation";
77   }
78 
79   // Platform-specific device options. Expressed as key-value pairs to avoid
80   // DeviceOptions subclass proliferation.
81   std::map<std::string, std::string> non_portable_tags;
82 
83  private:
84   unsigned flags_;
85 };
86 
87 }  // namespace stream_executor
88 
89 #endif  // TENSORFLOW_STREAM_EXECUTOR_DEVICE_OPTIONS_H_
90