00001
00023 #ifndef GALOIS_GALOIS_H
00024 #define GALOIS_GALOIS_H
00025
00026 #include "Galois/config.h"
00027 #include "Galois/WorkList/WorkList.h"
00028 #include "Galois/UserContext.h"
00029 #include "Galois/Threads.h"
00030 #include "Galois/Runtime/ParallelWork.h"
00031 #include "Galois/Runtime/DoAll.h"
00032 #include "Galois/Runtime/DeterministicWork.h"
00033 #include "Galois/Runtime/OrderedWork.h"
00034
00035 #ifdef GALOIS_USE_EXP
00036 #include "Galois/Runtime/ParallelWorkInline.h"
00037 #include "Galois/Runtime/ParaMeter.h"
00038 #endif
00039
00040 #include GALOIS_CXX11_STD_HEADER(utility)
00041 #include GALOIS_CXX11_STD_HEADER(type_traits)
00042 #include GALOIS_CXX11_STD_HEADER(tuple)
00043
00047 namespace Galois {
00048
00053 struct loopname {
00054 const char* n;
00055 loopname(const char* n = 0) :n(n) {}
00056 };
00057
00062 struct do_all_steal {
00063 bool b;
00064 do_all_steal(bool b = false) :b(b) {}
00065 };
00066
00067 struct wl_tag {};
00068
00072 template<typename WLTy>
00073 struct wl : public wl_tag {
00074 typedef WLTy WL;
00075 };
00076
00077
00078 namespace HIDDEN {
00079
00080 static constexpr unsigned GALOIS_DEFAULT_CHUNK_SIZE = 32;
00081 typedef WorkList::dChunkedFIFO<GALOIS_DEFAULT_CHUNK_SIZE> defaultWL;
00082
00083 template <typename T, typename S, int i = std::tuple_size<T>::value - 1>
00084 struct tuple_index {
00085 enum {
00086 value = std::is_base_of<S, typename std::tuple_element<i, T>::type>::value
00087 || std::is_same<S, typename std::tuple_element<i, T>::type>::value
00088 ? i : tuple_index<T, S, i-1>::value
00089 };
00090 };
00091
00092 template <typename T, typename S>
00093 struct tuple_index<T, S, -1> {
00094 enum { value = -1 };
00095 };
00096
00097 template<typename RangeTy, typename FunctionTy, typename Tuple>
00098 void for_each_gen(RangeTy r, FunctionTy fn, Tuple tpl) {
00099 typedef Tuple tupleType;
00100 static_assert(-1 == tuple_index<tupleType, char*>::value, "old loopname");
00101 static_assert(-1 == tuple_index<tupleType, char const*>::value, "old loopname");
00102 static_assert(-1 == tuple_index<tupleType, bool>::value, "old steal");
00103
00104
00105 constexpr unsigned iloopname = tuple_index<tupleType, loopname>::value;
00106 constexpr unsigned iwl = tuple_index<tupleType, wl_tag>::value;
00107 const char* ln = std::get<iloopname>(tpl).n;
00108 typedef typename std::tuple_element<iwl,tupleType>::type::WL WLTy;
00109 Runtime::for_each_impl<WLTy>(r, fn, ln);
00110 }
00111
00112 template<typename RangeTy, typename FunctionTy, typename Tuple>
00113 FunctionTy do_all_gen(RangeTy r, FunctionTy fn, Tuple tpl) {
00114 typedef Tuple tupleType;
00115 static_assert(-1 == tuple_index<tupleType, char*>::value, "old loopname");
00116 static_assert(-1 == tuple_index<tupleType, char const*>::value, "old loopname");
00117 static_assert(-1 == tuple_index<tupleType, bool>::value, "old steal");
00118
00119
00120 constexpr unsigned iloopname = tuple_index<tupleType, loopname>::value;
00121 constexpr unsigned isteal = tuple_index<tupleType, do_all_steal>::value;
00122 const char* ln = std::get<iloopname>(tpl).n;
00123 bool steal = std::get<isteal>(tpl).b;
00124 return Runtime::do_all_impl(r, fn, ln, steal);
00125 }
00126
00127 }
00128
00130
00132
00144 template<typename IterTy, typename FunctionTy, typename... Args>
00145 void for_each(IterTy b, IterTy e, FunctionTy fn, Args... args) {
00146 HIDDEN::for_each_gen(Runtime::makeStandardRange(b,e), fn, std::make_tuple(loopname(), wl<HIDDEN::defaultWL>(), args...));
00147 }
00148
00159 template<typename ItemTy, typename FunctionTy, typename... Args>
00160 void for_each(ItemTy i, FunctionTy fn, Args... args) {
00161 ItemTy iwl[1] = {i};
00162 HIDDEN::for_each_gen(Runtime::makeStandardRange(&iwl[0], &iwl[1]), fn, std::make_tuple(loopname(), wl<HIDDEN::defaultWL>(), args...));
00163 }
00164
00175 template<typename ConTy, typename FunctionTy, typename... Args>
00176 void for_each_local(ConTy& c, FunctionTy fn, Args... args) {
00177 HIDDEN::for_each_gen(Runtime::makeLocalRange(c), fn, std::make_tuple(loopname(), wl<HIDDEN::defaultWL>(), args...));
00178 }
00179
00190 template<typename IterTy,typename FunctionTy, typename... Args>
00191 FunctionTy do_all(const IterTy& b, const IterTy& e, FunctionTy fn, Args... args) {
00192 return HIDDEN::do_all_gen(Runtime::makeStandardRange(b, e), fn, std::make_tuple(loopname(), do_all_steal(), args...));
00193 }
00194
00204 template<typename ConTy,typename FunctionTy, typename... Args>
00205 FunctionTy do_all_local(ConTy& c, FunctionTy fn, Args... args) {
00206 return HIDDEN::do_all_gen(Runtime::makeLocalRange(c), fn, std::make_tuple(loopname(), do_all_steal(), args...));
00207 }
00208
00217 template<typename FunctionTy>
00218 static inline void on_each(FunctionTy fn, const char* loopname = 0) {
00219 Runtime::on_each_impl(fn, loopname);
00220 }
00221
00227 static inline void preAlloc(int num) {
00228 Runtime::preAlloc_impl(num);
00229 }
00230
00237 static inline void reportPageAlloc(const char* label) {
00238 Runtime::reportPageAlloc(label);
00239 }
00240
00256 template<typename Iter, typename Cmp, typename NhFunc, typename OpFunc>
00257 void for_each_ordered(Iter b, Iter e, const Cmp& cmp, const NhFunc& nhFunc, const OpFunc& fn, const char* loopname=0) {
00258 Runtime::for_each_ordered_impl(b, e, cmp, nhFunc, fn, loopname);
00259 }
00260
00279 template<typename Iter, typename Cmp, typename NhFunc, typename OpFunc, typename StableTest>
00280 void for_each_ordered(Iter b, Iter e, const Cmp& cmp, const NhFunc& nhFunc, const OpFunc& fn, const StableTest& stabilityTest, const char* loopname=0) {
00281 Runtime::for_each_ordered_impl(b, e, cmp, nhFunc, fn, stabilityTest, loopname);
00282 }
00283
00284 }
00285 #endif