From b5e221be890df49c46204b9c42fb5ef5d75e29a4 Mon Sep 17 00:00:00 2001 From: Richard Roberts Date: Fri, 26 Jul 2013 18:00:34 +0000 Subject: [PATCH] Another method of small node agglomeration --- gtsam/base/treeTraversal-inst.h | 59 ++++++++++++--------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/gtsam/base/treeTraversal-inst.h b/gtsam/base/treeTraversal-inst.h index e1053a736..e02f8c7ed 100644 --- a/gtsam/base/treeTraversal-inst.h +++ b/gtsam/base/treeTraversal-inst.h @@ -70,66 +70,51 @@ namespace gtsam { VISITOR_PRE& visitorPre; VISITOR_POST& visitorPost; int problemSizeThreshold; + bool makeNewTasks; PreOrderTask(const boost::shared_ptr& treeNode, const DATA& myData, - VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost, int problemSizeThreshold) : + VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost, int problemSizeThreshold, + bool makeNewTasks = true) : treeNode(treeNode), myData(myData), visitorPre(visitorPre), visitorPost(visitorPost), - problemSizeThreshold(problemSizeThreshold) {} + problemSizeThreshold(problemSizeThreshold), makeNewTasks(makeNewTasks) {} typedef ParallelTraversalNode ParallelTraversalNode; tbb::task* execute() { - // Shared data - int problemSize = 0; - - //std::cout << "New task: " << std::endl; - //BOOST_FOREACH(Key j, treeNode->keys) - // std::cout << j << " "; - //std::cout << std::endl; - // Process this node and its children - processNode(treeNode, myData, problemSize); + processNode(treeNode, myData); // Return NULL return NULL; } - void processNode(const boost::shared_ptr& node, DATA& myData, int& problemSize) + void processNode(const boost::shared_ptr& node, DATA& myData) { - tbb::task_list childTasks; - int nChildTasks = 0; - - // Increment problem size for this node - problemSize += node->problemSize(); - - // Visit children until problem size exceeds a threshold, then spawn a new task - BOOST_FOREACH(const boost::shared_ptr& child, node->children) + if(makeNewTasks) { - if(problemSize < problemSizeThreshold) - { - //std::cout << "problemSize = " << problemSize << std::endl; - //BOOST_FOREACH(Key j, child->keys) - // std::cout << j << " "; - //std::cout << std::endl; - // Process child sequentially (recursive call will increase problem size for children - DATA childData = visitorPre(child, myData); - processNode(child, childData, problemSize); - } - else + bool overThreshold = (node->problemSize() >= problemSizeThreshold); + + tbb::task_list childTasks; + BOOST_FOREACH(const boost::shared_ptr& child, node->children) { // Process child in a subtask childTasks.push_back(*new(allocate_child()) - PreOrderTask(child, visitorPre(child, myData), visitorPre, visitorPost, problemSizeThreshold)); - ++ nChildTasks; + PreOrderTask(child, visitorPre(child, myData), visitorPre, visitorPost, + problemSizeThreshold, overThreshold)); } - } - // If we have child tasks, start subtasks and wait for them to complete - if(nChildTasks > 0) { - set_ref_count(1 + nChildTasks); + // If we have child tasks, start subtasks and wait for them to complete + set_ref_count(1 + node->children.size()); spawn(childTasks); wait_for_all(); } + else + { + BOOST_FOREACH(const boost::shared_ptr& child, node->children) + { + processNode(child, visitorPre(child, myData)); + } + } // Run the post-order visitor (void) visitorPost(node, myData);