Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. template <class T> void map (std::deque<T> list, void (*f) (T)) {
  2. for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
  3. #ifdef PARALLEL_MAP
  4. ThreadPool::getIdle()->run<T>(f, *iter);
  5. #else
  6. f(*iter);
  7. #endif
  8. }
  9. }
  10. template <class T, class A1> void map (std::deque<T> list, void (*f) (T, A1), A1 arg1) {
  11. for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
  12. #ifdef PARALLEL_MAP
  13. ThreadPool::getIdle()->run<T, A1>(f, *iter, arg1);
  14. #else
  15. f(*iter, arg1);
  16. #endif
  17. }
  18. }
  19. template <class T, class A1, class A2> void map (std::deque<T> list, void (*f) (T, A1, A2), A1 arg1, A2 arg2) {
  20. for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
  21. #ifdef PARALLEL_MAP
  22. ThreadPool::getIdle()->run<T, A1, A2>(f, *iter, arg1, arg2);
  23. #else
  24. f(*iter, arg1, arg2);
  25. #endif
  26. }
  27. }
  28.  
  29. // [zero or more inputs]--> node -->[zero or more outputs]
  30. struct Node {
  31. Queue inbound[2];
  32. Node* outbound;
  33. void (*process) (Queue& inbound, Queue& outbound);
  34. };
  35. void processNode (Node* node, int index) {
  36. Queue& inbound = node->inbound[index];
  37. Queue& outbound = node->outbound->[1 - index];
  38. node->process(inbound, outbound);
  39. inbound.clear();
  40. }
  41.  
  42. #define PARALLEL_MAP
  43. void run (std::deque<Node> nodes) {
  44. int index = 0;
  45. while (true) {
  46. map<Node, int>(nodes, processNode, index);
  47. index = 1 - index;
  48. }
  49. }
  50.  
  51. /*
  52. 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].
  53. Node.process functions could be dynamically generated on startup, AOT compiled or JIT compiled.
  54.  
  55. [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.
  56. */
Add Comment
Please, Sign In to add comment