par_loop.h
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
00034
00035
00036
00037
00038
00039 #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
00040 #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
00041
00042 #include <omp.h>
00043 #include <parallel/settings.h>
00044 #include <parallel/base.h>
00045 #include <parallel/equally_split.h>
00046
00047 namespace __gnu_parallel
00048 {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 template<typename RandomAccessIterator,
00069 typename Op,
00070 typename Fu,
00071 typename Red,
00072 typename Result>
00073 Op
00074 for_each_template_random_access_ed(RandomAccessIterator begin,
00075 RandomAccessIterator end,
00076 Op o, Fu& f, Red r, Result base,
00077 Result& output,
00078 typename std::iterator_traits
00079 <RandomAccessIterator>::
00080 difference_type bound)
00081 {
00082 typedef std::iterator_traits<RandomAccessIterator> traits_type;
00083 typedef typename traits_type::difference_type difference_type;
00084 const difference_type length = end - begin;
00085 Result *thread_results;
00086 bool* constructed;
00087
00088 thread_index_t num_threads =
00089 __gnu_parallel::min<difference_type>(get_max_threads(), length);
00090
00091 # pragma omp parallel num_threads(num_threads)
00092 {
00093 # pragma omp single
00094 {
00095 num_threads = omp_get_num_threads();
00096 thread_results = static_cast<Result*>(
00097 ::operator new(num_threads * sizeof(Result)));
00098 constructed = new bool[num_threads];
00099 }
00100
00101 thread_index_t iam = omp_get_thread_num();
00102
00103
00104 Result* reduct = static_cast<Result*>(::operator new(sizeof(Result)));
00105
00106 difference_type
00107 start = equally_split_point(length, num_threads, iam),
00108 stop = equally_split_point(length, num_threads, iam + 1);
00109
00110 if (start < stop)
00111 {
00112 new(reduct) Result(f(o, begin + start));
00113 ++start;
00114 constructed[iam] = true;
00115 }
00116 else
00117 constructed[iam] = false;
00118
00119 for (; start < stop; ++start)
00120 *reduct = r(*reduct, f(o, begin + start));
00121
00122 thread_results[iam] = *reduct;
00123 }
00124
00125 for (thread_index_t i = 0; i < num_threads; ++i)
00126 if (constructed[i])
00127 output = r(output, thread_results[i]);
00128
00129
00130
00131 f.finish_iterator = begin + length;
00132
00133 delete[] thread_results;
00134 delete[] constructed;
00135
00136 return o;
00137 }
00138
00139 }
00140
00141 #endif