Skip to content

Commit aaa78bd

Browse files
authored
Merge pull request #205 from federicheddu/master
Add n_threads_hint overload to PARALLEL_FOR
2 parents 77591aa + 0262efe commit aaa78bd

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

include/cinolib/parallel_for.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ static void PARALLEL_FOR( uint beg,
6161
// estimate number of threads in the pool
6262
const static unsigned n_threads_hint = std::thread::hardware_concurrency();
6363
const static unsigned n_threads = (n_threads_hint==0u) ? 8u : n_threads_hint;
64+
// parallelize the loop
65+
PARALLEL_FOR(beg, end, serial_if_less_than, n_threads, func);
66+
}
67+
#else
68+
for(uint i=beg; i<end; ++i) func(i);
69+
#endif
70+
}
71+
72+
template<typename Func>
73+
CINO_INLINE
74+
static void PARALLEL_FOR( uint beg,
75+
uint end,
76+
const uint serial_if_less_than,
77+
const uint n_threads_hint,
78+
const Func & func)
79+
{
80+
#ifndef SERIALIZE_PARALLEL_FOR
81+
82+
uint n = end - beg + 1;
83+
84+
if(n<serial_if_less_than)
85+
{
86+
for(uint i=beg; i<end; ++i) func(i);
87+
}
88+
else
89+
{
90+
// estimate number of threads in the pool
91+
const static unsigned n_threads = (n_threads_hint==0u) ? 8u : n_threads_hint;
6492

6593
// split the full range into sub ranges of equal size
6694
uint slice = (uint)std::round(n/static_cast<double>(n_threads));

include/cinolib/parallel_for.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ static void PARALLEL_FOR( uint beg,
8585
uint end,
8686
const uint serial_if_less_than,
8787
const Func & func);
88+
89+
template<typename Func>
90+
CINO_INLINE
91+
static void PARALLEL_FOR( uint beg,
92+
uint end,
93+
const uint serial_if_less_than,
94+
const uint n_threads_hint,
95+
const Func & func);
8896
}
8997

9098
#ifndef CINO_STATIC_LIB

0 commit comments

Comments
 (0)