1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#ifndef TENSORFLOW_STREAM_EXECUTOR_RNG_H_ 17#define TENSORFLOW_STREAM_EXECUTOR_RNG_H_ 18 19#include <limits.h> 20#include <complex> 21 22#include "tensorflow/stream_executor/platform/logging.h" 23#include "tensorflow/stream_executor/platform/port.h" 24 25namespace perftools { 26namespace gputools { 27 28class Stream; 29template <typename ElemT> 30class DeviceMemory; 31 32namespace rng { 33 34// Random-number-generation support interface -- this can be derived from a GPU 35// executor when the underlying platform has an RNG library implementation 36// available. See StreamExecutor::AsRng(). 37// When a seed is not specified, the backing RNG will be initialized with the 38// default seed for that implementation. 39// 40// Thread-hostile: see StreamExecutor class comment for details on 41// thread-hostility. 42class RngSupport { 43 public: 44 static const int kMinSeedBytes = 16; 45 static const int kMaxSeedBytes = INT_MAX; 46 47 // Releases any random-number-generation resources associated with this 48 // support object in the underlying platform implementation. 49 virtual ~RngSupport() {} 50 51 // Populates a GPU memory allocation with random values appropriate for the 52 // DeviceMemory element type; i.e. populates DeviceMemory<float> with random 53 // float values. 54 virtual bool DoPopulateRandUniform(Stream *stream, 55 DeviceMemory<float> *v) = 0; 56 virtual bool DoPopulateRandUniform(Stream *stream, 57 DeviceMemory<double> *v) = 0; 58 virtual bool DoPopulateRandUniform(Stream *stream, 59 DeviceMemory<std::complex<float>> *v) = 0; 60 virtual bool DoPopulateRandUniform(Stream *stream, 61 DeviceMemory<std::complex<double>> *v) = 0; 62 63 // Populates a GPU memory allocation with random values sampled from a 64 // Gaussian distribution with the given mean and standard deviation. 65 virtual bool DoPopulateRandGaussian(Stream *stream, float mean, float stddev, 66 DeviceMemory<float> *v) { 67 LOG(ERROR) 68 << "platform's random number generator does not support gaussian"; 69 return false; 70 } 71 virtual bool DoPopulateRandGaussian(Stream *stream, double mean, 72 double stddev, DeviceMemory<double> *v) { 73 LOG(ERROR) 74 << "platform's random number generator does not support gaussian"; 75 return false; 76 } 77 78 // Specifies the seed used to initialize the RNG. 79 // This call does not transfer ownership of the buffer seed; its data should 80 // not be altered for the lifetime of this call. At least 16 bytes of seed 81 // data must be provided, but not all seed data will necessarily be used. 82 // seed: Pointer to seed data. Must not be null. 83 // seed_bytes: Size of seed buffer in bytes. Must be >= 16. 84 virtual bool SetSeed(Stream *stream, const uint8 *seed, 85 uint64 seed_bytes) = 0; 86 87 protected: 88 static bool CheckSeed(const uint8 *seed, uint64 seed_bytes); 89}; 90 91} // namespace rng 92} // namespace gputools 93} // namespace perftools 94 95#endif // TENSORFLOW_STREAM_EXECUTOR_RNG_H_ 96