You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by pc...@apache.org on 2018/05/14 08:02:44 UTC

[arrow] branch master updated: ARROW-2578: [Plasma] Use mersenne twister to generate random number

This is an automated email from the ASF dual-hosted git repository.

pcmoritz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b8511f  ARROW-2578: [Plasma] Use mersenne twister to generate random number
4b8511f is described below

commit 4b8511f92d24dc9d5ace150218a2902c8e32dc76
Author: Philipp Moritz <pc...@gmail.com>
AuthorDate: Mon May 14 01:02:35 2018 -0700

    ARROW-2578: [Plasma] Use mersenne twister to generate random number
    
    This gets rid of the std::random_device, which is slow and causes errors in valgrind. Instead we use the std::mt19937 Mersenne Twister.
    
    Author: Philipp Moritz <pc...@gmail.com>
    
    Closes #2039 from pcmoritz/new-rng and squashes the following commits:
    
    21d0e3f7 <Philipp Moritz> fixes
    be4bb84d <Philipp Moritz> fix
    beb5bab8 <Philipp Moritz> update
    83740b5c <Philipp Moritz> update
    f60bd99c <Philipp Moritz> more valgrind fixes
    62d412f3 <Philipp Moritz> fix on older versions of macOS
    841a67f0 <Philipp Moritz> fix linting
    cd95cf15 <Philipp Moritz> use mersenne twister to generate random number
---
 cpp/src/plasma/common.cc            | 12 ++++++++++--
 cpp/src/plasma/test/client_tests.cc |  4 ++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/cpp/src/plasma/common.cc b/cpp/src/plasma/common.cc
index 2de06d5..be3fc74 100644
--- a/cpp/src/plasma/common.cc
+++ b/cpp/src/plasma/common.cc
@@ -17,6 +17,8 @@
 
 #include "plasma/common.h"
 
+#include <limits>
+#include <mutex>
 #include <random>
 
 #include "plasma/plasma_generated.h"
@@ -28,9 +30,15 @@ using arrow::Status;
 UniqueID UniqueID::from_random() {
   UniqueID id;
   uint8_t* data = id.mutable_data();
-  std::random_device engine;
+  // NOTE(pcm): The right way to do this is to have one std::mt19937 per
+  // thread (using the thread_local keyword), but that's not supported on
+  // older versions of macOS (see https://stackoverflow.com/a/29929949)
+  static std::mutex mutex;
+  std::lock_guard<std::mutex> lock(mutex);
+  static std::mt19937 generator;
+  std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint8_t>::max());
   for (int i = 0; i < kUniqueIDSize; i++) {
-    data[i] = static_cast<uint8_t>(engine());
+    data[i] = static_cast<uint8_t>(dist(generator));
   }
   return id;
 }
diff --git a/cpp/src/plasma/test/client_tests.cc b/cpp/src/plasma/test/client_tests.cc
index b80862d..23d2c2b 100644
--- a/cpp/src/plasma/test/client_tests.cc
+++ b/cpp/src/plasma/test/client_tests.cc
@@ -51,8 +51,8 @@ class TestPlasmaStore : public ::testing::Test {
   // TODO(pcm): At the moment, stdout of the test gets mixed up with
   // stdout of the object store. Consider changing that.
   void SetUp() {
-    std::mt19937 rng;
-    rng.seed(std::random_device()());
+    uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
+    std::mt19937 rng(static_cast<uint32_t>(seed));
     std::string store_index = std::to_string(rng());
     store_socket_name_ = "/tmp/store" + store_index;
 

-- 
To stop receiving notification emails like this one, please contact
pcmoritz@apache.org.