39 #ifndef CGU_PARALLEL_H
40 #define CGU_PARALLEL_H
47 #include <type_traits>
62 virtual const char*
what()
const throw() {
return "ParallelError\n";}
65 #ifndef DOXYGEN_PARSING
72 namespace ParallelHelper2 {
74 template <
class ArgRefType,
class DiffType,
class Iterator>
78 DiffType* done_count) {
85 template <
class FType,
class ArgRefType,
class DestType>
86 void transform1_func(
const FType& func,
93 template <
class ArgRefType,
class DestType,
class DiffType,
class SourceIterator>
95 SourceIterator source_iter,
100 s_task(*source_iter, res);
105 *result = std::move(res);
110 template <
class FType,
class Arg1RefType,
111 class Arg2RefType,
class DestType>
112 void transform2_func(
const FType& func,
116 res = func(arg1, arg2);
120 template <
class Arg1RefType,
class Arg2RefType,
class DestType,
121 class DiffType,
class SourceIterator1,
class SourceIterator2>
123 SourceIterator1 source_iter1,
124 SourceIterator2 source_iter2,
125 Mutex* m, Cond* cond,
126 DiffType* done_count,
129 s_task(*source_iter1, *source_iter2, res);
134 *result = std::move(res);
139 template <
class DiffType>
140 void fail_func(Mutex* m, Cond* cond,
141 bool* error, DiffType* done_count) {
154 #endif // DOXYGEN_PARSING
251 template <
class Iterator,
class Func>
257 typedef typename std::iterator_traits<Iterator>::reference ArgRefType;
258 typedef typename std::iterator_traits<Iterator>::difference_type DiffType;
262 DiffType start_count = 0;
263 DiffType done_count = 0;
269 Cgu::Callback::lambda<ArgRefType>(std::forward<Func>(func))
279 for (; first != last; ++first, ++start_count) {
280 #ifdef CGU_USE_AUTO_PTR
281 typedef std::auto_ptr<const Callback::Callback> CbPtr;
283 typedef std::unique_ptr<const Callback::Callback> CbPtr;
298 tm.
add_task(std::move(task_cb), std::move(fail_cb));
302 while (start_count > done_count) cond.
wait(mutex);
438 template <
class SourceIterator,
class DestIterator,
class Func>
440 SourceIterator first,
445 if (first == last)
return;
447 typedef typename std::iterator_traits<SourceIterator>::reference ArgRefType;
448 typedef typename std::iterator_traits<SourceIterator>::difference_type DiffType;
449 typedef typename std::remove_const<typename std::remove_reference<Func>::type>::type FType;
454 typedef decltype(func(*first)) DestType;
458 DiffType start_count = 0;
459 DiffType done_count = 0;
465 std::unique_ptr<DestType[]> results(
new DestType[std::distance(first, last)]);
471 std::forward<Func>(func))
481 for (; first != last; ++first, ++start_count) {
482 #ifdef CGU_USE_AUTO_PTR
483 typedef std::auto_ptr<const Callback::Callback> CbPtr;
485 typedef std::unique_ptr<const Callback::Callback> CbPtr;
488 Cgu::Callback::lambda<>(std::bind(&ParallelHelper2::transform1_cb_func<ArgRefType, DestType, DiffType, SourceIterator>,
494 results.get() + start_count))
500 tm.
add_task(std::move(task_cb), std::move(fail_cb));
504 while (start_count > done_count) cond.
wait(mutex);
506 for (DiffType index = 0; index < start_count; ++dest, ++index) {
507 *dest = std::move(results[index]);
648 template <
class SourceIterator1,
class SourceIterator2,
class DestIterator,
class Func>
650 SourceIterator1 first1,
651 SourceIterator1 last1,
652 SourceIterator2 first2,
656 if (first1 == last1)
return;
658 typedef typename std::iterator_traits<SourceIterator1>::reference Arg1RefType;
659 typedef typename std::iterator_traits<SourceIterator1>::difference_type DiffType;
660 typedef typename std::iterator_traits<SourceIterator2>::reference Arg2RefType;
661 typedef typename std::remove_const<typename std::remove_reference<Func>::type>::type FType;
666 typedef decltype(func(*first1, *first2)) DestType;
670 DiffType start_count = 0;
671 DiffType done_count = 0;
677 std::unique_ptr<DestType[]> results(
new DestType[std::distance(first1, last1)]);
683 std::forward<Func>(func))
693 for (; first1 != last1; ++first1, ++first2, ++start_count) {
694 #ifdef CGU_USE_AUTO_PTR
695 typedef std::auto_ptr<const Callback::Callback> CbPtr;
697 typedef std::unique_ptr<const Callback::Callback> CbPtr;
700 Cgu::Callback::lambda<>(std::bind(&ParallelHelper2::transform2_cb_func<Arg1RefType, Arg2RefType, DestType, DiffType, SourceIterator1, SourceIterator2>,
707 results.get() + start_count))
713 tm.
add_task(std::move(task_cb), std::move(fail_cb));
717 while (start_count > done_count) cond.
wait(mutex);
719 for (DiffType index = 0; index < start_count; ++dest, ++index) {
720 *dest = std::move(results[index]);
839 template <
class Iterator,
class Func>
846 if (first == last || !max)
return first;
848 typedef typename std::iterator_traits<Iterator>::reference ArgRefType;
849 typedef typename std::iterator_traits<Iterator>::difference_type DiffType;
853 DiffType start_count = 0;
854 DiffType done_count = 0;
862 const DiffType local_max =
863 (max >= 0) ? max : std::numeric_limits<DiffType>::max();
868 Cgu::Callback::lambda<ArgRefType>(std::forward<Func>(func))
878 for (; first != last && start_count < local_max; ++first, ++start_count) {
879 #ifdef CGU_USE_AUTO_PTR
880 typedef std::auto_ptr<const Callback::Callback> CbPtr;
882 typedef std::unique_ptr<const Callback::Callback> CbPtr;
896 tm.
add_task(std::move(task_cb), std::move(fail_cb));
900 while (start_count > done_count) cond.
wait(mutex);
1039 template <
class SourceIterator,
class DestIterator,
class Func>
1040 std::pair<SourceIterator, DestIterator>
1042 SourceIterator first,
1043 SourceIterator last,
1048 if (first == last || !max)
return {first, dest};
1050 typedef typename std::iterator_traits<SourceIterator>::reference ArgRefType;
1051 typedef typename std::iterator_traits<SourceIterator>::difference_type DiffType;
1052 typedef typename std::remove_const<typename std::remove_reference<Func>::type>::type FType;
1057 typedef decltype(func(*first)) DestType;
1061 DiffType start_count = 0;
1062 DiffType done_count = 0;
1070 const DiffType local_max =
1071 (max >= 0) ? max : std::numeric_limits<DiffType>::max();
1076 std::unique_ptr<DestType[]> results(
new DestType[std::min(local_max,
1077 std::distance(first, last))]);
1083 std::forward<Func>(func))
1093 for (; first != last && start_count < local_max; ++first, ++start_count) {
1094 #ifdef CGU_USE_AUTO_PTR
1095 typedef std::auto_ptr<const Callback::Callback> CbPtr;
1097 typedef std::unique_ptr<const Callback::Callback> CbPtr;
1100 Cgu::Callback::lambda<>(std::bind(&ParallelHelper2::transform1_cb_func<ArgRefType, DestType, DiffType, SourceIterator>,
1106 results.get() + start_count))
1112 tm.
add_task(std::move(task_cb), std::move(fail_cb));
1116 while (start_count > done_count) cond.
wait(mutex);
1118 for (DiffType index = 0; index < start_count; ++dest, ++index) {
1119 *dest = std::move(results[index]);
1121 return {first, dest};
1266 template <
class SourceIterator1,
class SourceIterator2,
class DestIterator,
class Func>
1267 std::tuple<SourceIterator1, SourceIterator2, DestIterator>
1269 SourceIterator1 first1,
1270 SourceIterator1 last1,
1271 SourceIterator2 first2,
1276 if (first1 == last1 || !max)
return std::make_tuple(first1, first2, dest);
1278 typedef typename std::iterator_traits<SourceIterator1>::reference Arg1RefType;
1279 typedef typename std::iterator_traits<SourceIterator1>::difference_type DiffType;
1280 typedef typename std::iterator_traits<SourceIterator2>::reference Arg2RefType;
1281 typedef typename std::remove_const<typename std::remove_reference<Func>::type>::type FType;
1286 typedef decltype(func(*first1, *first2)) DestType;
1290 DiffType start_count = 0;
1291 DiffType done_count = 0;
1299 const DiffType local_max =
1300 (max >= 0) ? max : std::numeric_limits<DiffType>::max();
1305 std::unique_ptr<DestType[]> results(
new DestType[std::min(local_max,
1306 std::distance(first1, last1))]);
1312 std::forward<Func>(func))
1322 for (; first1 != last1 && start_count < local_max; ++first1, ++first2, ++start_count) {
1323 #ifdef CGU_USE_AUTO_PTR
1324 typedef std::auto_ptr<const Callback::Callback> CbPtr;
1326 typedef std::unique_ptr<const Callback::Callback> CbPtr;
1329 Cgu::Callback::lambda<>(std::bind(&ParallelHelper2::transform2_cb_func<Arg1RefType, Arg2RefType, DestType, DiffType, SourceIterator1, SourceIterator2>,
1336 results.get() + start_count))
1342 tm.
add_task(std::move(task_cb), std::move(fail_cb));
1346 while (start_count > done_count) cond.
wait(mutex);
1348 for (DiffType index = 0; index < start_count; ++dest, ++index) {
1349 *dest = std::move(results[index]);
1351 return std::make_tuple(first1, first2, dest);
1446 explicit IntIter(value_type val_ = 0): val(val_) {}
1631 return *iter1 == *iter2;
1651 return !(iter1 == iter2);
1671 return *iter1 < *iter2;
1691 return iter2 < iter1;
1711 return !(iter1 > iter2);
1731 return !(iter1 < iter2);
1751 return *iter1 - *iter2;
1816 #endif // CGU_PARALLEL_H
IntIter operator++(int)
Definition: parallel.h:1503
void add_task(const Callback::Callback *task)
Definition: task_manager.h:743
A thread-pool class for managing tasks in multi-threaded programs.
Definition: task_manager.h:401
IntIter operator+(IntIter iter, IntIter::difference_type n)
Definition: parallel.h:1770
CallbackArg< FreeArgs...> * make_ref(T &t, void(T::*func)(FreeArgs...))
Definition: callback.h:1695
CallbackArg< FreeArgs...> * make(T &t, void(T::*func)(FreeArgs...))
Definition: callback.h:1659
bool operator>(IntIter iter1, IntIter iter2)
Definition: parallel.h:1690
A wrapper class for pthread condition variables.
Definition: mutex.h:449
IntIter::difference_type operator-(IntIter iter1, IntIter iter2)
Definition: parallel.h:1750
This file provides classes for type erasure.
bool operator>=(IntIter iter1, IntIter iter2)
Definition: parallel.h:1730
bool operator==(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:600
virtual const char * what() const
Definition: parallel.h:62
std::pair< SourceIterator, DestIterator > parallel_transform_partial(TaskManager &tm, SourceIterator first, SourceIterator last, DestIterator dest, int max, Func &&func)
Definition: parallel.h:1041
int difference_type
Definition: parallel.h:1435
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:207
Definition: parallel.h:61
bool operator!=(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:613
int value_type
Definition: parallel.h:1432
bool operator<=(IntIter iter1, IntIter iter2)
Definition: parallel.h:1710
IntIter & operator-=(difference_type n)
Definition: parallel.h:1573
A wrapper class for pthread mutexes.
Definition: mutex.h:117
IntIter(value_type val_=0)
Definition: parallel.h:1446
void parallel_for_each(TaskManager &tm, Iterator first, Iterator last, Func &&func)
Definition: parallel.h:252
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Definition: application.h:44
reference operator[](difference_type n) const
Definition: parallel.h:1590
Iterator parallel_for_each_partial(TaskManager &tm, Iterator first, Iterator last, int max, Func &&func)
Definition: parallel.h:840
void parallel_transform(TaskManager &tm, SourceIterator first, SourceIterator last, DestIterator dest, Func &&func)
Definition: parallel.h:439
Functor class holding a Callback::CallbackArg object, with thread-safe reference count.
Definition: callback.h:726
int reference
Definition: parallel.h:1433
reference operator*() const
Definition: parallel.h:1607
IntIter & operator=(const IntIter &)=default
An iterator class providing a lazy integer range over a virtual container.
Definition: parallel.h:1430
IntIter & operator++()
Definition: parallel.h:1486
IntIter operator--(int)
Definition: parallel.h:1537
IntIter & operator+=(difference_type n)
Definition: parallel.h:1555
std::random_access_iterator_tag iterator_category
Definition: parallel.h:1436
IntIter & operator--()
Definition: parallel.h:1520
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
void pointer
Definition: parallel.h:1434
bool operator<(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:632
int wait(Mutex &mutex)
Definition: mutex.h:508