• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #ifndef TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_RNG_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_RNG_H_
18 
19 #include "absl/synchronization/mutex.h"
20 #include "tensorflow/core/platform/thread_annotations.h"
21 #include "tensorflow/stream_executor/gpu/gpu_types.h"
22 #include "tensorflow/stream_executor/platform/port.h"
23 #include "tensorflow/stream_executor/plugin_registry.h"
24 #include "tensorflow/stream_executor/rng.h"
25 
26 namespace stream_executor {
27 
28 class Stream;
29 template <typename ElemT>
30 class DeviceMemory;
31 
32 namespace gpu {
33 
34 // Opaque and unique identifier for the GPU RNG plugin.
35 extern const PluginId kGpuRandPlugin;
36 
37 class GpuExecutor;
38 
39 // GPU-platform implementation of the random number generation support
40 // interface.
41 //
42 // Thread-safe post-initialization.
43 class GpuRng : public rng::RngSupport {
44  public:
45   explicit GpuRng(GpuExecutor* parent);
46 
47   // Retrieves a gpu rng library generator handle. This is necessary for
48   // enqueuing random number generation work onto the device.
49   // TODO(leary) provide a way for users to select the RNG algorithm.
50   bool Init();
51 
52   // Releases a gpu rng library generator handle, if one was acquired.
53   ~GpuRng() override;
54 
55   // See rng::RngSupport for details on the following overrides.
56   bool DoPopulateRandUniform(Stream* stream, DeviceMemory<float>* v) override;
57   bool DoPopulateRandUniform(Stream* stream, DeviceMemory<double>* v) override;
58   bool DoPopulateRandUniform(Stream* stream,
59                              DeviceMemory<std::complex<float>>* v) override;
60   bool DoPopulateRandUniform(Stream* stream,
61                              DeviceMemory<std::complex<double>>* v) override;
62   bool DoPopulateRandGaussian(Stream* stream, float mean, float stddev,
63                               DeviceMemory<float>* v) override;
64   bool DoPopulateRandGaussian(Stream* stream, double mean, double stddev,
65                               DeviceMemory<double>* v) override;
66 
67   bool SetSeed(Stream* stream, const uint8* seed, uint64 seed_bytes) override;
68 
69  private:
70   // Actually performs the work of generating random numbers - the public
71   // methods are thin wrappers to this interface.
72   template <typename T>
73   bool DoPopulateRandUniformInternal(Stream* stream, DeviceMemory<T>* v);
74   template <typename ElemT, typename FuncT>
75   bool DoPopulateRandGaussianInternal(Stream* stream, ElemT mean, ElemT stddev,
76                                       DeviceMemory<ElemT>* v, FuncT func);
77 
78   // Sets the stream for the internal gpu rng generator.
79   //
80   // This is a stateful operation, as the handle can only have one stream set at
81   // a given time, so it is usually performed right before enqueuing work to do
82   // with random number generation.
83   bool SetStream(Stream* stream) TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
84 
85   // Guards the gpu rng library handle for this device.
86   absl::Mutex mu_;
87 
88   // GpuExecutor which instantiated this GpuRng.
89   // Immutable post-initialization.
90   GpuExecutor* parent_;
91 
92   // gpu rng library handle on the device.
93   GpuRngHandle rng_ TF_GUARDED_BY(mu_);
94 
95   SE_DISALLOW_COPY_AND_ASSIGN(GpuRng);
96 };
97 
98 template <typename T>
99 std::string TypeString();
100 
101 template <>
102 std::string TypeString<float>() {
103   return "float";
104 }
105 
106 template <>
107 std::string TypeString<double>() {
108   return "double";
109 }
110 
111 template <>
112 std::string TypeString<std::complex<float>>() {
113   return "std::complex<float>";
114 }
115 
116 template <>
117 std::string TypeString<std::complex<double>>() {
118   return "std::complex<double>";
119 }
120 
121 }  // namespace gpu
122 }  // namespace stream_executor
123 
124 #endif  // TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_RNG_H_
125