File: Task-Based_Programming.htm

package info (click to toggle)
tbb 4.2~20140122-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,492 kB
  • ctags: 21,278
  • sloc: cpp: 92,813; ansic: 9,775; asm: 1,070; makefile: 1,057; sh: 351; java: 226; objc: 98; pascal: 71; xml: 41
file content (156 lines) | stat: -rwxr-xr-x 6,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Task-Based Programming">
<meta name="DC.subject" content="Task-Based Programming">
<meta name="keywords" content="Task-Based Programming">
<meta name="DC.Relation" scheme="URI" content="../tbb_userguide/The_Task_Scheduler.htm">
<meta name="DC.Relation" scheme="URI" content="How_Task_Scheduling_Works.htm#tutorial_How_Task_Scheduling_Works">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="tutorial_Task-Based_Programming">
<link rel="stylesheet" type="text/css" href="../intel_css_styles.css">
<title>Task-Based Programming</title>
<xml>
<MSHelp:Attr Name="DocSet" Value="Intel"></MSHelp:Attr>
<MSHelp:Attr Name="Locale" Value="kbEnglish"></MSHelp:Attr>
<MSHelp:Attr Name="TopicType" Value="kbReference"></MSHelp:Attr>
</xml>
</head>
<body id="tutorial_Task-Based_Programming">
 <!-- ==============(Start:NavScript)================= -->
 <script src="..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
 <script language="JavaScript1.2" type="text/javascript">WriteNavLink(1);</script>
 <!-- ==============(End:NavScript)================= -->
<a name="tutorial_Task-Based_Programming"><!-- --></a>

 
  <h1 class="topictitle1">Task-Based Programming</h1>
 
  
  <div>
	 <p>When striving for performance, programming in terms of threads can be a
		poor way to do multithreaded programming. It is much better to formulate your
		program in terms of 
		<em>logical tasks</em>, not threads, for several reasons.
	 </p>
 
	 <ul type="disc">
		<li>
		  <p>Matching parallelism to available resources
		  </p>

		</li>

		<li>
		  <p>Faster task startup and shutdown
		  </p>

		</li>

		<li>
		  <p>More efficient evaluation order
		  </p>

		</li>

		<li>
		  <p>Improved load balancing
		  </p>

		</li>

		<li>
		  <p>Higher–level thinking
		  </p>

		</li>

	 </ul>

	 <p>The following paragraphs explain these points in detail.
	 </p>

	 <p>The threads you create with a threading package are 
		<em>logical</em> threads, which map onto the 
		<em>physical threads</em> of the hardware. For computations that do not
		wait on external devices, highest efficiency usually occurs when there is
		exactly one running logical thread per physical thread. Otherwise, there can be
		inefficiencies from the mismatch<em>. Undersubscription</em> occurs when there
		are not enough running logical threads to keep the physical threads working. 
		<em>Oversubscription</em> occurs when there are more running logical
		threads than physical threads. Oversubscription usually leads to 
		<em>time sliced</em> execution of logical threads, which incurs overheads
		as discussed in Appendix A, 
		<em>Costs of Time Slicing</em>. The scheduler tries to avoid
		oversubscription, by having one logical thread per physical thread, and mapping
		tasks to logical threads, in a way that tolerates interference by other threads
		from the same or other processes.
	 </p>

	 <p>The key advantage of tasks versus logical threads is that tasks are much
		
		<em>lighter weight</em> than logical threads. On Linux systems, starting
		and terminating a task is about 18 times faster than starting and terminating a
		thread. On Windows systems, the ratio is more than 100. This is because a
		thread has its own copy of a lot of resources, such as register state and a
		stack. On Linux, a thread even has its own process id. A task in Intel&reg;
		Threading Building Blocks, in contrast, is typically a small routine, and also,
		cannot be preempted at the task level (though its logical thread can be
		preempted).
	 </p>

	 <p>Tasks in Intel&reg; Threading Building Blocks are efficient too because 
		<em>the scheduler is unfair.</em> Thread schedulers typically distribute
		time slices in a round-robin fashion. This distribution is called "fair",
		because each logical thread gets its fair share of time. Thread schedulers are
		typically fair because it is the safest strategy to undertake without
		understanding the higher-level organization of a program. In task-based
		programming, the task scheduler does have some higher-level information, and so
		can sacrifice fairness for efficiency. Indeed, it often delays starting a task
		until it can make useful progress. 
	 </p>

	 <p>The scheduler does 
		<em>load balancing.</em> In addition to using the right number of threads,
		it is important to distribute work evenly across those threads. As long as you
		break your program into enough small tasks, the scheduler usually does a good
		job of assigning tasks to threads to balance load. With thread-based
		programming, you are often stuck dealing with load-balancing
		yourself, which can be tricky to get right. 
	 </p>

	 <div class="Note"><h3 class="NoteTipHead">
					Tip</h3>
		<p>Design your programs to try to create many more tasks than there are
		  threads, and let the task scheduler choose the mapping from tasks to threads.
		</p>

	 </div>
	 <p>Finally, the main advantage of using tasks instead of threads is that
		they let you think at a higher, task-based, level. With thread-based
		programming, you are forced to think at the low level of physical threads to
		get good efficiency, because you have one logical thread per physical thread to
		avoid undersubscription or oversubscription. You also have to deal with the
		relatively coarse grain of threads. With tasks, you can concentrate on the
		logical dependences between tasks, and leave the efficient scheduling to the
		scheduler.
	 </p>

  </div>

  
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong>&nbsp;<a href="../tbb_userguide/The_Task_Scheduler.htm">The Task Scheduler</a></div>
</div>
<div class="See Also">
<h2>See Also</h2>
<div class="linklist">
<div><a href="How_Task_Scheduling_Works.htm#tutorial_How_Task_Scheduling_Works">How Task Scheduling Works
		  </a></div></div>
</div>

</body>
</html>