Skip to content

Instantly share code, notes, and snippets.

@dublindan
Created February 26, 2010 11:41
Show Gist options
  • Save dublindan/315655 to your computer and use it in GitHub Desktop.
Save dublindan/315655 to your computer and use it in GitHub Desktop.
template <class T> void map (std::deque<T> list, void (*f) (T)) {
for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
#ifdef PARALLEL_MAP
ThreadPool::getIdle()->run<T>(f, *iter);
#else
f(*iter);
#endif
}
}
template <class T, class A1> void map (std::deque<T> list, void (*f) (T, A1), A1 arg1) {
for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
#ifdef PARALLEL_MAP
ThreadPool::getIdle()->run<T, A1>(f, *iter, arg1);
#else
f(*iter, arg1);
#endif
}
}
template <class T, class A1, class A2> void map (std::deque<T> list, void (*f) (T, A1, A2), A1 arg1, A2 arg2) {
for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
#ifdef PARALLEL_MAP
ThreadPool::getIdle()->run<T, A1, A2>(f, *iter, arg1, arg2);
#else
f(*iter, arg1, arg2);
#endif
}
}
// [zero or more inputs]--> node -->[zero or more outputs]
struct Node {
Queue inbound[2];
Node* outbound;
void (*process) (Queue& inbound, Queue& outbound);
};
void processNode (Node* node, int index) {
Queue& inbound = node->inbound[index];
Queue& outbound = node->outbound->[1 - index];
node->process(inbound, outbound);
inbound.clear();
}
#define PARALLEL_MAP
void run (std::deque<Node> nodes) {
int index = 0;
while (true) {
map<Node, int>(nodes, processNode, index);
index = 1 - index;
}
}
/*
Node.process would be set to the code that executes for this node. It is a function which does not return anything and takes two Queue objects as arguments - the input queue and an output queue[1].
Node.process functions could be dynamically generated on startup, AOT compiled or JIT compiled.
[1] could simply be an index into a global list of queues, on which a set of global functions (read and write) could operate - in case JIT etc does not know how to access C++ objects.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment