3 Parallelization is used in iMSTK in various locations. Primarily one will find loop parallelization. TBB is used as the parallelization backend which maintains a thread pool and does its own scheduling.
5 iMSTK haptics also, by default, runs on a separate STL thread.
9 iMSTK uses loop based parallelism throughout code. For this use parallelFor.
12 #include "imstkParallelUtils.h"
16 ParallelUtils::parallelFor(100000, [&](const size_T i)
24 Here something is done 100000 times in parallel. The optimal number of threads is used.
28 The `TaskGraph` in iMSTK contains a source and sink. A set of `TaskNode`'s, and set of directional edges. The edges express the succeeding and preceding tasks. A task may not execute until all its preceding tasks are complete. See examples here.
30 Task graphs are used internally in iMSTK as well. Every DynamicalModel (PBD, SPH, FEM) contains a TaskGraph. Sometimes it's as simple as a few tasks in sequence. Sometimes it's more complex. This is especially useful for inserting intermediate tasks and defining custom physics pipelines.
32 Here is a simple task graph construction:
37 // Create and add the nodes
38 std::shared_ptr<TaskNode> task1Node = graph.addFunction("Task1Name",
43 std::shared_ptr<TaskNode> task2Node = graph.addFunction("Task2Name",
49 // Define the edges, add and mult steps will be done in parallel
50 graph.addEdge(graph.getSource(), task1Node );
51 graph.addEdge(graph.getSource(), task2Node );
52 graph.addEdge(task1Node, graph.getSink());
53 graph.addEdge(task2Node, graph.getSink());
56 This defines the following graph where Task1Name and Task2Name may be done in parallel.
59 <img src="../media/parallelism.png" alt="A simple TaskGraph"/>
62 There are also numerous convenience functions in TaskGraph's. One may insert a node after or before a existing node in the graph (automatically adding edges).
65 auto task = std::make_shared<TaskNode>("MyTask",
70 graph->insertAfter(task1Node, task);
73 There are also functions to check for cyclic dependencies, remove non-functional nodes, remove redundant edges (transient reduction), and topological sort.
75 Additionally, if TaskNode timing is enabled, a stopwatch may be used for every node and times reported. In many iMSTK examples, if you press `P` twice, you may see the Scene's task graph node timings making bottlenecks very easy to find.
78 <img src="../media/profiler.png" alt="A simple TaskGraph"/>