Home | History | Annotate | Download | only in random
      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 #ifndef TENSORFLOW_LIB_RANDOM_PHILOX_RANDOM_TEST_UTILS_H_
     17 #define TENSORFLOW_LIB_RANDOM_PHILOX_RANDOM_TEST_UTILS_H_
     18 
     19 #include <algorithm>
     20 
     21 #include "tensorflow/core/lib/random/philox_random.h"
     22 #include "tensorflow/core/lib/random/random.h"
     23 #include "tensorflow/core/platform/logging.h"
     24 
     25 namespace tensorflow {
     26 namespace random {
     27 
     28 // Return a random seed.
     29 inline uint64 GetTestSeed() { return New64(); }
     30 
     31 // A utility function to fill the given array with samples from the given
     32 // distribution.
     33 template <class Distribution>
     34 void FillRandoms(PhiloxRandom gen, typename Distribution::ResultElementType* p,
     35                  int64 size) {
     36   const int granularity = Distribution::kResultElementCount;
     37 
     38   CHECK(size % granularity == 0)
     39       << " size: " << size << " granularity: " << granularity;
     40 
     41   Distribution dist;
     42   for (int i = 0; i < size; i += granularity) {
     43     const auto sample = dist(&gen);
     44     std::copy(&sample[0], &sample[0] + granularity, &p[i]);
     45   }
     46 }
     47 
     48 }  // namespace random
     49 }  // namespace tensorflow
     50 
     51 #endif  // TENSORFLOW_LIB_RANDOM_PHILOX_RANDOM_TEST_UTILS_H_
     52