?? exercise.html
字號:
<HTML><HEAD><TITLE>POSIX Threads Programming Exercise</TITLE><SCRIPT LANGUAGE="JavaScript" SRC="../tutorials.js"></SCRIPT><LINK REL=StyleSheet HREF="../tutorials.css" TYPE="text/css"></HEAD><BODY BGCOLOR=white><BASEFONT SIZE=3> <!-- default font size --><A NAME=top> </A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR><TD BGCOLOR=#3F5098> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH=900> <TR><TD BACKGROUND=../images/bg1.gif> <SCRIPT LANGUAGE="JavaScript">addNavigation()</SCRIPT> <P><BR> <H1>POSIX Threads Programming Exercise</H1> <P> </TD></TR></TABLE></TD></TR></TABLE><P><OL><P><B><LI>Login to the workshop machine</B><P> Workshops differ in how this is done. The instructor will go over this beforehand.<P> <B><LI>Copy the example files</B><P> In your home directory, create a pthreads subdirectory for the example codes, copy the example codes and then cd into your pthreads subdirectory and <TT>cd</TT> to it. Then copy the Pthreads example codes.<P><PRE><B>mkdir pthreads cp /usr/global/docs/training/blaise/pthreads/* ~/pthreadscd pthreads</B></PRE><B><LI> List the contents of your <TT>pthreads</TT> subdirectory</B><P>You should notice quite a few files. The table below lists and describes these files. <P><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=90%><TR VALIGN=top><TH>File Name</TH><TH>Description</TH></TR><TR VALIGN=top><TD><PRE><A HREF=samples/arrayloops.c TARGET=WE1>arrayloops.c</A><A HREF=samples/arrayloops.f TARGET=WE2>arrayloops.f</A></PRE></TD><TD>Data decomposition by loop distribution. Fortran example only worksunder IBM AIX: see comments in source code for compilation instructions.</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/condvar.c TARGET=WE3>condvar.c</A><TD>Condition variable example file. Similar to what was shown in thetutorial</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/detached.c TARGET=WE4>detached.c</A></PRE></TD><TD>Demonstrates how to explicitly create pthreads in a detached state.</TD> </TR><TR VALIGN=top><TD><PRE><A HREF=samples/dotprod_mutex.c TARGET=WE5>dotprod_mutex.c</A><A HREF=samples/dotprod_serial.c TARGET=WE6>dotprod_serial.c</A></PRE></TD> <TD>Mutex variable example using a dot product program. Botha serial and pthreads version of the code are available.</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/hello.c TARGET=WE7>hello.c</A></PRE></TD><TD>Simple "Hello World" example</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/hello32.c TARGET=WE8>hello32.c</A></PRE></TD> <TD>"Hello World" pthreads program demonstrating thread scheduling behavior.</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/hello_arg1.c TARGET=WE9>hello_arg1.c</A></PRE></TD><TD>One correct way of passing the pthread_create() argument.</TD> </TR><TR VALIGN=top><TD><PRE><A HREF=samples/hello_arg2.c TARGET=WE10>hello_arg2.c</A></PRE></TD><TD>Another correct method of passing the pthread_create() argument, this time using a structure to pass multiple arguments.</TD> </TR><TR VALIGN=top><TD><PRE><A HREF=samples/join.c TARGET=WE11>join.c</A></PRE></TD><TD>Demonstrates how to explicitly create pthreads in a joinable state for portability purposes. Also shows how to use the pthread_exit statusparameter.</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/mpithreads_both.c TARGET=WE12>mpithreads_both.c</A><A HREF=samples/mpithreads.makefile TARGET=WE13>mpithreads.makefile</A><A HREF=samples/mpithreads_mpi.c TARGET=WE14>mpithreads_mpi.c</A><A HREF=samples/mpithreads_serial.c TARGET=WE15>mpithreads_serial.c</A><A HREF=samples/mpithreads_threads.c TARGET=WE16>mpithreads_threads.c</A></PRE></TD><TD>A "series" of programs which demonstrate the progressionfor a serial dot product code to a hybrid MPI/Pthreads implementation.Files include the serial version, Pthreads version, MPI version,hybrid version and a makefile.</TD></TR><TR VALIGN=top><TD><PRE><A HREF=samples/bug1.c TARGET=WE17>bug1.c</A><A HREF=samples/bug1fix.c TARGET=WE18>bug1fix.c</A><A HREF=samples/bug2.c TARGET=WE19>bug2.c</A><A HREF=samples/bug2fix.c TARGET=WE20>bug2fix.c</A><A HREF=samples/bug3.c TARGET=WE21>bug3.c</A><A HREF=samples/bug4.c TARGET=WE22>bug4.c</A><A HREF=samples/bug4fix.c TARGET=WE23>bug4fix.c</A><A HREF=samples/bug5.c TARGET=WE24>bug5.c</A><A HREF=samples/bug6.c TARGET=WE25>bug6.c</A><A HREF=samples/bug6fix.c TARGET=WE26>bug6fix.c</A></PRE></TD><TD>Examples with bugs.</TD></TR></TABLE><P><BR><B><LI>Review and compile <TT>hello.c</TT></B><P><OL><LI>Begin with the simplest exercise code. After reviewing and understanding what <TT>hello.c</TT> does, use the thread-safe, IBM C compiler to compile the source code:<PRE><B>xlc_r -q64 -O2 -o hello hello.c</B></PRE><LI>Run the <TT>hello</TT> executable and notice its output.<P><TT><B>hello </B></TT><P>Try running it several times and notice if the order of the output stringschange. </OL><P><B><LI>Thread Scheduling</B><OL><P><LI>Review the example code <TT><B>hello32.c</B></TT>. Note that it will create 32 threads. A <TT>sleep();</TT> statement has been introduced to help insure that all threads will be in existence at the same time. Also, each thread performs actual work to demonstrate how the AIX scheduler behavior determines the order of thread completion.<P><LI>Compile and run the program. Notice the order in which thread output is displayed. Is it ever in the same order? How is this explained?</OL><P><B><LI>Argument Passing</B><P><OL><LI>Review the <B><TT>hello_arg1.c</TT></B> and <B><TT>hello_arg2.c</TT></B> example codes. Notice how the single argument is passed and how to pass multiple arguments through a structure.<P><LI>Compile and run both programs. </OL><P><B><LI>Review, compile and run the other Pthreads example codes</B><P>Try these other pthreads codes before moving on to the <TT>mpithreads</TT> or <TT>bugX.c</TT> sets of codes. Compile them as you did for <TT>hello.c</TT>.Some of these are similar to what was shown in the tutorial and some aren't.<UL><LI><TT><B>arrayloops.c</B></TT><LI><TT><B>condvar.c</B></TT><LI><TT><B>detached.c</B></TT><LI><TT><B>dotprod_mutex.c</B></TT><LI><TT><B>dotprod_serial.c</B></TT><LI><TT><B>join.c</B></TT></UL><P><B><LI>When things go wrong...</B><P>Part of the learning process is to see what happens when things go wrong.There are many things that can go wrong when developing pthreads programs. The<B><TT>bugX.c</TT></B> series of programs demonstrate just a few. See if you can figure out what the problem is with each case and then fix it.<P>Use <B><TT>xlc_r -q64 -O2</TT></B> to compile each code as appropriate.<P>The buggy behavior will differ for each example. Some hints are providedbelow.<P><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3 WIDTH=90%><TR VALIGN=top><TH>Code</TH><TH>Behavior</TH><TH>Hints/Notes</TH></TR><TR VALIGN=top><TD><TT><B>bug1.c<BR>bug1fix.c</B></TT></TD><TD>Hangs</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex01')></FONT></TD></TR><TR VALIGN=top><TD><TT><B>bug2.c<BR>bug2fix.c</B></TT></TD><TD>Seg fault/coredump</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex02')></FONT></TD></TR><TR VALIGN=top><TD><TT><B>bug3.c</B></TT></TD><TD>Wrong answers</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex03')></FONT></TD></TR><TR VALIGN=top><TD><TT><B>bug4.c<BR>bug4fix.c</B></TT></TD><TD>Hangs (usually)</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex04')></FONT></TD></TR><TR VALIGN=top><TD><TT><B>bug5.c</B></TT></TD><TD>Threads die and never get to do their work</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex05')></FONT> </TD></TR><TR VALIGN=top><TD><TT><B>bug6.c<BR>bug6fix.c</B></TT></TD><TD>Wrong answer</TD><TD><FONT SIZE=-1><INPUT TYPE=button VALUE=ExplanationonClick=Answers('pthreadex06')></FONT></TD></TR></TABLE><P><B><LI>Try the <TT>mpithreads</TT> series of codes</B><OL><P><LI>Your <TT>pthreads</TT> directory should contain the following 5 codes: <P><TT><B>mpithreads_serial.c<BR>mpithreads_threads.c<BR>mpithreads_mpi.c<BR>mpithreads_both.c<BR>mpithreads.makefile</B></TT><P>These codes implement a dot product calculation and are designed to showthe progression of developing a hybrid MPI / Pthreads program from aa serial code. The problem size increases as the examples go from serial,to threads/mpi to mpi with threads.<P>Suggestion: simply making and running this series of codes is rather unremarkable. Using the available lab time to understand what is actually happening is the intent. The instructor is available for your questions.<P><LI>As time permits, review each of the codes. (The order of the listing above shows the "progression").<P><LI>Use the provided makefile to compile all of the codes at once:<P><TT><B> make -f mpithreads.makefile </B></TT><P> Be sure to examine the makefile so that you are familiar with the actual compiler commands used.<P><LI>Run each of the codes. Note: in order to run the two codes which use MPI (<TT>mpithreads_mpi</TT> and <TT>mpithreads_both</TT>), you must <B><I>first</I></B> have the necessary POE environment variables set, as shown below. <P><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=90%><TR VALIGN=top><TH>Environment Variable Setting</TH><TH>Description</TH></TR><TR VALIGN=top><TD><PRE><B>setenv MP_PROCS 4</B></PRE></TD><TD>The number of MPI tasks will be 4</TD> </TR><TR VALIGN=top><TD><PRE><B>setenv MP_NODES 4</B></PRE></TD><TD>Only one MPI task per node. For the <TT>mpithreads_both</TT> example,the threads will utilize the extra cpus.</TD> </TR><TR VALIGN=top><TD><PRE><B>setenv MP_RMPOOL pclass</B></PRE></TD><TD>Set it to the workshop pool number.</TD><!-----------Ask the instructor, or click <FONT SIZE=-1><B><INPUT TYPE=button VALUE="HERE"onclick="openWindow('../misc/notes.html#MP_RMPOOL')"> </B></FONT>to determine the workshop node pool.---------------></TR></TABLE></OL></OL><P><HR><P><B>This completes the exercise.</B><P><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR VALIGN=top><TD><A HREF=../evaluation/index.html TARGET=evalForm> <IMG SRC=../images/evaluationForm.gif BORDER=0 ALT='Evaluation Form'></A> </TD><TD>Please complete the online evaluation form if you have not already done so for this tutorial. </TD></TR></TABLE><P><B>Where would you like to go now?</B><UL><LI><A HREF=../agenda/index.html>Agenda</A><LI><A HREF=index.html>Back to the tutorial</A></UL><!----------<UL><LI>Return to <A HREF=index.html>POSIX Threads Tutorial</A><LI>Return to <A HREF="javascript://" onClick="resetTop('../index.html');">Workshop Home</A></UL>-----------><!--------------------------------------------------------------------------><SCRIPT LANGUAGE="JavaScript">PrintFooter("UCRL-MI-133316")</SCRIPT><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -