Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Loops.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_LOOPS_H
21 #define GALOIS_LOOPS_H
22 
23 #include "galois/config.h"
31 
32 namespace galois {
33 
35 // Foreach
37 
51 template <typename RangeFunc, typename FunctionTy, typename... Args>
52 void for_each(const RangeFunc& rangeMaker, FunctionTy&& fn,
53  const Args&... args) {
54  auto tpl = std::make_tuple(args...);
55  runtime::for_each_gen(rangeMaker(tpl), std::forward<FunctionTy>(fn), tpl);
56 }
57 
70 template <typename RangeFunc, typename FunctionTy, typename... Args>
71 void do_all(const RangeFunc& rangeMaker, FunctionTy&& fn, const Args&... args) {
72  auto tpl = std::make_tuple(args...);
73  runtime::do_all_gen(rangeMaker(tpl), std::forward<FunctionTy>(fn), tpl);
74 }
75 
85 template <typename FunctionTy, typename... Args>
86 void on_each(FunctionTy&& fn, const Args&... args) {
87  runtime::on_each_gen(std::forward<FunctionTy>(fn), std::make_tuple(args...));
88 }
89 
96 static inline void preAlloc(int num) {
97  static const bool DISABLE_PREALLOC = false;
98  if (DISABLE_PREALLOC) {
99  galois::gWarn("preAlloc disabled");
100 
101  } else {
103  }
104 }
105 
112 static inline void reportPageAlloc(const char* label) {
114 }
115 
133 template <typename Iter, typename Cmp, typename NhFunc, typename OpFunc>
134 void for_each_ordered(Iter b, Iter e, const Cmp& cmp, const NhFunc& nhFunc,
135  const OpFunc& fn, const char* loopname = 0) {
136  runtime::for_each_ordered_impl(b, e, cmp, nhFunc, fn, loopname);
137 }
138 
159 template <typename Iter, typename Cmp, typename NhFunc, typename OpFunc,
160  typename StableTest>
161 void for_each_ordered(Iter b, Iter e, const Cmp& cmp, const NhFunc& nhFunc,
162  const OpFunc& fn, const StableTest& stabilityTest,
163  const char* loopname = 0) {
164  runtime::for_each_ordered_impl(b, e, cmp, nhFunc, fn, stabilityTest,
165  loopname);
166 }
167 
173 struct DoAll {
174  template <typename RangeFunc, typename F, typename... Args>
175  void operator()(const RangeFunc& rangeMaker, const F& f,
176  Args&&... args) const {
177  galois::do_all(rangeMaker, f, std::forward<Args>(args)...);
178  }
179 };
180 
186 struct StdForEach {
187  template <typename RangeFunc, typename F, typename... Args>
188  void operator()(const RangeFunc& rangeMaker, const F& f,
189  Args&&... args) const {
190  auto range = rangeMaker(std::make_tuple(args...));
191  std::for_each(range.begin(), range.end(), f);
192  }
193 };
194 
195 struct ForEach {
196  template <typename RangeFunc, typename F, typename... Args>
197  void operator()(const RangeFunc& rangeMaker, const F& f,
198  Args&&... args) const {
199  galois::for_each(rangeMaker, f, std::forward<Args>(args)...);
200  }
201 };
202 
203 template <typename Q>
204 struct WhileQ {
205  Q m_q;
206 
207  WhileQ(Q&& q = Q()) : m_q(std::move(q)) {}
208 
209  template <typename RangeFunc, typename F, typename... Args>
210  void operator()(const RangeFunc& rangeMaker, const F& f, Args&&... args) {
211 
212  auto range = rangeMaker(std::make_tuple(args...));
213 
214  m_q.push(range.begin(), range.end());
215 
216  while (!m_q.empty()) {
217  auto val = m_q.pop();
218 
219  f(val, m_q);
220  }
221  }
222 };
223 
224 } // namespace galois
225 
226 #endif // GALOIS_LOOPS_H
Definition: Traits.h:197
Definition: Loops.h:195
void for_each_gen(const RangeTy &r, FunctionTy &&fn, const TupleTy &tpl)
Normalize arguments to for_each.
Definition: Executor_ForEach.h:466
Helper functor class to invoke galois::do_all on provided args Can be used to choose between galois::...
Definition: Loops.h:173
void on_each_gen(FunctionTy &&fn, const TupleTy &tpl)
Definition: Executor_OnEach.h:74
WhileQ(Q &&q=Q())
Definition: Loops.h:207
void operator()(const RangeFunc &rangeMaker, const F &f, Args &&...args)
Definition: Loops.h:210
void reportPageAlloc(const char *category)
Reports Galois system memory stats for all threads.
Definition: Statistics.cpp:128
void for_each_ordered(Iter b, Iter e, const Cmp &cmp, const NhFunc &nhFunc, const OpFunc &fn, const char *loopname=0)
Galois ordered set iterator for stable source algorithms.
Definition: Loops.h:134
void for_each_ordered_impl(Iter GALOIS_UNUSED(beg), Iter GALOIS_UNUSED(end), const Cmp &GALOIS_UNUSED(cmp), const NhFunc &GALOIS_UNUSED(nhFunc), const OpFunc &GALOIS_UNUSED(opFunc), const char *GALOIS_UNUSED(loopname))
Definition: Executor_Ordered.h:31
void do_all_gen(const R &range, F &&func, const ArgsTuple &argsTuple)
Definition: Executor_DoAll.h:530
void preAlloc_impl(unsigned num)
Memory management functionality.
Definition: PreAlloc.cpp:24
Helper functor to invoke std::for_each with the same interface as galois::do_all. ...
Definition: Loops.h:186
void do_all(const RangeFunc &rangeMaker, FunctionTy &&fn, const Args &...args)
Standard do-all loop.
Definition: Loops.h:71
Q m_q
Definition: Loops.h:205
void on_each(FunctionTy &&fn, const Args &...args)
Low-level parallel loop.
Definition: Loops.h:86
void operator()(const RangeFunc &rangeMaker, const F &f, Args &&...args) const
Definition: Loops.h:175
Definition: Loops.h:204
void for_each(const RangeFunc &rangeMaker, FunctionTy &&fn, const Args &...args)
Galois unordered set iterator.
Definition: Loops.h:52
void operator()(const RangeFunc &rangeMaker, const F &f, Args &&...args) const
Definition: Loops.h:188
void gWarn(Args &&...args)
Prints a warning string from a sequence of things.
Definition: gIO.h:63
void operator()(const RangeFunc &rangeMaker, const F &f, Args &&...args) const
Definition: Loops.h:197