-
Notifications
You must be signed in to change notification settings - Fork 10
/
SimpleDispatch.hpp
38 lines (30 loc) · 968 Bytes
/
SimpleDispatch.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* Copyright (c) Teemu Suutari */
#ifndef SIMPLEDISPATCH_H
#define SIMPLEDISPATCH_H
#if defined(__APPLE__) || defined(HAS_LIBDISPATCH)
#include <dispatch/dispatch.h>
template<typename F,typename... Args>
void DispatchLoop(size_t iterations,size_t stride,F func,Args&&... args)
{
if (!iterations) return;
if (stride<1) stride=1;
dispatch_apply((iterations+stride-1)/stride,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^(size_t majorIt) {
majorIt*=stride;
for (size_t minorIt=0;minorIt<stride;minorIt++) {
size_t iter=majorIt+minorIt;
if (iter>=iterations) break;
func(iter,args...);
}
});
}
#else
#warning "Parallelization disabled!!!"
template<typename F,typename... Args>
void DispatchLoop(size_t iterations,size_t stride,F func,Args&&... args)
{
if (!iterations) return;
// Take a coffee, come back, wait, go to lunch, come back, wait, take a coffee...
for (size_t i=0;i<iterations;i++) func(i,args...);
}
#endif
#endif