graphtraversal Graph Traversal Problem Formulation graphtraversal_1GraphTraversalProblemFormulation Graph Representation graphtraversal_1GraphTraversalGraphRepresentation Static Traversal graphtraversal_1GraphTraversalStaticTraversal Dynamic Traversal graphtraversal_1GraphTraversalDynamicTraversal We study the graph traversal problem by visiting each vertex in parallel following their edge dependencies. Traversing a graph is a fundamental building block of many graph applications especially for large-scale graph analytics. Problem Formulation Given a directed acyclic graph (DAG), i.e., a graph that has no cycles, we would like to traverse each vertex in order without breaking dependency constraints defined by edges. The following figure shows a graph of six vertices and seven edges. Each vertex represents a particular task and each edge represents a task dependency between two tasks. Traversing the above graph in parallel, the maximum parallelism we can acquire is three. When Task1 finishes, we can run Task2, Task3, and Task4 in parallel. Graph Representation We define the data structure of our graph. The graph is represented by an array of nodes of the following structure: structNode{ std::stringname; size_tidx;//indexofthenodeinaarray boolvisited{false}; std::atomic<size_t>dependents{0};//numberofincomingedges std::vector<Node*>successors;//numberofoutgoingedges voidprecede(Node&n){ successors.emplace_back(&n); n.dependents++; } }; Based on the data structure, we randomly generate a DAG using ordered edges. std::unique_ptr<Node[]>make_dag(size_tnum_nodes,size_tmax_degree){ std::unique_ptr<Node[]>nodes(newNode[num_nodes]); //Makesurenodesareincleanstate for(size_ti=0;i<num_nodes;i++){ nodes[i].idx=i; nodes[i].name=std::to_string(i); } //CreateaDAGbyrandomlyinsertorderededges for(size_ti=0;i<num_nodes;i++){ size_tdegree{0}; for(size_tj=i+1;j<num_nodes&&degree<max_degree;j++){ if(std::rand()%2==1){ nodes[i].precede(nodes[j]); degree++; } } } returnnodes; } The function, make_dag, accepts two arguments, num_nodes and max_degree, to restrict the number of nodes in the graph and the maximum number of outgoing edges of every node. Static Traversal We create a taskflow to traverse the graph using static tasks (see Static Tasking). Each task does nothing but marks visited to true and subtracts dependents from one, both of which are used for validation after the graph is traversed. In practice, this computation may be replaced with a heavy function. tf::Taskflowtaskflow; tf::Executorexecutor; std::unique_ptr<Node[]>nodes=make_dag(100000,4); std::vector<tf::Task>tasks; //createthetraversaltaskforeachnode for(size_ti=0;i<num_nodes;++i){ tf::Tasktask=taskflow.emplace([v=&(nodes[i])](){ v->visited=true; for(size_tj=0;j<v->successors.size();++j){ v->successors[j]->dependents.fetch_sub(1); } }).name(nodes[i].name); tasks.push_back(task); } //createthedependencybetweennodesontopofthegraphstructure for(size_ti=0;i<num_nodes;++i){ for(size_tj=0;j<nodes[i].successors.size();++j){ tasks[i].precede(tasks[nodes[i].successors[j]->idx]); } } executor.run(taskflow).wait(); //afterthegraphistraversed,allnodesmustbevisitedwithnodependents for(size_ti=0;i<num_nodes;i++){ assert(nodes[i].visited); assert(nodes[i].dependents==0); } The code above has two parts to construct the parallel graph traversal. First, it iterates each node and constructs a traversal task for that node. Second, it iterates each outgoing edge of a node and creates a dependency between the node and the other end (successor) of that edge. The resulting taskflow structure is topologically equivalent to the given graph. With task parallelism, we flow computation naturally with the graph structure. The runtime autonomously distributes tasks across processor cores to obtain maximum task parallelism. You do not need to worry about details of scheduling. Dynamic Traversal We can traverse the graph dynamically using tf::Subflow (see Subflow Tasking). We start from the source nodes of zero incoming edges and recursively spawn subflows whenever the dependency of a node is meet. Since we are creating tasks from the execution context of another task, we need to store the task callable in advance. tf::Taskflowtaskflow; tf::Executorexecutor; //taskcallableoftraversinganodeusingsubflow std::function<void(Node*,tf::Subflow&)>traverse; traverse=[&](Node*n,tf::Subflow&subflow){ assert(!n->visited); n->visited=true; for(size_ti=0;i<n->successors.size();i++){ if(n->successors[i]->dependents.fetch_sub(1)==1){ subflow.emplace([s=n->successors[i],&traverse](tf::Subflow&subflow){ traverse(s,subflow); }).name(n->name); } } }; //createagraph std::unique_ptr<Node[]>nodes=make_dag(100000,4); //findthesourcenodes(noincomingedges) std::vector<Node*>src; for(size_ti=0;i<num_nodes;i++){ if(nodes[i].dependents==0){ src.emplace_back(&(nodes[i])); } } //createonlytasksforsourcenodes for(size_ti=0;i<src.size();i++){ taskflow.emplace([s=src[i],&traverse](tf::Subflow&subflow){ traverse(s,subflow); }).name(nodes[i].name); } executor.run(taskflow).wait(); //afterthegraphistraversed,allnodesmustbevisitedwithnodependents for(size_ti=0;i<num_nodes;i++){ assert(nodes[i].visited); assert(nodes[i].dependents==0); } A partial graph is shown as follows: In general, the dynamic version of graph traversal is slower than the static version due to the overhead incurred by spawning subflows. However, it may be useful for the situation where the graph structure is unknown at once but being partially explored during the traversal.