Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <atomic>
#include <stdint.h>
#include <utility>
 
#include <mutex>
 
namespace onnxruntime {
 
/**
 * Random seed generator used to generate new seeds for random engines
 * such as std::default_random_engine.  The default (global) generator
 * will use the seed provided by the user to SetRandomSeed().
 */
class RandomGenerator {
 public:
  explicit RandomGenerator(int64_t seed) : seed_(seed) {}
 
  /**
   * Resets the generator seed.
   */
  void SetSeed(int64_t seed) {
    seed_.store(seed);
  }
 
  /**
   * Gets the next seed, optionally incrementing it by the specified count.
   */
  int64_t NextSeed(int64_t count = 1) {
    return seed_.fetch_add(count);
  }
 
  /**
   * Gets the default global random generator.  This generator will use the
   * seed provided in SetRandomSeed(), and will update if the seed is reset.
   */
  static RandomGenerator& Default();
 
 private:
  std::atomic<int64_t> seed_;
};
 
/**
 * Philox pseudo-random number generator.  Philox uses a counter-based design.
 * This generator provides the seed and counter to initialize a Philox random
 * engine such as the CUDA Philox_4x32_10 generator.
 */
class PhiloxGenerator {
 public:
  explicit PhiloxGenerator(uint64_t seed) : seed_(seed), offset_(0) {}
 
  /**
   * Resets the seed and offset.
   */
  void SetSeed(uint64_t seed) {
    std::lock_guard<std::mutex> lock(mutex_);
    seed_ = seed;
    offset_ = 0;
  }
 
  /**
   * Gets the seed and offset pair, incrementing the offset by the specified count.
   */
  std::pair<uint64_t, uint64_t> NextPhiloxSeeds(uint64_t count) {
    std::lock_guard<std::mutex> lock(mutex_);
    auto seeds = std::make_pair(seed_, offset_);
    offset_ += count;
    return seeds;
  }
 
  /**
   * Get the default global random generator.  This generator will use the
   * seed provided in SetRandomSeed(), and will update if the seed is reset.
   */
  static PhiloxGenerator& Default();
 
 private:
  std::mutex mutex_;
  uint64_t seed_;
  uint64_t offset_;
};
 
}  // namespace onnxruntime