Home | History | Annotate | Download | only in stream_executor
      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 perftools {
     29 namespace gputools {
     30 
     31 // Indicates a set of options for a device's usage, which generally must be
     32 // provided at StreamExecutor device-initialization time.
     33 //
     34 // These are intended to be useful-but-not-mandatorily-supported options for
     35 // using devices on the underlying platform. Presently, if the option requested
     36 // is not available on the target platform, a warning will be emitted.
     37 struct DeviceOptions {
     38  public:
     39   // When it is observed that more memory has to be allocated for thread stacks,
     40   // this flag prevents it from ever being deallocated. Potentially saves
     41   // thrashing the thread stack memory allocation, but at the potential cost of
     42   // some memory space.
     43   static const unsigned kDoNotReclaimStackAllocation = 0x1;
     44 
     45   // The following options refer to synchronization options when
     46   // using SynchronizeStream or SynchronizeContext.
     47 
     48   // Synchronize with spinlocks.
     49   static const unsigned kScheduleSpin = 0x02;
     50   // Synchronize with spinlocks that also call CPU yield instructions.
     51   static const unsigned kScheduleYield = 0x04;
     52   // Synchronize with a "synchronization primitive" (e.g. mutex).
     53   static const unsigned kScheduleBlockingSync = 0x08;
     54 
     55   static const unsigned kMask = 0xf;  // Mask of all available flags.
     56 
     57   // Constructs an or-d together set of device options.
     58   explicit DeviceOptions(unsigned flags) : flags_(flags) {
     59     CHECK((flags & kMask) == flags);
     60   }
     61 
     62   // Factory for the default set of device options.
     63   static DeviceOptions Default() { return DeviceOptions(0); }
     64 
     65   unsigned flags() const { return flags_; }
     66 
     67   bool operator==(const DeviceOptions& other) const {
     68     return flags_ == other.flags_;
     69   }
     70 
     71   bool operator!=(const DeviceOptions& other) const {
     72     return !(*this == other);
     73   }
     74 
     75   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<string, string> non_portable_tags;
     82 
     83  private:
     84   unsigned flags_;
     85 };
     86 
     87 }  // namespace gputools
     88 }  // namespace perftools
     89 
     90 #endif  // TENSORFLOW_STREAM_EXECUTOR_DEVICE_OPTIONS_H_
     91