📄 38.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif;} a.at-term { font-style: italic; } </style> <title>Self Test</title> <meta name="Generator" content="ATutor"> <meta name="Keywords" content=""></head><body> <!--<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>-->
<style type="text/css">
@import url(base.css);
@import url(content.css);
</style>
<!--<title>Multilevel_Sec3.16</title>-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="common.js"></script>
<!--</head>
<body>-->
<!--<div id="content">
<div id="header">
</div>-->
<div id="main">
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Multilevel Parallel Programing</p></div>
</div>
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Question 1</p></div>
<div class="MultichoiceIdevice" id="id37">
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="libot_drag.js"></script>
<div class="iDevice emphasis1">
<img alt="IDevice Question Icon" class="iDevice_icon" src="icon_question.gif" />
<span class="iDeviceTitle">Question 1</span><br/>
<div class="iDevice_inner">
<div id="taquestion37_2" class="block" style="display:block">Write a program in which every even-numbered thread prints "Hello
world" along with its thread ID and the total number of threads.
</div>
<table>
<tbody>
<tr><td><input type="radio" name="option37_2" id="i37_5" onclick="getFeedback(0,1,'37_2','multi')"/></td><td>
<div id="taans37_5" class="block" style="display:block">Click here for solutions in C and Fortran
</div></td></tr>
</tbody>
</table>
<div id="sa0b37_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf37_5" class="block" style="display:block"><b><span class="success">Correct</span></b><br />
<p>
C solution:
</p>
<pre>
#include <stdio.h>
#include <omp.h>
int main () {
int myid, size;
myid = omp_get_thread_num();
size = omp_get_num_threads();
if (myid % 2 == 0)
printf("Hello world! Thread %d of %d\n", myid, size);
return 0;
}
</pre>
<hr />
Fortran solution:
<pre>
program hello
integer myrank, size
myrank = OMP_GET_THREAD_NUM()
size = OMP_GET_NUM_THREADS()
if (MOD(myrank,2).EQ.0) then
print *,'Hello world! Thread', myrank, ' of', size
endif
end program hello
</pre>
</div></div>
<br/></div>
</div>
</div>
</div>
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Question 2</p></div>
<div class="MultichoiceIdevice" id="id38">
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="libot_drag.js"></script>
<div class="iDevice emphasis1">
<img alt="IDevice Question Icon" class="iDevice_icon" src="icon_question.gif" />
<span class="iDeviceTitle">Question 2</span><br/>
<div class="iDevice_inner">
<div id="taquestion38_2" class="block" style="display:block">The following loop may be safely parallelized:
<pre>
do i=2, N
x(i) = x(i) + c*x(i-1)
end do
</pre>
</div>
<table>
<tbody>
<tr><td><input type="radio" name="option38_2" id="i38_5" onclick="getFeedback(0,2,'38_2','multi')"/></td><td>
<div id="taans38_5" class="block" style="display:block">True.
</div></td></tr>
<tr><td><input type="radio" name="option38_2" id="i38_104" onclick="getFeedback(1,2,'38_2','multi')"/></td><td>
<div id="taans38_104" class="block" style="display:block">False.
</div></td></tr>
</tbody>
</table>
<div id="sa0b38_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf38_5" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
No, in this case the loop should not be
parallelized. It contains a <i>dependency</i>,
meaning that the result of the loop
depends on the order in which the
loop iterations are processed. The
dependency in this case is signalled by
the occurrence of <code>x(i-1)</code> on the
right hand side of the loop expression;
which value is actually used here
depends on whether the (i-1)th
iteration has been processed before
the ith iteration. Since there is no
synchronization between threads, the
results of this loop would vary
unpredictably from run to run if
parallelized.
</div></div>
<div id="sa1b38_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf38_104" class="block" style="display:block"><b><span class="success">Correct</span></b><br />
Correct! The loop contains a dependency, indicating that the results
depend on the order in which the iterations are evaluated. In such
cases, the results generally vary unpredictably from run to run. A
situation in which the result of parallel code depends on the precise
order of execution between threads is known as a <i>race condition</i>.
</div></div>
<br/></div>
</div>
</div>
</div>
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Question 3</p></div>
<div class="MultichoiceIdevice" id="id39">
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="libot_drag.js"></script>
<div class="iDevice emphasis1">
<img alt="IDevice Question Icon" class="iDevice_icon" src="icon_question.gif" />
<span class="iDeviceTitle">Question 3</span><br/>
<div class="iDevice_inner">
<div id="taquestion39_2" class="block" style="display:block">Consider the following code fragment, which is intended to compute
the global maximun of an array:
<pre>
xmax = 0
C$OMP PARALLEL DO
do i=1,n
if (a(i).GT.xmax) then
C$OMP CRITICAL
xmax = a(i)
C$OMP END CRITICAL
endif
enddo
</pre>
Does this code work as intended?
</div>
<table>
<tbody>
<tr><td><input type="radio" name="option39_2" id="i39_5" onclick="getFeedback(0,2,'39_2','multi')"/></td><td>
<div id="taans39_5" class="block" style="display:block">Yes
</div></td></tr>
<tr><td><input type="radio" name="option39_2" id="i39_107" onclick="getFeedback(1,2,'39_2','multi')"/></td><td>
<div id="taans39_107" class="block" style="display:block">No
</div></td></tr>
</tbody>
</table>
<div id="sa0b39_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf39_5" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
Not quite. To see why it is incorrect, try considering a two iteration
loop (n=2) for which both <code>a(1)</code> and <code>a(2)</code> are greater than zero. The
value that ultimately winds up in <code>xmax</code> depends on which thread enters
the critical section last. Since there is no synchronization between
threads, this is impossible to predict; varying (and in most cases
wrong) results may be expected on subsequent runs of the code.
</div></div>
<div id="sa1b39_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf39_107" class="block" style="display:block"><b><span class="success">Correct</span></b><br />
Correct! The final value of <code>xmax</code> depends in general on the order in
which the different threads execute the code. To fix this, you could
either move the <code>C$OMP CRITICAL</code> directive up one line (i.e., just
before the <code>if</code> statement), or else compute the global maximum using a
reduction operation. The latter will in the majority of cases be
the more efficient solution.
</div></div>
<br/></div>
</div>
</div>
</div>
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Question 4</p></div>
<div class="MultichoiceIdevice" id="id40">
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="libot_drag.js"></script>
<div class="iDevice emphasis1">
<img alt="IDevice Question Icon" class="iDevice_icon" src="icon_question.gif" />
<span class="iDeviceTitle">Question 4</span><br/>
<div class="iDevice_inner">
<div id="taquestion40_2" class="block" style="display:block">Consider the following code fragment:
<pre>
i = 10
x = 42
C$OMP PARALLEL DO
C$OMP& SHARED(z), PRIVATE(i,x)
do i=1,1000
z(i) = x*a(i)
end do
x = i
</pre>
<p>
Inside the parallel do loop, what is the value of the variable x?
</p>
</div>
<table>
<tbody>
<tr><td><input type="radio" name="option40_2" id="i40_5" onclick="getFeedback(0,3,'40_2','multi')"/></td><td>
<div id="taans40_5" class="block" style="display:block">42
</div></td></tr>
<tr><td><input type="radio" name="option40_2" id="i40_110" onclick="getFeedback(1,3,'40_2','multi')"/></td><td>
<div id="taans40_110" class="block" style="display:block">Undefined
</div></td></tr>
<tr><td><input type="radio" name="option40_2" id="i40_113" onclick="getFeedback(2,3,'40_2','multi')"/></td><td>
<div id="taans40_113" class="block" style="display:block">0
</div></td></tr>
</tbody>
</table>
<div id="sa0b40_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf40_5" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
Not in general. Private variables <i>mask</i> variables with the same name in the serial part of the code. The private <code>x</code> associated with each thread is unrelated to the <code>x</code> in the serial portion. Some compilers <i>may</i>
initialize these private variables to have the value from the preceding
serial code, but this is not guaranteed and you should never assume it.
If you require this initialization, use the <code>firstprivate clause.</code>
</div></div>
<div id="sa1b40_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf40_110" class="block" style="display:block"><b><span class="success">Correct</span></b><br />
Correct! Private variables <i>mask</i> variables with the same name in the serial portion of the code. This code would fail in general, as <code>x</code>
is used before it is defined. Note that some compilers may initialize
the private variables with the value from the preceding serial code,
but this is not guaranteed and you should never assume it.
</div></div>
<div id="sa2b40_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf40_113" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
Not in general. In some cases a compiler may initialize private
variables to zero (or to the value in the preceding serial portion of
the code), but you should not depend on this. The value of <code>x</code> is <i>undefined</i> at the start of the parallel region.
</div></div>
<br/></div>
</div>
</div>
</div>
<div class="node">
<div id="nodeDecoration"><p id="nodeTitle">Question 5</p></div>
<div class="MultichoiceIdevice" id="id41">
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="libot_drag.js"></script>
<div class="iDevice emphasis1">
<img alt="IDevice Question Icon" class="iDevice_icon" src="icon_question.gif" />
<span class="iDeviceTitle">Question 5</span><br/>
<div class="iDevice_inner">
<div id="taquestion41_2" class="block" style="display:block">For the program in the previous question, what is the value of <code>x</code> after the last statement?
</div>
<table>
<tbody>
<tr><td><input type="radio" name="option41_2" id="i41_5" onclick="getFeedback(0,3,'41_2','multi')"/></td><td>
<div id="taans41_5" class="block" style="display:block">10
</div></td></tr>
<tr><td><input type="radio" name="option41_2" id="i41_116" onclick="getFeedback(1,3,'41_2','multi')"/></td><td>
<div id="taans41_116" class="block" style="display:block">1001
</div></td></tr>
<tr><td><input type="radio" name="option41_2" id="i41_119" onclick="getFeedback(2,3,'41_2','multi')"/></td><td>
<div id="taans41_119" class="block" style="display:block">Undefined
</div></td></tr>
</tbody>
</table>
<div id="sa0b41_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf41_5" class="block" style="display:block"><b><span class="success">Correct</span></b><br />
Correct. After the parallel region, the previous (serial) value of i is restored. Thus x has this value.
</div></div>
<div id="sa1b41_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf41_116" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
No, the private variable <code>i</code> masks the <code>i</code> in the serial portion of the code. Thus after the parallel region the previous serial value of <code>i</code> is restored. To keep the last value of <code>i</code> in the parallel loop past the end of the loop, you can declare <code>i</code> to be <code>lastprivate</code>. This causes the value of <code>i</code> corresponding to the last serial iteration of the loop to be saved in the subsequent serial region.
</div></div>
<div id="sa2b41_2" style="color: rgb(0, 51, 204);display: none;"><div id="taf41_119" class="block" style="display:block"><b><span class="alert">Incorrect</span></b><br />
No, in this case the value from the preceding serial portion of the code is restored; the private <code>i</code> masks the variable <code>i</code> in the serial code.
</div></div>
<br/></div>
</div>
</div>
</div>
</div>
<!--</div>
</body></html>--></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -