omp_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 #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_H
00034 #define _GLIBCXX_PARALLEL_OMP_LOOP_H 1
00035
00036 #include <omp.h>
00037
00038 #include <parallel/settings.h>
00039 #include <parallel/basic_iterator.h>
00040 #include <parallel/base.h>
00041
00042 namespace __gnu_parallel
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_omp_loop(_RAIter __begin, _RAIter __end,
00068 _Op __o, _Fu& __f, _Red __r,
00069 _Result __base,
00070 _Result& __output,
00071 typename std::iterator_traits<_RAIter>::difference_type __bound)
00072 {
00073 typedef typename std::iterator_traits<_RAIter>::difference_type
00074 _DifferenceType;
00075
00076 _DifferenceType __length = __end - __begin;
00077 _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType>
00078 (__get_max_threads(), __length);
00079
00080 _Result *__thread_results;
00081
00082 # pragma omp parallel num_threads(__num_threads)
00083 {
00084 # pragma omp single
00085 {
00086 __num_threads = omp_get_num_threads();
00087 __thread_results = new _Result[__num_threads];
00088
00089 for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
00090 __thread_results[__i] = _Result();
00091 }
00092
00093 _ThreadIndex __iam = omp_get_thread_num();
00094
00095 #pragma omp for schedule(dynamic, _Settings::get().workstealing_chunk_size)
00096 for (_DifferenceType __pos = 0; __pos < __length; ++__pos)
00097 __thread_results[__iam] = __r(__thread_results[__iam],
00098 __f(__o, __begin+__pos));
00099 }
00100
00101 for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
00102 __output = __r(__output, __thread_results[__i]);
00103
00104 delete [] __thread_results;
00105
00106
00107
00108 __f._M_finish_iterator = __begin + __length;
00109
00110 return __o;
00111 }
00112
00113 }
00114
00115 #endif