Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Used standard random generators instean of rand_r
Browse files Browse the repository at this point in the history
  • Loading branch information
lebeg committed Aug 9, 2018
1 parent 67de467 commit 4ae3439
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
40 changes: 25 additions & 15 deletions src/operator/rnn_impl.h
Expand Up @@ -34,6 +34,7 @@
#include <vector>
#include <string>
#include <utility>
#include <random>
#include "./math.h"
#include "./math_functions-inl.h"
#include "./operator_common.h"
Expand Down Expand Up @@ -149,7 +150,6 @@ void LstmForwardTraining(DType* ws,
const int r_size = D * T * N * H * 6;
const int y_offset = T * N * H * 5;
const int cell_size = N * H;
unsigned int seed_ = 17 + rand() % 4096; // NOLINT(runtime/threadsafe_fn)
int idx = 0; // state & cell state's idx;
const int omp_threads = mxnet::engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
for (int i = 0; i < L; ++i) {
Expand All @@ -174,17 +174,21 @@ void LstmForwardTraining(DType* ws,
w_ptr += w_size;
b_ptr += b_size;
if (dropout > 0.0f) {
#pragma omp parallel for num_threads(omp_threads)
for (int j = 0; j < T * N * H * D; j++) {
int rand_data = rand_r(&seed_);
if (static_cast<float>(rand_data % 1000) < static_cast<float>(1000 * dropout)) {
dropout_random[i * T * N * H * D + j] = 0;
y.dptr_[j] = 0;
} else {
dropout_random[i * T * N * H * D + j] = 1.0f - dropout;
y.dptr_[j] = y.dptr_[j] / (1.0f - dropout);
#pragma omp parallel for num_threads(omp_threads)
for (int j = 0; j < T * N * H * D; j++) {
static thread_local std::random_device device;
static thread_local std::default_random_engine generator(device());
static thread_local std::uniform_int_distribution<int> distribution;
static thread_local auto dice = std::bind(distribution, generator);
int rand_data = dice();
if (static_cast<float>(rand_data % 1000) < static_cast<float>(1000 * dropout)) {
dropout_random[i * T * N * H * D + j] = 0;
y.dptr_[j] = 0;
} else {
dropout_random[i * T * N * H * D + j] = 1.0f - dropout;
y.dptr_[j] = y.dptr_[j] / (1.0f - dropout);
}
}
}
}
x_ptr = y.dptr_;
rs2 += r_size;
Expand Down Expand Up @@ -994,7 +998,6 @@ void GruForwardTraining(DType* ws,
DType* bx_l = bx;
DType* bh_l = bh;
DType* y_tmp = x_ptr;
unsigned int seed_ = 17 + rand() % 4096; // NOLINT(runtime/threadsafe_fn)
for (int l = 0; l < L; l++) {
if (l != 0) {
y_tmp = y_l;
Expand All @@ -1004,7 +1007,11 @@ void GruForwardTraining(DType* ws,
const int omp_threads = mxnet::engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
#pragma omp parallel for num_threads(omp_threads)
for (int i = 0; i < T * N * I; i++) {
int rand_data = rand_r(&seed_);
static thread_local std::random_device device;
static thread_local std::default_random_engine generator(device());
static thread_local std::uniform_int_distribution<int> distribution;
static thread_local auto dice = std::bind(distribution, generator);
int rand_data = dice();
if (static_cast<float>(rand_data % 1000) < static_cast<float>(1000 * dropout)) {
dropout_random[(l - 1) * T * N * I + i] = 0;
y_tmp[i] = 0;
Expand Down Expand Up @@ -1881,7 +1888,6 @@ void VanillaRNNForwardTraining(DType* ws,
DType* bh_l = bh;
DType* y_tmp = x_ptr;
const int omp_threads = mxnet::engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
unsigned int seed_ = 17 + rand() % 4096; // NOLINT(runtime/threadsafe_fn)
for (int l = 0; l < L; l++) {
if (l != 0) {
y_tmp = y_l;
Expand All @@ -1890,7 +1896,11 @@ void VanillaRNNForwardTraining(DType* ws,
if (dropout > 0.0f && l > 0) {
#pragma omp parallel for num_threads(omp_threads)
for (int i = 0; i < T * N * I; i++) {
int rand_data = rand_r(&seed_);
static thread_local std::random_device device;
static thread_local std::default_random_engine generator(device());
static thread_local std::uniform_int_distribution<int> distribution;
static thread_local auto dice = std::bind(distribution, generator);
int rand_data = dice();
if (static_cast<float>(rand_data % 1000) < static_cast<float>(1000 * dropout)) {
dropout_random[(l - 1) * T * N * I + i] = 0;
y_tmp[i] = 0;
Expand Down
13 changes: 9 additions & 4 deletions tests/cpp/engine/threaded_engine_test.cc
Expand Up @@ -33,6 +33,7 @@
#include <thread>
#include <chrono>
#include <vector>
#include <random>

#include "../src/engine/engine_impl.h"
#include "../include/test_util.h"
Expand All @@ -58,17 +59,21 @@ void GenerateWorkload(int num_workloads, int num_var,
int min_read, int max_read,
int min_time, int max_time,
std::vector<Workload>* workloads) {
static thread_local std::default_random_engine generator(seed_);
static thread_local std::uniform_int_distribution<int> distribution;
static thread_local auto dice = std::bind(distribution, generator);

workloads->clear();
workloads->resize(num_workloads);
for (int i = 0; i < num_workloads; ++i) {
auto& wl = workloads->at(i);
wl.write = rand_r(&seed_) % num_var;
int r = rand_r(&seed_);
wl.write = dice() % num_var;
int r = dice();
int num_read = min_read + (r % (max_read - min_read));
for (int j = 0; j < num_read; ++j) {
wl.reads.push_back(rand_r(&seed_) % num_var);
wl.reads.push_back(dice() % num_var);
}
wl.time = min_time + rand_r(&seed_) % (max_time - min_time);
wl.time = min_time + dice() % (max_time - min_time);
}
}

Expand Down
17 changes: 8 additions & 9 deletions tests/cpp/include/test_ndarray_utils.h
Expand Up @@ -29,6 +29,8 @@
#include <cstdlib>
#include <string>
#include <map>
#include <random>

#include "test_util.h"
#include "test_op.h"

Expand All @@ -47,16 +49,13 @@ inline void CheckDataRegion(const TBlob &src, const TBlob &dst) {
EXPECT_EQ(equals, 0);
}

inline unsigned gen_rand_seed() {
time_t timer;
::time(&timer);
return static_cast<unsigned>(timer);
}

inline float RandFloat() {
static unsigned seed = gen_rand_seed();
double v = rand_r(&seed) * 1.0 / RAND_MAX;
return static_cast<float>(v);
static thread_local std::random_device device;
static thread_local std::default_random_engine generator(device());
static thread_local std::uniform_real_distribution<float> distribution;
static thread_local auto dice = std::bind(distribution, generator);

return dice();
}

// Get an NDArray with provided indices, prepared for a RowSparse NDArray.
Expand Down

0 comments on commit 4ae3439

Please sign in to comment.