mesh-rb 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/ext/mesh/extconf.rb +22 -4
  4. data/ext/mesh/mesh.tar.gz +0 -0
  5. data/lib/mesh/version.rb +1 -1
  6. data/mesh.gemspec +3 -2
  7. metadata +4 -120
  8. data/ext/mesh/mesh/.bazelrc +0 -20
  9. data/ext/mesh/mesh/.bazelversion +0 -1
  10. data/ext/mesh/mesh/.clang-format +0 -15
  11. data/ext/mesh/mesh/.dockerignore +0 -5
  12. data/ext/mesh/mesh/.editorconfig +0 -16
  13. data/ext/mesh/mesh/.gitattributes +0 -4
  14. data/ext/mesh/mesh/.github/workflows/main.yml +0 -144
  15. data/ext/mesh/mesh/.gitignore +0 -51
  16. data/ext/mesh/mesh/AUTHORS +0 -5
  17. data/ext/mesh/mesh/CMakeLists.txt +0 -270
  18. data/ext/mesh/mesh/CODE_OF_CONDUCT.md +0 -77
  19. data/ext/mesh/mesh/Dockerfile +0 -30
  20. data/ext/mesh/mesh/LICENSE +0 -201
  21. data/ext/mesh/mesh/Makefile +0 -81
  22. data/ext/mesh/mesh/README.md +0 -97
  23. data/ext/mesh/mesh/WORKSPACE +0 -50
  24. data/ext/mesh/mesh/bazel +0 -350
  25. data/ext/mesh/mesh/mesh-pldi19-powers.pdf +0 -0
  26. data/ext/mesh/mesh/src/BUILD +0 -222
  27. data/ext/mesh/mesh/src/CMakeLists.txt +0 -85
  28. data/ext/mesh/mesh/src/bitmap.h +0 -590
  29. data/ext/mesh/mesh/src/cheap_heap.h +0 -170
  30. data/ext/mesh/mesh/src/common.h +0 -377
  31. data/ext/mesh/mesh/src/copts.bzl +0 -31
  32. data/ext/mesh/mesh/src/d_assert.cc +0 -75
  33. data/ext/mesh/mesh/src/fixed_array.h +0 -124
  34. data/ext/mesh/mesh/src/global_heap.cc +0 -547
  35. data/ext/mesh/mesh/src/global_heap.h +0 -569
  36. data/ext/mesh/mesh/src/gnu_wrapper.cc +0 -75
  37. data/ext/mesh/mesh/src/internal.h +0 -356
  38. data/ext/mesh/mesh/src/libmesh.cc +0 -239
  39. data/ext/mesh/mesh/src/mac_wrapper.cc +0 -528
  40. data/ext/mesh/mesh/src/measure_rss.cc +0 -44
  41. data/ext/mesh/mesh/src/measure_rss.h +0 -20
  42. data/ext/mesh/mesh/src/meshable_arena.cc +0 -776
  43. data/ext/mesh/mesh/src/meshable_arena.h +0 -309
  44. data/ext/mesh/mesh/src/meshing.h +0 -60
  45. data/ext/mesh/mesh/src/mini_heap.h +0 -532
  46. data/ext/mesh/mesh/src/mmap_heap.h +0 -104
  47. data/ext/mesh/mesh/src/one_way_mmap_heap.h +0 -77
  48. data/ext/mesh/mesh/src/partitioned_heap.h +0 -111
  49. data/ext/mesh/mesh/src/plasma/mesh.h +0 -33
  50. data/ext/mesh/mesh/src/real.cc +0 -52
  51. data/ext/mesh/mesh/src/real.h +0 -36
  52. data/ext/mesh/mesh/src/rng/mwc.h +0 -296
  53. data/ext/mesh/mesh/src/rng/mwc64.h +0 -58
  54. data/ext/mesh/mesh/src/rpl_printf.c +0 -1991
  55. data/ext/mesh/mesh/src/runtime.cc +0 -393
  56. data/ext/mesh/mesh/src/runtime.h +0 -114
  57. data/ext/mesh/mesh/src/shuffle_vector.h +0 -287
  58. data/ext/mesh/mesh/src/size_classes.def +0 -251
  59. data/ext/mesh/mesh/src/static/if.h +0 -36
  60. data/ext/mesh/mesh/src/static/log.h +0 -43
  61. data/ext/mesh/mesh/src/testing/benchmark/local_refill.cc +0 -103
  62. data/ext/mesh/mesh/src/testing/big-alloc.c +0 -28
  63. data/ext/mesh/mesh/src/testing/fragmenter.cc +0 -128
  64. data/ext/mesh/mesh/src/testing/global-large-stress.cc +0 -25
  65. data/ext/mesh/mesh/src/testing/local-alloc.c +0 -16
  66. data/ext/mesh/mesh/src/testing/meshing_benchmark.cc +0 -189
  67. data/ext/mesh/mesh/src/testing/thread.cc +0 -35
  68. data/ext/mesh/mesh/src/testing/unit/alignment.cc +0 -56
  69. data/ext/mesh/mesh/src/testing/unit/bitmap_test.cc +0 -274
  70. data/ext/mesh/mesh/src/testing/unit/concurrent_mesh_test.cc +0 -185
  71. data/ext/mesh/mesh/src/testing/unit/mesh_test.cc +0 -143
  72. data/ext/mesh/mesh/src/testing/unit/rng_test.cc +0 -22
  73. data/ext/mesh/mesh/src/testing/unit/size_class_test.cc +0 -66
  74. data/ext/mesh/mesh/src/testing/unit/triple_mesh_test.cc +0 -285
  75. data/ext/mesh/mesh/src/testing/userfaultfd-kernel-copy.cc +0 -164
  76. data/ext/mesh/mesh/src/thread_local_heap.cc +0 -163
  77. data/ext/mesh/mesh/src/thread_local_heap.h +0 -268
  78. data/ext/mesh/mesh/src/wrapper.cc +0 -433
  79. data/ext/mesh/mesh/support/export_mesh.cmake +0 -28
  80. data/ext/mesh/mesh/support/gen-size-classes +0 -57
  81. data/ext/mesh/mesh/support/install_all_configs +0 -33
  82. data/ext/mesh/mesh/support/remove_export_mesh.cmake +0 -48
  83. data/ext/mesh/mesh/support/update-bazelisk +0 -8
  84. data/ext/mesh/mesh/theory/32m80.png +0 -0
  85. data/ext/mesh/mesh/theory/64m80ind.png +0 -0
  86. data/ext/mesh/mesh/theory/bound_comparison.py +0 -67
  87. data/ext/mesh/mesh/theory/bounds/impdeg+1 +0 -135
  88. data/ext/mesh/mesh/theory/choose.py +0 -43
  89. data/ext/mesh/mesh/theory/common.py +0 -42
  90. data/ext/mesh/mesh/theory/compute_exp_Y.py +0 -134
  91. data/ext/mesh/mesh/theory/createRandomString.py +0 -69
  92. data/ext/mesh/mesh/theory/deg_bound_check.py +0 -100
  93. data/ext/mesh/mesh/theory/degcheck.py +0 -47
  94. data/ext/mesh/mesh/theory/dumps/32,1,80,dumb.txt +0 -81
  95. data/ext/mesh/mesh/theory/dumps/32,2,80,dumb.txt +0 -81
  96. data/ext/mesh/mesh/theory/dumps/32,3,80,dumb.txt +0 -81
  97. data/ext/mesh/mesh/theory/dumps/32,4,80,dumb.txt +0 -81
  98. data/ext/mesh/mesh/theory/dumps/32,5,80,dumb.txt +0 -81
  99. data/ext/mesh/mesh/theory/dumps/32,6,80,dumb.txt +0 -81
  100. data/ext/mesh/mesh/theory/dumps/32,7,80,dumb.txt +0 -81
  101. data/ext/mesh/mesh/theory/dumps/32,8,80,dumb.txt +0 -81
  102. data/ext/mesh/mesh/theory/dumps/32,9,80,dumb.txt +0 -81
  103. data/ext/mesh/mesh/theory/experiment.py +0 -303
  104. data/ext/mesh/mesh/theory/experiment_raw_results/.gitignore +0 -0
  105. data/ext/mesh/mesh/theory/greedy_experiment.py +0 -66
  106. data/ext/mesh/mesh/theory/greedy_experiment_copy.py +0 -46
  107. data/ext/mesh/mesh/theory/greedy_experiment_q.py +0 -75
  108. data/ext/mesh/mesh/theory/makeGraph.py +0 -64
  109. data/ext/mesh/mesh/theory/manyreps.png +0 -0
  110. data/ext/mesh/mesh/theory/manystrings.png +0 -0
  111. data/ext/mesh/mesh/theory/match_vs_color_experiment.py +0 -94
  112. data/ext/mesh/mesh/theory/maxmatch_vs_E[Y].py +0 -162
  113. data/ext/mesh/mesh/theory/maxmatch_vs_greedymatch.py +0 -96
  114. data/ext/mesh/mesh/theory/maxvdeg+1imp++32,80.png +0 -0
  115. data/ext/mesh/mesh/theory/mesh_util.py +0 -322
  116. data/ext/mesh/mesh/theory/meshers.py +0 -452
  117. data/ext/mesh/mesh/theory/meshingBenchmark.py +0 -96
  118. data/ext/mesh/mesh/theory/occupancyComparison.py +0 -133
  119. data/ext/mesh/mesh/theory/randmatch_vs_greedymatch.py +0 -97
  120. data/ext/mesh/mesh/theory/randmatch_vs_greedymatch_q.py +0 -103
  121. data/ext/mesh/mesh/theory/randmatch_vs_greedymatch_time.py +0 -117
  122. data/ext/mesh/mesh/theory/read_mesh_dump.py +0 -82
  123. data/ext/mesh/mesh/theory/test.py +0 -70
  124. data/ext/mesh/mesh/tools/bazel +0 -1
@@ -1,104 +0,0 @@
1
- // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Heap-Layers and Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #pragma once
7
- #ifndef MESH_MMAP_HEAP_H
8
- #define MESH_MMAP_HEAP_H
9
-
10
- #if defined(_WIN32)
11
- #error "TODO"
12
- #include <windows.h>
13
- #else
14
- // UNIX
15
- #include <fcntl.h>
16
- #include <stdlib.h>
17
- #include <sys/mman.h>
18
- #include <sys/stat.h>
19
- #include <sys/types.h>
20
- #include <unistd.h>
21
- #include <map>
22
- #endif
23
-
24
- #include "internal.h"
25
- #include "one_way_mmap_heap.h"
26
-
27
- namespace mesh {
28
-
29
- // MmapHeap extends OneWayMmapHeap to track allocated address space
30
- // and will free memory with calls to munmap.
31
- class MmapHeap : public OneWayMmapHeap {
32
- private:
33
- DISALLOW_COPY_AND_ASSIGN(MmapHeap);
34
- typedef OneWayMmapHeap SuperHeap;
35
-
36
- public:
37
- enum { Alignment = MmapWrapper::Alignment };
38
-
39
- MmapHeap() : SuperHeap() {
40
- }
41
-
42
- inline void *malloc(size_t sz) {
43
- auto ptr = map(sz, MAP_PRIVATE | MAP_ANONYMOUS, -1);
44
-
45
- d_assert(_vmaMap.find(ptr) == _vmaMap.end());
46
- _vmaMap[ptr] = sz;
47
- d_assert(_vmaMap.find(ptr) != _vmaMap.end());
48
- d_assert(_vmaMap[ptr] == sz);
49
-
50
- return ptr;
51
- }
52
-
53
- inline size_t getSize(void *ptr) const {
54
- auto entry = _vmaMap.find(ptr);
55
- if (unlikely(entry == _vmaMap.end())) {
56
- debug("mmap: invalid getSize: %p", ptr);
57
- abort();
58
- return 0;
59
- }
60
- return entry->second;
61
- }
62
-
63
- inline bool inBounds(void *ptr) const {
64
- auto entry = _vmaMap.find(ptr);
65
- if (unlikely(entry == _vmaMap.end())) {
66
- return false;
67
- }
68
- // FIXME: this isn't right -- we want inclusion not exact match
69
- return true;
70
- }
71
-
72
- inline void free(void *ptr) {
73
- auto entry = _vmaMap.find(ptr);
74
- if (unlikely(entry == _vmaMap.end())) {
75
- debug("mmap: invalid free, possibly from memalign: %p", ptr);
76
- // abort();
77
- return;
78
- }
79
-
80
- auto sz = entry->second;
81
-
82
- munmap(ptr, sz);
83
- // madvise(ptr, sz, MADV_DONTNEED);
84
- // mprotect(ptr, sz, PROT_NONE);
85
-
86
- _vmaMap.erase(entry);
87
- d_assert(_vmaMap.find(ptr) == _vmaMap.end());
88
- }
89
-
90
- // return the sum of the sizes of all large allocations
91
- size_t arenaSize() const {
92
- size_t sz = 0;
93
- for (auto it = _vmaMap.begin(); it != _vmaMap.end(); it++) {
94
- sz += it->second;
95
- }
96
- return sz;
97
- }
98
-
99
- protected:
100
- internal::unordered_map<void *, size_t> _vmaMap{};
101
- };
102
- } // namespace mesh
103
-
104
- #endif // MESH_MESH_MMAP_H
@@ -1,77 +0,0 @@
1
- // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Heap-Layers and Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #pragma once
7
- #ifndef MESH_ONE_WAY_MMAP_HEAP_H
8
- #define MESH_ONE_WAY_MMAP_HEAP_H
9
-
10
- #if defined(_WIN32)
11
- #error "TODO"
12
- #include <windows.h>
13
- #else
14
- // UNIX
15
- #include <fcntl.h>
16
- #include <stdlib.h>
17
- #include <sys/mman.h>
18
- #include <sys/stat.h>
19
- #include <sys/types.h>
20
- #include <unistd.h>
21
- #endif
22
-
23
- #include "common.h"
24
-
25
- #ifndef HL_MMAP_PROTECTION_MASK
26
- #error "define HL_MMAP_PROTECTION_MASK before including mmapheap.h"
27
- #endif
28
-
29
- #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
30
- #define MAP_ANONYMOUS MAP_ANON
31
- #endif
32
-
33
- namespace mesh {
34
-
35
- // OneWayMmapHeap allocates address space through calls to mmap and
36
- // will never unmap address space.
37
- class OneWayMmapHeap {
38
- private:
39
- DISALLOW_COPY_AND_ASSIGN(OneWayMmapHeap);
40
-
41
- public:
42
- enum { Alignment = MmapWrapper::Alignment };
43
-
44
- OneWayMmapHeap() {
45
- }
46
-
47
- inline void *map(size_t sz, int flags, int fd = -1) {
48
- if (sz == 0)
49
- return nullptr;
50
-
51
- // Round up to the size of a page.
52
- sz = (sz + kPageSize - 1) & (size_t) ~(kPageSize - 1);
53
-
54
- void *ptr = mmap(nullptr, sz, HL_MMAP_PROTECTION_MASK, flags, fd, 0);
55
- if (ptr == MAP_FAILED)
56
- abort();
57
-
58
- d_assert(reinterpret_cast<size_t>(ptr) % Alignment == 0);
59
-
60
- return ptr;
61
- }
62
-
63
- inline void *malloc(size_t sz) {
64
- return map(sz, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1);
65
- }
66
-
67
- inline size_t getSize(void *ATTRIBUTE_UNUSED ptr) const {
68
- return 0;
69
- }
70
-
71
- inline void free(void *ATTRIBUTE_UNUSED ptr) {
72
- }
73
- };
74
-
75
- } // namespace mesh
76
-
77
- #endif // MESH_ONE_WAY_MMAP_HEAP_H
@@ -1,111 +0,0 @@
1
- // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #pragma once
7
- #ifndef MESH_PARTITIONED_HEAP_H
8
- #define MESH_PARTITIONED_HEAP_H
9
-
10
- // we can only include "common.h", as the partitioned heap is used as
11
- // the allocator-internal heap in "internal.h"
12
- #include "common.h"
13
-
14
- #include "cheap_heap.h"
15
- #include "one_way_mmap_heap.h"
16
-
17
- namespace mesh {
18
-
19
- static constexpr int kPartitionedHeapNBins = 16;
20
- static constexpr int kPartitionedHeapArenaSize = 512 * 1024 * 1024; // 512 MB
21
- static constexpr int kPartitionedHeapSizePer = kPartitionedHeapArenaSize / kPartitionedHeapNBins;
22
-
23
- // Fast allocation for multiple size classes
24
- class PartitionedHeap : public OneWayMmapHeap {
25
- private:
26
- DISALLOW_COPY_AND_ASSIGN(PartitionedHeap);
27
- typedef OneWayMmapHeap SuperHeap;
28
-
29
- public:
30
- PartitionedHeap() : SuperHeap() {
31
- _smallArena = reinterpret_cast<char *>(SuperHeap::malloc(kPartitionedHeapArenaSize));
32
- hard_assert(_smallArena != nullptr);
33
- _smallArenaEnd = _smallArena + kPartitionedHeapArenaSize;
34
-
35
- auto freelist = reinterpret_cast<char *>(SuperHeap::malloc(kPartitionedHeapArenaSize));
36
- hard_assert(freelist != nullptr);
37
-
38
- for (size_t i = 0; i < kPartitionedHeapNBins; ++i) {
39
- auto arenaStart = _smallArena + i * kPartitionedHeapSizePer;
40
- auto freelistStart = freelist + i * kPartitionedHeapSizePer;
41
-
42
- const auto allocSize = powerOfTwo::ByteSizeForClass(i);
43
- const auto maxCount = kPartitionedHeapSizePer / allocSize;
44
-
45
- // mesh::debug("internal heap of allocSize %zu\n", allocSize);
46
-
47
- _smallHeaps[i].init(allocSize, maxCount, arenaStart, reinterpret_cast<void **>(freelistStart));
48
- }
49
-
50
- // TODO: calc freelist + arena offsets
51
- }
52
-
53
- inline void *malloc(size_t sz) {
54
- const auto sizeClass = powerOfTwo::ClassForByteSize(sz);
55
-
56
- if (unlikely(sizeClass >= kPartitionedHeapNBins)) {
57
- auto res = _bigHeap.malloc(sz);
58
- // debug("internalHeap::malloc(%zu): %p (big)\n", sz, res);
59
- return res;
60
- }
61
-
62
- auto res = _smallHeaps[sizeClass].alloc();
63
- // debug("internalHeap::malloc(%zu): %p\n", sz, res);
64
- return res;
65
- }
66
-
67
- inline void free(void *ptr) {
68
- // debug("internalHeap::free(): %p\n", ptr);
69
- if (unlikely(!contains(ptr))) {
70
- _bigHeap.free(ptr);
71
- return;
72
- }
73
-
74
- const auto sizeClass = getSizeClass(ptr);
75
- d_assert(sizeClass >= 0);
76
- d_assert(sizeClass < kPartitionedHeapNBins);
77
-
78
- return _smallHeaps[sizeClass].free(ptr);
79
- }
80
-
81
- inline size_t getSize(void *ptr) {
82
- if (unlikely(!contains(ptr))) {
83
- return _bigHeap.getSize(ptr);
84
- }
85
-
86
- return powerOfTwo::ByteSizeForClass(getSizeClass(ptr));
87
- }
88
-
89
- // must be protected with a contains()
90
- inline int getSizeClass(void *ptr) const {
91
- const char *ptrval = reinterpret_cast<char *>(ptr);
92
- const auto sizeClass = (ptrval - _smallArena) / kPartitionedHeapSizePer;
93
- d_assert(sizeClass >= 0);
94
- d_assert(sizeClass < kPartitionedHeapNBins);
95
- return sizeClass;
96
- }
97
-
98
- inline bool contains(void *ptr) const {
99
- const auto ptrval = reinterpret_cast<char *>(ptr);
100
- return ptrval >= _smallArena && ptrval < _smallArenaEnd;
101
- }
102
-
103
- private:
104
- char *_smallArena{nullptr};
105
- char *_smallArenaEnd{nullptr};
106
- DynCheapHeap _smallHeaps[kPartitionedHeapNBins]{};
107
- HL::MmapHeap _bigHeap{};
108
- };
109
- } // namespace mesh
110
-
111
- #endif // MESH_PARTITIONED_HEAP_H
@@ -1,33 +0,0 @@
1
- // -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #pragma once
7
- #ifndef PLASMA__MESH_H
8
- #define PLASMA__MESH_H
9
-
10
- #include <stddef.h>
11
-
12
- #define MESH_VERSION_MAJOR 1
13
- #define MESH_VERSION_MINOR 0
14
-
15
- #ifdef __cplusplus
16
- extern "C" {
17
- #endif
18
-
19
- // Same API as je_mallctl, allows a program to query stats and set
20
- // allocator-related options.
21
- int mesh_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
22
-
23
- // 0 if not in bounds, 1 if is.
24
- int mesh_in_bounds(void *ptr);
25
-
26
- // returns the usable size of an allocation
27
- size_t mesh_usable_size(void *ptr);
28
-
29
- #ifdef __cplusplus
30
- }
31
- #endif
32
-
33
- #endif /* PLASMA__MESH_H */
@@ -1,52 +0,0 @@
1
- // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #include <dlfcn.h>
7
-
8
- #include "real.h"
9
-
10
- #include "common.h"
11
-
12
- #define DEFINE_REAL(name) decltype(::name) *name
13
- #define INIT_REAL(name, handle) \
14
- do { \
15
- name = reinterpret_cast<decltype(::name) *>(dlsym(handle, #name)); \
16
- hard_assert_msg(name != nullptr, "mesh::real: expected %s", #name); \
17
- } while (false)
18
-
19
- namespace mesh {
20
- namespace real {
21
- #ifdef __linux__
22
- DEFINE_REAL(epoll_pwait);
23
- DEFINE_REAL(epoll_wait);
24
- #endif
25
-
26
- DEFINE_REAL(pthread_create);
27
- DEFINE_REAL(pthread_exit);
28
-
29
- DEFINE_REAL(sigaction);
30
- DEFINE_REAL(sigprocmask);
31
-
32
- void init() {
33
- static mutex initLock;
34
- static bool initialized;
35
-
36
- lock_guard<mutex> lock(initLock);
37
- if (initialized)
38
- return;
39
- initialized = true;
40
- #ifdef __linux__
41
- INIT_REAL(epoll_pwait, RTLD_NEXT);
42
- INIT_REAL(epoll_wait, RTLD_NEXT);
43
- #endif
44
-
45
- INIT_REAL(pthread_create, RTLD_NEXT);
46
- INIT_REAL(pthread_exit, RTLD_NEXT);
47
-
48
- INIT_REAL(sigaction, RTLD_NEXT);
49
- INIT_REAL(sigprocmask, RTLD_NEXT);
50
- }
51
- } // namespace real
52
- } // namespace mesh
@@ -1,36 +0,0 @@
1
- // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2
- // Copyright 2019 The Mesh Authors. All rights reserved.
3
- // Use of this source code is governed by the Apache License,
4
- // Version 2.0, that can be found in the LICENSE file.
5
-
6
- #include <pthread.h>
7
- #include <signal.h>
8
-
9
- #ifdef __linux__
10
- #include <sys/epoll.h>
11
- #endif
12
-
13
- #pragma once
14
- #ifndef MESH_REAL_H
15
- #define MESH_REAL_H
16
-
17
- #define DECLARE_REAL(name) extern decltype(::name) *name
18
-
19
- namespace mesh {
20
- namespace real {
21
- void init();
22
-
23
- #ifdef __linux__
24
- DECLARE_REAL(epoll_pwait);
25
- DECLARE_REAL(epoll_wait);
26
- #endif
27
-
28
- DECLARE_REAL(pthread_create);
29
- DECLARE_REAL(pthread_exit);
30
-
31
- DECLARE_REAL(sigaction);
32
- DECLARE_REAL(sigprocmask);
33
- } // namespace real
34
- } // namespace mesh
35
-
36
- #endif // MESH_REAL_H
@@ -1,296 +0,0 @@
1
- // -*- C++ -*-
2
-
3
- #ifndef MESH_MWC_H
4
- #define MESH_MWC_H
5
-
6
- #include "../common.h"
7
- //#include <assert.h>
8
- //#define d_assert assert
9
-
10
- #include "mwc64.h"
11
- // #include "sx.hh"
12
-
13
- /**
14
- * @class MWC
15
- * @brief A super-fast multiply-with-carry pseudo-random number generator.
16
- * @author Emery Berger <http://www.cs.umass.edu/~emery>
17
- * @note Copyright (C) 2005-2011 by Emery Berger, University of Massachusetts Amherst.
18
- */
19
-
20
- class RealMWC {
21
- public:
22
- explicit RealMWC(unsigned int seed1, unsigned int seed2) : z(seed1), w(seed2) {
23
- d_assert(seed1 != 0);
24
- d_assert(seed2 != 0);
25
- // debug("MWC seed1: %u seed2: %u\n", seed1, seed2);
26
- }
27
-
28
- inline unsigned int next(void) {
29
- d_assert(w != 0);
30
- d_assert(z != 0);
31
- // These magic numbers are derived from a note by George Marsaglia.
32
- unsigned int znew = 36969 * (z & 65535) + (z >> 16);
33
- unsigned int wnew = 18000 * (w & 65535) + (w >> 16);
34
- unsigned int x = (znew << 16) + wnew;
35
- // debug("MWC: %8x\n", x);
36
- d_assert(wnew != 0);
37
- d_assert(znew != 0);
38
- w = wnew;
39
- z = znew;
40
- return x;
41
- }
42
-
43
- private:
44
- unsigned int z;
45
- unsigned int w;
46
- };
47
-
48
- class MWC : public MWC64 {
49
- public:
50
- MWC(uint64_t s1, uint64_t s2) : MWC64::MWC64(s1, s2) {
51
- }
52
- // returns a number between min and max (inclusive)
53
- inline unsigned int ATTRIBUTE_ALWAYS_INLINE inRange(size_t min, size_t max) {
54
- size_t range = 1 + max - min;
55
-
56
- // return min + next() % range;
57
- // adapted from https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
58
- return min + (((uint64_t)(uint32_t)next() * (uint64_t)range) >> 32);
59
- }
60
- };
61
-
62
- #endif
63
-
64
- #if 0
65
-
66
- #include <stdio.h>
67
- #define znew (z = 36969 * (z & 65535) + (z >> 16))
68
- #define wnew (w = 18000 * (w & 65535) + (w >> 16))
69
- #define MWC ((znew << 16) + wnew)
70
- #define SHR3 (jsr ^= (jsr << 17), jsr ^= (jsr >> 13), jsr ^= (jsr << 5))
71
- #define CONG (jcong = 69069 * jcong + 1234567)
72
- #define FIB ((b = a + b), (a = b - a))
73
- #define KISS ((MWC ^ CONG) + SHR3)
74
- #define LFIB4 (c++, t[c] = t[c] + t[UC(c + 58)] + t[UC(c + 119)] + t[UC(c + 178)])
75
- #define SWB (c++, bro = (x < y), t[c] = (x = t[UC(c + 34)]) - (y = t[UC(c + 19)] + bro))
76
- #define UNI (KISS * 2.328306e-10)
77
- #define VNI ((int)KISS) * 4.656613e-10
78
- #define UC (unsigned char)/*a cast operation*/
79
- typedef unsigned int UL;
80
-
81
- /* Global static variables: */
82
- static UL z = 362436069, w = 521288629, jsr = 123456789, jcong = 380116160;
83
- static UL a = 224466889, b = 7584631, t[256];
84
- /* Use random seeds to reset z,w,jsr,jcong,a,b, and the table t[256]*/
85
-
86
- static UL x = 0, y = 0, bro;
87
- static unsigned char c = 0;
88
-
89
- /* Example procedure to set the table, using KISS: */
90
- void settable(UL i1, UL i2, UL i3, UL i4, UL i5, UL i6) {
91
- int i;
92
- z = i1;
93
- w = i2, jsr = i3;
94
- jcong = i4;
95
- a = i5;
96
- b = i6;
97
- for (i = 0; i < 256; i = i + 1)
98
- t[i] = KISS;
99
- }
100
-
101
- /* This is a test main program. It should compile and print 7 0's. */
102
- int main(void) {
103
- int i;
104
- UL k;
105
- settable(12345, 65435, 34221, 12345, 9983651, 95746118);
106
-
107
- for (i = 1; i < 1000001; i++) {
108
- k = LFIB4;
109
- }
110
- printf("%u\n", k - 1064612766U);
111
- for (i = 1; i < 1000001; i++) {
112
- k = SWB;
113
- }
114
- printf("%u\n", k - 627749721U);
115
- for (i = 1; i < 1000001; i++) {
116
- k = KISS;
117
- }
118
- printf("%u\n", k - 1372460312U);
119
- for (i = 1; i < 1000001; i++) {
120
- k = CONG;
121
- }
122
- printf("%u\n", k - 1529210297U);
123
- for (i = 1; i < 1000001; i++) {
124
- k = SHR3;
125
- }
126
- printf("%u\n", k - 2642725982U);
127
- for (i = 1; i < 1000001; i++) {
128
- k = MWC;
129
- }
130
- printf("%u\n", k - 904977562U);
131
- for (i = 1; i < 1000001; i++) {
132
- k = FIB;
133
- }
134
- printf("%u\n", k - 3519793928U);
135
- }
136
-
137
- /*-----------------------------------------------------
138
- Write your own calling program and try one or more of
139
- the above, singly or in combination, when you run a
140
- simulation. You may want to change the simple 1-letter
141
- names, to avoid conflict with your own choices. */
142
-
143
- /* All that follows is comment, mostly from the initial
144
- post. You may want to remove it */
145
-
146
- /* Any one of KISS, MWC, FIB, LFIB4, SWB, SHR3, or CONG
147
- can be used in an expression to provide a random 32-bit
148
- integer.
149
-
150
-
151
- The KISS generator, (Keep It Simple Stupid), is
152
- designed to combine the two multiply-with-carry
153
- generators in MWC with the 3-shift register SHR3 and
154
- the congruential generator CONG, using addition and
155
- exclusive-or. Period about 2^123.
156
- It is one of my favorite generators.
157
-
158
-
159
- The MWC generator concatenates two 16-bit multiply-
160
- with-carry generators, x(n)=36969x(n-1)+carry,
161
- y(n)=18000y(n-1)+carry mod 2^16, has period about
162
- 2^60 and seems to pass all tests of randomness. A
163
- favorite stand-alone generator---faster than KISS,
164
- which contains it.
165
-
166
-
167
- FIB is the classical Fibonacci sequence
168
- x(n)=x(n-1)+x(n-2),but taken modulo 2^32.
169
- Its period is 3*2^31 if one of its two seeds is odd
170
- and not 1 mod 8. It has little worth as a RNG by
171
- itself, but provides a simple and fast component for
172
- use in combination generators.
173
-
174
-
175
- SHR3 is a 3-shift-register generator with period
176
- 2^32-1. It uses y(n)=y(n-1)(I+L^17)(I+R^13)(I+L^5),
177
- with the y's viewed as binary vectors, L the 32x32
178
- binary matrix that shifts a vector left 1, and R its
179
- transpose. SHR3 seems to pass all except those
180
- related to the binary rank test, since 32 successive
181
- values, as binary vectors, must be linearly
182
- independent, while 32 successive truly random 32-bit
183
- integers, viewed as binary vectors, will be linearly
184
- independent only about 29% of the time.
185
-
186
-
187
- CONG is a congruential generator with the widely used 69069
188
- multiplier: x(n)=69069x(n-1)+1234567. It has period
189
- 2^32. The leading half of its 32 bits seem to pass
190
- tests, but bits in the last half are too regular.
191
-
192
-
193
- LFIB4 is an extension of what I have previously
194
- defined as a lagged Fibonacci generator:
195
- x(n)=x(n-r) op x(n-s), with the x's in a finite
196
- set over which there is a binary operation op, such
197
- as +,- on integers mod 2^32, * on odd such integers,
198
- exclusive-or(xor) on binary vectors. Except for
199
- those using multiplication, lagged Fibonacci
200
- generators fail various tests of randomness, unless
201
- the lags are very long. (See SWB below).
202
- To see if more than two lags would serve to overcome
203
- the problems of 2-lag generators using +,- or xor, I
204
- have developed the 4-lag generator LFIB4 using
205
- addition: x(n)=x(n-256)+x(n-179)+x(n-119)+x(n-55)
206
- mod 2^32. Its period is 2^31*(2^256-1), about 2^287,
207
- and it seems to pass all tests---in particular,
208
- those of the kind for which 2-lag generators using
209
- +,-,xor seem to fail. For even more confidence in
210
- its suitability, LFIB4 can be combined with KISS,
211
- with a resulting period of about 2^410: just use
212
- (KISS+LFIB4) in any C expression.
213
-
214
-
215
- SWB is a subtract-with-borrow generator that I
216
- developed to give a simple method for producing
217
- extremely long periods:
218
- x(n)=x(n-222)-x(n-237)- borrow mod 2^32.
219
- The 'borrow' is 0, or set to 1 if computing x(n-1)
220
- caused overflow in 32-bit integer arithmetic. This
221
- generator has a very long period, 2^7098(2^480-1),
222
- about 2^7578. It seems to pass all tests of
223
- randomness, except for the Birthday Spacings test,
224
- which it fails badly, as do all lagged Fibonacci
225
- generators using +,- or xor. I would suggest
226
- combining SWB with KISS, MWC, SHR3, or CONG.
227
- KISS+SWB has period >2^7700 and is highly
228
- recommended.
229
- Subtract-with-borrow has the same local behaviour
230
- as lagged Fibonacci using +,-,xor---the borrow
231
- merely provides a much longer period.
232
- SWB fails the birthday spacings test, as do all
233
- lagged Fibonacci and other generators that merely
234
- combine two previous values by means of =,- or xor.
235
- Those failures are for a particular case: m=512
236
- birthdays in a year of n=2^24 days. There are
237
- choices of m and n for which lags >1000 will also
238
- fail the test. A reasonable precaution is to always
239
- combine a 2-lag Fibonacci or SWB generator with
240
- another kind of generator, unless the generator uses
241
- *, for which a very satisfactory sequence of odd
242
- 32-bit integers results.
243
-
244
-
245
- The classical Fibonacci sequence mod 2^32 from FIB
246
- fails several tests. It is not suitable for use by
247
- itself, but is quite suitable for combining with
248
- other generators.
249
-
250
-
251
- The last half of the bits of CONG are too regular,
252
- and it fails tests for which those bits play a
253
- significant role. CONG+FIB will also have too much
254
- regularity in trailing bits, as each does. But keep
255
- in mind that it is a rare application for which
256
- the trailing bits play a significant role. CONG
257
- is one of the most widely used generators of the
258
- last 30 years, as it was the system generator for
259
- VAX and was incorporated in several popular
260
- software packages, all seemingly without complaint.
261
-
262
-
263
- Finally, because many simulations call for uniform
264
- random variables in 0<x<1 or -1<x<1, I use #define
265
- statements that permit inclusion of such variates
266
- directly in expressions: using UNI will provide a
267
- uniform random real (float) in (0,1), while VNI will
268
- provide one in (-1,1).
269
-
270
-
271
- All of these: MWC, SHR3, CONG, KISS, LFIB4, SWB, FIB
272
- UNI and VNI, permit direct insertion of the desired
273
- random quantity into an expression, avoiding the
274
- time and space costs of a function call. I call
275
- these in-line-define functions. To use them, static
276
- variables z,w,jsr,jcong,a and b should be assigned
277
- seed values other than their initial values. If
278
- LFIB4 or SWB are used, the static table t[256] must
279
- be initialized.
280
-
281
-
282
- A note on timing: It is difficult to provide exact
283
- time costs for inclusion of one of these in-line-
284
- define functions in an expression. Times may differ
285
- widely for different compilers, as the C operations
286
- may be deeply nested and tricky. I suggest these
287
- rough comparisons, based on averaging ten runs of a
288
- routine that is essentially a long loop:
289
- for(i=1;i<10000000;i++) L=KISS; then with KISS
290
- replaced with SHR3, CONG,... or KISS+SWB, etc. The
291
- times on my home PC, a Pentium 300MHz, in nanoseconds:
292
- FIB 49;LFIB4 77;SWB 80;CONG 80;SHR3 84;MWC 93;KISS 157;
293
- VNI 417;UNI 450;
294
- */
295
-
296
- #endif