/Src/SimulatorQt/Util/qt/Win32/include/Qt/qtconcurrentmapkernel.h

https://github.com/alon/bhuman2009fork · C Header · 273 lines · 184 code · 42 blank · 47 comment · 5 complexity · 2d410b76e0809e074ef9d06beae168cb MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
  4. ** Contact: Qt Software Information (qt-info@nokia.com)
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial Usage
  10. ** Licensees holding valid Qt Commercial licenses may use this file in
  11. ** accordance with the Qt Commercial License Agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Nokia.
  14. **
  15. ** GNU Lesser General Public License Usage
  16. ** Alternatively, this file may be used under the terms of the GNU Lesser
  17. ** General Public License version 2.1 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.LGPL included in the
  19. ** packaging of this file. Please review the following information to
  20. ** ensure the GNU Lesser General Public License version 2.1 requirements
  21. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  22. **
  23. ** In addition, as a special exception, Nokia gives you certain
  24. ** additional rights. These rights are described in the Nokia Qt LGPL
  25. ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
  26. ** package.
  27. **
  28. ** GNU General Public License Usage
  29. ** Alternatively, this file may be used under the terms of the GNU
  30. ** General Public License version 3.0 as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL included in the
  32. ** packaging of this file. Please review the following information to
  33. ** ensure the GNU General Public License version 3.0 requirements will be
  34. ** met: http://www.gnu.org/copyleft/gpl.html.
  35. **
  36. ** If you are unsure which license is appropriate for your use, please
  37. ** contact the sales department at qt-sales@nokia.com.
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef QTCONCURRENT_MAPKERNEL_H
  42. #define QTCONCURRENT_MAPKERNEL_H
  43. #include <QtCore/qglobal.h>
  44. #ifndef QT_NO_CONCURRENT
  45. #include <QtCore/qtconcurrentiteratekernel.h>
  46. #include <QtCore/qtconcurrentreducekernel.h>
  47. QT_BEGIN_HEADER
  48. QT_BEGIN_NAMESPACE
  49. QT_MODULE(Core)
  50. #ifndef qdoc
  51. namespace QtConcurrent {
  52. // map kernel, works with both parallel-for and parallel-while
  53. template <typename Iterator, typename MapFunctor>
  54. class MapKernel : public IterateKernel<Iterator, void>
  55. {
  56. MapFunctor map;
  57. public:
  58. typedef void ReturnType;
  59. MapKernel(Iterator begin, Iterator end, MapFunctor _map)
  60. : IterateKernel<Iterator, void>(begin, end), map(_map)
  61. { }
  62. bool runIteration(Iterator it, int, void *)
  63. {
  64. map(*it);
  65. return false;
  66. }
  67. bool runIterations(Iterator sequenceBeginIterator, int beginIndex, int endIndex, void *)
  68. {
  69. Iterator it = sequenceBeginIterator;
  70. advance(it, beginIndex);
  71. for (int i = beginIndex; i < endIndex; ++i) {
  72. runIteration(it, i, 0);
  73. advance(it, 1);
  74. }
  75. return false;
  76. }
  77. };
  78. template <typename ReducedResultType,
  79. typename Iterator,
  80. typename MapFunctor,
  81. typename ReduceFunctor,
  82. typename Reducer = ReduceKernel<ReduceFunctor,
  83. ReducedResultType,
  84. typename MapFunctor::result_type> >
  85. class MappedReducedKernel : public IterateKernel<Iterator, ReducedResultType>
  86. {
  87. ReducedResultType reducedResult;
  88. MapFunctor map;
  89. ReduceFunctor reduce;
  90. Reducer reducer;
  91. public:
  92. typedef ReducedResultType ReturnType;
  93. MappedReducedKernel(Iterator begin, Iterator end, MapFunctor _map, ReduceFunctor _reduce, ReduceOptions reduceOptions)
  94. : IterateKernel<Iterator, ReducedResultType>(begin, end), reducedResult(), map(_map), reduce(_reduce), reducer(reduceOptions)
  95. { }
  96. MappedReducedKernel(ReducedResultType initialValue,
  97. MapFunctor _map,
  98. ReduceFunctor _reduce)
  99. : reducedResult(initialValue), map(_map), reduce(_reduce)
  100. { }
  101. bool runIteration(Iterator it, int index, ReducedResultType *)
  102. {
  103. IntermediateResults<typename MapFunctor::result_type> results;
  104. results.begin = index;
  105. results.end = index + 1;
  106. results.vector.append(map(*it));
  107. reducer.runReduce(reduce, reducedResult, results);
  108. return false;
  109. }
  110. bool runIterations(Iterator sequenceBeginIterator, int begin, int end, ReducedResultType *)
  111. {
  112. IntermediateResults<typename MapFunctor::result_type> results;
  113. results.begin = begin;
  114. results.end = end;
  115. results.vector.reserve(end - begin);
  116. Iterator it = sequenceBeginIterator;
  117. advance(it, begin);
  118. for (int i = begin; i < end; ++i) {
  119. results.vector.append(map(*(it)));
  120. advance(it, 1);
  121. }
  122. reducer.runReduce(reduce, reducedResult, results);
  123. return false;
  124. }
  125. void finish()
  126. {
  127. reducer.finish(reduce, reducedResult);
  128. }
  129. bool shouldThrottleThread()
  130. {
  131. return IterateKernel<Iterator, ReducedResultType>::shouldThrottleThread() || reducer.shouldThrottle();
  132. }
  133. bool shouldStartThread()
  134. {
  135. return IterateKernel<Iterator, ReducedResultType>::shouldStartThread() && reducer.shouldStartThread();
  136. }
  137. typedef ReducedResultType ResultType;
  138. ReducedResultType *result()
  139. {
  140. return &reducedResult;
  141. }
  142. };
  143. template <typename Iterator, typename MapFunctor>
  144. class MappedEachKernel : public IterateKernel<Iterator, typename MapFunctor::result_type>
  145. {
  146. MapFunctor map;
  147. typedef typename MapFunctor::result_type T;
  148. public:
  149. typedef T ReturnType;
  150. typedef T ResultType;
  151. MappedEachKernel(Iterator begin, Iterator end, MapFunctor _map)
  152. : IterateKernel<Iterator, T>(begin, end), map(_map) { }
  153. bool runIteration(Iterator it, int, T *result)
  154. {
  155. *result = map(*it);
  156. return true;
  157. }
  158. bool runIterations(Iterator sequenceBeginIterator, int begin, int end, T *results)
  159. {
  160. Iterator it = sequenceBeginIterator;
  161. advance(it, begin);
  162. for (int i = begin; i < end; ++i) {
  163. runIteration(it, i, results + (i - begin));
  164. advance(it, 1);
  165. }
  166. return true;
  167. }
  168. };
  169. template <typename Iterator, typename Functor>
  170. inline ThreadEngineStarter<void> startMap(Iterator begin, Iterator end, Functor functor)
  171. {
  172. return startThreadEngine(new MapKernel<Iterator, Functor>(begin, end, functor));
  173. }
  174. template <typename T, typename Iterator, typename Functor>
  175. inline ThreadEngineStarter<T> startMapped(Iterator begin, Iterator end, Functor functor)
  176. {
  177. return startThreadEngine(new MappedEachKernel<Iterator, Functor>(begin, end, functor));
  178. }
  179. /*
  180. The SequnceHolder class is used to hold a reference to the
  181. sequence we are working on.
  182. */
  183. template <typename Sequence, typename Base, typename Functor>
  184. struct SequenceHolder1 : public Base
  185. {
  186. SequenceHolder1(const Sequence &_sequence, Functor functor)
  187. : Base(_sequence.begin(), _sequence.end(), functor), sequence(_sequence)
  188. { }
  189. Sequence sequence;
  190. void finish()
  191. {
  192. Base::finish();
  193. // Clear the sequence to make sure all temporaries are destroyed
  194. // before finished is signaled.
  195. sequence = Sequence();
  196. }
  197. };
  198. template <typename T, typename Sequence, typename Functor>
  199. inline ThreadEngineStarter<T> startMapped(const Sequence &sequence, Functor functor)
  200. {
  201. typedef SequenceHolder1<Sequence,
  202. MappedEachKernel<typename Sequence::const_iterator , Functor>, Functor>
  203. SequenceHolderType;
  204. return startThreadEngine(new SequenceHolderType(sequence, functor));
  205. }
  206. template <typename IntermediateType, typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
  207. inline ThreadEngineStarter<ResultType> startMappedReduced(const Sequence & sequence,
  208. MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
  209. ReduceOptions options)
  210. {
  211. typedef typename Sequence::const_iterator Iterator;
  212. typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
  213. typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
  214. typedef SequenceHolder2<Sequence, MappedReduceType, MapFunctor, ReduceFunctor> SequenceHolderType;
  215. return startThreadEngine(new SequenceHolderType(sequence, mapFunctor, reduceFunctor, options));
  216. }
  217. template <typename IntermediateType, typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
  218. inline ThreadEngineStarter<ResultType> startMappedReduced(Iterator begin, Iterator end,
  219. MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
  220. ReduceOptions options)
  221. {
  222. typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
  223. typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
  224. return startThreadEngine(new MappedReduceType(begin, end, mapFunctor, reduceFunctor, options));
  225. }
  226. } // namespace QtConcurrent
  227. #endif //qdoc
  228. QT_END_NAMESPACE
  229. QT_END_HEADER
  230. #endif // QT_NO_CONCURRENT
  231. #endif