Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
00034 #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
00035
00036 #include <omp.h>
00037 #include <parallel/settings.h>
00038 #include <parallel/base.h>
00039 #include <parallel/equally_split.h>
00040
00041 namespace __gnu_parallel
00042 {
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 template<typename _RAIter,
00062 typename _Op,
00063 typename _Fu,
00064 typename _Red,
00065 typename _Result>
00066 _Op
00067 __for_each_template_random_access_ed(_RAIter __begin, _RAIter __end,
00068 _Op __o, _Fu& __f, _Red __r,
00069 _Result __base, _Result& __output,
00070 typename std::iterator_traits<_RAIter>::difference_type __bound)
00071 {
00072 typedef std::iterator_traits<_RAIter> _TraitsType;
00073 typedef typename _TraitsType::difference_type _DifferenceType;
00074 const _DifferenceType __length = __end - __begin;
00075 _Result *__thread_results;
00076 bool* __constructed;
00077
00078 _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType>
00079 (__get_max_threads(), __length);
00080
00081 # pragma omp parallel num_threads(__num_threads)
00082 {
00083 # pragma omp single
00084 {
00085 __num_threads = omp_get_num_threads();
00086 __thread_results = static_cast<_Result*>
00087 (::operator new(__num_threads * sizeof(_Result)));
00088 __constructed = new bool[__num_threads];
00089 }
00090
00091 _ThreadIndex __iam = omp_get_thread_num();
00092
00093
00094 _Result* __reduct = static_cast<_Result*>
00095 (::operator new(sizeof(_Result)));
00096
00097 _DifferenceType
00098 __start = equally_split_point(__length, __num_threads, __iam),
00099 __stop = equally_split_point(__length, __num_threads, __iam + 1);
00100
00101 if (__start < __stop)
00102 {
00103 new(__reduct) _Result(__f(__o, __begin + __start));
00104 ++__start;
00105 __constructed[__iam] = true;
00106 }
00107 else
00108 __constructed[__iam] = false;
00109
00110 for (; __start < __stop; ++__start)
00111 *__reduct = __r(*__reduct, __f(__o, __begin + __start));
00112
00113 __thread_results[__iam] = *__reduct;
00114 }
00115
00116 for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
00117 if (__constructed[__i])
00118 __output = __r(__output, __thread_results[__i]);
00119
00120
00121
00122 __f._M_finish_iterator = __begin + __length;
00123
00124 delete[] __thread_results;
00125 delete[] __constructed;
00126
00127 return __o;
00128 }
00129
00130 }
00131
00132 #endif