Skip to content

Commit 649bb7a

Browse files
committed
core: parallel_for_(): update RNG state of the main thread
1 parent ebdd741 commit 649bb7a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

modules/core/include/opencv2/core.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,8 @@ class CV_EXPORTS RNG
28342834
double gaussian(double sigma);
28352835

28362836
uint64 state;
2837+
2838+
bool operator ==(const RNG& other) const;
28372839
};
28382840

28392841
/** @brief Mersenne Twister random number generator

modules/core/include/opencv2/core/operations.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next(
349349
inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
350350
inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
351351

352+
inline bool RNG::operator ==(const RNG& other) const { return state == other.state; }
353+
352354
inline unsigned RNG::next()
353355
{
354356
state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);

modules/core/src/parallel.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ namespace
166166
class ParallelLoopBodyWrapper : public cv::ParallelLoopBody
167167
{
168168
public:
169-
ParallelLoopBodyWrapper(const cv::ParallelLoopBody& _body, const cv::Range& _r, double _nstripes)
169+
ParallelLoopBodyWrapper(const cv::ParallelLoopBody& _body, const cv::Range& _r, double _nstripes) :
170+
is_rng_used(false)
170171
{
171172

172173
body = &_body;
@@ -181,13 +182,23 @@ namespace
181182
pThreadRoot = cv::instr::getInstrumentTLSStruct().pCurrentNode;
182183
#endif
183184
}
184-
#ifdef ENABLE_INSTRUMENTATION
185185
~ParallelLoopBodyWrapper()
186186
{
187+
#ifdef ENABLE_INSTRUMENTATION
187188
for(size_t i = 0; i < pThreadRoot->m_childs.size(); i++)
188189
SyncNodes(pThreadRoot->m_childs[i]);
189-
}
190190
#endif
191+
if (is_rng_used)
192+
{
193+
// Some parallel backends execute nested jobs in the main thread,
194+
// so we need to restore initial RNG state here.
195+
cv::theRNG() = rng;
196+
// We can't properly update RNG state based on RNG usage in worker threads,
197+
// so lets just change main thread RNG state to the next value.
198+
// Note: this behaviour is not equal to single-threaded mode.
199+
cv::theRNG().next();
200+
}
201+
}
191202
void operator()(const cv::Range& sr) const
192203
{
193204
#ifdef ENABLE_INSTRUMENTATION
@@ -207,6 +218,9 @@ namespace
207218
r.end = sr.end >= nstripes ? wholeRange.end : (int)(wholeRange.start +
208219
((uint64)sr.end*(wholeRange.end - wholeRange.start) + nstripes/2)/nstripes);
209220
(*body)(r);
221+
222+
if (!is_rng_used && !(cv::theRNG() == rng))
223+
is_rng_used = true;
210224
}
211225
cv::Range stripeRange() const { return cv::Range(0, nstripes); }
212226

@@ -215,6 +229,7 @@ namespace
215229
cv::Range wholeRange;
216230
int nstripes;
217231
cv::RNG rng;
232+
mutable bool is_rng_used;
218233
#ifdef ENABLE_INSTRUMENTATION
219234
cv::instr::InstrNode *pThreadRoot;
220235
#endif

0 commit comments

Comments
 (0)