📄 node59.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>5.8 Mapping</TITLE>
</HEAD>
<BODY>
<meta name="description" value="5.8 Mapping">
<meta name="keywords" value="book">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR><a href="msgs0.htm#2" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/tppmsgs/msgs0.htm#2"><img ALIGN=MIDDLE src="asm_color_tiny.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/asm_color_tiny.gif" alt="[DBPP]"></a> <A NAME=tex2html2643 HREF="node58.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node58.html"><IMG ALIGN=MIDDLE ALT="previous" SRC="previous_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/previous_motif.gif"></A> <A NAME=tex2html2651 HREF="node60.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node60.html"><IMG ALIGN=MIDDLE ALT="next" SRC="next_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/next_motif.gif"></A> <A NAME=tex2html2649 HREF="node51.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node51.html"><IMG ALIGN=MIDDLE ALT="up" SRC="up_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/up_motif.gif"></A> <A NAME=tex2html2653 HREF="node1.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node1.html"><IMG ALIGN=MIDDLE ALT="contents" SRC="contents_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/contents_motif.gif"></A> <A NAME=tex2html2654 HREF="node133.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node133.html"><IMG ALIGN=MIDDLE ALT="index" SRC="index_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/index_motif.gif"></A> <a href="msgs0.htm#3" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/tppmsgs/msgs0.htm#3"><img ALIGN=MIDDLE src="search_motif.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/search_motif.gif" alt="[Search]"></a> <BR>
<B> Next:</B> <A NAME=tex2html2652 HREF="node60.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node60.html">5.9 Modularity</A>
<B>Up:</B> <A NAME=tex2html2650 HREF="node51.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node51.html">5 Compositional C++
</A>
<B> Previous:</B> <A NAME=tex2html2644 HREF="node58.html" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node58.html">5.7 Determinism</A>
<BR><HR><P>
<H1><A NAME=SECTION03280000000000000000>5.8 Mapping</A></H1>
<P>
<A NAME=secccmap> </A>
<P>
<A NAME=7384> </A>
A parallel program defined in terms of CC++
constructs can be
<A NAME=7385> </A>
executed on both uniprocessor and multiprocessor computers. In the
latter case, a complete program must also specify how the processor
objects created by a CC++
program are mapped to processors. Recall
from Chapter <A HREF="node14.html#chap2" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node14.html#chap2">2</A> that this is an important part of parallel
algorithm design.
<P>
<P><A NAME=8341> </A><IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img849.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/img849.gif">
<BR><STRONG>Figure 5.6:</STRONG> <em> Mapping in CC++
. First, threads are mapped to processor
objects. Then, processor objects are mapped to physical
processors.</em><A NAME=figccmap> </A><BR>
<P>
<P>
Mapping in CC++
is a two-stage process (Figure <A HREF="node59.html#figccmap" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node59.html#figccmap">5.6</A>).
First, threads are mapped to processor objects, and then processor
objects are mapped to processors. The mapping of threads to processor
objects can be one-to-one, in which case it is the mapping of
processor objects to physical processors that is important.
Alternatively, the mapping of processor objects to physical processors
may be one-to-one, in which case it is the mapping of threads to
processor objects that is important. If both mappings are one-to-one,
then the mapping problem is straightforward.
<P>
An important aspect of the second mapping stage, processor object
placement, is that it <em> influences performance but not
correctness</em>. Hence, we can develop a program on a uniprocessor and
then tune performance on a parallel computer by changing placement
decisions. This is consistent with the design methodology of
Chapter <A HREF="node14.html#chap2" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node14.html#chap2">2</A>, in which mapping is the fourth and final stage
of the design process. The first mapping stage, thread placement, has
this property only if threads do not share data structures.
<P>
<H2><A NAME=SECTION03281000000000000000>5.8.1 Processor Object Placement</A></H2>
<P>
<A NAME=7395> </A>
By default, a newly created processor object is placed on the same
processor as its creator. An alternative placement can be specified
by using the <em> placement
</em> argument to the <tt> new</tt> operator.
In C++
, this argument is used to position an object in memory space;
in CC++
, it can also be used to position a processor object in
processor space. (It can also be used to specify where in a file
system to find the code for a processor object; however, we do not
discuss this facility here.) The location is specified by an
implementation-dependent class named <tt> proc_t</tt>. The constructor
functions <tt> proc_t</tt> and <tt> node_t</tt> defined in the
CC++
library can be used to construct a placement structure with a
specified processor name. These are used in the following code
fragment, which creates a new processor object (of type <tt> MyClass</tt>)
on a processor called <tt> mymachine</tt>.
<P>
<PRE>MyClass *global G;
proc_t location(node_t("mymachine"));
G = new (location) MyClass;
</PRE>
<P>
The <tt> new</tt> statement creates a new processor object; the supplied
<tt> proc_t</tt> object (<tt> location</tt>) specifies the machine name. To
place the new processor object on a different processor, one need
change only the second line of this code fragment, for example to the
following.
<P>
<PRE>proc_t location(node_t("yourmachine"));
</PRE>
<P>
As a further example, the following code creates 32 processor objects,
placing each on a different processor of a multicomputer with nodes
named <tt> sp#0</tt>, <tt> sp#1</tt>, ..., <tt> sp#31</tt>. Notice how <tt>
parfor</tt> is used to create the different processor objects
concurrently.
<P>
<PRE>MyClass *global G[32];
parfor (int i=0; i<31; i++) {
char node_name[256];
sprintf(node_name,"sp#%
proc_t location(node_t(node_name));
G[i] = new (location) MyClass;
}
</PRE>
<P>
Although simple, this code represents bad programming practice, in
that it embeds information about the environment in which the program
is executing. A better approach is to encapsulate mapping decisions
in a separate class, for example, the class <tt> Mapping</tt> defined in
Program <A HREF="node59.html#progccmap2" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node59.html#progccmap2">5.8</A>. This class encapsulates two private
variables (<tt> P</tt> and <tt> proc_names</tt>) that represent the
environment in which a program is to execute. The member function
<tt> initmap</tt> is used to initialize these variables. Two additional
member functions, <tt> processor</tt> and <tt> random_p</tt>, return a <tt>
proc_t</tt> object representing the <tt> i</tt>th processor and a
randomly-selected processor, respectively. Finally, two data transfer
functions (omitted for brevity) package and unpackage the node list
associated with a mapping object, allowing a mapping to be passed as
an argument when creating a new processor object. The use of the <tt>
Mapping</tt> class is illustrated in the following example.
<P>
<P><A NAME=progccmap2> </A><IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img850.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/img850.gif"><P>
<P>
<BR><HR>
<b> Example <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img852.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/img852.gif">.<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img851.gif" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/img851.gif"> Search (II)</b>:<A NAME=excctree4> </A>
<P>
Recall that Program <A HREF="node55.html#progcctree" tppabs="http://www.dit.hcmut.edu.vn/books/system/par_anl/node55.html#progcctree">5.4</A> explores a search tree in
parallel by creating new threads to explore the subtrees rooted at
each nonleaf node. Each thread executes in a new processor object.
This program does not specify a mapping strategy for these processor
objects. One strategy is to place each newly created processor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -