Skip to content

Commit ee143ff

Browse files
author
Will Feng
committed
fix mutex bug on OS X
1 parent e8c82ec commit ee143ff

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

aten/src/TH/THRandom.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static THGenerator* THGenerator_newUnseeded()
1818
self->left = 1;
1919
self->seeded = 0;
2020
self->normal_is_valid = 0;
21+
self->mutex = new std::mutex();
2122
return self;
2223
}
2324

@@ -32,11 +33,13 @@ THGenerator* THGenerator_new()
3233
THGenerator* THGenerator_copy(THGenerator *self, THGenerator *from)
3334
{
3435
memcpy(self, from, sizeof(THGenerator));
36+
self->mutex = new std::mutex();
3537
return self;
3638
}
3739

3840
void THGenerator_free(THGenerator *self)
3941
{
42+
delete self->mutex;
4043
THFree(self);
4144
}
4245

aten/src/TH/THRandom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct THGenerator {
1515
int seeded; /* = 0; */
1616
uint64_t next;
1717
uint64_t state[_MERSENNE_STATE_N]; /* the array for the state vector */
18-
std::mutex mutex; /* mutex for using this generator */
18+
std::mutex *mutex; /* mutex for using this generator */
1919
/********************************/
2020

2121
/* For normal distribution */

aten/src/TH/generic/THTensorRandom.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
void THTensor_(random)(THTensor *self, THGenerator *_generator)
66
{
7-
std::lock_guard<std::mutex> lock(_generator->mutex);
7+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
88
#if defined(TH_REAL_IS_BYTE)
99
TH_TENSOR_APPLY(real, self, *self_data = (uint8_t)(THRandom_random(_generator) % (UINT8_MAX + 1)););
1010
#elif defined(TH_REAL_IS_CHAR)
@@ -26,7 +26,7 @@ void THTensor_(random)(THTensor *self, THGenerator *_generator)
2626
}
2727

2828
void THTensor_(clampedRandom)(THTensor *self, THGenerator *_generator, int64_t min, int64_t max) {
29-
std::lock_guard<std::mutex> lock(_generator->mutex);
29+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
3030
THArgCheck(max > min, 2, "max must be greater than min, but got: min = %lld, max = %lld", min, max);
3131
uint64_t range = max - min;
3232
#if defined(TH_REAL_IS_LONG) || defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
@@ -45,25 +45,25 @@ void THTensor_(cappedRandom)(THTensor *self, THGenerator *_generator, int64_t ma
4545

4646
void THTensor_(geometric)(THTensor *self, THGenerator *_generator, double p)
4747
{
48-
std::lock_guard<std::mutex> lock(_generator->mutex);
48+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
4949
TH_TENSOR_APPLY(real, self, *self_data = (real)THRandom_geometric(_generator, p););
5050
}
5151

5252
void THTensor_(bernoulli)(THTensor *self, THGenerator *_generator, double p)
5353
{
54-
std::lock_guard<std::mutex> lock(_generator->mutex);
54+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
5555
TH_TENSOR_APPLY(real, self, *self_data = (real)THRandom_bernoulli(_generator, p););
5656
}
5757

5858
void THTensor_(bernoulli_FloatTensor)(THTensor *self, THGenerator *_generator, THFloatTensor *p)
5959
{
60-
std::lock_guard<std::mutex> lock(_generator->mutex);
60+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
6161
TH_TENSOR_APPLY2(real, self, float, p, *self_data = (real)THRandom_bernoulli(_generator, (double)*p_data););
6262
}
6363

6464
void THTensor_(bernoulli_DoubleTensor)(THTensor *self, THGenerator *_generator, THDoubleTensor *p)
6565
{
66-
std::lock_guard<std::mutex> lock(_generator->mutex);
66+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
6767
TH_TENSOR_APPLY2(real, self, double, p, *self_data = (real)THRandom_bernoulli(_generator, (double)*p_data););
6868
}
6969

@@ -86,7 +86,7 @@ void THTensor_(bernoulli_Tensor)(THTensor *self, THGenerator *_generator, THTens
8686

8787
void THTensor_(uniform)(THTensor *self, THGenerator *_generator, double a, double b)
8888
{
89-
std::lock_guard<std::mutex> lock(_generator->mutex);
89+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
9090
#if defined(TH_REAL_IS_FLOAT)
9191
TH_TENSOR_APPLY(real, self, *self_data =
9292
(real)THRandom_uniformFloat(_generator, (real)a, (real)b););
@@ -98,7 +98,7 @@ void THTensor_(uniform)(THTensor *self, THGenerator *_generator, double a, doubl
9898

9999
void THTensor_(normal)(THTensor *self, THGenerator *_generator, double mean, double stddev)
100100
{
101-
std::lock_guard<std::mutex> lock(_generator->mutex);
101+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
102102
const int64_t size = THTensor_(numel)(self);
103103
if (size >= 16 && THTensor_(isContiguous)(self)) {
104104
THVector_(normal_fill)(self->storage->data, size, _generator, mean, stddev);
@@ -132,13 +132,13 @@ void THTensor_(normal_means_stddevs)(THTensor *self, THGenerator *gen, THTensor
132132

133133
void THTensor_(exponential)(THTensor *self, THGenerator *_generator, double lambda)
134134
{
135-
std::lock_guard<std::mutex> lock(_generator->mutex);
135+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
136136
TH_TENSOR_APPLY(real, self, *self_data = (real)THRandom_exponential(_generator, lambda););
137137
}
138138

139139
void THTensor_(standard_gamma)(THTensor *self, THGenerator *_generator, THTensor *alpha)
140140
{
141-
std::lock_guard<std::mutex> lock(_generator->mutex);
141+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
142142
THTensor_(resizeAs)(self, alpha);
143143
TH_TENSOR_APPLY2(real, self, real, alpha, {
144144
const real sample = THRandom_standard_gamma(_generator, *alpha_data);
@@ -150,13 +150,13 @@ void THTensor_(standard_gamma)(THTensor *self, THGenerator *_generator, THTensor
150150

151151
void THTensor_(cauchy)(THTensor *self, THGenerator *_generator, double median, double sigma)
152152
{
153-
std::lock_guard<std::mutex> lock(_generator->mutex);
153+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
154154
TH_TENSOR_APPLY(real, self, *self_data = (real)THRandom_cauchy(_generator, median, sigma););
155155
}
156156

157157
void THTensor_(logNormal)(THTensor *self, THGenerator *_generator, double mean, double stdv)
158158
{
159-
std::lock_guard<std::mutex> lock(_generator->mutex);
159+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
160160
TH_TENSOR_APPLY(real, self, *self_data = (real)THRandom_logNormal(_generator, mean, stdv););
161161
}
162162

@@ -249,7 +249,7 @@ void THTensor_(multinomialAliasSetup)(THTensor *probs, THLongTensor *J, THTensor
249249
}
250250
void THTensor_(multinomialAliasDraw)(THLongTensor *self, THGenerator *_generator, THLongTensor *J, THTensor *q)
251251
{
252-
std::lock_guard<std::mutex> lock(_generator->mutex);
252+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
253253
int64_t K = THLongTensor_nElement(J);
254254
int64_t output_nelem = THLongTensor_nElement(self);
255255
int64_t i = 0, _mask=0;
@@ -273,7 +273,7 @@ void THTensor_(multinomialAliasDraw)(THLongTensor *self, THGenerator *_generator
273273
}
274274
void THTensor_(multinomial)(THLongTensor *self, THGenerator *_generator, THTensor *prob_dist, int n_sample, int with_replacement)
275275
{
276-
std::lock_guard<std::mutex> lock(_generator->mutex);
276+
std::lock_guard<std::mutex> lock(*(_generator->mutex));
277277
int64_t start_dim = THTensor_(nDimension)(prob_dist);
278278
int64_t n_dist;
279279
int64_t n_categories;
@@ -447,6 +447,7 @@ void THTensor_(getRNGState)(THGenerator *_generator, THTensor *self)
447447
THArgCheck(THTensor_(isContiguous)(self), 1, "RNG state needs to be contiguous");
448448
rng_state = (THGenerator *)THTensor_(data)(self);
449449
THGenerator_copy(rng_state, _generator);
450+
rng_state->mutex = NULL; // mutex should not be part of the generator state
450451
}
451452

452453
void THTensor_(setRNGState)(THGenerator *_generator, THTensor *self)

0 commit comments

Comments
 (0)