<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Seventh Sense &#187; Technology</title>
	<atom:link href="http://sgowtham.net/blog/category/technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://sgowtham.net/blog</link>
	<description>Rambling about life&#039;s little things, in 7 ≡ 1 (mod 6) fashion</description>
	<lastBuildDate>Tue, 28 Feb 2012 23:25:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
	<title>Seventh Sense</title>
	<url>http://sgowtham.net/images/sg_logo_rss.png</url>
	<link>http://sgowtham.net/blog</link>
	<width>32</width>
	<height>32</height>
	</image>
		<item>
		<title>HPCC 1.4.1 Benchmark With GCC 4.1.2 On Rocks 5.4.2</title>
		<link>http://sgowtham.net/blog/2012/02/28/hpcc-1-4-1-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/</link>
		<comments>http://sgowtham.net/blog/2012/02/28/hpcc-1-4-1-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 18:27:14 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Clusters]]></category>
		<category><![CDATA[HPCC]]></category>
		<category><![CDATA[HPL]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Rocks]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3411</guid>
		<description><![CDATA[Disclaimer The instructions/steps given below worked for me (and Michigan Technological University) running NPACI Rocks 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>The instructions/steps given below worked for me (and <a href="http://www.mtu.edu/" target="_blank" class="underline">Michigan Technological University</a>) running <a href="http://www.rocksclusters.org/" class="underline" target="_blank">NPACI Rocks</a> 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <em>sgowtham.net</em>, nor its author (or Michigan Technological University) is responsible for any/all damage – intellectual and/or otherwise.</p>
<p><br clear="all"></p>
<h3 class="blog">A Bit About HPCC</h3>
<p>Citing <a href="http://icl.cs.utk.edu/hpcc/" target="_blank" clas="underline">HPCC website</a>,</p>
<blockquote><p>
HPCC benchmark suite has been released by <a href="http://www.darpa.mil/Our_Work/MTO/Programs/High_Productivity_Computing_Systems_%28HPCS%29.aspx" target="_blank" class="underline">DARPA High Productivity Computing Systems (HPCS)</a> program to help define the performance boundaries of future Petascale computing systems. HPCC is a suite of tests that examine the performance of HPC architectures using kernels with memory access patterns more challenging than those of the HPL benchmark used in <a href="http://www.top500.org/" target="_blank" class="underline">Top500</a>. This suite is designed to augment the list, providing benchmarks that bound the performance of many real applications as a function of memory access characteristics. For e.g., spatial and temporal locality, and providing a framework for including additional tests. HPCC consists of seven tests that attempt to span high and low spatial and temporal locality space. </p>
<p>By design, the HPCC tests are scalable with the size of data sets being a function of the largest HPL matrix tested for the system. Since HPCC kernels consist of simple mathematical operations, it provides a unique opportunity to look at language and parallel programming issues. To characterize the architecture of the system, the following three scenarios are considered:</p>
<ol>
<li><strong>Local</strong> &#8211; only a single processor is performing the computations</li>
<li><strong><a href="http://en.wikipedia.org/wiki/Embarrassingly_parallel" target="_blank" class="underline">Embarrassingly Parallel</a></strong> &#8211; each processor in the entire system is performing the computations but without explicit communication with each other</li>
<li><strong>Global</strong> &#8211; all processors in the system are performing the computations with explicit communication with each other</li>
</ol>
<p>The seven tests that make up HPCC benchmark are as follows:</p>
<ol>
<li><strong><a href="http://www.netlib.org/benchmark/hpl/" target="_blank" class="underline">HPL</a></strong> &#8211; the Linpack TPP benchmark which measures the floating point rate of execution for solving a linear system of equations</li>
<li><strong><a href="http://en.wikipedia.org/wiki/General_Matrix_Multiply" target="_blank" class="underline">DGEMM</a></strong> &#8211; measures the floating point rate of execution of double precision real matrix-matrix multiplication</li>
<li><strong><a href="http://www.cs.virginia.edu/stream/" target="_blank" class="underline">STREAM</a></strong> &#8211; a simple synthetic benchmark program that measures sustainable memory bandwidth (in GB/s) and the corresponding computation rate for simple vector kernel</li>
<li><strong><a href="http://www.netlib.org/parkbench/html/matrix-kernels.html" target="_blank" class="underline">PTRANS</a> (parallel matrix transpose)</strong> &#8211; exercises the communications where pairs of processors communicate with each other simultaneously. It is a useful test of the total communications capacity of the network</li>
<li><strong><a href="http://icl.cs.utk.edu/projectsfiles/hpcc/RandomAccess/" target="_blank" class="underline">RandomAccess</a></strong> &#8211; measures the rate of integer random updates of memory (GUPS)</li>
<li><strong><a href="http://www.ffte.jp/" target="_blank" class="underline">FFT</a></strong> &#8211; measures the floating point rate of execution of double precision complex one-dimensional Discrete Fourier Transform (DFT)</li>
<li><strong><a href="https://fs.hlrs.de/projects/par/mpi/b_eff/" target="_blank" class="underline">Communication bandwidth and latency</a></strong> &#8211; a set of tests to measure latency and bandwidth of a number of simultaneous communication patterns; based on b<sub>eff</sub> (effective bandwidth benchmark)</li>
</ol>
</blockquote>
<p><br clear="all"></p>
<h3 class="blog">Pre-requisites: MPI &#038; Goto BLAS</h3>
<p>Please refer to the corresponding sections in <a href="http://sgowtham.net/blog/2012/02/27/hpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/" class="underline" target="_blank">HPL 2.0 Benchmark With GCC 4.1.2 On Rocks 5.4.2</a> to learn more about required pre-requisites as well as instructions for installing/compiling them. Following Rocks recommendations, pre-requisites will be installed under <code>/share/apps/</code>; software installed by me, in clusters at Michigan Tech, have the following template for their folder structure:</p>
<p class="bpcaption"><code>/share/apps/Software/Software_Version/Compiler/Compiler_Version</code></p>
<p><br clear="all"></p>
<h3 class="blog">HPCC Installation/Compilation</h3>
<p>With MPICH2 and Goto BLAS2 in place, HPCC 1.4.1 will be installed under</p>
<p class="bpcaption"><code>/share/apps/hpcc/1.4.1/mpich2/1.4.1p1/gcc/4.1.2</code></p>
<p><br clear="all"><br />
The following script, used in installation, will assume that one has downloaded HPCC 1.4.1 from <a href="http://icl.cs.utk.edu/hpcc/software/" target="_blank" class="underline">here</a> &#038; the necessary <code>Make.MPICH2141p1_GCC412.HPCC</code> (listed below; it does look identical to the one used when compiling HPL 2.0 &#8211; the only difference is in the definition of <code>TOPdir</code> variable) and placed them in <code>/share/apps/tmp/</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
</pre></td><td class="code"><pre class="make" style="font-family:monospace;"><span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Makefile (Make.MPICH2141p1_GCC412) used to compile HPCC </span>
<span style="color: #339900; font-style: italic;"># on HPC clusters running NPACI Rocks (5.4.2) with CentOS (5.5)</span>
<span style="color: #339900; font-style: italic;"># at Michigan Technological University.</span>
<span style="color: #339900; font-style: italic;"># </span>
<span style="color: #339900; font-style: italic;"># Disclaimer:</span>
<span style="color: #339900; font-style: italic;"># Please note that you are using these instructions </span>
<span style="color: #339900; font-style: italic;"># at your very own risk and that Michigan Technological</span>
<span style="color: #339900; font-style: italic;"># University is not responsible for any/all damage caused to </span>
<span style="color: #339900; font-style: italic;"># your property, intellectual or otherwise.</span>
<span style="color: #339900; font-style: italic;"># </span>
<span style="color: #339900; font-style: italic;"># For additional help and/or comments, questions, suggestions,</span>
<span style="color: #339900; font-style: italic;"># please contact </span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Gowtham</span>
<span style="color: #339900; font-style: italic;"># Information Technology Services &amp; Security</span>
<span style="color: #339900; font-style: italic;"># Michigan Technological University</span>
<span style="color: #339900; font-style: italic;"># g@mtu.edu</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#  -- High Performance Computing Linpack Benchmark (HPL)                </span>
<span style="color: #339900; font-style: italic;">#     HPL - 2.0 - September 10, 2008                          </span>
<span style="color: #339900; font-style: italic;">#     Antoine P. Petitet                                                </span>
<span style="color: #339900; font-style: italic;">#     University of Tennessee, Knoxville                                </span>
<span style="color: #339900; font-style: italic;">#     Innovative Computing Laboratory                                 </span>
<span style="color: #339900; font-style: italic;">#     (C) Copyright 2000-2008 All Rights Reserved                       </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  -- Copyright notice and Licensing terms:                             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  Redistribution and use in source and binary forms, with or without</span>
<span style="color: #339900; font-style: italic;">#  modification, are permitted provided that the following conditions</span>
<span style="color: #339900; font-style: italic;">#  are met:                                                             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  1. Redistributions of source code must retain the above copyright</span>
<span style="color: #339900; font-style: italic;">#  notice, this list of conditions and the following disclaimer.        </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  2. Redistributions in binary form must reproduce the above copyright</span>
<span style="color: #339900; font-style: italic;">#  notice, this list of conditions, and the following disclaimer in the</span>
<span style="color: #339900; font-style: italic;">#  documentation and/or other materials provided with the distribution. </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  3. All advertising materials mentioning features or use of this</span>
<span style="color: #339900; font-style: italic;">#  software must display the following acknowledgement:                 </span>
<span style="color: #339900; font-style: italic;">#  This product includes software developed at the University of</span>
<span style="color: #339900; font-style: italic;">#  Tennessee, Knoxville, Innovative Computing Laboratory.             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  4. The name of the University, the name of the Laboratory, or the</span>
<span style="color: #339900; font-style: italic;">#  names of its contributors may not be used to endorse or promote</span>
<span style="color: #339900; font-style: italic;">#  products derived from this software without specific written</span>
<span style="color: #339900; font-style: italic;">#  permission.</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#  -- Disclaimer:</span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<span style="color: #339900; font-style: italic;">#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<span style="color: #339900; font-style: italic;">#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<span style="color: #339900; font-style: italic;">#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY</span>
<span style="color: #339900; font-style: italic;">#  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<span style="color: #339900; font-style: italic;">#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<span style="color: #339900; font-style: italic;">#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<span style="color: #339900; font-style: italic;">#  DATA OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<span style="color: #339900; font-style: italic;">#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span style="color: #339900; font-style: italic;">#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<span style="color: #339900; font-style: italic;">#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span style="color: #339900; font-style: italic;">#  </span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Shell Details</span>
SHELL        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>bin<span style="color: #004400;">/</span>sh
CD           <span style="color: #004400;">=</span> cd
CP           <span style="color: #004400;">=</span> cp
LN_S         <span style="color: #004400;">=</span> ln <span style="color: #004400;">-</span>s
MKDIR        <span style="color: #004400;">=</span> mkdir
RM           <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>bin<span style="color: #004400;">/</span>rm <span style="color: #004400;">-</span>f
TOUCH        <span style="color: #004400;">=</span> touch
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Platform Identifier</span>
ARCH         <span style="color: #004400;">=</span> MPICH2141p1_GCC412
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Directory Structure / HPL library</span>
TOPdir       <span style="color: #004400;">=</span> <span style="color: #004400;">../../..</span>
INCdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span>
BINdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>bin<span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span>
LIBdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>lib<span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span>
HPLlib       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>libhpl<span style="color: #004400;">.</span>a 
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Message Passing library (MPI)</span>
MPdir        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>share<span style="color: #004400;">/</span>apps<span style="color: #004400;">/</span>mpich2<span style="color: #004400;">/</span>1<span style="color: #004400;">.</span>4<span style="color: #004400;">.</span>1p1<span style="color: #004400;">/</span>gcc<span style="color: #004400;">/</span>4<span style="color: #004400;">.</span>1<span style="color: #004400;">.</span>2
MPinc        <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span>
MPlib        <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>lib<span style="color: #004400;">/</span>libmpich<span style="color: #004400;">.</span>a
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Linear Algebra library (BLAS)</span>
LAlib        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>share<span style="color: #004400;">/</span>apps<span style="color: #004400;">/</span>gotoblas2<span style="color: #004400;">/</span><span style="color: #CC2200;">1.13</span><span style="color: #004400;">/</span>gcc<span style="color: #004400;">/</span>4<span style="color: #004400;">.</span>1<span style="color: #004400;">.</span>2<span style="color: #004400;">/</span>libgoto2<span style="color: #004400;">.</span>a
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># F77 / C Interface</span>
<span style="color: #339900; font-style: italic;"># You can skip this section if and only if you are not planning to use</span>
<span style="color: #339900; font-style: italic;"># a BLAS library featuring a Fortran 77 interface. Otherwise, it is</span>
<span style="color: #339900; font-style: italic;"># necessary to fill out the F2CDEFS variable with the appropriate</span>
<span style="color: #339900; font-style: italic;"># options. **One and only one** option should be chosen in **each** of</span>
<span style="color: #339900; font-style: italic;"># the 3 following categories:</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 1) name space (How C calls a Fortran 77 routine)</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DAdd_              : all lower case and a suffixed underscore (Suns,</span>
<span style="color: #339900; font-style: italic;">#                       Intel, ...) [default]</span>
<span style="color: #339900; font-style: italic;"># -DNoChange          : all lower case (IBM RS6000)</span>
<span style="color: #339900; font-style: italic;"># -DUpCase            : all upper case (Cray)</span>
<span style="color: #339900; font-style: italic;"># -DAdd__             : the FORTRAN compiler in use is f2c</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 2) C and Fortran 77 integer mapping</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=int   : Fortran 77 INTEGER is a C int [default]</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=long  : Fortran 77 INTEGER is a C long</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=short : Fortran 77 INTEGER is a C short</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 3) Fortran 77 string handling</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DStringSunStyle    : The string address is passed at the string location</span>
<span style="color: #339900; font-style: italic;">#                       on the stack, and the string length is then</span>
<span style="color: #339900; font-style: italic;">#                       passed as an F77_INTEGER after all explicit</span>
<span style="color: #339900; font-style: italic;">#                       stack arguments [default]</span>
<span style="color: #339900; font-style: italic;"># -DStringStructPtr   : The address of a structure is passed by a</span>
<span style="color: #339900; font-style: italic;">#                       Fortran 77 string, and the structure is of the</span>
<span style="color: #339900; font-style: italic;">#                       form: struct {char *cp; F77_INTEGER len;}</span>
<span style="color: #339900; font-style: italic;"># -DStringStructVal   : A structure is passed by value for each Fortran</span>
<span style="color: #339900; font-style: italic;">#                       77 string, and the structure is of the form:</span>
<span style="color: #339900; font-style: italic;">#                       struct {char *cp; F77_INTEGER len;}</span>
<span style="color: #339900; font-style: italic;"># -DStringCrayStyle   : Special option for Cray machines, which uses</span>
<span style="color: #339900; font-style: italic;">#                       Cray fcd (fortran character descriptor) for</span>
<span style="color: #339900; font-style: italic;">#                       interoperation</span>
F2CDEFS      <span style="color: #004400;">=</span>  <span style="color: #004400;">-</span>DAdd_
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Includes / Libraries / Specifics</span>
HPL_INCLUDES <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCdir</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LAinc</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPinc</span><span style="color: #004400;">&#41;</span>
HPL_LIBS     <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPLlib</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LAlib</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPlib</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Compile Time Options</span>
<span style="color: #339900; font-style: italic;"># -DHPL_COPY_L           force the copy of the panel L before bcast</span>
<span style="color: #339900; font-style: italic;"># -DHPL_CALL_CBLAS       call the cblas interface</span>
<span style="color: #339900; font-style: italic;"># -DHPL_CALL_VSIPL       call the vsip library</span>
<span style="color: #339900; font-style: italic;"># -DHPL_DETAILED_TIMING  enable detailed timers</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># By default HPL will:</span>
<span style="color: #339900; font-style: italic;">#    *) not copy L before broadcast</span>
<span style="color: #339900; font-style: italic;">#    *) call the BLAS Fortran 77 interface</span>
<span style="color: #339900; font-style: italic;">#    *) not display detailed timing information</span>
HPL_OPTS     <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>DHPL_COPY_L <span style="color: #004400;">-</span>DHPL_CALL_CBLAS <span style="color: #004400;">-</span>DHPL_DETAILED_TIMING 
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Definitions</span>
HPL_DEFS     <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">F2CDEFS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_OPTS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_INCLUDES</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Compilers / linkers - Optimization Flags</span>
CC           <span style="color: #004400;">=</span> mpicc
CCNOOPT      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_DEFS</span><span style="color: #004400;">&#41;</span>
CCFLAGS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_DEFS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>fomit<span style="color: #004400;">-</span>frame<span style="color: #004400;">-</span>pointer <span style="color: #004400;">-</span>O3 <span style="color: #004400;">-</span>funroll<span style="color: #004400;">-</span>loops <span style="color: #004400;">-</span>W <span style="color: #004400;">-</span>Wall
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># On some platforms, it is necessary to use the Fortran linker </span>
<span style="color: #339900; font-style: italic;"># to find the Fortran internals used in the BLAS library</span>
LINKER       <span style="color: #004400;">=</span> mpif77
LINKFLAGS    <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CCFLAGS</span><span style="color: #004400;">&#41;</span>
ARCHIVER     <span style="color: #004400;">=</span> ar
ARFLAGS      <span style="color: #004400;">=</span> r
RANLIB       <span style="color: #004400;">=</span> echo
<span style="color: #339900; font-style: italic;">#</span></pre></td></tr></table></div>

<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_hpcc.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install HPCC 1.4.1 (compiled against MPICH2 1.4.1p1) on a</span>
<span style="color: #666666; font-style: italic;"># Rocks 5.4.2 cluster's front end</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># Set necessary variables</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HPCC_VERSION</span>=<span style="color: #ff0000;">&quot;1.4.1&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MPICH2_VERSION</span>=<span style="color: #ff0000;">&quot;1.4.1p1&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GCC_VERSION</span>=<span style="color: #ff0000;">&quot;4.1.2&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HPCC_INSTALL</span>=<span style="color: #ff0000;">&quot;/share/apps/hpcc/<span style="color: #007800;">${HPCC_VERSION}</span>/mpich2/<span style="color: #007800;">${MPICH2_VERSION}</span>/gcc/<span style="color: #007800;">${GCC_VERSION}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MAKEFILE_ARCH</span>=<span style="color: #ff0000;">&quot;MPICH2141p1_GCC412&quot;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Begin HPCC-${HPCC_VERSION}.tar.gz check IF</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;/share/apps/tmp/hpcc-<span style="color: #007800;">${HPCC_VERSION}</span>.tar.gz&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>hpcc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HPCC_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>hpcc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HPCC_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hpcc-<span style="color: #800000;">${HPCC_VERSION}</span>.tar.gz
    <span style="color: #c20cb9; font-weight: bold;">mv</span> hpcc-<span style="color: #800000;">${HPCC_VERSION}</span> <span style="color: #800000;">${GCC_VERSION}</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #0: copy the makefile, make clean and make&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #800000;">${HPCC_INSTALL}</span>
    <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Make.<span style="color: #800000;">${MAKEFILE_ARCH}</span>.HPCC .<span style="color: #000000; font-weight: bold;">/</span>hpl<span style="color: #000000; font-weight: bold;">/</span>Make.<span style="color: #800000;">${MAKEFILE_ARCH}</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">make</span> clean <span style="color: #007800;">arch</span>=<span style="color: #800000;">${MAKEFILE_ARCH}</span>
    <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #007800;">arch</span>=<span style="color: #800000;">${MAKEFILE_ARCH}</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #1: Update <span style="color: #007800;">$HOME</span>/.bashrc&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
&nbsp;
  Add the following lines to $HOME/.bashrc and remember to source it
&nbsp;
  # HPCC (${HPCC_VERSION}) settings
  export HPCC=&quot;${HPCC_INSTALL}&quot;
  export PATH=&quot;\${PATH}:\${HPCC}/bin/${MAKEFILE_ARCH}&quot;
  export LD_LIBRARY_PATH=&quot;\${LD_LIBRARY_PATH}:\${HPCC}/${MAKEFILE_ARCH}&quot;
&nbsp;
EOF</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #666666; font-style: italic;"># End hpcc-${HPCC_VERSION}.tar.gz check IF</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Running HPCC Benchmark</h3>
<p>Please refer to the corresponding sections in <a href="http://sgowtham.net/blog/2012/02/27/hpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/" class="underline" target="_blank">HPL 2.0 Benchmark With GCC 4.1.2 On Rocks 5.4.2</a> to learn how to get a fix on <code>N</code>, <code>NB</code>, etc., given the hardware configuration of compute nodes. Running HPCC benchmark is very similar to running HPL 2.0 benchmark.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2012%2F02%2F28%2Fhpcc-1-4-1-benchmark-with-gcc-4-1-2-on-rocks-5-4-2%2F&amp;title=HPCC%201.4.1%20Benchmark%20With%20GCC%204.1.2%20On%20Rocks%205.4.2" id="wpa2a_2"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2012/02/28/hpcc-1-4-1-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HPL 2.0 Benchmark With GCC 4.1.2 On Rocks 5.4.2</title>
		<link>http://sgowtham.net/blog/2012/02/27/hpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/</link>
		<comments>http://sgowtham.net/blog/2012/02/27/hpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 23:44:17 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Clusters]]></category>
		<category><![CDATA[HPL]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MichiganTech]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Rocks]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3361</guid>
		<description><![CDATA[Disclaimer The instructions/steps given below worked for me (and Michigan Technological University) running NPACI Rocks 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>The instructions/steps given below worked for me (and <a href="http://www.mtu.edu/" target="_blank" class="underline">Michigan Technological University</a>) running <a href="http://www.rocksclusters.org/" class="underline" target="_blank">NPACI Rocks</a> 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <em>sgowtham.net</em>, nor its author (or Michigan Technological University) is responsible for any/all damage – intellectual and/or otherwise.</p>
<p><br clear="all"></p>
<h3 class="blog">A Bit About HPL</h3>
<blockquote><p><a href="http://netlib.org/linpack/" class="underline" target="_blank">LINPACK</a> is a software library for performing numerical linear algebra on computers. It was written in FORTRAN by Jack Dongarra, Jim Bunch, Cleve Moler and Gilbert Stewart. LINPACK makes use of the <a href="http://www.netlib.org/blas/" class="underline" target="_blank">BLAS</a> libraries for performing basic vector and matrix operations. It has largely been superseded by LAPACK, which runs more efficiently on modern architectures.</p>
<p>The LINPACK benchmarks are a measure of a system&#8217;s floating point computing power. Introduced by Jack Dongarra, they measure how fast a computer solves a dense <code>N * N</code> system of linear equations, <code>Ax = b</code>. The solution is obtained by <em>Gaussian Elimination with Partial Pivoting</em>, with</p>
<p class="bpcaption"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B2%7D%7B3%7D%5C%3AN%5E3%20%5C%3A%2B%5C%3A%202%5C%3AN%5E2%20%5C%3A%2B%5C%3A%20%5Cmathcal%7BO%7D%5Cleft%28N%5Cright%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{2}{3}\:N^3 \:+\: 2\:N^2 \:+\: \mathcal{O}\left(N\right)' title='\frac{2}{3}\:N^3 \:+\: 2\:N^2 \:+\: \mathcal{O}\left(N\right)' class='latex' /></p>
<p>floating point iterations. The result is often expressed in billions of floating point operations per second (<code>GFLOPS</code>). <a href="http://netlib.org/benchmark/hpl/" class="underline" target="_blank">HPL</a>, a portable implementation of <em>High Performance LINPACK Benchmark</em>, is used as a performance measure for ranking the supercomputers in the <a href="http://www.top500.org/" target="_blank" class="underline">Top500</a> list.
</p></blockquote>
<p><br clear="all"></p>
<h3 class="blog">Pre-requisite #1: MPI</h3>
<p>Following Rocks recommendations, this and other pre-requisites will be installed under <code>/share/apps/</code>; software installed by me, in clusters at Michigan Tech, have the following template for their folder structure:</p>
<p class="bpcaption"><code>/share/apps/Software/Software_Version/Compiler/Compiler_Version</code></p>
<p><br clear="all"><br />
Rocks 5.4.2 installation has an instance (of few flavors) of MPI but I prefer to compile <a href="http://www.mcs.anl.gov/research/projects/mpich2/" class="underline" target="_blank">MPICH2</a> using GCC 4.1.2. At the time of writing this post, the latest stable version of MPICH2 is 1.4.1p1 and it may be downloaded from <a href="http://www.mcs.anl.gov/research/projects/mpich2/" target="_blank" class="underline">here</a>.  Following folder structure/template mentioned above, it will be installed under</p>
<p class="bpcaption"><code>/share/apps/mpich2/1.4.1p1/gcc/4.1.2</code></p>
<p><br clear="all"><br />
To avoid confusion and/or missed steps leading to undesired results, steps associated with installation of MPICH2 have been put in the following script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_mpich2.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install MPICH2 (compiled against GCC 4.1.2) on a</span>
<span style="color: #666666; font-style: italic;"># Rocks 5.4.2 cluster's front end</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># Set necessary variables</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CC</span>=<span style="color: #ff0000;">&quot;gcc&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CXX</span>=<span style="color: #ff0000;">&quot;g++&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">FC</span>=<span style="color: #ff0000;">&quot;gfortran&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">F77</span>=<span style="color: #ff0000;">&quot;gfortran&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MPICH2_VERSION</span>=<span style="color: #ff0000;">&quot;1.4.1p1&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GCC_VERSION</span>=<span style="color: #ff0000;">&quot;4.1.2&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MPICH2_INSTALL</span>=<span style="color: #ff0000;">&quot;/share/apps/mpich2/<span style="color: #007800;">${MPICH2_VERSION}</span>/gcc/<span style="color: #007800;">${GCC_VERSION}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ANL</span>=<span style="color: #ff0000;">&quot;http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs&quot;</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #0: Download MPICH2 to /share/apps/tmp&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #800000;">${ANL}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>mpich2-<span style="color: #800000;">${MPICH2_VERSION}</span>.tar.gz
&nbsp;
  <span style="color: #666666; font-style: italic;"># Begin mpich2-${MPICH2_VERSION}.tar.gz check IF</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;mpich2-<span style="color: #007800;">${MPICH2_VERSION}</span>.tar.gz&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #1: configure, make clean, make and make install&quot;</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxpf</span> mpich2-<span style="color: #800000;">${MPICH2_VERSION}</span>.tar.gz
    <span style="color: #7a0874; font-weight: bold;">cd</span> mpich2-<span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>
    .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #800000;">${MPICH2_INSTALL}</span>
    <span style="color: #c20cb9; font-weight: bold;">make</span> clean
    <span style="color: #c20cb9; font-weight: bold;">make</span>
    <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #2: Update <span style="color: #007800;">$HOME</span>/.bashrc&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
&nbsp;
  Add the following lines to $HOME/.bashrc and remember to source it
&nbsp;
  # MPICH2 (${MPICH2_VERSION}) settings
  export MPICH2=&quot;${MPICH2_INSTALL}&quot;
  export PATH=&quot;\${PATH}:\${MPICH2}/bin:\${MPICH2}/sbin&quot;
  export MANPATH=&quot;\${MANPATH}:\${MPICH2}/man&quot;
  export LD_LIBRARY_PATH=&quot;\${LD_LIBRARY_PATH}:\${MPICH2}/lib&quot;
&nbsp;
EOF</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #666666; font-style: italic;"># End mpich2-${MPICH2_VERSION}.tar.gz check IF</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"><br />
A good test of a successful installation is that <em>which mpicc</em>, <em>which mpif77</em>, etc. return the respective commands located in <code>${MPICH2_INSTALL}</code>.</p>
<p><br clear="all"></p>
<h3 class="blog">Pre-requisite #2: Goto BLAS</h3>
<p>As in the case of pre-requisite #1, this one will be installed under</p>
<p class="bpcaption"><code>/share/apps/gotoblas2/1.13/gcc/4.1.2</code></p>
<p><br clear="all"><br />
The following script, used in installation, will assume that one has downloaded Goto BLAS2 1.13 from <a href="http://www.tacc.utexas.edu/tacc-projects/gotoblas2" target="_blank" class="underline">here</a> and placed it in<br />
<code>/share/apps/tmp/</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_gotoblas2.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install Goto BLAS2 (compiled against GCC 4.1.2) on a</span>
<span style="color: #666666; font-style: italic;"># Rocks 5.4.2 cluster's front end</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># Set necessary variables</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CC</span>=<span style="color: #ff0000;">&quot;gcc&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CXX</span>=<span style="color: #ff0000;">&quot;g++&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">FC</span>=<span style="color: #ff0000;">&quot;gfortran&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">F77</span>=<span style="color: #ff0000;">&quot;gfortran&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GOTOBLAS2_VERSION</span>=<span style="color: #ff0000;">&quot;1.13&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GCC_VERSION</span>=<span style="color: #ff0000;">&quot;4.1.2&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GOTOBLAS2_INSTALL</span>=<span style="color: #ff0000;">&quot;/share/apps/gotoblas2/<span style="color: #007800;">${GOTOBLAS2_VERSION}</span>/gcc/<span style="color: #007800;">${GCC_VERSION}</span>&quot;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Begin GotoBLAS2-${GOTOBLAS2_VERSION}.tar.gz check IF</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;/share/apps/tmp/GotoBLAS2-<span style="color: #007800;">${GOTOBLAS2_VERSION}</span>.tar.gz&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>gotoblas2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${GOTOBLAS2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>gotoblas2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${GOTOBLAS2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>GotoBLAS2-<span style="color: #800000;">${GOTOBLAS2_VERSION}</span>.tar.gz
    <span style="color: #c20cb9; font-weight: bold;">mv</span> GotoBLAS2 <span style="color: #800000;">${GCC_VERSION}</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #0: make clean and make&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #800000;">${GOTOBLAS2_INSTALL}</span>
    <span style="color: #c20cb9; font-weight: bold;">make</span> clean
    <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #007800;">BINARY</span>=<span style="color: #000000;">64</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #1: Update <span style="color: #007800;">$HOME</span>/.bashrc&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
&nbsp;
  Add the following lines to $HOME/.bashrc and remember to source it
&nbsp;
  # GOTOBLAS2 (${GOTOBLAS2_VERSION}) settings
  export GOTOBLAS2=&quot;${GOTOBLAS2_INSTALL}&quot;
  export LD_LIBRARY_PATH=&quot;\${LD_LIBRARY_PATH}:\${GOTOBLAS2}&quot;
&nbsp;
EOF</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #666666; font-style: italic;"># End GotoBLAS2-${GOTOBLAS2_VERSION}.tar.gz check IF</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">HPL Installation/Compilation</h3>
<p>With MPICH2 and Goto BLAS2 in place, HPL 2.0 will be installed under</p>
<p class="bpcaption"><code>/share/apps/hpl/2.0/mpich2/1.4.1p1/gcc/4.1.2</code></p>
<p><br clear="all"><br />
The following script, used in installation, will assume that one has downloaded HPL 2.0 from <a href="http://netlib.org/benchmark/hpl/" target="_blank" class="underline">here</a> &#038; the necessary <code>Make.MPICH2141p1_GCC412.HPL</code> (listed below) and placed them in <code>/share/apps/tmp/</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
</pre></td><td class="code"><pre class="make" style="font-family:monospace;"><span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Makefile (Make.MPICH2141p1_GCC412) used to compile HPL </span>
<span style="color: #339900; font-style: italic;"># on HPC clusters running NPACI Rocks (5.4.2) with CentOS (5.5)</span>
<span style="color: #339900; font-style: italic;"># at Michigan Technological University.</span>
<span style="color: #339900; font-style: italic;"># </span>
<span style="color: #339900; font-style: italic;"># Disclaimer:</span>
<span style="color: #339900; font-style: italic;"># Please note that you are using these instructions </span>
<span style="color: #339900; font-style: italic;"># at your very own risk and that Michigan Technological</span>
<span style="color: #339900; font-style: italic;"># University is not responsible for any/all damage caused to </span>
<span style="color: #339900; font-style: italic;"># your property, intellectual or otherwise.</span>
<span style="color: #339900; font-style: italic;"># </span>
<span style="color: #339900; font-style: italic;"># For additional help and/or comments, questions, suggestions,</span>
<span style="color: #339900; font-style: italic;"># please contact </span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Gowtham</span>
<span style="color: #339900; font-style: italic;"># Information Technology Services &amp; Security</span>
<span style="color: #339900; font-style: italic;"># Michigan Technological University</span>
<span style="color: #339900; font-style: italic;"># g@mtu.edu</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#  -- High Performance Computing Linpack Benchmark (HPL)                </span>
<span style="color: #339900; font-style: italic;">#     HPL - 2.0 - September 10, 2008                          </span>
<span style="color: #339900; font-style: italic;">#     Antoine P. Petitet                                                </span>
<span style="color: #339900; font-style: italic;">#     University of Tennessee, Knoxville                                </span>
<span style="color: #339900; font-style: italic;">#     Innovative Computing Laboratory                                 </span>
<span style="color: #339900; font-style: italic;">#     (C) Copyright 2000-2008 All Rights Reserved                       </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  -- Copyright notice and Licensing terms:                             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  Redistribution and use in source and binary forms, with or without</span>
<span style="color: #339900; font-style: italic;">#  modification, are permitted provided that the following conditions</span>
<span style="color: #339900; font-style: italic;">#  are met:                                                             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  1. Redistributions of source code must retain the above copyright</span>
<span style="color: #339900; font-style: italic;">#  notice, this list of conditions and the following disclaimer.        </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  2. Redistributions in binary form must reproduce the above copyright</span>
<span style="color: #339900; font-style: italic;">#  notice, this list of conditions, and the following disclaimer in the</span>
<span style="color: #339900; font-style: italic;">#  documentation and/or other materials provided with the distribution. </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  3. All advertising materials mentioning features or use of this</span>
<span style="color: #339900; font-style: italic;">#  software must display the following acknowledgement:                 </span>
<span style="color: #339900; font-style: italic;">#  This product includes software developed at the University of</span>
<span style="color: #339900; font-style: italic;">#  Tennessee, Knoxville, Innovative Computing Laboratory.             </span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  4. The name of the University, the name of the Laboratory, or the</span>
<span style="color: #339900; font-style: italic;">#  names of its contributors may not be used to endorse or promote</span>
<span style="color: #339900; font-style: italic;">#  products derived from this software without specific written</span>
<span style="color: #339900; font-style: italic;">#  permission.</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;">#  -- Disclaimer:</span>
<span style="color: #339900; font-style: italic;">#                                                                       </span>
<span style="color: #339900; font-style: italic;">#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<span style="color: #339900; font-style: italic;">#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<span style="color: #339900; font-style: italic;">#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<span style="color: #339900; font-style: italic;">#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY</span>
<span style="color: #339900; font-style: italic;">#  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<span style="color: #339900; font-style: italic;">#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<span style="color: #339900; font-style: italic;">#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<span style="color: #339900; font-style: italic;">#  DATA OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<span style="color: #339900; font-style: italic;">#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span style="color: #339900; font-style: italic;">#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<span style="color: #339900; font-style: italic;">#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span style="color: #339900; font-style: italic;">#  </span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Shell Details</span>
SHELL        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>bin<span style="color: #004400;">/</span>sh
CD           <span style="color: #004400;">=</span> cd
CP           <span style="color: #004400;">=</span> cp
LN_S         <span style="color: #004400;">=</span> ln <span style="color: #004400;">-</span>s
MKDIR        <span style="color: #004400;">=</span> mkdir
RM           <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>bin<span style="color: #004400;">/</span>rm <span style="color: #004400;">-</span>f
TOUCH        <span style="color: #004400;">=</span> touch
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Platform Identifier</span>
ARCH         <span style="color: #004400;">=</span> MPICH2141p1_GCC412
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Directory Structure / HPL library</span>
TOPdir       <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>share<span style="color: #004400;">/</span>apps<span style="color: #004400;">/</span>hpl<span style="color: #004400;">/</span><span style="color: #CC2200;">2.0</span><span style="color: #004400;">/</span>mpich2<span style="color: #004400;">/</span>1<span style="color: #004400;">.</span>4<span style="color: #004400;">.</span>1p1<span style="color: #004400;">/</span>gcc<span style="color: #004400;">/</span>4<span style="color: #004400;">.</span>1<span style="color: #004400;">.</span>2
INCdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span>
BINdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>bin<span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span>
LIBdir       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TOPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>lib<span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span>
HPLlib       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>libhpl<span style="color: #004400;">.</span>a 
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Message Passing library (MPI)</span>
MPdir        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>share<span style="color: #004400;">/</span>apps<span style="color: #004400;">/</span>mpich2<span style="color: #004400;">/</span>1<span style="color: #004400;">.</span>4<span style="color: #004400;">.</span>1p1<span style="color: #004400;">/</span>gcc<span style="color: #004400;">/</span>4<span style="color: #004400;">.</span>1<span style="color: #004400;">.</span>2
MPinc        <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span>
MPlib        <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/</span>lib<span style="color: #004400;">/</span>libmpich<span style="color: #004400;">.</span>a
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Linear Algebra library (BLAS)</span>
LAlib        <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>share<span style="color: #004400;">/</span>apps<span style="color: #004400;">/</span>gotoblas2<span style="color: #004400;">/</span><span style="color: #CC2200;">1.13</span><span style="color: #004400;">/</span>gcc<span style="color: #004400;">/</span>4<span style="color: #004400;">.</span>1<span style="color: #004400;">.</span>2<span style="color: #004400;">/</span>libgoto2<span style="color: #004400;">.</span>a
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># F77 / C Interface</span>
<span style="color: #339900; font-style: italic;"># You can skip this section if and only if you are not planning to use</span>
<span style="color: #339900; font-style: italic;"># a BLAS library featuring a Fortran 77 interface. Otherwise, it is</span>
<span style="color: #339900; font-style: italic;"># necessary to fill out the F2CDEFS variable with the appropriate</span>
<span style="color: #339900; font-style: italic;"># options. **One and only one** option should be chosen in **each** of</span>
<span style="color: #339900; font-style: italic;"># the 3 following categories:</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 1) name space (How C calls a Fortran 77 routine)</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DAdd_              : all lower case and a suffixed underscore (Suns,</span>
<span style="color: #339900; font-style: italic;">#                       Intel, ...) [default]</span>
<span style="color: #339900; font-style: italic;"># -DNoChange          : all lower case (IBM RS6000)</span>
<span style="color: #339900; font-style: italic;"># -DUpCase            : all upper case (Cray)</span>
<span style="color: #339900; font-style: italic;"># -DAdd__             : the FORTRAN compiler in use is f2c</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 2) C and Fortran 77 integer mapping</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=int   : Fortran 77 INTEGER is a C int [default]</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=long  : Fortran 77 INTEGER is a C long</span>
<span style="color: #339900; font-style: italic;"># -DF77_INTEGER=short : Fortran 77 INTEGER is a C short</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># 3) Fortran 77 string handling</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># -DStringSunStyle    : The string address is passed at the string location</span>
<span style="color: #339900; font-style: italic;">#                       on the stack, and the string length is then</span>
<span style="color: #339900; font-style: italic;">#                       passed as an F77_INTEGER after all explicit</span>
<span style="color: #339900; font-style: italic;">#                       stack arguments [default]</span>
<span style="color: #339900; font-style: italic;"># -DStringStructPtr   : The address of a structure is passed by a</span>
<span style="color: #339900; font-style: italic;">#                       Fortran 77 string, and the structure is of the</span>
<span style="color: #339900; font-style: italic;">#                       form: struct {char *cp; F77_INTEGER len;}</span>
<span style="color: #339900; font-style: italic;"># -DStringStructVal   : A structure is passed by value for each Fortran</span>
<span style="color: #339900; font-style: italic;">#                       77 string, and the structure is of the form:</span>
<span style="color: #339900; font-style: italic;">#                       struct {char *cp; F77_INTEGER len;}</span>
<span style="color: #339900; font-style: italic;"># -DStringCrayStyle   : Special option for Cray machines, which uses</span>
<span style="color: #339900; font-style: italic;">#                       Cray fcd (fortran character descriptor) for</span>
<span style="color: #339900; font-style: italic;">#                       interoperation</span>
F2CDEFS      <span style="color: #004400;">=</span>  <span style="color: #004400;">-</span>DAdd_
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Includes / Libraries / Specifics</span>
HPL_INCLUDES <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCdir</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCdir</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">/$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ARCH</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LAinc</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPinc</span><span style="color: #004400;">&#41;</span>
HPL_LIBS     <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPLlib</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LAlib</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MPlib</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Compile Time Options</span>
<span style="color: #339900; font-style: italic;"># -DHPL_COPY_L           force the copy of the panel L before bcast</span>
<span style="color: #339900; font-style: italic;"># -DHPL_CALL_CBLAS       call the cblas interface</span>
<span style="color: #339900; font-style: italic;"># -DHPL_CALL_VSIPL       call the vsip library</span>
<span style="color: #339900; font-style: italic;"># -DHPL_DETAILED_TIMING  enable detailed timers</span>
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># By default HPL will:</span>
<span style="color: #339900; font-style: italic;">#    *) not copy L before broadcast</span>
<span style="color: #339900; font-style: italic;">#    *) call the BLAS Fortran 77 interface</span>
<span style="color: #339900; font-style: italic;">#    *) not display detailed timing information</span>
HPL_OPTS     <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>DHPL_COPY_L <span style="color: #004400;">-</span>DHPL_CALL_CBLAS <span style="color: #004400;">-</span>DHPL_DETAILED_TIMING 
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># HPL Definitions</span>
HPL_DEFS     <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">F2CDEFS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_OPTS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_INCLUDES</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Compilers / linkers - Optimization Flags</span>
CC           <span style="color: #004400;">=</span> mpicc
CCNOOPT      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_DEFS</span><span style="color: #004400;">&#41;</span>
CCFLAGS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HPL_DEFS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>fomit<span style="color: #004400;">-</span>frame<span style="color: #004400;">-</span>pointer <span style="color: #004400;">-</span>O3 <span style="color: #004400;">-</span>funroll<span style="color: #004400;">-</span>loops <span style="color: #004400;">-</span>W <span style="color: #004400;">-</span>Wall
&nbsp;
<span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># On some platforms, it is necessary to use the Fortran linker </span>
<span style="color: #339900; font-style: italic;"># to find the Fortran internals used in the BLAS library</span>
LINKER       <span style="color: #004400;">=</span> mpif77
LINKFLAGS    <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CCFLAGS</span><span style="color: #004400;">&#41;</span>
ARCHIVER     <span style="color: #004400;">=</span> ar
ARFLAGS      <span style="color: #004400;">=</span> r
RANLIB       <span style="color: #004400;">=</span> echo
<span style="color: #339900; font-style: italic;">#</span></pre></td></tr></table></div>

<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_hpl.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install HPL 2.0 (compiled against MPICH2 1.4.1p1) on a</span>
<span style="color: #666666; font-style: italic;"># Rocks 5.4.2 cluster's front end</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># Set necessary variables</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HPL_VERSION</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MPICH2_VERSION</span>=<span style="color: #ff0000;">&quot;1.4.1p1&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GCC_VERSION</span>=<span style="color: #ff0000;">&quot;4.1.2&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HPL_INSTALL</span>=<span style="color: #ff0000;">&quot;/share/apps/hpl/<span style="color: #007800;">${HPL_VERSION}</span>/mpich2/<span style="color: #007800;">${MPICH2_VERSION}</span>/gcc/<span style="color: #007800;">${GCC_VERSION}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MAKEFILE_ARCH</span>=<span style="color: #ff0000;">&quot;MPICH2141p1_GCC412&quot;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Begin hpl-${HPL_VERSION}.tar.gz check IF</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;/share/apps/tmp/hpl-<span style="color: #007800;">${HPL_VERSION}</span>.tar.gz&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>hpl<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HPL_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>hpl<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HPL_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${MPICH2_VERSION}</span><span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hpl-<span style="color: #800000;">${HPL_VERSION}</span>.tar.gz
    <span style="color: #c20cb9; font-weight: bold;">mv</span> hpl-<span style="color: #800000;">${HPL_VERSION}</span> <span style="color: #800000;">${GCC_VERSION}</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #0: copy the makefile, make clean and make&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #800000;">${HPL_INSTALL}</span>
    <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Make.<span style="color: #800000;">${MAKEFILE_ARCH}</span>.HPC .<span style="color: #000000; font-weight: bold;">/</span>Make.<span style="color: #800000;">${MAKEFILE_ARCH}</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HPL_INSTALL}</span>/bin/<span style="color: #007800;">${MAKEFILE_ARCH}</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #c20cb9; font-weight: bold;">make</span> clean <span style="color: #007800;">arch</span>=<span style="color: #800000;">${MAKEFILE_ARCH}</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(ls -A ${HPL_INSTALL}/bin)</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>;
    <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #c20cb9; font-weight: bold;">make</span> clean_arch_all <span style="color: #007800;">arch</span>=<span style="color: #800000;">${MAKEFILE_ARCH}</span> 
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #007800;">arch</span>=<span style="color: #800000;">${MAKEFILE_ARCH}</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #1: Update <span style="color: #007800;">$HOME</span>/.bashrc&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
&nbsp;
  Add the following lines to $HOME/.bashrc and remember to source it
&nbsp;
  # HPL (${HPL_VERSION}) settings
  export HPL=&quot;${HPL_INSTALL}&quot;
  export PATH=&quot;\${PATH}:\${HPL}/bin/${MAKEFILE_ARCH}&quot;
  export LD_LIBRARY_PATH=&quot;\${LD_LIBRARY_PATH}:\${HPL}/${MAKEFILE_ARCH}&quot;
&nbsp;
EOF</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #666666; font-style: italic;"># End hpl-${HPL_VERSION}.tar.gz check IF</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Running HPL Benchmark</h3>
<p>The amount of memory used by HPL is essentially the size of the co-efficient matrix, <code>A</code>. Following standard binary definition, 1 GB is 1024 * 1024 * 1024 bytes (<code>TOTAL_MEMORY_BYTES</code>). Most scientific/engineering computations use double precision numbers, with each such double precision number taking 8 bytes of memory. Thus, 1 GB can accommodate 134,217,728 double precision entities (<code>TOTAL_DP_ELEMENTS = TOTAL_MEMORY_BYTES/8</code>). Theoretically, <code>sqrt(TOTAL_DP_ELEMENTS)</code> represents the maximum possible value of <code>N</code>. However, operating system needs some memory to perform some necessary operations. As such, HPL benchmark is usually performed for the following values of <code>N</code> &#8211; with <code>m</code> representing the fraction of total memory &#8211; making sure that swapping did not occur (which would result in reduced performance).</p>
<p class="bpcaption"><img src='http://s.wordpress.com/latex.php?latex=N%20%5C%3A%3D%5C%3A%20m%5C%3A%5Csqrt%7B%5Cmathrm%7BTOTAL%5C_DP%5C_ELEMENTS%7D%7D%20%20%5Chspace%7B0.50in%7D%20m%3A%200.50%5C%3A%280.10%29%5C%3A0.80&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N \:=\: m\:\sqrt{\mathrm{TOTAL\_DP\_ELEMENTS}}  \hspace{0.50in} m: 0.50\:(0.10)\:0.80' title='N \:=\: m\:\sqrt{\mathrm{TOTAL\_DP\_ELEMENTS}}  \hspace{0.50in} m: 0.50\:(0.10)\:0.80' class='latex' /></p>
<p><br clear="all"><br />
HPL uses the block size (<code>NB</code>) for the data distribution as well as for the computational granularity. From a data distribution perspective, the smaller <code>NB</code>, the better the load balance. From a computational perspective, too small of a value for <code>NB</code> may limit the computational performance by a large factor since almost no data re-use will occur in the highest level of the memory hierarchy. The number of messages will also increase. In my case, this benchmark was performed for <code>NB</code> values of 128, 256 and 512.</p>
<p>The results so obtained are compared with the theoretical peak value, <img src='http://s.wordpress.com/latex.php?latex=%5Cmathrm%7BGFLOPS%7D_%5Cmathrm%7BTheory%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathrm{GFLOPS}_\mathrm{Theory}' title='\mathrm{GFLOPS}_\mathrm{Theory}' class='latex' />, computed as follows:</p>
<p><br clear="all"></p>
<p class="bpcaption"><img src='http://s.wordpress.com/latex.php?latex=%20%5Cmbox%7B%5C%23%20of%20Nodes%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B%5C%23%20of%20Sockets%2FNode%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B%5C%23%20of%20Cores%2FSocket%7D%20%5C%3B%5Ctimes%5C%3B%20%5C%5C%5C%5C%5Cmbox%7BCPU%20Frequency%20%28Cycles%2Fsecond%29%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B%5C%23%20of%20Floating%20Point%20Operations%2FCycle%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=' \mbox{\# of Nodes} \;\times\; \mbox{\# of Sockets/Node} \;\times\; \mbox{\# of Cores/Socket} \;\times\; \\\\\mbox{CPU Frequency (Cycles/second)} \;\times\; \mbox{\# of Floating Point Operations/Cycle}' title=' \mbox{\# of Nodes} \;\times\; \mbox{\# of Sockets/Node} \;\times\; \mbox{\# of Cores/Socket} \;\times\; \\\\\mbox{CPU Frequency (Cycles/second)} \;\times\; \mbox{\# of Floating Point Operations/Cycle}' class='latex' /></p>
<p><br clear="all"><br />
For e.g., for a cluster with 16 identical <em>recent</em> Intel architecture compute nodes, each compute node with dual hex cores @ 3.00 GHz, <img src='http://s.wordpress.com/latex.php?latex=%5Cmathrm%7BGFLOPS%7D_%5Cmathrm%7BTheory%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathrm{GFLOPS}_\mathrm{Theory}' title='\mathrm{GFLOPS}_\mathrm{Theory}' class='latex' /> will be</p>
<p><br clear="all"></p>
<p class="bpcaption"><img src='http://s.wordpress.com/latex.php?latex=%20%5Cmbox%7B16%20%28%5C%23%20of%20Nodes%29%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B2%20%28%5C%23%20of%20Sockets%2FNode%29%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B6%20%28%5C%23%20of%20Cores%2FSocket%29%7D%20%5C%3B%5Ctimes%5C%3B%20%5C%5C%5C%5C%5Cmbox%7B3%20G%20%5BCPU%20Frequency%20%28Cycles%2Fsecond%29%5D%7D%20%5C%3B%5Ctimes%5C%3B%20%5Cmbox%7B4%20%28%5C%23%20of%20Floating%20Point%20Operations%2FCycle%29%7D%5C%5C%5C%5C%20%3D%202304%20%5Cmbox%7B%20GFLOPS%7D%20%5C%5C%5C%5C%20%5Capprox%202.3%20%5Cmbox%7B%20TFLOPS%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=' \mbox{16 (\# of Nodes)} \;\times\; \mbox{2 (\# of Sockets/Node)} \;\times\; \mbox{6 (\# of Cores/Socket)} \;\times\; \\\\\mbox{3 G [CPU Frequency (Cycles/second)]} \;\times\; \mbox{4 (\# of Floating Point Operations/Cycle)}\\\\ = 2304 \mbox{ GFLOPS} \\\\ \approx 2.3 \mbox{ TFLOPS}' title=' \mbox{16 (\# of Nodes)} \;\times\; \mbox{2 (\# of Sockets/Node)} \;\times\; \mbox{6 (\# of Cores/Socket)} \;\times\; \\\\\mbox{3 G [CPU Frequency (Cycles/second)]} \;\times\; \mbox{4 (\# of Floating Point Operations/Cycle)}\\\\ = 2304 \mbox{ GFLOPS} \\\\ \approx 2.3 \mbox{ TFLOPS}' class='latex' /></p>
<p><br clear="all"><br />
If each of these nodes had 24 GB RAM, then <code>TOTAL_MEMORY_BYTES = 1024 * 1024 * 1024 * 24 * 16 = 412316860416</code><br />
and <code>TOTAL_DP_ELEMENTS = 412316860416/8 = 51539607552</code>. </p>
<p>As such, <code>N</code> values will be</p>
<p><br clear="all"></p>
<p class="bpcaption"><img src='http://s.wordpress.com/latex.php?latex=N%20%5C%3A%3D%5C%3A%20m%5C%3A%5Ctimes%5C%3A%20%5Csqrt%7B51539607552%7D%20%5C%3A%5Capprox%5C%3A%20m%5C%3A%5Ctimes%5C%3A%20227020%20%5Chspace%7B0.50in%7D%20m%3A%200.50%5C%3A%280.10%29%5C%3A0.80&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N \:=\: m\:\times\: \sqrt{51539607552} \:\approx\: m\:\times\: 227020 \hspace{0.50in} m: 0.50\:(0.10)\:0.80' title='N \:=\: m\:\times\: \sqrt{51539607552} \:\approx\: m\:\times\: 227020 \hspace{0.50in} m: 0.50\:(0.10)\:0.80' class='latex' /></p>
<p><br clear="all"></p>
<h3 class="blog">What if the cluster has heterogeneous compute nodes?</h3>
<p>Computing <img src='http://s.wordpress.com/latex.php?latex=%5Cmathrm%7BGFLOPS%7D_%5Cmathrm%7BTheory%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathrm{GFLOPS}_\mathrm{Theory}' title='\mathrm{GFLOPS}_\mathrm{Theory}' class='latex' /> isn&#8217;t easy in this case; becomes even more so when these compute nodes belong to different generations as one has to account for <em>aging factor</em>. It has been a practice at Michigan Tech, in such cases, to split the cluster into different queues &#8211; one for each generation/type of compute nodes &#8211; and run the HPL benchmark separately.</p>
<p><br clear="all"></p>
<h3 class="blog">Thanks be to</h3>
<p><a href="http://marc.info/?l=npaci-rocks-discussion" target="_blank" class="underline">Rocks mailing list</a> and its participants.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2012%2F02%2F27%2Fhpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2%2F&amp;title=HPL%202.0%20Benchmark%20With%20GCC%204.1.2%20On%20Rocks%205.4.2" id="wpa2a_4"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2012/02/27/hpl-2-0-benchmark-with-gcc-4-1-2-on-rocks-5-4-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ganglia – gmond Python Module For Monitoring NVIDIA GPU On Rocks 5.4.2</title>
		<link>http://sgowtham.net/blog/2012/02/17/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rocks-5-4-2/</link>
		<comments>http://sgowtham.net/blog/2012/02/17/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rocks-5-4-2/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 23:24:43 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Clusters]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[Ganglia]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MichiganTech]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Rocks]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3238</guid>
		<description><![CDATA[Disclaimer The instructions/steps given below worked for me (and Michigan Technological University) running NPACI Rocks 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>The instructions/steps given below worked for me (and <a href="http://www.mtu.edu/" target="_blank" class="underline">Michigan Technological University</a>) running <a href="http://www.rocksclusters.org/" class="underline" target="_blank">NPACI Rocks</a> 5.4.2 (with CentOS 5.5) &#8211; as has been a common practice for several years now, a full version of Operating System was installed. These instructions may very well work for you (or your institution), on Rocks-like or other linux clusters. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <em>sgowtham.net</em>, nor its author (or Michigan Technological University) is responsible for any/all damage – intellectual and/or otherwise.</p>
<p><br clear="all"></p>
<h3 class="blog">A Bit About Ganglia (gmond &amp; gmetad)</h3>
<p>Citing <a href="http://ganglia.info/" target="_blank" class="underline">Ganglia website</a>, </p>
<blockquote><p>Ganglia is a scalable distributed monitoring system for high-performance computing systems such as clusters and Grids. It is based on a hierarchical design targeted at federations of clusters. It leverages widely used technologies such as XML for data representation, XDR for compact, portable data transport, and RRD tool for data storage and visualization. It uses carefully engineered data structures and algorithms to achieve very low per-node overheads and high concurrency. The implementation is robust, has been ported to an extensive set of operating systems and processor architectures, and is currently in use on thousands of clusters around the world. It has been used to link clusters across university campuses and around the world and can scale to handle clusters with 2000 nodes.</p></blockquote>
<p>Further, citing <a href="http://en.wikipedia.org/wiki/Ganglia_%28software%29" class="underline" target="_blank">Wikipedia</a>,</p>
<blockquote><p><code>gmond</code> (Ganglia Monitoring Daemon) is a multi-threaded daemon which runs on each cluster node that needs to be monitored. Installation does not require having a common NFS file system or a database back-end, install special accounts or maintain configuration files. It has four main responsibilities: monitor changes in host state; announce relevant changes; listen to the state of all other ganglia nodes via a unicast or multicast channel; answer requests for an XML description of the cluster state.</p>
<p>Each <code>gmond</code> transmits in information in two different ways: unicasting or multicasting host state in external data representation (XDR) format using UDP messages OR sending XML over a TCP connection.</p>
<p>Federation in Ganglia is achieved using a tree of point-to-point connections amongst representative cluster nodes to aggregate the state of multiple clusters. At each node in the tree, a Ganglia Meta Daemon (<code>gmetad</code>) periodically polls a collection of child data sources, parses the collected XML, saves all numeric, volatile metrics to round-robin databases and exports the aggregated XML over a TCP sockets to clients. Data sources may be either <code>gmond</code> daemons, representing specific clusters, or other <code>gmetad</code> daemons, representing sets of clusters. Data sources use source IP addresses for access control and can be specified using multiple IP addresses for fail over. The latter capability is natural for aggregating data from clusters since each <code>gmond</code> daemon contains the entire state of its cluster.</p>
<p>The Ganglia web front-end provides a view of the gathered information via real-time dynamic web pages. Most importantly, it displays Ganglia data in a meaningful way for system administrators and computer users. Although the web front-end to ganglia started as a simple HTML view of the XML tree, it has evolved into a system that keeps a colourful history of all collected data. The Ganglia web front-end caters to system administrators and users (for e.g., one can view the CPU utilization over the past hour, day, week, month, or year). The web front-end shows similar graphs for memory usage, disk usage, network statistics, number of running processes, and all other Ganglia metrics. The web front-end depends on the existence of the <code>gmetad</code> which provides it with data from several Ganglia sources. </p>
<p>Specifically, the web front-end will open the local port 8651 (by default) and expects to receive a Ganglia XML tree. The web pages themselves are highly dynamic; any change to the Ganglia data appears immediately on the site. This behaviour leads to a very responsive site, but requires that the full XML tree be parsed on every page access. Therefore, the Ganglia web front-end should run on a fairly powerful, dedicated machine if it presents a large amount of data. The Ganglia web front-end is written in the PHP scripting language, and uses graphs generated by <code>gmetad</code> to display history information.
</p></blockquote>
<p><br clear="all"></p>
<h3 class="blog">Installation &#038; Configuration</h3>
<p>Rocks 5.4.2 installation in itself takes care of almost everything pertaining to installing and configuring Ganglia, <code>gmond</code>, <code>gmetad</code> and Ganglia web interface. However, by default &#038; design, a Rocks cluster&#8217;s web interface is not publicly accessible. To fix this, following commands were run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># update_web_firewall.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to run necessary 'rocks' commands to update the firewall rules on a</span>
<span style="color: #666666; font-style: italic;"># Rocks 5.4.2 cluster's front end to make the web interface accessible from anywhere</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #0: display current firewall rules&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks report host firewall localhost
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #1: removing the current rule for www&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks remove host firewall localhost <span style="color: #007800;">chain</span>=INPUT \
    <span style="color: #007800;">flags</span>=<span style="color: #ff0000;">&quot;-m state --state NEW --source &amp;Kickstart_PublicNetwork;/&amp;Kickstart_PublicNetmask;&quot;</span> \
    <span style="color: #007800;">protocol</span>=tcp <span style="color: #007800;">service</span>=www <span style="color: #007800;">action</span>=ACCEPT <span style="color: #007800;">network</span>=public
&nbsp;
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks <span style="color: #c20cb9; font-weight: bold;">sync</span> host firewall localhost
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #2: removing the current rule for https&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks remove host firewall localhost <span style="color: #007800;">chain</span>=INPUT \
    <span style="color: #007800;">flags</span>=<span style="color: #ff0000;">&quot;-m state --state NEW --source &amp;Kickstart_PublicNetwork;/&amp;Kickstart_PublicNetmask;&quot;</span> \
    <span style="color: #007800;">protocol</span>=tcp <span style="color: #007800;">service</span>=https <span style="color: #007800;">action</span>=ACCEPT <span style="color: #007800;">network</span>=public
&nbsp;
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks <span style="color: #c20cb9; font-weight: bold;">sync</span> host firewall localhost
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #3: adding new rule for www&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks add host firewall localhost <span style="color: #007800;">chain</span>=INPUT \
    <span style="color: #007800;">flags</span>=<span style="color: #ff0000;">&quot;-m state --state NEW --source 0.0.0.0/0.0.0.0&quot;</span> \
    <span style="color: #007800;">protocol</span>=tcp <span style="color: #007800;">service</span>=www <span style="color: #007800;">action</span>=ACCEPT <span style="color: #007800;">network</span>=public
&nbsp;
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks <span style="color: #c20cb9; font-weight: bold;">sync</span> host firewall localhost
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #4: adding new rule for https&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks add host firewall localhost <span style="color: #007800;">chain</span>=INPUT \
    <span style="color: #007800;">flags</span>=<span style="color: #ff0000;">&quot;-m state --state NEW --source 0.0.0.0/0.0.0.0&quot;</span> \
    <span style="color: #007800;">protocol</span>=tcp <span style="color: #007800;">service</span>=https <span style="color: #007800;">action</span>=ACCEPT <span style="color: #007800;">network</span>=public
&nbsp;
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks <span style="color: #c20cb9; font-weight: bold;">sync</span> host firewall localhost
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Step #5: display current firewall rules&quot;</span>
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rocks report host firewall localhost
  <span style="color: #7a0874; font-weight: bold;">echo</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"><br />
Upon pointing the browser to the <code>http://FQDN/ganglia/</code>, the web page should display the relevant information.</p>
<p><br clear="all"></p>
<h3 class="blog">Monitoring NVIDIA GPU</h3>
<p>The aforementioned set up works fine and as expected but it doesn&#8217;t necessarily provide any information about GPU(s) that may be part of the hardware. For e.g., the cluster used in this case has two <code>NVIDIA GeForce GTX 260</code> cards in each compute node. For testing purposes, only one compute node was installed &#8211; also, one of the <code>GTX 260</code> cards was replaced with a <code>NVIDIA Quadro 6000</code>. With more and more scientific &amp; engineering computations tending towards GPU based computing, it&#8217;d be useful to include their status/usage information in Ganglia&#8217;s web portal. To this effect, NVIDIA released <a href="http://developer.nvidia.com/ganglia-monitoring-system" target="_blank"><code>gmond</code> Python module for GPUs</a> (made aware of it by one of Michigan Tech ITSS directors). The instructions given in the NVIDIA-linked pages do work as described &#8211; however, Rocks 5.4.2 uses <code>python 2.4</code> while one requires <code>python 2.5</code> (or higher) to get the GPU metrics to show up in Ganglia. </p>
<p><br clear="all"></p>
<h4>Rebuilding Rocks Distribution with Python ctypes Library</h4>
<p>I downloaded <code>python-ctypes-1.0.2-2.el5.x86_64.rpm</code> from <a href="http://ftp.osuosl.org/pub/fedora-epel/5/x86_64/" target="_blank" class="underline">http://ftp.osuosl.org/pub/fedora-epel/5/x86_64/</a> and placed it in<br />
<code>/export/rocks/install/contrib/5.4/x86_64/RPMS/</code> &#8211; rebuilding of the distribution, with following commands as usual, was uneventful.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># update_rocks_distribution.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to download python-ctypes-1.0.2-2.el5.x86_64.rpm from </span>
<span style="color: #666666; font-style: italic;"># http://ftp.osuosl.org/pub/fedora-epel/5/x86_64/ and rebuild the</span>
<span style="color: #666666; font-style: italic;"># rocks distribution</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>export<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>contrib<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">5.4</span><span style="color: #000000; font-weight: bold;">/</span>x86_64<span style="color: #000000; font-weight: bold;">/</span>RPMS<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ftp.osuosl.org<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>fedora-epel<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">5</span><span style="color: #000000; font-weight: bold;">/</span>x86_64<span style="color: #000000; font-weight: bold;">/</span>python-ctypes-1.0.2-<span style="color: #000000;">2</span>.el5.x86_64.rpm
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>export<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>
  rocks create distro
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"><br />
Re-install the compute node(s) [in this case, <code>compute-0-0</code>]. Without and with <code>ctypes</code> library, <code>gmond</code> (when run in debug mode, i.e. <code>gmond -d9 -f</code>), results in the following message.</p>
<p><br clear="all"></p>
<p>
<a href="http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_41.png" title="" class="shutterset_singlepic39"  rel="lightbox[3238]">
	<img class="ngg-singlepic ngg-left" src="http://sgowtham.net/blog/wp-content/gallery/cache/39__320x240_ganglia_gpu_41.png" alt="ganglia_gpu_41" title="ganglia_gpu_41" />
</a>

<a href="http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_42.png" title="" class="shutterset_singlepic40"  rel="lightbox[3238]">
	<img class="ngg-singlepic ngg-left" src="http://sgowtham.net/blog/wp-content/gallery/cache/40__320x240_ganglia_gpu_42.png" alt="ganglia_gpu_42" title="ganglia_gpu_42" />
</a>
</p>
<p><br clear="all"><br />
<br clear="all"></p>
<h4>NVIDIA Driver Installation</h4>
<p>Once the compute node(s) are re-installed, NVIDIA driver, <code>NVIDIA-Linux-x86_64-285.05.33.run</code>, was installed using the following script.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_nvidia_driver.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install NVIDIA driver in compute node(s) - save this in /share/apps/bin/</span>
<span style="color: #666666; font-style: italic;"># Assumes that NVIDIA-Linux-x86_64-285.05.33.run is located in /share/apps/src/nvidia_cuda/</span>
<span style="color: #666666; font-style: italic;"># Also, assumes that CUDA SDK 4.1.28 has been installed on front end in /share/apps/cuda/</span>
<span style="color: #666666; font-style: italic;"># Must be root to run this script and run this in all compute nodes from the front end via</span>
<span style="color: #666666; font-style: italic;"># the command</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># rocks run host '/share/apps/bin/install_nvidia_driver.sh'</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #666666; font-style: italic;"># 1. Install NVIDIA driver</span>
  <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nvidia_cuda<span style="color: #000000; font-weight: bold;">/</span>NVIDIA-Linux-x86_64-285.05.33.run <span style="color: #660033;">--silent</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># 2: Updating /etc/ld.so.config</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;/share/apps/cuda/lib64&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ld.so.conf
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;/share/apps/cuda/lib&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ld.so.conf
  <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>ldconfig
&nbsp;
  <span style="color: #666666; font-style: italic;"># 3: Creating missing symbolic links to necessary libraries</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> libXmu.so.6.2.0 libXmu.so
  <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> libXi.so.6.0.0  libXi.so
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h4>Python Bindings for the NVIDIA Management Library</h4>
<p>This provides Python access to static information and monitoring data for NVIDIA GPUs, as well as management capabilities. It exposes the functionality of the <a href="http://developer.nvidia.com/nvidia-management-library-nvml" target="_blank" class="underline">NVML</a> and one may download these from <a href="http://pypi.python.org/pypi/nvidia-ml-py/" target="_blank" class="underline">here</a> &#8211; as before, the necessary steps are included in a BASH script.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_python_nvml_bindings.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install Python Bindings for the NVML in compute node(s) - save this in /share/apps/bin/</span>
<span style="color: #666666; font-style: italic;"># Assumes that nvidia-ml-py-2.285.01.tar.gz is in /share/apps/src/nvidia_ganglia/</span>
<span style="color: #666666; font-style: italic;"># Must be root to run this script and run this in all compute nodes from the front end via the command</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># rocks run host '/share/apps/bin/install_python_nvml_bindings.sh'</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Download and install</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nvidia_ganglia<span style="color: #000000; font-weight: bold;">/</span>nvidia-ml-py-2.285.01.tar.gz .
&nbsp;
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> nvidia-ml-py-2.285.01.tar.gz
  <span style="color: #7a0874; font-weight: bold;">cd</span> nvidia-ml-py-2.285.01
  python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Copy nvidia_smi.py &amp; pynvml.py to /opt/ganglia/lib64/ganglia/python_modules/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> nvidia_smi.py <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> pynvml.py <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h4><code>gmond</code> Python Module For Monitoring NVIDIA GPUs using NVML</h4>
<p>After downloading <code>ganglia-gmond_python_modules-3dfa553.tar.gz</code> from <a href="https://github.com/ganglia/gmond_python_modules/tree/master/gpu/nvidia" target="_blank" class="underline">GitHub for ganglia / gmond_python_modules</a> to<br />
<code>/share/apps/src/nvidia_ganglia/</code>, the following steps need to be performed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># copy_ganglia_gmond_python_computenodes.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to copy relevant files from ganglia-gmond_python_modules to Ganglia, </span>
<span style="color: #666666; font-style: italic;"># and restart gmond - save this in /share/apps/bin/</span>
<span style="color: #666666; font-style: italic;"># Assumes that ganglia-gmond_python_modules-3dfa553.tar.gz is in /share/apps/src/nvidia_ganglia/</span>
<span style="color: #666666; font-style: italic;"># Must be root to run this script and run this in all compute nodes from the front end via the command</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># rocks run host '/share/apps/bin/copy_ganglia_gmond_python_computenodes.sh'</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Copy relevant files to Ganglia</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nvidia_ganglia<span style="color: #000000; font-weight: bold;">/</span>ganglia-gmond_python_modules-3dfa553.tar.gz
  <span style="color: #7a0874; font-weight: bold;">cd</span> ganglia-gmond_python_modules-3dfa553<span style="color: #000000; font-weight: bold;">/</span>gpu<span style="color: #000000; font-weight: bold;">/</span>nvidia<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> python_modules<span style="color: #000000; font-weight: bold;">/</span>nvidia.py <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> conf.d<span style="color: #000000; font-weight: bold;">/</span>nvidia.pyconf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Restart gmond</span>
  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmond restart
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># copy_ganglia_gmond_python_frontend.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to copy relevant files from ganglia-gmond_python_modules to Ganglia, </span>
<span style="color: #666666; font-style: italic;"># apply patch for Ganglia web interface and restart necessary services </span>
<span style="color: #666666; font-style: italic;"># Assumes that ganglia-gmond_python_modules-3dfa553.tar.gz is in /share/apps/src/nvidia_ganglia/</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script and run this only on front end</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Apply web patch for Ganglia to display custom graphs</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> <span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nvidia_ganglia<span style="color: #000000; font-weight: bold;">/</span>ganglia-gmond_python_modules-3dfa553.tar.gz
  <span style="color: #7a0874; font-weight: bold;">cd</span> ganglia-gmond_python_modules-3dfa553<span style="color: #000000; font-weight: bold;">/</span>gpu<span style="color: #000000; font-weight: bold;">/</span>nvidia<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> graph.d<span style="color: #000000; font-weight: bold;">/*</span>.php <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>graph.d<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p0</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>ganglia-gmond_python_modules-3dfa553<span style="color: #000000; font-weight: bold;">/</span>gpu<span style="color: #000000; font-weight: bold;">/</span>nvidia<span style="color: #000000; font-weight: bold;">/</span>ganglia_web.patch
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Restart necessary services</span>
  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmetad restart
  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmond restart
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"><br />
Upon pointing the browser to the <code>http://FQDN/ganglia/</code> (e.g., <a href="http://paracuda.math.mtu.edu/ganglia/" target="_blank"><code>http://paracuda.math.mtu.edu/ganglia/</code></a> &#8211; the link will probably die or be changed to something else in due course), the display should include information about GPU as well, as shown in screenshots below:</p>
<p><br clear="all"></p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_51.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_51.png' alt='ganglia_gpu_51' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_52.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_52.png' alt='ganglia_gpu_52' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_53.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_53.png' alt='ganglia_gpu_53' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_54.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_54.png' alt='ganglia_gpu_54' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_55.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_55.png' alt='ganglia_gpu_55' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_56.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_56.png' alt='ganglia_gpu_56' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_57.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_57.png' alt='ganglia_gpu_57' class='ngg-singlepic ngg-left' /></a> </p>
<p><br clear="all"><br />
<br clear="all"><br />
With little more work, the rather unaesthetic looking Ganglia web interface can be made to look like a given institution&#8217;s theme:</p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_58.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_58.png' alt='ganglia_gpu_58' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_59.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_59.png' alt='ganglia_gpu_59' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_60.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_60.png' alt='ganglia_gpu_60' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_61.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_61.png' alt='ganglia_gpu_61' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_62.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_62.png' alt='ganglia_gpu_62' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_63.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_63.png' alt='ganglia_gpu_63' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_64.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_64.png' alt='ganglia_gpu_64' class='ngg-singlepic ngg-left' /></a> </p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_65.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_65.png' alt='ganglia_gpu_65' class='ngg-singlepic ngg-left' /></a>  <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_66.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_66.png' alt='ganglia_gpu_66' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_67.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_67.png' alt='ganglia_gpu_67' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_68.png' title='' rel="lightbox[3238]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_68.png' alt='ganglia_gpu_68' class='ngg-singlepic ngg-left' /></a> </p>
<p><br clear="all"><br />
<br clear="all"></p>
<h3 class="blog">Thanks be to</h3>
<p><a href="http://www.math.mtu.edu/~struther/" target="_blank" class="underline">Dr. Allan Struthers</a> for letting his <code>paracuda.math</code> be used for this purpose; my friendly neighbors for their kindness in letting me borrow a NVIDIA Quadro 6000 card; Robert Alexander of NVIDIA (<a href="http://developer.nvidia.com/ganglia-monitoring-system/" target="_blank" class="underline">http://developer.nvidia.com/ganglia-monitoring-system/</a>), <a href="http://www.lbl.gov/" target="_blank" class="underline">Bernard Li of Lawrence Berkeley National Laboratory</a> and <a href="http://www.ncsa.illinois.edu/" target="_blank" class="underline">Jeremy Enos of National Center for Supercomputing Applications</a> for developing this <code>gmond Python module</code> as well as making time to answer my questions.</p>
<p><br clear="all"><br />
</p>
<h3 class="blog">Near Future Work</h3>
<p>Work is currently underway to include all of the compute node related steps in the above described procedure in the local Rocks distribution, so that the compute nodes get them as soon as they are installed.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2012%2F02%2F17%2Fganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rocks-5-4-2%2F&amp;title=Ganglia%20%E2%80%93%20gmond%20Python%20Module%20For%20Monitoring%20NVIDIA%20GPU%20On%20Rocks%205.4.2" id="wpa2a_6"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2012/02/17/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rocks-5-4-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CUDA/C &#8211; Hello, World!</title>
		<link>http://sgowtham.net/blog/2012/02/13/cuda-c-hello-world/</link>
		<comments>http://sgowtham.net/blog/2012/02/13/cuda-c-hello-world/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 20:10:01 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[MichiganTech]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3223</guid>
		<description><![CDATA[Disclaimer The instructions/steps/programs given below worked for me (and Michigan Technological University) running site licensed Red Hat Enterprise Linux 6.2, with NVIDIA CUDA SDK 4.1.28, NVIDIA GPU Driver v290.10 &#038; two NVIDIA GeForce GTX 570 cards &#8211; as has been a common practice for several years now, a full version of Operating System was installed [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>The instructions/steps/programs given below worked for me (and <a href="http://www.mtu.edu/" target="_blank" class="underline">Michigan Technological University</a>) running site licensed Red Hat Enterprise Linux 6.2, with NVIDIA CUDA SDK 4.1.28, NVIDIA GPU Driver v290.10 &#038; two NVIDIA GeForce GTX 570 cards &#8211; as has been a common practice for several years now, a full version of Operating System was installed and all necessary patches/upgrades have been applied. These instructions may very well work for you (or your institution), on Red Hat-like or other linux distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <em>sgowtham.net</em>, nor its author (or Michigan Technological University) is responsible for any/all damage – intellectual and/or otherwise.</p>
<p><br clear="all"></p>
<h3 class="blog">The Program</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* hello_world_cuda.cu
   A CUDA C PROGRAM TO PRINT 'HELLO, WORLD!' TO THE SCREEN
&nbsp;
   TESTED SUCCESSFULLY WITH CUDA SDK 4.1.28 AND NVIDIA GPU DRIVER 
   VERSION 290.10 RUNNING ON NVIDIA GeForce GTX 270
&nbsp;
   COMPILATION:
   #1: NON-MAKEFILE APPROACH
       nvcc -g hello_world_cuda.cu -o hello_world_cuda.x
&nbsp;
   #2. MAKEFILE APPROACH (USE THE ASSOCIATED Makefile)
       make
&nbsp;
   EXECUTION:
   ./hello_world_cuda.x
&nbsp;
   PORTIONS OF THE COMMENTS ARE ADOPTED FROM
&nbsp;
     NVIDIA CUDA C
     PROGRAMMING GUIDE
     VERSION 4.0 (5/6/2011)
&nbsp;
   FIRST WRITTEN: GOWTHAM; Mon, 13 Feb 2012 14:06:30 -0500
   LAST MODIFIED: GOWTHAM; Mon, 13 Feb 2012 14:15:30 -0500
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
   REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;cuda.h&gt;   /* CUDA related stuff                                   */</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* KERNEL DEFINITION
   CUDA C EXTENDS C BY ALLOWING THE PROGRAMMER TO DEFINE C FUNCTIONS,
   CALLED KERNELS, THAT, WHEN CALLED, ARE EXECUTED N TIMES IN PARALLEL
   BY N DIFFERENT CUDA THREADS, AS OPPOSED TO ONLY ONCE LIKE REGULAR
   C FUNCTIONS
&nbsp;
   A KERNEL IS DEFINED USING THE __global__ DECLARATION SPECIFIER.
   THE NUMBER OF CUDA THREADS THAT EXECUTE THAT KERNEL FOR A GIVEN
   KERNEL CALL IS SPECIFIED USING &lt;&lt;&lt; &gt;&gt;&gt; (EXECUTION CONFIGURATION)
   SYNTAX. EXECUTION CONFIGURATION DEFINES THE DIMENSION OF THE
   GRIDS AND BLOCKS THAT WILL BE USED TO EXECUTE THE FUNCTION ON THE 
   DEVICE AS WELL AS THE ASSOCIATED STREAM
&nbsp;
   EACH THREAD THAT EXECUTES THE KERNEL IS GIVEN A UNIQUE 'THREAD ID'
   THAT IS ACCESSIBLE WITHIN THE KERNEL THROUGH THE BUILT-IN
   threadIdx VARIABLE
&nbsp;
   A FUNCTION DECLARED AS
&nbsp;
     __global__ void Function(float* parameter);
&nbsp;
   MUST BE CALLED AS FOLLOWS:
&nbsp;
     Function&lt;&lt;&lt; Dg, Db, Ns &gt;&gt;&gt;(parameter);
&nbsp;
   WHERE
&nbsp;
   -- Dg : OF TYPE dim3, IT SPECIFIES THE DIMENSION AND SIZE OF THE GRID
           SUCH THAT Dg.x * Dg.y * Dg.z EQUALS THE NUMBER OF BLOCKS BEING
           LAUNCHED
&nbsp;
   -- Db : OF TYPE dim3, IT SPECIFIES THE DIMENSION AND SIZE OF EACH BLOCK
           SUCH THAT Db.x * Db.y * Db.z EQUALS THE NUMBER OF THREADS PER
           BLOCK
&nbsp;
   -- Ns : OF TYPE size_t, IT SPECIFIES THE NUMBER OF BYTES IN SHARED MEMORY
           THAT IS DYNAMICALLY ALLOCATED PER BLOCK FOR THIS CALL IN ADDITION
           TO THE STATICALLY ALLOCATED MEMORY. THIS DYNAMICALLY ALLOCATED
           MEMORY IS USED BY ANY OF THE VARIABLES DECLARED AS AN EXTERNAL
           ARRAY. NOTE THAT THIS IS AN OPTIONAL ARGUMENT THAT DEFAULTS TO 0
&nbsp;
   -- S  : OF TYPE cudaStream_t, IT SPECIFIES THE ASSOCIATED STREAM. THIS
           TOO IS AN OPTIONAL ARGUMENT THAT DEFAULTS TO 0
*/</span>
__global__ <span style="color: #993333;">void</span> kernel<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Dg = 1; Db = 1; Ns = 0; S = 0 */</span>
  kernel<span style="color: #339933;">&lt;&lt;&lt;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">&gt;&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* PRINT 'HELLO, WORLD!' TO THE SCREEN */</span>
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Hello, World!<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="make" style="font-family:monospace;"><span style="color: #339900; font-style: italic;"># Simple version of the Makefile used to systematically compile</span>
<span style="color: #339900; font-style: italic;"># one of many CUDA C programs, taking into account respective dependencies</span>
<span style="color: #339900; font-style: italic;"># </span>
<span style="color: #339900; font-style: italic;"># First written: Gowtham; Mon, 13 Feb 2012 14:32:36 -0500</span>
<span style="color: #339900; font-style: italic;"># Last modified: Gowtham; Mon, 13 Feb 2012 15:00:42 -0500</span>
<span style="color: #339900; font-style: italic;"># </span>
&nbsp;
<span style="color: #339900; font-style: italic;"># Necessary variables</span>
CC        <span style="color: #004400;">=</span> nvcc
CFLAGS    <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>g <span style="color: #004400;">-</span>c
OFLAGS    <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>O3
MYPROGRAM <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CPROGRAM</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #339900; font-style: italic;"># If CPROGRAM is not defined (and hence MYPROGRAM is empty), </span>
<span style="color: #339900; font-style: italic;"># display help message</span>
<span style="color: #666622; font-weight: bold;">ifndef</span> CPROGRAM
help<span style="color: #004400;">:</span>
<span style="color: #666622; font-weight: bold;">endif</span>
&nbsp;
<span style="color: #339900; font-style: italic;"># Default target</span>
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>x
&nbsp;
<span style="color: #339900; font-style: italic;"># Print help message</span>
help<span style="color: #004400;">:</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;  To compile, choose one of the following:&quot;</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;    make CPROGRAM=hello_world_cuda&quot;</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;  To clean, choose one of the following:&quot;</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;    make clean-all-programs&quot;</span>
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;    make clean CPROGRAM=hello_world_cuda&quot;</span>
	<span style="color: #004400;">@</span>echo
&nbsp;
&nbsp;
<span style="color: #339900; font-style: italic;"># $(MYPROGRAM).x (depends on $(MYPROGRAM).o)</span>
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>x<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>o
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>o <span style="color: #004400;">-</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>x
&nbsp;
&nbsp;
<span style="color: #339900; font-style: italic;"># $(MYPROGRAM).o (depends on $(MYPROGRAM).cu)</span>
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>cu
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>cu
&nbsp;
&nbsp;
<span style="color: #339900; font-style: italic;"># Remove the appropriate object file and executable</span>
clean<span style="color: #004400;">:</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;Deleting $(MYPROGRAM).o and $(MYPROGRAM).x&quot;</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MYPROGRAM</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>x
	<span style="color: #004400;">@</span>echo
&nbsp;
<span style="color: #339900; font-style: italic;"># Remove all object files and executables</span>
clean<span style="color: #004400;">-</span>all<span style="color: #004400;">-</span>programs<span style="color: #004400;">:</span>
	<span style="color: #004400;">@</span>echo
	<span style="color: #004400;">@</span>echo <span style="color: #CC2200;">&quot;Deleting *.o and *.x files&quot;</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*.</span>o <span style="color: #004400;">*.</span>x
	<span style="color: #004400;">@</span>echo</pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2012%2F02%2F13%2Fcuda-c-hello-world%2F&amp;title=CUDA%2FC%20%26%238211%3B%20Hello%2C%20World%21" id="wpa2a_8"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2012/02/13/cuda-c-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ganglia &#8211; gmond Python Module For Monitoring NVIDIA GPU On RHEL 6.2</title>
		<link>http://sgowtham.net/blog/2012/02/11/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rhel-6-2/</link>
		<comments>http://sgowtham.net/blog/2012/02/11/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rhel-6-2/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 22:27:29 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[CUDA]]></category>
		<category><![CDATA[Ganglia]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MichiganTech]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Rocks]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3154</guid>
		<description><![CDATA[Disclaimer The instructions/steps given below worked for me (and Michigan Technological University) running site licensed Red Hat Enterprise Linux 6.2 &#8211; as has been a common practice for several years now, a full version of Operating System was installed and all necessary patches/upgrades have been applied. These instructions may very well work for you (or [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>The instructions/steps given below worked for me (and <a href="http://www.mtu.edu/" target="_blank" class="underline">Michigan Technological University</a>) running site licensed Red Hat Enterprise Linux 6.2 &#8211; as has been a common practice for several years now, a full version of Operating System was installed and all necessary patches/upgrades have been applied. These instructions may very well work for you (or your institution), on Red Hat-like or other linux distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <em>sgowtham.net</em>, nor its author (or Michigan Technological University) is responsible for any/all damage – intellectual and/or otherwise.</p>
<p><br clear="all"></p>
<h3 class="blog">A Bit About Ganglia (gmond &amp; gmetad)</h3>
<p>Citing <a href="http://ganglia.info/" target="_blank" class="underline">Ganglia website</a>, </p>
<blockquote><p>Ganglia is a scalable distributed monitoring system for high-performance computing systems such as clusters and Grids. It is based on a hierarchical design targeted at federations of clusters. It leverages widely used technologies such as XML for data representation, XDR for compact, portable data transport, and RRD tool for data storage and visualization. It uses carefully engineered data structures and algorithms to achieve very low per-node overheads and high concurrency. The implementation is robust, has been ported to an extensive set of operating systems and processor architectures, and is currently in use on thousands of clusters around the world. It has been used to link clusters across university campuses and around the world and can scale to handle clusters with 2000 nodes.</p></blockquote>
<p>Further, citing <a href="http://en.wikipedia.org/wiki/Ganglia_%28software%29" class="underline" target="_blank">Wikipedia</a>,</p>
<blockquote><p><code>gmond</code> (Ganglia Monitoring Daemon) is a multi-threaded daemon which runs on each cluster node that needs to be monitored. Installation does not require having a common NFS file system or a database back-end, install special accounts or maintain configuration files. It has four main responsibilities: monitor changes in host state; announce relevant changes; listen to the state of all other ganglia nodes via a unicast or multicast channel; answer requests for an XML description of the cluster state.</p>
<p>Each <code>gmond</code> transmits in information in two different ways: unicasting or multicasting host state in external data representation (XDR) format using UDP messages OR sending XML over a TCP connection.</p>
<p>Federation in Ganglia is achieved using a tree of point-to-point connections amongst representative cluster nodes to aggregate the state of multiple clusters. At each node in the tree, a Ganglia Meta Daemon (<code>gmetad</code>) periodically polls a collection of child data sources, parses the collected XML, saves all numeric, volatile metrics to round-robin databases and exports the aggregated XML over a TCP sockets to clients. Data sources may be either <code>gmond</code> daemons, representing specific clusters, or other <code>gmetad</code> daemons, representing sets of clusters. Data sources use source IP addresses for access control and can be specified using multiple IP addresses for fail over. The latter capability is natural for aggregating data from clusters since each <code>gmond</code> daemon contains the entire state of its cluster.</p>
<p>The Ganglia web front-end provides a view of the gathered information via real-time dynamic web pages. Most importantly, it displays Ganglia data in a meaningful way for system administrators and computer users. Although the web front-end to ganglia started as a simple HTML view of the XML tree, it has evolved into a system that keeps a colourful history of all collected data. The Ganglia web front-end caters to system administrators and users (for e.g., one can view the CPU utilization over the past hour, day, week, month, or year). The web front-end shows similar graphs for memory usage, disk usage, network statistics, number of running processes, and all other Ganglia metrics. The web front-end depends on the existence of the <code>gmetad</code> which provides it with data from several Ganglia sources. </p>
<p>Specifically, the web front-end will open the local port 8651 (by default) and expects to receive a Ganglia XML tree. The web pages themselves are highly dynamic; any change to the Ganglia data appears immediately on the site. This behaviour leads to a very responsive site, but requires that the full XML tree be parsed on every page access. Therefore, the Ganglia web front-end should run on a fairly powerful, dedicated machine if it presents a large amount of data. The Ganglia web front-end is written in the PHP scripting language, and uses graphs generated by <code>gmetad</code> to display history information.
</p></blockquote>
<p><br clear="all"></p>
<h3 class="blog">Installation</h3>
<p>In order to make sure that none of the required steps are missed when performing similar installation on other machines (or repeating on the same machine), a BASH script was written.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_ganglia.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to install Ganglia on RHEL 6.2</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Enable EPEL repository</span>
  <span style="color: #666666; font-style: italic;"># EPEL: Extra Packages for Enterprise Linux</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>download.fedoraproject.org<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>epel<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">6</span><span style="color: #000000; font-weight: bold;">/</span>i386<span style="color: #000000; font-weight: bold;">/</span>epel-release-<span style="color: #000000;">6</span>-<span style="color: #000000;">5</span>.noarch.rpm
  rpm <span style="color: #660033;">-ivh</span> epel-release-<span style="color: #000000;">6</span>-<span style="color: #000000;">5</span>.noarch.rpm
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Install Ganglia</span>
  yum <span style="color: #c20cb9; font-weight: bold;">install</span> ganglia ganglia-gmetad ganglia-gmond ganglia-web ganglia-gmond-python
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Make sure httpd, gmond and gmetad automatically start after each reboot</span>
  chkconfig <span style="color: #660033;">--level</span> <span style="color: #000000;">345</span> httpd on
  chkconfig <span style="color: #660033;">--level</span> <span style="color: #000000;">345</span> gmond on
  chkconfig <span style="color: #660033;">--level</span> <span style="color: #000000;">345</span> gmetad on
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Configuration</h3>
<p>Assuming all went well so far, one can expect to have the following files: <code>/etc/ganglia/gmetad.conf</code>, <code>/etc/ganglia/gmond.conf</code> and<br />
<code>/etc/httpd/conf.d/ganglia.conf</code></p>
<p><br clear="all"><br />
Edit <code>/etc/ganglia/gmetad.conf</code> to have the following line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">data_source <span style="color: #ff0000;">&quot;dirac.dcs&quot;</span> localhost:<span style="color: #000000;">8649</span></pre></td></tr></table></div>

<p><br clear="all"><br />
<code>/etc/ganglia/gmond.conf</code> will have the following edits:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * The cluster attributes specified will be used as part of the &lt;CLUSTER&gt;
 * tag that will wrap all hosts collected by this instance.
 */</span>
cluster <span style="color: #009900;">&#123;</span>
  name <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;dirac.dcs&quot;</span>
  owner <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Michigan Technological University&quot;</span>
  latlong <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;N47.11 W88.57&quot;</span>
  url <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;http://www.it.mtu.edu/&quot;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Feel free to specify as many udp_send_channels as you like.
 * Gmond used to only support having a single channel 
*/</span>
udp_send_channel <span style="color: #009900;">&#123;</span>
  bind_hostname <span style="color: #339933;">=</span> yes  <span style="color: #339933;"># Highly recommended, soon to be default.</span>
                       <span style="color: #339933;"># This option tells gmond to use a source address</span>
                       <span style="color: #339933;"># that resolves to the machine's hostname.  Without</span>
                       <span style="color: #339933;"># this, the metrics may appear to come from any</span>
                       <span style="color: #339933;"># interface and the DNS names associated with</span>
                       <span style="color: #339933;"># those IPs will be used to create the RRDs.</span>
  mcast_join <span style="color: #339933;">=</span> 239.2.11.71
  host <span style="color: #339933;">=</span> dirac.<span style="color: #202020;">dcs</span>.<span style="color: #202020;">it</span>.<span style="color: #202020;">mtu</span>.<span style="color: #202020;">edu</span>
  port <span style="color: #339933;">=</span> <span style="color: #0000dd;">8649</span>
  ttl <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* You can specify as many udp_recv_channels as you like as well. */</span>
udp_recv_channel <span style="color: #009900;">&#123;</span>
  mcast_join <span style="color: #339933;">=</span> 239.2.11.71
  port <span style="color: #339933;">=</span> <span style="color: #0000dd;">8649</span>
  bind <span style="color: #339933;">=</span> 239.2.11.71
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* You can specify as many tcp_accept_channels as you like to share
 * an xml description of the state of the cluster 
*/</span>
tcp_accept_channel <span style="color: #009900;">&#123;</span>
  port <span style="color: #339933;">=</span> <span style="color: #0000dd;">8649</span>
  acl <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">default</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;deny&quot;</span>
&nbsp;
    access <span style="color: #009900;">&#123;</span>
      ip <span style="color: #339933;">=</span> 127.0.0.1
      mask <span style="color: #339933;">=</span> <span style="color: #0000dd;">32</span>
      action <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;allow&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
<code>/etc/httpd/conf.d/ganglia.conf</code> will have the following edits:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Ganglia monitoring system php web frontend</span>
  <span style="color: #666666; font-style: italic;">#</span>
&nbsp;
  Alias <span style="color: #000000; font-weight: bold;">/</span>ganglia <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>ganglia
&nbsp;
  <span style="color: #000000; font-weight: bold;">&lt;</span>Location <span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">&gt;</span>
    Order deny,allow
    <span style="color: #666666; font-style: italic;"># Deny from all</span>
    <span style="color: #666666; font-style: italic;"># Allow from 127.0.0.1</span>
    <span style="color: #666666; font-style: italic;"># Allow from ::1</span>
    <span style="color: #666666; font-style: italic;"># # Allow from .example.com</span>
    Allow from all
  <span style="color: #000000; font-weight: bold;">&lt;/</span>Location<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
The firewall needs to be modified so that it accepts UDP &#038; TCP requests on port 8649. To that effect, <code>/etc/sysconfig/iptables</code> will have the following lines:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Ganglia gmond/gmetad</span>
<span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> udp <span style="color: #660033;">-p</span> udp <span style="color: #660033;">--dport</span> <span style="color: #000000;">8649</span> <span style="color: #660033;">-j</span> ACCEPT
<span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> tcp <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">8649</span> <span style="color: #660033;">-j</span> ACCEPT
<span style="color: #666666; font-style: italic;">#</span></pre></td></tr></table></div>

<p><br clear="all"><br />
(Re)Start the necessary services:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>iptables restart
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmetad restart
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmond restart
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>httpd restart</pre></td></tr></table></div>

<p><br clear="all"><br />
After a few minutes of collecting the data and upon pointing the browser to the <code>http://FQDN/ganglia/</code>, the web page should display the relevant information.</p>
<p>If, instead of relevant information, the web page displays the following error message</p>
<blockquote><p>There was an error collecting ganglia data (127.0.0.1:8652): fsockopen error: Permission denied</p></blockquote>
<p>then, more often than not, it hints to a <code>selinux</code> related issue. Edit the file, <code>/etc/sysconfig/selinux</code>, to look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="http" style="font-family:monospace;"># This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted</pre></td></tr></table></div>

<p><br clear="all"><br />
Restart the machine and point the browser to the <code>http://FQDN/ganglia/</code>, the web page should display the relevant information.</p>
<p><br clear="all"></p>
<h3 class="blog">Monitoring NVIDIA GPU</h3>
<p>The aforementioned set up works fine and as expected but it doesn&#8217;t necessarily provide any information about GPU(s) that may be part of the hardware. For e.g., the test machine used in our case has two NVIDIA GeForce GTX 570 cards. With more and more scientific &amp; engineering computations tending towards GPU based computing, it&#8217;d be useful to include their status/usage information in Ganglia&#8217;s web portal. To this effect, NVIDIA released <a href="http://developer.nvidia.com/ganglia-monitoring-system" target="_blank"><code>gmond</code> Python module for GPUs</a> (made aware of it by one of Michigan Tech ITSS directors). The instructions given in the NVIDIA-linked pages do work as described and are included here for the sake of completeness.</p>
<p><br clear="all"></p>
<h4>Python Bindings for the NVIDIA Management Library</h4>
<p>This provides Python access to static information and monitoring data for NVIDIA GPUs, as well as management capabilities. It exposes the functionality of the <a href="http://developer.nvidia.com/nvidia-management-library-nvml" target="_blank" class="underline">NVML</a> and one may download these from <a href="http://pypi.python.org/pypi/nvidia-ml-py/" target="_blank" class="underline">here</a> &#8211; as before, the necessary steps are included in a BASH script.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># install_python_nvml_bindings.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to download and install Python Bindings for the NVML</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
<span style="color: #666666; font-style: italic;"># Does not work with Python 2.4 - needs higher/more recent version</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Download and install</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>pypi.python.org<span style="color: #000000; font-weight: bold;">/</span>packages<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>n<span style="color: #000000; font-weight: bold;">/</span>nvidia-ml-py<span style="color: #000000; font-weight: bold;">/</span>nvidia-ml-py-2.285.01.tar.gz
&nbsp;
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> nvidia-ml-py-2.285.01.tar.gz
  <span style="color: #7a0874; font-weight: bold;">cd</span> nvidia-ml-py-2.285.01
  python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h4><code>gmond</code> Python Module For Monitoring NVIDIA GPUs using NVML</h4>
<p>After downloading <code>ganglia-gmond_python_modules-3dfa553.tar.gz</code> from <a href="https://github.com/ganglia/gmond_python_modules/tree/master/gpu/nvidia" target="_blank" class="underline">GitHub for ganglia / gmond_python_modules</a> to <code>/tmp/</code>, the following steps need to be performed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># copy_ganglia_gmond_python.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to copy Copy nvidia_smi.py &amp; pynvml.py to </span>
<span style="color: #666666; font-style: italic;"># /usr/lib64/ganglia/python_modules/, relevant files from </span>
<span style="color: #666666; font-style: italic;"># ganglia-gmond_python_modules to Ganglia, apply patch</span>
<span style="color: #666666; font-style: italic;"># for Ganglia web interface and restart necessary services</span>
<span style="color: #666666; font-style: italic;"># Must be root (or at least have sudo privilege) to run this script</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Begin root-check IF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$UID</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  You must be logged in as root!&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Exiting...&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Copy nvidia_smi.py &amp; pynvml.py to /usr/lib64/ganglia/python_modules/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nvidia-ml-py-2.285.01<span style="color: #000000; font-weight: bold;">/</span>nvidia_smi.py <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nvidia-ml-py-2.285.01<span style="color: #000000; font-weight: bold;">/</span>pynvml.py <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Copy relevant files to Ganglia</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> ganglia-gmond_python_modules-3dfa553.tar.gz
  <span style="color: #7a0874; font-weight: bold;">cd</span> ganglia-gmond_python_modules-3dfa553
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">cd</span> gpu<span style="color: #000000; font-weight: bold;">/</span>nvidia<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> python_modules<span style="color: #000000; font-weight: bold;">/</span>nvidia.py <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>python_modules<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> conf.d<span style="color: #000000; font-weight: bold;">/</span>nvidia.pyconf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">cp</span> graph.d<span style="color: #000000; font-weight: bold;">/*</span>.php <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>graph.d<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Apply web patch for Ganglia to display custom graphs</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>ganglia<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p0</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>ganglia-gmond_python_modules-3dfa553<span style="color: #000000; font-weight: bold;">/</span>gpu<span style="color: #000000; font-weight: bold;">/</span>nvidia<span style="color: #000000; font-weight: bold;">/</span>ganglia_web.patch
&nbsp;
  <span style="color: #666666; font-style: italic;">#</span>
  <span style="color: #666666; font-style: italic;"># Restart necessary services</span>
  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmetad restart
  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>gmond restart
&nbsp;
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># End root-check IF</span></pre></td></tr></table></div>

<p><br clear="all"><br />
Upon pointing the browser to the <code>http://FQDN/ganglia/</code> (e.g., <a href="http://dirac.dcs.it.mtu.edu/ganglia/" target="_blank"><code>http://dirac.dcs.it.mtu.edu/ganglia/</code></a> &#8211; the link will probably die or be changed to something else in due course), the display should include information about GPU as well, as shown in screenshots below:</p>
<p><br clear="all"></p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_00.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_00.png' alt='ganglia_gpu_00' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_01.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_01.png' alt='ganglia_gpu_01' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_02.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_02.png' alt='ganglia_gpu_02' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_03.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_03.png' alt='ganglia_gpu_03' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_04.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_04.png' alt='ganglia_gpu_04' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_05.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_05.png' alt='ganglia_gpu_05' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_06.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_06.png' alt='ganglia_gpu_06' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_07.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_07.png' alt='ganglia_gpu_07' class='ngg-singlepic ngg-center' /></a></p>
<p><br clear="all"><br />
With little more work, the rather unaesthetic looking Ganglia web interface can be made to look like a given institution&#8217;s theme:</p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_08.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_08.png' alt='ganglia_gpu_08' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_09.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_09.png' alt='ganglia_gpu_09' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_10.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_10.png' alt='ganglia_gpu_10' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_11.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_11.png' alt='ganglia_gpu_11' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_12.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_12.png' alt='ganglia_gpu_12' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_13.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_13.png' alt='ganglia_gpu_13' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_14.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_14.png' alt='ganglia_gpu_14' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_15.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_15.png' alt='ganglia_gpu_15' class='ngg-singlepic ngg-center' /></a> </p>
<p><a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_16.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_16.png' alt='ganglia_gpu_16' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_17.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_17.png' alt='ganglia_gpu_17' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_18.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_18.png' alt='ganglia_gpu_18' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_19.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_19.png' alt='ganglia_gpu_19' class='ngg-singlepic ngg-left' /></a> <a class="shutterset_" href='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/ganglia_gpu_20.png' title='' rel="lightbox[3154]"><img src='http://sgowtham.net/blog/wp-content/gallery/ganglia_gpu/thumbs/thumbs_ganglia_gpu_20.png' alt='ganglia_gpu_20' class='ngg-singlepic ngg-left' /></a></p>
<p><br clear="all"><br />
</p>
<h3 class="blog">Near Future Work</h3>
<p>Work is currently underway, most certainly with help from <a href="http://marc.info/?l=npaci-rocks-discussion" target="_blank" class="underline">the ever-awesome Rocks mailing list</a>, to integrate the above into a NPACI Rocks 5.4.2 cluster with compute nodes having one or more GPUs. Another post will come along as and when this work has been completed and tested.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2012%2F02%2F11%2Fganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rhel-6-2%2F&amp;title=Ganglia%20%26%238211%3B%20gmond%20Python%20Module%20For%20Monitoring%20NVIDIA%20GPU%20On%20RHEL%206.2" id="wpa2a_10"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2012/02/11/ganglia-gmond-python-module-for-monitoring-nvidia-gpu-on-rhel-6-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BASH &#8211; Smart Extract Utility</title>
		<link>http://sgowtham.net/blog/2011/05/04/bash-smart-extract-utility/</link>
		<comments>http://sgowtham.net/blog/2011/05/04/bash-smart-extract-utility/#comments</comments>
		<pubDate>Wed, 04 May 2011 19:38:34 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3090</guid>
		<description><![CDATA[Why This Script? As a systems administrator or as someone who dabbles in compilation technologies, it often becomes necessary to extract one or more variety of compressed files/folders. This script will automagically handle the options necessary for uncompressing different kinds of compressions. The Script 1 2 3 4 5 6 7 8 9 10 11 [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Why This Script?</h3>
<p>As a systems administrator or as someone who dabbles in compilation technologies, it often becomes necessary to extract one or more variety of compressed files/folders. This script will automagically handle the options necessary for uncompressing different kinds of compressions.</p>
<p><br clear="all"></p>
<h3 class="blog">The Script</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># BASH script to automagically uncompress different kinds of compressed files/folders</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Usage: extract.sh [COMPRESSED_FILENAME]</span>
<span style="color: #666666; font-style: italic;"># First written: Wed, 04 May 2011 05:14:10 -0400</span>
<span style="color: #666666; font-style: italic;"># Last modified: Wed, 04 May 2011 05:14:10 -0400</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #007800;">E_BADARGS</span>=<span style="color: #000000;">65</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Usage: <span style="color: #780078;">`basename $0`</span> [COMPRESSED_FILENAME]&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #007800;">$E_BADARGS</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">in</span>
      <span style="color: #000000; font-weight: bold;">*</span>.tar<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvf</span>  <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.tgz<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.tar.gz<span style="color: #7a0874; font-weight: bold;">&#41;</span>    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.tbz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>      <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvjf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.tar.bz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvjf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.gz<span style="color: #7a0874; font-weight: bold;">&#41;</span>        <span style="color: #c20cb9; font-weight: bold;">gunzip</span>    <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.bz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">bunzip2</span>   <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span>.zip<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">unzip</span>     <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>           <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  '$1' file type unknown&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
  <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  '$1' is not a regular file&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Use it as a function!</h3>
<p>Add the following lines to <code>${HOME}/.bashrc</code> (and remember to source it).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Smart extract function</span>
extract <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Usage: extract [COMPRESSED_FILENAME]&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
    <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">in</span>
        <span style="color: #000000; font-weight: bold;">*</span>.tar<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvf</span>  <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.tgz<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.tar.gz<span style="color: #7a0874; font-weight: bold;">&#41;</span>    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.tbz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>      <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvjf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.tar.bz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvjf</span> <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.gz<span style="color: #7a0874; font-weight: bold;">&#41;</span>        <span style="color: #c20cb9; font-weight: bold;">gunzip</span>    <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.bz2<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">bunzip2</span>   <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span>.zip<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #c20cb9; font-weight: bold;">unzip</span>     <span style="color: #007800;">$1</span>  <span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>           <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  '$1' file type unknown&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
      <span style="color: #000000; font-weight: bold;">esac</span>
    <span style="color: #000000; font-weight: bold;">else</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  '$1' is not a regular file&quot;</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2011%2F05%2F04%2Fbash-smart-extract-utility%2F&amp;title=BASH%20%26%238211%3B%20Smart%20Extract%20Utility" id="wpa2a_12"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2011/05/04/bash-smart-extract-utility/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; 2 Dimensional Heat Equation [Square Grid]</title>
		<link>http://sgowtham.net/blog/2010/12/11/mpi-c-2-dimensional-heat-equation-square-grid/</link>
		<comments>http://sgowtham.net/blog/2010/12/11/mpi-c-2-dimensional-heat-equation-square-grid/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 22:54:28 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3055</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Program Listing 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 [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* heat_equation_2d.c
 * PARALLEL [MPI] C PROGRAM TO SOLVE THE 2 DIMENSIONAL HEAT EQUATION.
 * THE INITIAL TEMPERATURE IS COMPUTED TO BE HIGH IN THE MIDDLE OF THE
 * DOMAIN UNDER CONSIDERATION AND ZERO AT THE BOUNDARIES. THE BOUNDARIES
 * ARE HELD AT ZERO TEMPERATURE THROUGH OUT THE SIMULATION.
 *
 * THE GIVEN GRID IS DECOMPOSED BY THE MASTER AND THEN DISTRIBUTED BY
 * COLUMNS TO WORKERS. A GRID POINT'S CURRENT TEMPERATURE DEPENDS UPON 
 * ITS PREVIOUS TIME STEP VALUE AS WELL AS THE VALUES OF THE NEIGHBORING
 * GRID POINTS. AS SUCH, AT EACH TIME STEP, WORKERS EXCHANGE BORDER/BOUNDARY
 * DATA WITH NEIGHBORS. ONCE ALL THE TIME STEPS ARE COMPLETED, WORKERS
 * SEND THEIR RESULTS TO MASTER.
 *
 * THE PROGRAM GENERATES A DATA FILE THAT CAN BE VISUALIZED WITH THIRD 
 * PARTY PROGRAMS [GNUPLOT, MATLAB, ETC.]
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * ORIGINAL AUTHOR : BLAISE BARNEY; 2009/12/11
 *                   LAWRENCE LIVERMORE NATIONAL LABORATORY
 * FIRST COPIED    : GOWTHAM; Fri, 26 Nov 2010 21:30:30 -0500
 * LAST MODIFIED   : GOWTHAM; Fri, 10 Dec 2010 01:34:52 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/12/11/mpi-c-2-dimensional-heat-equation/
 *
 * COMPILATION:
 * mpicc -g -Wall -lm heat_equation_2d.c -o heat_equation_2d.x 
 *
 * EXECUTION:
 * mpirun -np NPROC -machinefile MACHINEFILE ./heat_equation_2d.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM;
 *               NXY [GRID SIZE] MUST BE DIVISIBLE BY [NPROC - 1]
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
 * OUTPUT      : DATA FILE 'heat_equation_2d.dat' WITH THE FOLLOWING
 *               FORMAT
 *
 *               TSTEPS=#TSTEPS
 *               U(x, y) AT t = 0         [NXY x NXY]
 *               U(x, y) AT t = 1         [NXY x NXY]
 *               ...
 *               U(x, y) AT t = TSTEP     [NXY x NXY]
 *
 *               TOTAL NUMBER OF LINES IN 'heat_equation_2d.dat'
 *               (NXY * (TSTEPS + 1)) + 1
 *
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #339933;">#define NXY          20          /* Number of (x or y) grids in U(x,y) */</span>
                                 <span style="color: #808080; font-style: italic;">/* NXY must be divisible by NPROC - 1 */</span>
<span style="color: #339933;">#define TSTEPS      500          /* Number of time steps               */</span>
<span style="color: #339933;">#define MASTER        0          /* Process ID for MASTER              */</span>
<span style="color: #339933;">#define BEGIN         1          /* Message tag                        */</span>
<span style="color: #339933;">#define DONE          2          /* Message tag                        */</span>
<span style="color: #339933;">#define LNEIGHBOR     3          /* Left neighbor tag                  */</span>
<span style="color: #339933;">#define RNEIGHBOR     4          /* Right neigbor tag                  */</span>
<span style="color: #339933;">#define NONE          5          /* Indicates no neighbor              */</span>
&nbsp;
<span style="color: #993333;">struct</span> ThermalDiffusivity <span style="color: #009900;">&#123;</span>      <span style="color: #808080; font-style: italic;">/* Parameters used in difference      */</span>
  <span style="color: #993333;">double</span> cx<span style="color: #339933;">;</span>                     <span style="color: #808080; font-style: italic;">/* equations - refer to class         */</span>
  <span style="color: #993333;">double</span> cy<span style="color: #339933;">;</span>                     <span style="color: #808080; font-style: italic;">/* notes. Physically, these refer to  */</span>
<span style="color: #009900;">&#125;</span> parameters <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color:#800080;">0.25</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.25</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* thermal diffusivity                */</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION &amp; INITIALIZATION */</span>
  <span style="color: #993333;">double</span> U<span style="color: #009900;">&#91;</span>NXY<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>NXY<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>      <span style="color: #808080; font-style: italic;">/* 2D array to hold current values        */</span>
         Unew<span style="color: #009900;">&#91;</span>NXY<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>NXY<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>   <span style="color: #808080; font-style: italic;">/* 2D array to hold new values            */</span>
         start_time<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Wall clock - start time                */</span>
         end_time<span style="color: #339933;">;</span>         <span style="color: #808080; font-style: italic;">/* Wall clock - end time                  */</span>
&nbsp;
  <span style="color: #993333;">int</span>    proc_id<span style="color: #339933;">,</span>          <span style="color: #808080; font-style: italic;">/* Process ID                             */</span>
         n_procs<span style="color: #339933;">,</span>          <span style="color: #808080; font-style: italic;">/* Number of processors                   */</span>
         n_workers<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Number of WORKERS [nprocs - 1]         */</span>
         cols_per_worker<span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/* Number of columns of U(x,y) per WORKER */</span> 
         offset<span style="color: #339933;">,</span>           <span style="color: #808080; font-style: italic;">/* Offset                                 */</span>
         source<span style="color: #339933;">,</span>           <span style="color: #808080; font-style: italic;">/* Source for a message                   */</span>
         destination<span style="color: #339933;">,</span>      <span style="color: #808080; font-style: italic;">/* Destination for a message              */</span>
         lneighbor<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Process ID of left neighbor            */</span>
         rneighbor<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Process ID of right neighbor           */</span>
         start<span style="color: #339933;">,</span>            <span style="color: #808080; font-style: italic;">/* Starting # for columns in WORKERS      */</span>
         end<span style="color: #339933;">,</span>              <span style="color: #808080; font-style: italic;">/* Ending # for columns in WORKERS        */</span>
         i<span style="color: #339933;">,</span>                <span style="color: #808080; font-style: italic;">/* Dummy/Running index                    */</span>
         j<span style="color: #339933;">,</span>                <span style="color: #808080; font-style: italic;">/* Dummy/Running index                    */</span>
         h<span style="color: #339933;">;</span>                <span style="color: #808080; font-style: italic;">/* Dummy/Running index for TSTEPS         */</span>
&nbsp;
  FILE   <span style="color: #339933;">*</span>output<span style="color: #339933;">;</span>          <span style="color: #808080; font-style: italic;">/* File pointer                           */</span>
&nbsp;
  MPI_Status status<span style="color: #339933;">;</span>       <span style="color: #808080; font-style: italic;">/* MPI structure containing return codes
                              for message passing operations         */</span>
&nbsp;
  MPI_Datatype cvector<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* A custom MPI data type defined to 
                              contain column vectors                 */</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* NUMBER OF WORKERS [NPROC - 1] */</span>
  n_workers <span style="color: #339933;">=</span> n_procs <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* MPI DATA TYPE [CUSTOM] - TO SEND BLOCKS OF U(x,y) TO WORKERS AS COLUMNS
   * THANKS TO DR. STEVEN SEIDEL [http://www.cs.mtu.edu/~steve/] 
   * FOR INFORMING/TEACHING ME THE POSSIBILITY OF DEFINING CUSTOM DATA TYPES 
   * IN MPI, DURING A PREVIOUS DISCUSSION ABOUT MATRIX MULTIPILICATION
  */</span>
  MPI_Type_vector<span style="color: #009900;">&#40;</span>NXY<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> NXY<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>cvector<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Type_commit <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cvector<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING:
   * PRINT GENERAL INFORMATION ABOUT THE PROGRAM
   * CHECK IF NPROCS MEETS THE PROGRAM REQUIREMENTS
   * POPULATE THE U(x,y) TO SIMULATE INITIAL TEMPERATURE DISTRIBUTION
   * SEND RELEVANT INFORMATION ABOUT NEIGHBORS, OF U(x,y) TO WORKERS
   * RECEIVE RELAVANT INFORMATION FROM WORKERS, AT EVERY TIME STEP
   * DISPLAY U(x,y) AT INITIAL AND FINAL STEPS
   * PRINT U(x,y) AT EVERY TIME STEP SO THAT IT CAN BE PLOTTED/ANIMATED
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  2D Heat Equation [MPI/C]<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of x-grids        : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> NXY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of y-grids        : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> NXY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of processors     : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of workers        : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> n_workers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of time steps     : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> TSTEPS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Thermal diffusivity (Cx) : %4.3lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> parameters.<span style="color: #202020;">cx</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Thermal diffusivity (Cy) : %4.3lf<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> parameters.<span style="color: #202020;">cy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* CHECK IF NXY IS DIVISIBLE BY [NPROC + 1]
     * IF NOT, PRINT AN ERROR MESSAGE AND QUIT THE PROGRAM 
    */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>NXY<span style="color: #339933;">%</span><span style="color: #009900;">&#40;</span>n_procs <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    ERROR:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    NXY not divisible by (NPROC + 1)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* OPEN THE FILE FOR WRITING [OVER-WRITE, IF EXISTS]
     * PRINT ERROR MESSAGE, IF ANY &amp; TERMINATE THE PROGRAM
    */</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>output <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;heat_equation_2d.dat&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>    ERROR:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;    Unable to open the output file<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* START THE CLOCK */</span>
    start_time <span style="color: #339933;">=</span> MPI_Wtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* INITIALIZE THE GRID */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Temperature distribution (at time t=0)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* HEADER LINE INDICATING TOTAL NUMBER OF TIME STEPS
     * NEEDED BY MATLAB (&amp;/OR OTHER) FOR PLOTTING
    */</span> 
    fprintf<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;TSTEPS=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> TSTEPS <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* POPULATE THE ARRAY, U(x,y) 
     * BORDER ELEMENTS ARE ALL ZERO &amp; HIGH VALUE IN THE MIDDLE
     * PRINT THE ARRAY - TO THE SCREEN AND TO THE FILE
    */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span> <span style="color: #339933;">*</span> i <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>NXY <span style="color: #339933;">-</span> i <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> j <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>NXY <span style="color: #339933;">-</span> j <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    %08.0lf&quot;</span><span style="color: #339933;">,</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        fprintf<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%012.4lf  &quot;</span><span style="color: #339933;">,</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      fprintf<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* NUMBER OF COLUMNS PER WORKER 
     * IT'S IMPORTANT TO ALLOCATE EQUAL NUMBER OF COLUMNS TO
     * EACH WORKER - ELSE, SOME MIGHT FINISH FASTER WITHOUT
     * ADDITIONAL WORK
     */</span>
    cols_per_worker <span style="color: #339933;">=</span> NXY<span style="color: #339933;">/</span>n_workers<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* INITIALIZE offset */</span>
    offset <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> n_workers<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* INFORM EACH WORKER OF ITS NEIGHBORS */</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        lneighbor <span style="color: #339933;">=</span> NONE<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        lneighbor <span style="color: #339933;">=</span> i <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> n_workers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        rneighbor <span style="color: #339933;">=</span> NONE<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        rneighbor <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* DESTINATION PROCESSOR */</span>
      destination <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* SEND INFORMATION TO EACH WORKER
       * OFFSET
       * NUMBER OF COLUMNS EACH WORKER HAS TO WORK WITH 
       * LEFT &amp; RIGHT NEIGHBORS FOR EACH WORKER
       * RELEVANT PORTION OF U(x,y) TO EACH WORKER
      */</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>offset<span style="color: #339933;">,</span>    <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> destination<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cols_per_worker<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> destination<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lneighbor<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> destination<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>rneighbor<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> destination<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cols_per_worker<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> destination<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* INCREMENT THE offset */</span>
      offset <span style="color: #339933;">=</span> offset <span style="color: #339933;">+</span> cols_per_worker<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE RELEVANT INFORMATION FROM WORKERS  FOR EVERY TIME STEP */</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>h <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> h <span style="color: #339933;">&lt;=</span> TSTEPS<span style="color: #339933;">;</span> h<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> n_workers<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* SOURCE PROCESSOR */</span>
        source <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
&nbsp;
        MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>offset<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> source<span style="color: #339933;">,</span> DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cols_per_worker<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> source<span style="color: #339933;">,</span> DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cols_per_worker<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> source<span style="color: #339933;">,</span> h<span style="color: #339933;">*</span>DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* PRINT U(x,y), THE TEMPERATURE DISTRIBUTION AFTER EVERY TIME STEP TO THE FILE */</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          fprintf<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%012.4lf  &quot;</span><span style="color: #339933;">,</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        fprintf<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* CLOSE THE FILE */</span>
    fclose<span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* PRINT U(x,y), THE TEMPERATURE DISTRIBUTION AFTER 'TSTEPS' STEPS TO THE SCREEN */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>    Temperature distribution (after %d time steps)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> h<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    %08.0lf&quot;</span><span style="color: #339933;">,</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* STOP THE CLOCK */</span>
    end_time <span style="color: #339933;">=</span> MPI_Wtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* PRINT TIMING INFORMATION */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Time elapsed (sec)   : %6.4f<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> end_time <span style="color: #339933;">-</span> start_time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MASTER LOOP ENDS */</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF WORKER, THEN DO THE FOLLOWING:
   * INITIALIZE U(x,y) &amp; Unew(x,y) TO ZERO
   * RECEIVE RELEVANT INFORMATION FROM MASTER
   * CALCULATE U(x, y, t+1) BY COMMUNICATING APPROPRIATELY WITH NEIGHBORING WORKERS
   * SEND RELEVANT INFORMATION TO MASTER 
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">!=</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* INITIALIZE ALL ELEMENTS OF U(x,y) &amp; Unew(x,y) TO ZERO */</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> NXY<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
        Unew<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE THE COLUMNS, NEIGHBORS AND GRID INFORMATION FROM MASTER */</span>
    MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>offset<span style="color: #339933;">,</span>    <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cols_per_worker<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lneighbor<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>rneighbor<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cols_per_worker<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> BEGIN<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* FOR DEBUGGING PURPOSES ONLY 
     * RUN WITH NPROC = 3 WITH NXY = 16
     * THERE SHOULD BE TWO BLOCKS OF COMPLEMENTARY MATRICES
&nbsp;
    if (proc_id == 1) {
    printf(&quot;\n&quot;);
      for (i = 0; i &lt; NXY; i++) {
        for (j = 0; j &lt; cols_per_worker; j++) {
          printf(&quot; %08.0lf&quot;, U[0][i][j]);
        }
        printf(&quot;\n&quot;);
      }
    }
&nbsp;
    if (proc_id == 2) {
    printf(&quot;\n&quot;);
      for (i = 0; i &lt; NXY; i++) {
        for (j = cols_per_worker; j &lt; 2*cols_per_worker; j++) {
          printf(&quot; %08.0lf&quot;, U[0][i][j]);
        }
        printf(&quot;\n&quot;);
      }
    }
    */</span> 
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* DETERMINE BORDER ELEMENTS - FIRST &amp; LAST COLUMNS NEED EXTRA CARE:
     * ROW 0 CANNOT EXCHANGE INFORMATION WITH 0-1
     * ROW N CANNOT EXCHANGE INFORMATION WITH N+1
    */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>offset <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #808080; font-style: italic;">/* DO NOT INCLUDE ZEROTH COLUMN */</span>
      start <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      start <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>offset <span style="color: #339933;">+</span> cols_per_worker<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> NXY<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #808080; font-style: italic;">/* DO NOT INCLUDE THE LAST COLUMN */</span>
      end <span style="color: #339933;">=</span> offset <span style="color: #339933;">+</span> cols_per_worker <span style="color: #339933;">-</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      end <span style="color: #339933;">=</span> offset <span style="color: #339933;">+</span> cols_per_worker <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* PERFORM TIME STEP [TSTEPS] ITERATIONS.
     * MUST COMMUNICATE BORDER COLUMNS WITH NEIGHBORS.
     * IF IT'S THE FIRST OR THE LAST COLUMN, THEN THE COMMUNICATION TAKES 
     * PLACE ONLY WITH ONE NEIGHBOR
    */</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>h <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> h <span style="color: #339933;">&lt;=</span> TSTEPS<span style="color: #339933;">;</span> h<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>lneighbor <span style="color: #339933;">!=</span> NONE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> lneighbor<span style="color: #339933;">,</span> RNEIGHBOR<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> lneighbor<span style="color: #339933;">,</span> LNEIGHBOR<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> 
                 <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>rneighbor <span style="color: #339933;">!=</span> NONE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>cols_per_worker<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> rneighbor<span style="color: #339933;">,</span>
                 LNEIGHBOR<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>cols_per_worker<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> rneighbor<span style="color: #339933;">,</span>
                 RNEIGHBOR<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* UPDATE THE VALUE OF GRID POINTS 
       * REFER TO THE SLIDES FROM WEEK #14 [PART #02]
      */</span>
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> NXY<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;=</span> end<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          Unew<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> 
                       parameters.<span style="color: #202020;">cx</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>U<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
                       parameters.<span style="color: #202020;">cy</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* SET U(x,y) TO Unew(x,y) */</span>
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> NXY<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;=</span> end<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          U<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Unew<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* SEND RELEVANT INFORMATION TO MASTER */</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>offset<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cols_per_worker<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_INT<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cols_per_worker<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>U<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> cvector<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> h<span style="color: #339933;">*</span>DONE<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* WORKER LOOP ENDS */</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc heat_equation_2d.c <span style="color: #660033;">-o</span> heat_equation_2d.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">5</span> .<span style="color: #000000; font-weight: bold;">/</span>heat_equation_2d.x
&nbsp;
  2D Heat Equation <span style="color: #7a0874; font-weight: bold;">&#91;</span>MPI<span style="color: #000000; font-weight: bold;">/</span>C<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
    Number of x-grids        : <span style="color: #000000;">20</span>
    Number of y-grids        : <span style="color: #000000;">20</span>
    Number of processors     : <span style="color: #000000;">5</span>
    Number of workers        : <span style="color: #000000;">4</span>
    Number of <span style="color: #000000; font-weight: bold;">time</span> steps     : <span style="color: #000000;">5000</span>
    Thermal diffusivity <span style="color: #7a0874; font-weight: bold;">&#40;</span>Cx<span style="color: #7a0874; font-weight: bold;">&#41;</span> : <span style="color: #000000;">0.050</span>
    Thermal diffusivity <span style="color: #7a0874; font-weight: bold;">&#40;</span>Cy<span style="color: #7a0874; font-weight: bold;">&#41;</span> : <span style="color: #000000;">0.050</span>
&nbsp;
    Temperature distribution <span style="color: #7a0874; font-weight: bold;">&#40;</span>at <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #007800;">t</span>=<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00032400    00061200    00086400    00108000    00126000    00140400    00151200    00158400    00162000    00162000    00158400    00151200    00140400    00126000    00108000    00086400    00061200    00032400    00000000
    00000000    00061200    00115600    00163200    00204000    00238000    00265200    00285600    00299200    00306000    00306000    00299200    00285600    00265200    00238000    00204000    00163200    00115600    00061200    00000000
    00000000    00086400    00163200    00230400    00288000    00336000    00374400    00403200    00422400    00432000    00432000    00422400    00403200    00374400    00336000    00288000    00230400    00163200    00086400    00000000
    00000000    00108000    00204000    00288000    00360000    00420000    00468000    00504000    00528000    00540000    00540000    00528000    00504000    00468000    00420000    00360000    00288000    00204000    00108000    00000000
    00000000    00126000    00238000    00336000    00420000    00490000    00546000    00588000    00616000    00630000    00630000    00616000    00588000    00546000    00490000    00420000    00336000    00238000    00126000    00000000
    00000000    00140400    00265200    00374400    00468000    00546000    00608400    00655200    00686400    00702000    00702000    00686400    00655200    00608400    00546000    00468000    00374400    00265200    00140400    00000000
    00000000    00151200    00285600    00403200    00504000    00588000    00655200    00705600    00739200    00756000    00756000    00739200    00705600    00655200    00588000    00504000    00403200    00285600    00151200    00000000
    00000000    00158400    00299200    00422400    00528000    00616000    00686400    00739200    00774400    00792000    00792000    00774400    00739200    00686400    00616000    00528000    00422400    00299200    00158400    00000000
    00000000    00162000    00306000    00432000    00540000    00630000    00702000    00756000    00792000    00810000    00810000    00792000    00756000    00702000    00630000    00540000    00432000    00306000    00162000    00000000
    00000000    00162000    00306000    00432000    00540000    00630000    00702000    00756000    00792000    00810000    00810000    00792000    00756000    00702000    00630000    00540000    00432000    00306000    00162000    00000000
    00000000    00158400    00299200    00422400    00528000    00616000    00686400    00739200    00774400    00792000    00792000    00774400    00739200    00686400    00616000    00528000    00422400    00299200    00158400    00000000
    00000000    00151200    00285600    00403200    00504000    00588000    00655200    00705600    00739200    00756000    00756000    00739200    00705600    00655200    00588000    00504000    00403200    00285600    00151200    00000000
    00000000    00140400    00265200    00374400    00468000    00546000    00608400    00655200    00686400    00702000    00702000    00686400    00655200    00608400    00546000    00468000    00374400    00265200    00140400    00000000
    00000000    00126000    00238000    00336000    00420000    00490000    00546000    00588000    00616000    00630000    00630000    00616000    00588000    00546000    00490000    00420000    00336000    00238000    00126000    00000000
    00000000    00108000    00204000    00288000    00360000    00420000    00468000    00504000    00528000    00540000    00540000    00528000    00504000    00468000    00420000    00360000    00288000    00204000    00108000    00000000
    00000000    00086400    00163200    00230400    00288000    00336000    00374400    00403200    00422400    00432000    00432000    00422400    00403200    00374400    00336000    00288000    00230400    00163200    00086400    00000000
    00000000    00061200    00115600    00163200    00204000    00238000    00265200    00285600    00299200    00306000    00306000    00299200    00285600    00265200    00238000    00204000    00163200    00115600    00061200    00000000
    00000000    00032400    00061200    00086400    00108000    00126000    00140400    00151200    00158400    00162000    00162000    00158400    00151200    00140400    00126000    00108000    00086400    00061200    00032400    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
&nbsp;
&nbsp;
    Temperature distribution <span style="color: #7a0874; font-weight: bold;">&#40;</span>after <span style="color: #000000;">5000</span> <span style="color: #000000; font-weight: bold;">time</span> steps<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000001    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000    00000000
&nbsp;
&nbsp;
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">2.5108</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><br clear="all"></p>
<h3 class="blog">Initial and final temperature distribution</h3>
<p align="center"><img src="http://sgowtham.net/blog/files/20101211/heat_equation_2d_0000.png" alt="Temperature distribution at t=0" title="Temperature distribution at t=0"></p>
<p class="bpcaption">Temperature distribution at t=0</strong></p>
<p align="center"><img src="http://sgowtham.net/blog/files/20101211/heat_equation_2d_5000.png" alt="Temperature distribution after 5000 time steps" title="Temperature distribution after 5000 time steps"></p>
<p class="bpcaption">Temperature distribution after 5000 time steps</strong></p>
<p align="center"><img src="http://sgowtham.net/blog/files/20101211/heat_equation_2d.png" alt="Temperature distribution" title="Temperature distribution"></p>
<p class="bpcaption">Combined temperature distribution</strong></p>
<p><br clear="all"></p>
<h3 class="blog">Animation &#8211; using MATLAB</h3>
<p><a href="http://sgowtham.net/blog/files/20101211/heat_equation_2d.avi" target="_blank">Time lapse of temperature distribution, U(x, y) &#8211; 500 time steps</a>, created using the code below.<br />
<br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% heat_equation_2d.m</span>
<span style="color: #228B22;">% MATLAB 'SCRIPT m' FILE TO READ THE FILE 'heat_equation_2d.dat', </span>
<span style="color: #228B22;">% ONE DATA SET AT A TIME AND PLOT IT REPEATEDLY TO SIMULATE AN ANIMATION.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">% TESTED SUCCESSFULLY WITH MATLAB (v7.8.0.347 R2009a; STUDENT VERSION) IN A MACBOOK PRO </span>
<span style="color: #228B22;">% (OS X 10.6.5) WITH INTEL CORE 2 DUO PROCESSOR (2.4 GHz) &amp; 4GB OF RAM.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">% FIRST WRITTEN: GOWTHAM; Fri, 10 Dec 2010 23:12:18 -0500</span>
<span style="color: #228B22;">% LAST MODIFIED: GOWTHAM; Sun, 12 Dec 2010 09:34:23 -0500</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">% INPUT:</span>
<span style="color: #228B22;">% 'heat_equation_2d.dat' PRODUCED BY RUNNING 'heat_equation_2d.c'</span>
<span style="color: #228B22;">% AND IT HAS THE FOLLOWING FORMAT:</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% TSTEPS=# </span>
<span style="color: #228B22;">% U(x,y) AT t = 0         [NXY x NXY]</span>
<span style="color: #228B22;">% U(x,y) AT t = 1         [NXY x NXY]</span>
<span style="color: #228B22;">% U(x,y) AT t = 2         [NXY x NXY]</span>
<span style="color: #228B22;">% ...</span>
<span style="color: #228B22;">% U(x,y) AT t = TSTEPS    [NXY x NXY]</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">% FOR AN EXPLANATION OF 'NXY', 'TSTEPS', ETC., PLEASE REFER TO 'heat_equation_2d.c'.</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% OUTPUT:</span>
<span style="color: #228B22;">% IF 'mencoder' [AND OTHER REQUIRED UTILITIES] IS AVAILABLE, THIS SCRIP WILL PRODUCE </span>
<span style="color: #228B22;">% 'heat_equation_2d.avi'. IF NOT, 'New Screen Recording' OPTION MAY BE USED IN</span>
<span style="color: #228B22;">% 'QuickTime Player' TO CREATE THE ANIMATION.</span>
<span style="color: #228B22;">%</span>
&nbsp;
<span style="color: #228B22;">% CLEAR THE PREVIOUSLY SET VARIABLES</span>
<span style="color: #0000FF;">clear</span>
&nbsp;
<span style="color: #228B22;">% CLEAR THE COMMAND WINDOW</span>
<span style="color: #0000FF;">clc</span>
&nbsp;
<span style="color: #228B22;">% PAUSE FOR 15 SECONDS</span>
<span style="color: #228B22;">% USEFUL WHEN 'mencoder' IS NOT AVAILABLE AS THIS WILL GIVE JUST ENOUGH TIME TO HIDE </span>
<span style="color: #228B22;">% UNWANTED SCREENS / WINDOWS AND START RECORDING VIA QuickTime Player.</span>
<span style="color: #228B22;">% COMMENT IT OUT IF NOT NEEDED.</span>
<span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">15</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% OPEN THE DATA FILE FOR READING. </span>
<span style="color: #228B22;">% SAME BASENAME WILL BE USED FOR THE ANIMATION FILE</span>
filename   = <span style="color:#A020F0;">'heat_equation_2d'</span>;
filehandle = <span style="color: #0000FF;">fopen</span><span style="color: #080;">&#40;</span>filename <span style="color:#A020F0;">'.dat'</span>, <span style="color:#A020F0;">'r'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% COUNT THE NUMBER OF TIME STEPS - GIVEN IN THE FIRST LINE</span>
TSTEPS = <span style="color: #0000FF;">fscanf</span><span style="color: #080;">&#40;</span>filehandle, <span style="color:#A020F0;">'TSTEPS=%d\n'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% VARIABLE DECLARATION AND INITIALIZATION</span>
<span style="color: #228B22;">% THIS IS THE NUMBER OF ROWS &amp; COLUMNS IN EACH BLOCK OF DATA</span>
NXY = <span style="color: #33f;">20</span>;
&nbsp;
<span style="color: #228B22;">% x &amp; y GRIDS FOR THE MESH [3D] PLOT</span>
x = <span style="color: #33f;">1</span>:<span style="color: #33f;">1</span>:NXY;
y = <span style="color: #33f;">1</span>:<span style="color: #33f;">1</span>:NXY;
&nbsp;
<span style="color: #228B22;">% GENERATE X &amp; Y ARRAYS FOR 3D PLOT</span>
<span style="color: #228B22;">% TRANSFORMS THE DOMAIN SPECIFIED BY VECTORS x &amp; y INTO ARRAYS X and Y, </span>
<span style="color: #228B22;">% WHICH CAN BE USED TO EVALUATE FUNCTIONS OF TWO VARIABLES AND 3D</span>
<span style="color: #228B22;">% MESH/SURFACE PLOTS. THE ROWS OF THE OUTPUT ARRAY X ARE COPIES OF THE</span>
<span style="color: #228B22;">% VECTOR x; COLUMNS OF THE OUTPUT ARRAY Y ARE COPIES OF THE VECTOR y.</span>
<span style="color: #228B22;">% http://www.mathworks.com/help/techdoc/ref/meshgrid.html</span>
<span style="color: #080;">&#91;</span>X, Y<span style="color: #080;">&#93;</span> = <span style="color: #0000FF;">meshgrid</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% LOOP THROUGH THE DATA FILE TO READ 'NXY x NXY' ELEMENTS AT ONCE</span>
<span style="color: #228B22;">% AND IMPORT THEM INTO 'TSTEPS' NUMBER OF MATRICES OF ORDER NXY.</span>
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span> = <span style="color: #33f;">1</span>:TSTEPS
&nbsp;
  <span style="color: #228B22;">% INITIALIZE ALL ELEMENTS OF MATRIX [OF ORDER NXY] TO ZERO</span>
  Temperature<span style="color: #080;">&#40;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#41;</span>.<span style="">Z</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span>NXY, NXY<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% SCAN THE DATA FILE, READ 'NXY x NXY' ELEMENTS, TREAT THEM</span>
  <span style="color: #228B22;">% AS DOUBLE PRECISION AND STORE THEM IN THE MATRIX.</span>
  <span style="color: #228B22;">% SYNTAX:</span>
  <span style="color: #228B22;">% Z = fscanf(filehandle, 'FORMAT', [COLS, ROWS]);</span>
  Temperature<span style="color: #080;">&#40;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#41;</span>.<span style="">Z</span> = <span style="color: #0000FF;">fscanf</span><span style="color: #080;">&#40;</span>filehandle, <span style="color:#A020F0;">'%f'</span>, <span style="color: #080;">&#91;</span>NXY, NXY<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% PLOT THE DATA IN THE ABOVE MATRIX AS A FUNCTION OF X &amp; Y</span>
  figure1 = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">mesh</span><span style="color: #080;">&#40;</span>X, Y, Temperature<span style="color: #080;">&#40;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#41;</span>.<span style="">Z</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% HANDLE FOR AXIS PROPERTY SO THAT IT CAN BE MODIFIED TO</span>
  <span style="color: #228B22;">% FIT OUR REQUIREMENS</span>
  ap = <span style="color: #0000FF;">gca</span>;
&nbsp;
  <span style="color: #228B22;">% MODIFY CERTAIN AXIS PROPERTIES APPROPRIATELY</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>ap, <span style="color:#A020F0;">'XLimMode'</span>, <span style="color:#A020F0;">'manual'</span>, <span style="color:#A020F0;">'YLimMode'</span>, <span style="color:#A020F0;">'manual'</span>, <span style="color: #080;">...</span>
          <span style="color:#A020F0;">'ZLimMode'</span>, <span style="color:#A020F0;">'manual'</span>, <span style="color: #080;">...</span>
          <span style="color:#A020F0;">'Xlim'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Ylim'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Zlim'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">1000000</span><span style="color: #080;">&#93;</span>, <span style="color: #080;">...</span>
          <span style="color:#A020F0;">'FontSize'</span>, <span style="color: #33f;">14</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% CHANGE THE COLORMAP FROM 'default' TO 'hsv'</span>
  <span style="color: #228B22;">% THE hsv COLORMAP IS CREATED WITH 65536 COLORS</span>
  <span style="color: #228B22;">% http://www.mathworks.com/help/techdoc/ref/colormap.html</span>
  <span style="color: #0000FF;">colormap</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">hsv</span><span style="color: #080;">&#40;</span><span style="color: #33f;">65536</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% DISPLAY A COLOR BAR THAT WILL SERVE AS A LEGEND</span>
  <span style="color: #0000FF;">colorbar</span>
&nbsp;
  <span style="color: #228B22;">% AXES LABELS FOR THE PLOT</span>
  <span style="color: #0000FF;">xlabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Grid points along x-axis'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">ylabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Grid points along y-axis'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">zlabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'U(x, y), Temperature distribution'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% TITLE FOR THE PLOT</span>
  <span style="color: #0000FF;">title</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'TSTEP= '</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% CAPTURE MOVIE FRAME AS AN ARRAY FOR LATER USE.</span>
  <span style="color: #228B22;">% COMMENT THIS LINE IF 'mencoder' IS NOT AVAILABLE.</span>
  <span style="color: #228B22;">% http://www.mathworks.com/help/techdoc/ref/getframe.html</span>
  M<span style="color: #080;">&#40;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#41;</span> = getframe;
&nbsp;
  <span style="color: #228B22;">% TIME LAG BETWEEN SUCCESSIVE PLOTS</span>
  <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.00001</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">end</span> 
<span style="color: #228B22;">% LOOP THROUGH DATA FILE ENDS</span>
&nbsp;
<span style="color: #228B22;">% CLOSE THE DATA FILE</span>
<span style="color: #0000FF;">fclose</span><span style="color: #080;">&#40;</span>datafilename<span style="color: #080;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #228B22;">% CREATE AN ANIMATION USING THE MOVIE FRAMES.</span>
<span style="color: #228B22;">% COMMENT THIS SECTION IF 'mencoder' IS NOT AVAILABLE</span>
&nbsp;
<span style="color: #228B22;">% CREATE AN AVI [AUDIO/VIDEO INTERLEAVED] FILE FROM THE MOVIE FRAMES ARRAY</span>
<span style="color: #228B22;">% 'None' IS THE ONLY COMPRESSION AVAILABLE OPTION ON UNIX (LIKE) OS.</span>
<span style="color: #228B22;">% http://www.mathworks.com/help/techdoc/ref/movie2avi.html</span>
movie2avi<span style="color: #080;">&#40;</span>M, <span style="color: #080;">&#91;</span>filename <span style="color:#A020F0;">'.avi'</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'compression'</span>, <span style="color:#A020F0;">'None'</span>, <span style="color:#A020F0;">'fps'</span>, <span style="color: #33f;">25</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% USE 'mencoder' TO COMPRESS THE MOVIE SO THAT IT CAN BE PLAYED ON </span>
<span style="color: #228B22;">% QuickTime Player OR SOME OTHER SUCH APPLICATION.</span>
<span style="color: #228B22;">% USE eval TO RUN THE MAC/LINUX COMMAND</span>
<span style="color: #228B22;">% http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-selecting-codec.html</span>
avicompression = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'!mencoder -oac copy -ovc xvid -xvidencopts bitrate=1000 '</span> filename <span style="color:#A020F0;">'.avi -o '</span> filename <span style="color:#A020F0;">'_compressed.avi'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>avicompression<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% RENAME THE COMPRESSED AVI</span>
<span style="color: #228B22;">% USE eval TO RUN THE MAC/LINUX COMMAND</span>
avirename = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'!mv '</span> filename <span style="color:#A020F0;">'_compressed.avi '</span> filename <span style="color:#A020F0;">'.avi'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>avirename<span style="color: #080;">&#41;</span>;</pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F12%2F11%2Fmpi-c-2-dimensional-heat-equation-square-grid%2F&amp;title=MPI%2FC%20%26%238211%3B%202%20Dimensional%20Heat%20Equation%20%5BSquare%20Grid%5D" id="wpa2a_14"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/12/11/mpi-c-2-dimensional-heat-equation-square-grid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://sgowtham.net/blog/files/20101211/heat_equation_2d.avi" length="2636892" type="video/avi" />
		</item>
		<item>
		<title>MPI/C &#8211; Monte Carlo Method Of Finding PI</title>
		<link>http://sgowtham.net/blog/2010/12/02/mpi-c-monte-carlo-method-of-finding-pi/</link>
		<comments>http://sgowtham.net/blog/2010/12/02/mpi-c-monte-carlo-method-of-finding-pi/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 05:20:12 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3038</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Program Listing 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 [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* pi_montecarlo.c
 * PARALLEL [MPI] C PROGRAM TO COMPUTE THE VALUE OF 'PI' USING MONTE CARLO 
 * [DARTBOARD ALGORITHM] METHOD. MASTER COLLECTS THE RESULTS FROM THE WORKERS 
 * USING MPI_Reduce AND AVERAGES THEM.
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * FIRST WRITTEN : GOWTHAM; Fri, 19 Nov 2010 01:11:34 -0500
 * LAST MODIFIED : GOWTHAM; Fri, 19 Nov 2010 03:53:31 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/12/02/mpi-c-monte-carlo-method-of-finding-pi/ 
 *
 * COMPILATION:
 * mpicc -g -Wall -lm pi_montecarlo.c -o pi_montecarlo.x 
 *
 * EXECUTION:
 * mpirun -np NPROC -machinefile MACHINEFILE ./pi_montecarlo.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #339933;">#define MASTER             0   /* Process ID of MASTER                      */</span>
<span style="color: #339933;">#define PI      3.1415926535   /* Known vaue of PI                          */</span>
<span style="color: #339933;">#define NDARTS     100000000   /* Number of darts thrown                    */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* FUNCTION 'pseudo_random' BEGINS
 * GIVEN TWO NUMBERS, llimit &amp; ulimit, THIS FUNCTION GENERATES A RANDOM
 * NUMBER BETWEEN llimit &amp; ulimit. SEED FOR rand() COMES FROM THE
 * PARENT FUNCTION, 'pi_dartboard'.
*/</span>
<span style="color: #993333;">double</span> pseudo_random<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION &amp; INITIALIZATION */</span>
  <span style="color: #993333;">double</span> r<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* Random number */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GENERATE RANDOM NUMBER BETWEEN llimit &amp; ulimit
   * rand() IS USUALLY AN INTEGER &amp; SO IS RAND_MAX
   * (double) rand() and (double) RAND_MAX CONVERTS THEM TO DOUBLE PRECISION
   * USING LINEAR SCALING [REFER TO CLASS NOTES/SLIDES], RANDOM NUMBER IS
   * SCALED TO LIE BETWEEN llimit &amp; ulimit
  */</span>
  r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>b <span style="color: #339933;">-</span> a<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span> rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span> RAND_MAX<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> a<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* RETURN THE VALUE OF RANDOM NUMBER
   * INDICATE THE TERMINATION OF THIS MODULE/FUNCTION
  */</span>
  <span style="color: #b1b100;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* FUNCTION 'pseudo_random' ENDS */</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION &amp; INITIALIZATION */</span>
  <span style="color: #993333;">int</span>    proc_id<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Process ID                           */</span>
         n_procs<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Number of processors                 */</span>
         llimit<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Lower limit for random numbers       */</span>
         ulimit<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Upper limit for random numbers       */</span>
         n_circle<span style="color: #339933;">,</span>      <span style="color: #808080; font-style: italic;">/* Number of darts that hit the circle  */</span>
         i<span style="color: #339933;">;</span>             <span style="color: #808080; font-style: italic;">/* Dummy/Running index                  */</span>
&nbsp;
  <span style="color: #993333;">double</span> pi_current<span style="color: #339933;">,</span>    <span style="color: #808080; font-style: italic;">/* PI calculated by each WORKER         */</span>
         pi_sum<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Sum of PI values from each WORKER    */</span>
         x<span style="color: #339933;">,</span>             <span style="color: #808080; font-style: italic;">/* x coordinate, betwen -1 &amp; +1         */</span>
         y<span style="color: #339933;">,</span>             <span style="color: #808080; font-style: italic;">/* y coordinate, betwen -1 &amp; +1         */</span>
         z<span style="color: #339933;">,</span>             <span style="color: #808080; font-style: italic;">/* Sum of x^2 and y^2                   */</span>
         error<span style="color: #339933;">,</span>         <span style="color: #808080; font-style: italic;">/* Error in calculation of PI           */</span>
         start_time<span style="color: #339933;">,</span>    <span style="color: #808080; font-style: italic;">/* Wall clock - start time              */</span>
         end_time<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* Wall clock - end time                */</span>
&nbsp;
  <span style="color: #993333;">struct</span> timeval stime<span style="color: #339933;">;</span>
&nbsp;
  llimit   <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  ulimit   <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  n_circle <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESS ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING:
   * ACCEPT THE VALUE OF NDARTS FROM COMMAND LINE
   * PRINT THE MESSAGE TO THE SCREEN
   * START THE WALL CLOCK
  */</span> 
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Monte Carlo method of finding PI<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of processors : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Number of darts      : %d<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> NDARTS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* START THE CLOCK */</span>
    start_time <span style="color: #339933;">=</span> MPI_Wtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* ALL PROCESSORS, INCLUDING MASTER, DO THE FOLLOWING:
   * SEED THE RANDOM NUMBER GENERATOR
   * THROW DARTS AT THE BOARD [FOR LOOP]
   *   COORDINATES OF THE DART [x, y] ARE GENERATED AT RANDOM
   *   CHECK IF THE DART LANDED IN THE CIRCLE
   *   IF YES, INCREMENT THE COUNT
   * CALCULATE THE VALUE OF PI
  */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* SEED FOR RANDOM NUMBER GENERATOR */</span>
  <span style="color: #808080; font-style: italic;">/* RESULTS IN THE FOLLOWING WARNING - MAY BE IGNORED
   * warning: implicit declaration of function ‚Äògettimeofday‚Äô
  */</span>
  gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>stime<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  srand<span style="color: #009900;">&#40;</span>stime.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">*</span> stime.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">*</span> stime.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">*</span> stime.<span style="color: #202020;">tv_usec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* THROW NDARTS LOOP BEGINS */</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> NDARTS<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* GENERATE RANDOM NUMBERS x &amp; y BETWEEN -1 &amp; +1 */</span>
    x <span style="color: #339933;">=</span> pseudo_random<span style="color: #009900;">&#40;</span>llimit<span style="color: #339933;">,</span> ulimit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    y <span style="color: #339933;">=</span> pseudo_random<span style="color: #009900;">&#40;</span>llimit<span style="color: #339933;">,</span> ulimit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* CALCULATE x^2 + y^2 */</span>
    z <span style="color: #339933;">=</span> pow<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> pow<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* IF THE DART IS IN THE CIRCLE, INCREMENT n_circle */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>z <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       n_circle<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* THROW NDARTS LOOP ENDS */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* CALCULATE THE VALUE OF PI */</span>
  pi_current <span style="color: #339933;">=</span> <span style="color:#800080;">4.0</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span>n_circle<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span>NDARTS<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* USE MPI_Reduce TO SUM VALUES OF pi_current ACROSS ALL WORKERS
   * THIS HAPPENS ONLY ON MASTER
  */</span> 
  MPI_Reduce<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>pi_current<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>pi_sum<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> MPI_SUM<span style="color: #339933;">,</span> MASTER<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING
   * CALCULATE THE AVERAGE VALUE OF PI 
   * CALCULATE THE ERROR ASSOCIATED WITH THE COMPUTED VALUE OF PI
   * STOP THE WALL CLOCK
   * PRINT THE RESULTS TO THE SCREEN
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* AVERAGE VALUE OF COMPUTED PI */</span>
    pi_sum <span style="color: #339933;">=</span> pi_sum <span style="color: #339933;">/</span> n_procs<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* % ERROR ASSOCIATED WITH COMPUTED VALUE OF PI */</span>
    error <span style="color: #339933;">=</span> fabs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>pi_sum <span style="color: #339933;">-</span> PI<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>PI<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* STOP THE CLOCK */</span>
    end_time <span style="color: #339933;">=</span> MPI_Wtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* PRINT THE RESULTS TO THE SCREEN */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Known value of  PI   : %11.10f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> PI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Average value of PI  : %11.10f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> pi_sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Percentage Error     : %10.8f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    Time elapsed (sec)   : %10.8f<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> end_time <span style="color: #339933;">-</span> start_time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc pi_montecarlo.c <span style="color: #660033;">-o</span> pi_montecarlo.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">1</span> .<span style="color: #000000; font-weight: bold;">/</span>pi_montecarlo.x
&nbsp;
  Monte Carlo Method <span style="color: #000000; font-weight: bold;">for</span> finding PI
&nbsp;
    Number of processors : <span style="color: #000000;">1</span>
    Number of darts      : <span style="color: #000000;">100000000</span>
&nbsp;
    Known value of  PI   : <span style="color: #000000;">3.1415926535</span>
    Average value of PI  : <span style="color: #000000;">3.1418926800</span>
    Percentage Error     : <span style="color: #000000;">0.00955014</span>
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">9.11504292</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>pi_montecarlo.x
&nbsp;
  Monte Carlo Method <span style="color: #000000; font-weight: bold;">for</span> finding PI
&nbsp;
    Number of processors : <span style="color: #000000;">2</span>
    Number of darts      : <span style="color: #000000;">100000000</span>
&nbsp;
    Known value of  PI   : <span style="color: #000000;">3.1415926535</span>
    Average value of PI  : <span style="color: #000000;">3.1415510000</span>
    Percentage Error     : <span style="color: #000000;">0.00132587</span>
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">9.10987115</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">4</span> .<span style="color: #000000; font-weight: bold;">/</span>pi_montecarlo.x
&nbsp;
  Monte Carlo Method <span style="color: #000000; font-weight: bold;">for</span> finding PI
&nbsp;
    Number of processors : <span style="color: #000000;">4</span>
    Number of darts      : <span style="color: #000000;">100000000</span>
&nbsp;
    Known value of  PI   : <span style="color: #000000;">3.1415926535</span>
    Average value of PI  : <span style="color: #000000;">3.1415546600</span>
    Percentage Error     : <span style="color: #000000;">0.00120937</span>
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">9.09918213</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">8</span> .<span style="color: #000000; font-weight: bold;">/</span>pi_montecarlo.x
&nbsp;
  Monte Carlo Method <span style="color: #000000; font-weight: bold;">for</span> finding PI
&nbsp;
    Number of processors : <span style="color: #000000;">8</span>
    Number of darts      : <span style="color: #000000;">100000000</span>
&nbsp;
    Known value of  PI   : <span style="color: #000000;">3.1415926535</span>
    Average value of PI  : <span style="color: #000000;">3.1416389850</span>
    Percentage Error     : <span style="color: #000000;">0.00147478</span>
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">19.53732681</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">16</span> .<span style="color: #000000; font-weight: bold;">/</span>pi_montecarlo.x
&nbsp;
  Monte Carlo Method <span style="color: #000000; font-weight: bold;">for</span> finding PI
&nbsp;
    Number of processors : <span style="color: #000000;">16</span>
    Number of darts      : <span style="color: #000000;">100000000</span>
&nbsp;
    Known value of  PI   : <span style="color: #000000;">3.1415926535</span>
    Average value of PI  : <span style="color: #000000;">3.1416104100</span>
    Percentage Error     : <span style="color: #000000;">0.00056521</span>
    Time elapsed <span style="color: #7a0874; font-weight: bold;">&#40;</span>sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>   : <span style="color: #000000;">39.17838287</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F12%2F02%2Fmpi-c-monte-carlo-method-of-finding-pi%2F&amp;title=MPI%2FC%20%26%238211%3B%20Monte%20Carlo%20Method%20Of%20Finding%20PI" id="wpa2a_16"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/12/02/mpi-c-monte-carlo-method-of-finding-pi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; Matrix Multiplication [Order N]</title>
		<link>http://sgowtham.net/blog/2010/11/30/mpi-c-matrix-multiplication-order-n/</link>
		<comments>http://sgowtham.net/blog/2010/11/30/mpi-c-matrix-multiplication-order-n/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 09:11:19 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3047</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Program Listing 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 [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* matrix_multiply.c
 * PARALLEL [MPI] C PROGRAM TO PERFORM MATRIX MULTIPLICATION. 
 * DISPLAY OF MATRIX ELEMENTS IS SET FOR 'INTEGER-LIKE'. IF [SOME/ALL OF] 
 * THE MATRIX ELEMENTS ARE NON-INTEGERS, MODIFY THE DISPLAY IN PRINTF 
 * STATEMENTS APPROPRIATELY.
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * STAGE #01:
 * SQUARE MATRICES ONLY
 * MATRIX ELEMENTS GENERATED WITHIN THE PROGRAM
 *
 * STAGE #02: (PROPOSED)
 * SQUARE MATRICES ONLY
 * MATRIX ELEMENTS READ FROM EXTERNAL DATA FILES - USEFUL
 * WHEN A THIRD PARTY PROGRAM GENERATES DATA. ALSO REMOVES
 * THE NEED FOR RECOMPILING THE PROGRAM EVERY TIME ORDER OF
 * MATRICES CHANGE. WRITE THE PRODUCT MATRIX TO A FILE.
 *
 * STAGE #03: (PROPOSED)
 * NOT NECESSARILY SQUARE MATRICES, i.e.
 * A(I x J) * B (J x K) = C(I x K)
 * READ MATRICES FROM EXTERNAL DATA FILES
 * CHECK TO MAKE SURE 'COLS(A) = ROWS(B)' BEFORE
 * ATTEMPTING TO PERFORM THE ACTUAL MULTIPLICATION.
 * WRITE THE PRODUCT MATRIX TO A FILE.
 *
 * FIRST WRITTEN: GOWTHAM; Fri, 10 Sep 2010 23:33:28 -0400
 * LAST MODIFIED: GOWTHAM; Sat, 20 Nov 2010 02:19:38 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/11/30/mpi-c-matrix-multiplication-order-n/
 *
 * COMPILATION:
 * mpicc -g -Wall -lm matrix_multiply.c -o matrix_multiply.x
 *
 * EXECUTION:
 * mpirun -machinefile MACHINEFILE -np NPROC ./matrix_multiply.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM;
 *               MUST BE LESS THAN OR EQUAL TO THE ORDER OF THE 
 *               MATRICES INVOLVED. 
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #339933;">#define MASTER   0  /* Process ID for MASTER                                */</span>
<span style="color: #339933;">#define N      128  /* Order of the matrices                                */</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* FUNCTION TO MAP THE PROCESSOR ID
 * 'dest' OPTION IN MPI_Send MUST BE AN INTEGER BETWEEN 0 AND 'n_proc - 1' 
*/</span>
<span style="color: #993333;">int</span> map_processors<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n_procs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  n_procs <span style="color: #339933;">=</span> n_procs <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> ceil<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span> N <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> processor <span style="color: #339933;">=</span> i <span style="color: #339933;">/</span> r<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* RETURN THE VALUE 'processor + 1' */</span>
  <span style="color: #b1b100;">return</span> processor <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION */</span>
  <span style="color: #993333;">int</span>    n_procs<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Number of processors                   */</span>
         proc_id<span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Process identifier                     */</span>
         proc_source<span style="color: #339933;">,</span>    <span style="color: #808080; font-style: italic;">/* Mapped processor ID of the source      */</span>
         proc_dest<span style="color: #339933;">,</span>      <span style="color: #808080; font-style: italic;">/* Mapped processor ID of the destination */</span>
         proc_current<span style="color: #339933;">,</span>   <span style="color: #808080; font-style: italic;">/* Mapped processor ID                    */</span>
         i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span>        <span style="color: #808080; font-style: italic;">/* Dummy / running indices                */</span>
&nbsp;
  <span style="color: #993333;">double</span> A<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Matrix A - N x N                       */</span>
         B<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Matrix B - N x N                       */</span>
         C<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* Matrix C - N x N                       */</span> 
         sum<span style="color: #339933;">,</span>            <span style="color: #808080; font-style: italic;">/* Cumulated sum of matrix product        */</span>
         Abuf<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>        <span style="color: #808080; font-style: italic;">/* 1D array to hold the value of relevant 
                            portion of A received from MASTER      */</span>
         Cbuf<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>        <span style="color: #808080; font-style: italic;">/* 1D array to hold the value of 'sum' 
                            so that it can be sent to MASTER       */</span>
&nbsp;
  MPI_Status status<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* MPI structure containing return codes
                            for message passing operations         */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING:
   * POPULATE THE MATRICES A &amp; B
   * SEND FULL MATRIX B TO ALL WORKERS
   * SEND RELEVANT PORTIONS OF MATRIX A TO ALL WORKERS
   * RECEIVE MATRIX C FROM WORKERS
   * DISPLAY MATRIX C
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* POPULATE MATRIX A */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Matrix A:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* EACH MATRIX ELEMENT IS THE SUM OF INDICES */</span>
        A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> j<span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; %4.0lf&quot;</span><span style="color: #339933;">,</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* POPULATE MATRIX B */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Matrix B:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* EACH MATRIX ELEMENT IS THE PRODUCT OF INDICES */</span>
        B<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i <span style="color: #339933;">*</span> j<span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; %4.0lf&quot;</span><span style="color: #339933;">,</span> B<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* CONVERT MATRIX B INTO 1D BUFFERS;
     * SEND ALL THOSE 1D BUFFERS TO ALL WORKERS 
    */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n_procs<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* MPI_Send(buf, count, datatype, dest, tag, comm) */</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>B<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j <span style="color: #339933;">+</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* SEND RELEVANT PORTIONS OF A [SPLIT ROW-WISE] TO ALL WORKERS */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* ID OF THE PROCESSOR THAT'S RECEIVING THE INFORMATION (dest) */</span>
      proc_dest <span style="color: #339933;">=</span> map_processors<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* MPI_Send(buf, count, datatype, dest, tag, comm) */</span>
      MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> proc_dest<span style="color: #339933;">,</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">10000</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE RESULT FROM WORKERS */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* ID OF THE PROCESSOR THAT'S SENDING THE INFORMATION (source) */</span>
      proc_source <span style="color: #339933;">=</span> map_processors<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* MPI_Recv(buf, count, datatype, source, tag, comm, status) */</span>
      MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>C<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> proc_source<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* PRINT MATRIX C */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Matrix C:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; %4.0lf&quot;</span><span style="color: #339933;">,</span> C<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MASTER LOOP ENDS */</span>
&nbsp;
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF WORKER, THEN DO THE FOLLOWING:
   * RECEIVE ENTIRE MATRIX B FROM MASTER
   * RECEIVE RELEVANT PORTION OF MATRIX A FROM MASTER
   * PERFORM RELEVANT PORTION OF A * B CALCULATION
   * SEND RELEVANT PORTION OF MATRIX C TO MASTER 
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">&gt;</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE THE ENTIRE MATRIX B FROM MASTER AS A SET OF 1D BUFFERS */</span>
    <span style="color: #808080; font-style: italic;">/* MPI_Recv(buf, count, datatype, source, tag, comm, status) */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>B<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* FOR LOOP - MATRIX MULTIPLICATION - BEGINS */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* ID OF THE PROCESSOR THAT'S SENDING THE INFORMATION (source) */</span>
      proc_current <span style="color: #339933;">=</span> map_processors<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">/* IF LOOP - CHECK CURRENT PROCESSOR ID - BEGINS */</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_current <span style="color: #339933;">==</span> proc_id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* RECEIVE RELEVANT PORTIONS OF A [SPLIT ROW-WISE] FROM MASTER */</span>
        MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>Abuf<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">10000</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
          <span style="color: #808080; font-style: italic;">/* sum NEEDS TO BE SET TO ZERO FOR EVERY VALUE OF j */</span>
          sum <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #808080; font-style: italic;">/* CALCULATE THE sum, CUMULATIVE SUM OF PRODUCT OF 
           * Abuf[i] &amp; B[j][k]
          */</span>
          <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sum <span style="color: #339933;">=</span> sum <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>Abuf<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> B<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
&nbsp;
          <span style="color: #808080; font-style: italic;">/* ASSIGN THE VALUE OF 'sum' to Cbuf[j] SO THAT MPI_Send CAN 
           * SEND IT TO MASTER
          */</span> 
          Cbuf<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> sum<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* SEND Cbuf[j] TO MASTER */</span>
        MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>Cbuf<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* IF LOOP - CHECK CURRENT PROCESSOR ID - ENDS */</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* FOR LOOP - MATRIX MULTIPLICATION - ENDS */</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* WORKER LOOP ENDS */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc matrix_multiply.c <span style="color: #660033;">-o</span> matrix_multiply.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>matrix_multiply.x
&nbsp;
  Matrix A:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>
    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>
&nbsp;
  Matrix B:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>
&nbsp;
  Matrix C:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">2</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>matrix_multiply.x
&nbsp;
  Matrix A:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>
    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>
    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>
    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>
&nbsp;
  Matrix B:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">6</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">9</span>
&nbsp;
  Matrix C:
&nbsp;
    <span style="color: #000000;">0</span>   <span style="color: #000000;">14</span>   <span style="color: #000000;">28</span>   <span style="color: #000000;">42</span>
    <span style="color: #000000;">0</span>   <span style="color: #000000;">20</span>   <span style="color: #000000;">40</span>   <span style="color: #000000;">60</span>
    <span style="color: #000000;">0</span>   <span style="color: #000000;">26</span>   <span style="color: #000000;">52</span>   <span style="color: #000000;">78</span>
    <span style="color: #000000;">0</span>   <span style="color: #000000;">32</span>   <span style="color: #000000;">64</span>   <span style="color: #000000;">96</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>matrix_multiply.x
&nbsp;
  Matrix A:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>
    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>
    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>
    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">10</span>
    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">11</span>
    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">11</span>   <span style="color: #000000;">12</span>
    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">11</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">13</span>
    <span style="color: #000000;">7</span>    <span style="color: #000000;">8</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">11</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">13</span>   <span style="color: #000000;">14</span>
&nbsp;
  Matrix B:
&nbsp;
    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">5</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">7</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">2</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">8</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">14</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">3</span>    <span style="color: #000000;">6</span>    <span style="color: #000000;">9</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">15</span>   <span style="color: #000000;">18</span>   <span style="color: #000000;">21</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">4</span>    <span style="color: #000000;">8</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">16</span>   <span style="color: #000000;">20</span>   <span style="color: #000000;">24</span>   <span style="color: #000000;">28</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">5</span>   <span style="color: #000000;">10</span>   <span style="color: #000000;">15</span>   <span style="color: #000000;">20</span>   <span style="color: #000000;">25</span>   <span style="color: #000000;">30</span>   <span style="color: #000000;">35</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">6</span>   <span style="color: #000000;">12</span>   <span style="color: #000000;">18</span>   <span style="color: #000000;">24</span>   <span style="color: #000000;">30</span>   <span style="color: #000000;">36</span>   <span style="color: #000000;">42</span>
    <span style="color: #000000;">0</span>    <span style="color: #000000;">7</span>   <span style="color: #000000;">14</span>   <span style="color: #000000;">21</span>   <span style="color: #000000;">28</span>   <span style="color: #000000;">35</span>   <span style="color: #000000;">42</span>   <span style="color: #000000;">49</span>
&nbsp;
  Matrix C:
&nbsp;
    <span style="color: #000000;">0</span>  <span style="color: #000000;">140</span>  <span style="color: #000000;">280</span>  <span style="color: #000000;">420</span>  <span style="color: #000000;">560</span>  <span style="color: #000000;">700</span>  <span style="color: #000000;">840</span>  <span style="color: #000000;">980</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">168</span>  <span style="color: #000000;">336</span>  <span style="color: #000000;">504</span>  <span style="color: #000000;">672</span>  <span style="color: #000000;">840</span> <span style="color: #000000;">1008</span> <span style="color: #000000;">1176</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">196</span>  <span style="color: #000000;">392</span>  <span style="color: #000000;">588</span>  <span style="color: #000000;">784</span>  <span style="color: #000000;">980</span> <span style="color: #000000;">1176</span> <span style="color: #000000;">1372</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">224</span>  <span style="color: #000000;">448</span>  <span style="color: #000000;">672</span>  <span style="color: #000000;">896</span> <span style="color: #000000;">1120</span> <span style="color: #000000;">1344</span> <span style="color: #000000;">1568</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">252</span>  <span style="color: #000000;">504</span>  <span style="color: #000000;">756</span> <span style="color: #000000;">1008</span> <span style="color: #000000;">1260</span> <span style="color: #000000;">1512</span> <span style="color: #000000;">1764</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">280</span>  <span style="color: #000000;">560</span>  <span style="color: #000000;">840</span> <span style="color: #000000;">1120</span> <span style="color: #000000;">1400</span> <span style="color: #000000;">1680</span> <span style="color: #000000;">1960</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">308</span>  <span style="color: #000000;">616</span>  <span style="color: #000000;">924</span> <span style="color: #000000;">1232</span> <span style="color: #000000;">1540</span> <span style="color: #000000;">1848</span> <span style="color: #000000;">2156</span>
    <span style="color: #000000;">0</span>  <span style="color: #000000;">336</span>  <span style="color: #000000;">672</span> <span style="color: #000000;">1008</span> <span style="color: #000000;">1344</span> <span style="color: #000000;">1680</span> <span style="color: #000000;">2016</span> <span style="color: #000000;">2352</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F30%2Fmpi-c-matrix-multiplication-order-n%2F&amp;title=MPI%2FC%20%26%238211%3B%20Matrix%20Multiplication%20%5BOrder%20N%5D" id="wpa2a_18"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/30/mpi-c-matrix-multiplication-order-n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; An Advanced Send &amp; Receive</title>
		<link>http://sgowtham.net/blog/2010/11/28/mpi-c-an-advanced-send-receive/</link>
		<comments>http://sgowtham.net/blog/2010/11/28/mpi-c-an-advanced-send-receive/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 22:04:39 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3001</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. MPI_Send &#038; MPI_Recv Rarely does the MASTER in a parallelized program sends some information to a WORKER solely for the purpose of being displayed. Albeit simple, the program below demonstrates usage of MPI_Send &#038; MPI_Recv where the WORKER manipulates the [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">MPI_Send &#038; MPI_Recv</h3>
<p>Rarely does the MASTER in a parallelized program sends some information to a WORKER solely for the purpose of being displayed. Albeit simple, the program below demonstrates usage of <code>MPI_Send</code> &#038; <code>MPI_Recv</code> where the WORKER manipulates the information received from MASTER; sends that <em>new information</em> to MASTER and MASTER displays it.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* send_receive_advanced.c
 * PARALLEL [MPI] C PROGRAM TO DEMONSTRATE MPI_Send &amp; MPI_Recv FUNCTIONS.
 * MASTER [proc_id = 0] SENDS SOME DATA TO A WORKER [proc_id = 1].
 * WORKER PERFORMS SOME MATHEMATICAL OPERATION AND RETURNS THE
 * 'NEW INFORMATION' TO MASTER.
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * FIRST WRITTEN: GOWTHAM; Sat, 27 Nov 2010 17:10:10 -0500
 * LAST MODIFIED: GOWTHAM; Sat, 27 Nov 2010 18:15:23 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/11/28/mpi-c-an-advanced-send-receive/
 *
 * COMPILATION:
 * mpicc -g -Wall -lm send_receive_advanced.c -o send_receive_advanced.x
 *
 * EXECUTION:
 * mpirun -machinefile MACHINEFILE -np NPROC ./send_receive_advanced.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM;
 *               MUST BE EQUAL TO 2
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #339933;">#define MASTER  0   /* Process ID for MASTER                                */</span>
<span style="color: #339933;">#define WORKER  1   /* Process ID for WORKER                                */</span>
<span style="color: #339933;">#define N      10   /* Array size                                           */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION */</span>
  <span style="color: #993333;">int</span>    proc_id<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Process identifier                    */</span>
         n_procs<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Number of processors                  */</span>
         i<span style="color: #339933;">;</span>             <span style="color: #808080; font-style: italic;">/* Dummy/Running index                   */</span>
&nbsp;
  <span style="color: #993333;">double</span> x<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>          <span style="color: #808080; font-style: italic;">/* 1D array of size N
                           Information sent from MASTER
                           Information received by WORKER        */</span>
         y<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>          <span style="color: #808080; font-style: italic;">/* 1D array of size N
                           Information sent from WORKER
                           Information received by MASTER        */</span>
&nbsp;
  MPI_Status status<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* MPI structure containing return codes
                           for message passing operations        */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESS ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING:
   * POPULATE x[N]
   * SEND x[N] TO WORKER 
   * RECEIVE y[N] FROM WORKER
   * DISPLAY y[N]
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* POPULATE x[N] - EACH ARRAY ELEMENT IS JUST THE INDEX */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* SEND x[N] TO WORKER */</span>
    <span style="color: #808080; font-style: italic;">/* MPI_Send(buf, count, datatype, dest, tag, comm) */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Sending x[N] to WORKER [proc_id = 1] with TAG = 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Send<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE y[N] FROM WORKER */</span>
    <span style="color: #808080; font-style: italic;">/* MPI_Recv(buf, count, datatype, source, tag, comm, status) */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Receiving y[N] from WORKER [proc_id = 1] with TAG = 1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Recv<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* DISPLAY y[N] */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;    y[%d] = %2.0lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> y<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MASTER LOOP ENDS */</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF WORKER, THEN DO THE FOLLOWING:
   * RECEIVE x[N] FROM MASTER 
   * CREATE y[N]
   * SEND y[N] TO MASTER
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> WORKER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE x[N] FROM MASTER */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Receiving x[N] from MASTER [proc_id = 0] with TAG = 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Recv<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* CREATE A NEW ARRAY y[N]
     * A SIMPLE MATHEMATICAL MANIPULATION 
     * TAKE THE ELEMENTS OF x[N] AND SQUARE THEM
    */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      y<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* SEND y[N] TO MASTER */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Sending y[N] to MASTER [proc_id = 0] with TAG = 1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MPI_Send<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* WORKER LOOP ENDS */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc send_receive_advanced.c <span style="color: #660033;">-o</span> send_receive_advanced.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>send_receive_advanced.x
&nbsp;
  Sending x<span style="color: #7a0874; font-weight: bold;">&#91;</span>N<span style="color: #7a0874; font-weight: bold;">&#93;</span> to WORKER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">0</span>
&nbsp;
  Receiving y<span style="color: #7a0874; font-weight: bold;">&#91;</span>N<span style="color: #7a0874; font-weight: bold;">&#93;</span> from WORKER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">1</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> =  <span style="color: #000000;">0</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> =  <span style="color: #000000;">1</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> =  <span style="color: #000000;">4</span>
&nbsp;
  Receiving x<span style="color: #7a0874; font-weight: bold;">&#91;</span>N<span style="color: #7a0874; font-weight: bold;">&#93;</span> from MASTER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">0</span>
&nbsp;
  Sending y<span style="color: #7a0874; font-weight: bold;">&#91;</span>N<span style="color: #7a0874; font-weight: bold;">&#93;</span> to MASTER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">1</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> =  <span style="color: #000000;">9</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">16</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">25</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">36</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">7</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">49</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">8</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">64</span>
    y<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> = <span style="color: #000000;">81</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><br clear="all"><br />
Often, the output is not synchronized &#8211; by that, one means that <code>printf</code> statements from MASTER and WORKERS don&#8217;t always show up in the logically expected order. One way to fix this issue is to remove all <code>printf</code> statements from WORKERS.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F28%2Fmpi-c-an-advanced-send-receive%2F&amp;title=MPI%2FC%20%26%238211%3B%20An%20Advanced%20Send%20%26%23038%3B%20Receive" id="wpa2a_20"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/28/mpi-c-an-advanced-send-receive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; A Simple Send &amp; Receive</title>
		<link>http://sgowtham.net/blog/2010/11/28/mpi-c-a-simple-send-receive/</link>
		<comments>http://sgowtham.net/blog/2010/11/28/mpi-c-a-simple-send-receive/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 19:46:23 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2996</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. MPI_Send &#038; MPI_Recv More often than not, a sequential/serial program gets parallelized &#8211; assuming resources are available for such a conversion as well as execution &#8211; to solve a problem faster. Implied here is the fact that there is some [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">MPI_Send &#038; MPI_Recv</h3>
<p>More often than not, a sequential/serial program gets parallelized &#8211; assuming resources are available for such a conversion as well as execution &#8211; to solve a problem faster. Implied here is the fact that there is some sort of communication between the MASTER processor and its WORKER processors. <code>MPI_Send</code> &#038; <code>MPI_Recv</code> are often used to accomplish such a communication and the program below demonstrates a simple usage.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* send_receive_simple.c
 * PARALLEL [MPI] C PROGRAM TO DEMONSTRATE MPI_Send &amp; MPI_Recv FUNCTIONS.
 * MASTER [proc_id = 0] SENDS SOME DATA TO A WORKER [proc_id = 1].  
 * WORKER DISPLAYS THE DATA RECEIVED. 
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * FIRST WRITTEN: GOWTHAM; Sat, 27 Nov 2010 16:01:01 -0500
 * LAST MODIFIED: GOWTHAM; Sat, 27 Nov 2010 16:49:11 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/11/28/mpi-c-a-simple-send-receive/
 *
 * COMPILATION:
 * mpicc -g -Wall -lm send_receive_simple.c -o send_receive_simple.x
 *
 * EXECUTION:
 * mpirun -machinefile MACHINEFILE -np NPROC ./send_receive_simple.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM;
 *               MUST BE EQUAL TO 2
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #339933;">#define MASTER  0   /* Process ID for MASTER                                */</span>
<span style="color: #339933;">#define WORKER  1   /* Process ID for WORKER                                */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION */</span>
  <span style="color: #993333;">int</span>    proc_id<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Process identifier                    */</span>
         n_procs<span style="color: #339933;">;</span>       <span style="color: #808080; font-style: italic;">/* Number of processors                  */</span>
&nbsp;
  <span style="color: #993333;">double</span> x<span style="color: #339933;">;</span>             <span style="color: #808080; font-style: italic;">/* Information sent from MASTER
                           Information received by WORKER        */</span> 
&nbsp;
  MPI_Status status<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* MPI structure containing return codes
                           for message passing operations        */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESS ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF MASTER, THEN DO THE FOLLOWING:
   * INITIALIZE x
   * SEND IT TO THE WORKER 
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> MASTER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* INITIALIZE x */</span>
    x <span style="color: #339933;">=</span> <span style="color:#800080;">100.001</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* SEND x TO WORKER */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Sending x to WORKER [proc_id = 1] with TAG = 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* MPI_Send syntax:
     * MPI_Send(buf, count, datatype, dest, tag, comm)
     * [IN buf]      initial address of send buffer (choice)
     * [IN count]    number of elements in send buffer (nonnegative integer)
     * [IN datatype] datatype of each send buffer element (handle)
     * [IN dest]     rank of destination (integer)
     * [IN tag]      message tag (integer)
     *               must be unique for a pair of Send/Recv statements
     * [IN comm]     communicator (handle) 
    */</span>
    MPI_Send<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>x<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MASTER LOOP ENDS */</span>
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* IF WORKER, THEN DO THE FOLLOWING:
   * RECEIVE x FROM MASTER
   * DISPLAY IT 
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">==</span> WORKER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* RECEIVE x FROM MASTER */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>  Receiving x from MASTER [proc_id = 0] with TAG = 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* MPI_Recv syntax:
     * MPI_Recv(buf, count, datatype, source, tag, comm, status)
     * [OUT buf]     initial address of receive buffer (choice)
     * [IN count]    number of elements in receive buffer (integer)
     * [IN datatype] datatype of each receive buffer element (handle)
     * [IN source]   rank of source (integer)
     * [IN tag]      message tag (integer)
     *               must be same as the tag in Send statement
     * [IN comm]     communicator (handle)
     * [OUT status]  status object (Status) 
    */</span>
    MPI_Recv<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>x<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MPI_DOUBLE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* DISPLAY x */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>    x = %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* WORKER LOOP ENDS */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc send_receive_simple.c <span style="color: #660033;">-o</span> send_receive_simple.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>send_receive_simple.x
&nbsp;
  Sending x to WORKER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">0</span>
&nbsp;
  Receiving x from MASTER <span style="color: #7a0874; font-weight: bold;">&#91;</span>proc_id = <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> with TAG = <span style="color: #000000;">0</span>
&nbsp;
    x = <span style="color: #000000;">100.001000</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F28%2Fmpi-c-a-simple-send-receive%2F&amp;title=MPI%2FC%20%26%238211%3B%20A%20Simple%20Send%20%26%23038%3B%20Receive" id="wpa2a_22"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/28/mpi-c-a-simple-send-receive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; Oddly [or Evenly] Saying ello!</title>
		<link>http://sgowtham.net/blog/2010/11/28/mpi-c-oddly-or-evenly-saying-ello/</link>
		<comments>http://sgowtham.net/blog/2010/11/28/mpi-c-oddly-or-evenly-saying-ello/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 17:19:34 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2962</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Saying ello! &#038; Printing Odd/Even Processors For the simplest version of just saying Hello, World!, please refer to this post. This particular program not only prints Hello, World! from every processor, but also indicates whether processor ID is either odd [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Saying ello! &#038; Printing Odd/Even Processors</h3>
<p>For the simplest version of just saying <em>Hello, World!</em>, please refer to this <a href="http://sgowtham.net/blog/2010/11/28/mpi-c-saying-ello/" target="_blank">post</a>. This particular program not only prints <em>Hello, World!</em> from every processor, but also indicates whether processor ID is either odd or even.</p>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* hello_world_oddeven.c
 * PARALLEL [MPI] C PROGRAM TO PRINT 'HELLO, WORLD!' TO THE SCREEN AS
 * WELL AS WHETHER THE PROCESSOR ID IS EITHER ODD OR EVEN.
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * FIRST WRITTEN: GOWTHAM; Sat, 27 Nov 2010 10:30:11 -0500
 * LAST MODIFIED: GOWTHAM; Sat, 27 Nov 2010 11:25:34 -0500
 *
 * URL:
 * http://sgowtham.net/blog/2010/11/28/mpi-c-oddly-or-evenly-saying-ello/
 *
 * COMPILATION:
 * mpicc -g -Wall hello_world_oddeven.c -o hello_world_oddeven.x
 *
 * EXECUTION:
 * mpirun -machinefile MACHINEFILE -np NPROC ./hello_world_oddeven.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION */</span>
  <span style="color: #993333;">int</span> proc_id<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Process identifier   */</span>
      n_procs<span style="color: #339933;">;</span>       <span style="color: #808080; font-style: italic;">/* Number of processors */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESS ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* PRINT 'HELLO, WORLD!' FROM EVERY PROCESSOR 
   * ALSO INDICATE WHETHER THE PROCESSOR ID IS ODD OR EVEN
  */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc_id <span style="color: #339933;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, World! From %d [e] out of %d processors<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> proc_id<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, World! From %d [o] out of %d processors<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> proc_id<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc hello_world_oddeven.c <span style="color: #660033;">-o</span> hello_world_oddeven.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">1</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world_oddeven.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">1</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world_oddeven.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">2</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">2</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">4</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world_oddeven.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">4</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">8</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world_oddeven.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">7</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">5</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>o<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">6</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">4</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>e<span style="color: #7a0874; font-weight: bold;">&#93;</span> out of <span style="color: #000000;">8</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F28%2Fmpi-c-oddly-or-evenly-saying-ello%2F&amp;title=MPI%2FC%20%26%238211%3B%20Oddly%20%5Bor%20Evenly%5D%20Saying%20ello%21" id="wpa2a_24"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/28/mpi-c-oddly-or-evenly-saying-ello/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MPI/C &#8211; Saying ello!</title>
		<link>http://sgowtham.net/blog/2010/11/28/mpi-c-saying-ello/</link>
		<comments>http://sgowtham.net/blog/2010/11/28/mpi-c-saying-ello/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 15:02:21 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2929</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Saying ello! It&#8217;s an unwritten [and an auspicious] rule in the programming world to begin learning a new programming language by writing a program that prints Hello, World! to the screen. May be it&#8217;s just a manifestation of wishful/hopeful thinking: [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Saying ello!</h3>
<p>It&#8217;s an unwritten [and an auspicious] rule in the programming world to begin learning a new programming language by writing a program that prints <em>Hello, World!</em> to the screen. May be it&#8217;s just a manifestation of wishful/hopeful thinking: </p>
<blockquote><p>Hello, World! Here begins my journey of learning this new language. Bear with my mistakes and help me along the way.</p></blockquote>
<h3 class="blog">Program Listing</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* hello_world.c
 * PARALLEL [MPI] C PROGRAM TO PRINT 'HELLO, WORLD!' TO THE SCREEN.
 *
 * TESTED SUCCESSFULLY WITH MPICH2 (1.3.1) COMPILED AGAINST GCC (4.1.2) 
 * IN A LINUX BOX WITH QUAD CORE INTEL XEON PROCESSOR (1.86 GHz) &amp; 4GB OF RAM.
 *
 * FIRST WRITTEN: GOWTHAM; Sat, 27 Nov 2010 08:32:34 -0500
 * LAST MODIFIED: GOWTHAM; Sat, 27 Nov 2010 09:15:23 -0500
 *
 * URL: 
 * http://sgowtham.net/blog/2010/11/28/mpi-c-saying-ello/
 *
 * COMPILATION:
 * mpicc -g -Wall hello_world.c -o hello_world.x
 *
 * EXECUTION:
 * mpirun -machinefile MACHINEFILE -np NPROC ./hello_world.x
 *
 * NPROC       : NUMBER OF PROCESSORS ALLOCATED TO RUNNING THIS PROGRAM
 * MACHINEFILE : FILE LISTING THE HOSTNAMES OF PROCESSORS ALLOCATED TO
 *               RUNNING THIS PROGRAM
 *
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;  /* Core input/output operations                         */</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; /* Conversions, random numbers, memory allocation, etc. */</span>
<span style="color: #339933;">#include &lt;math.h&gt;   /* Common mathematical functions                        */</span>
<span style="color: #339933;">#include &lt;time.h&gt;   /* Converting between various date/time formats         */</span>
<span style="color: #339933;">#include &lt;mpi.h&gt;    /* MPI functionality                                    */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* MAIN PROGRAM BEGINS */</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* VARIABLE DECLARATION */</span>
  <span style="color: #993333;">int</span> proc_id<span style="color: #339933;">,</span>       <span style="color: #808080; font-style: italic;">/* Process identifier   */</span>
      n_procs<span style="color: #339933;">;</span>       <span style="color: #808080; font-style: italic;">/* Number of processors */</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INITIALIZE MPI */</span>
  MPI_Init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* GET THE PROCESS ID AND NUMBER OF PROCESSORS */</span>
  MPI_Comm_rank<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>proc_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MPI_Comm_size<span style="color: #009900;">&#40;</span>MPI_COMM_WORLD<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* PRINT 'HELLO, WORLD!' FROM EVERY PROCESSOR */</span>
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, World! From %d out of %d processors<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> proc_id<span style="color: #339933;">,</span> n_procs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* FINALIZE MPI */</span>
  MPI_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* INDICATE THE TERMINATION OF THE PROGRAM */</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #808080; font-style: italic;">/* MAIN PROGRAM ENDS */</span></pre></td></tr></table></div>

<h3 class="blog">Program Compilation &#038; Execution</h3>
<p>The machine where I am running this calculation, <em>dirac</em>, has 4 processors and has <a href="http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/" target="_blank">MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpicc
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">'mpicc -g -Wall -lm'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpicc
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">which</span> mpirun
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">'mpirun -machinefile $HOME/machinefile'</span>
	~<span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mpirun
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpicc hello_world.c <span style="color: #660033;">-o</span> hello_world.x
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">1</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> out of <span style="color: #000000;">1</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">2</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> out of <span style="color: #000000;">2</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> out of <span style="color: #000000;">2</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">4</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">2</span> out of <span style="color: #000000;">4</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">3</span> out of <span style="color: #000000;">4</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mpirun <span style="color: #660033;">-np</span> <span style="color: #000000;">8</span> .<span style="color: #000000; font-weight: bold;">/</span>hello_world.x
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">1</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">2</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">4</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">7</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">3</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">5</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">0</span> out of <span style="color: #000000;">8</span> processors
Hello, World<span style="color: #000000; font-weight: bold;">!</span> From <span style="color: #000000;">6</span> out of <span style="color: #000000;">8</span> processors
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>guest<span style="color: #000000; font-weight: bold;">@</span>dirac mpi_samples<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F28%2Fmpi-c-saying-ello%2F&amp;title=MPI%2FC%20%26%238211%3B%20Saying%20ello%21" id="wpa2a_26"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/28/mpi-c-saying-ello/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MPICH2 MPI With GNU Compilers</title>
		<link>http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/</link>
		<comments>http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 18:11:14 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[GCC]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2947</guid>
		<description><![CDATA[MPI For my understanding of what MPI is &#038;/or does, please refer to this post. Compiling MPICH2 (v1.3.1) with GNU Compilers (GCC v4.1.2) mkdir $HOME/tmp/ Download MPICH2 from the Argonne National Laboratory Save mpich2-1.3.1.tar.gz in $HOME/tmp/ cd $HOME/tmp/ tar -zxvpf mpich2-1.3.1.tar.gz cd mpich2-1.3.1 &#160; ./configure --prefix=$HOME/mpich2/1.3.1/gcc/4.1.2 --enable-f77 --enable-f90modules &#160; make &#160; make install Once the [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">MPI</h3>
<p>For my understanding of what MPI is &#038;/or does, please refer to <a href="http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/" target="_blank">this post</a>.</p>
<h3 class="blog">Compiling MPICH2 (v1.3.1) with GNU Compilers (GCC v4.1.2)</h3>
<ol>
<li><code>mkdir $HOME/tmp/</code></li>
<li>Download MPICH2 from the <a class="underline" href="http://www.mcs.anl.gov/research/projects/mpich2/" target="_blank">Argonne National Laboratory</a></li>
<li>Save <code>mpich2-1.3.1.tar.gz</code> in <code>$HOME/tmp/</code></li>
<li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvpf</span> mpich2-1.3.1.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> mpich2-1.3.1
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>mpich2<span style="color: #000000; font-weight: bold;">/</span>1.3.1<span style="color: #000000; font-weight: bold;">/</span>gcc<span style="color: #000000; font-weight: bold;">/</span>4.1.2 <span style="color: #660033;">--enable-f77</span> <span style="color: #660033;">--enable-f90modules</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">make</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

</li>
<li>Once the make install} process is successfully completed, add the following lines to <code>$HOME/.bashrc</code>:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MPICH2</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/mpich2/1.3.1/gcc/4.1.2&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PATH}</span>:<span style="color: #007800;">${MPICH2}</span>/bin:<span style="color: #007800;">${MPICH2}</span>/sbin&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MANPATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${MANPATH}</span>:<span style="color: #007800;">${MPICH2}</span>/man&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_LIBRARY_PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${MPICH2}</span>/lib:<span style="color: #007800;">${LD_LIBRARY_PATH}</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Useful aliases/shortcuts</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpicc</span>=<span style="color: #ff0000;">&quot;mpicc -g -Wall -lm&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mpirun</span>=<span style="color: #ff0000;">&quot;mpirun -machinefile <span style="color: #007800;">${HOME}</span>/machinefile&quot;</span></pre></div></div>

</li>
<li>Save and quit <code>$HOME/.bashrc</code></li>
<li>Run the command
<p>      <code>. $HOME/.bashrc</code></li>
<li>Run the command:
<p>      <code>hostname > $HOME/machinefile</code></li>
<li>Parallel calculations can now be performed.</li>
</ol>
<p>Refer to <a href="http://www.mcs.anl.gov/research/projects/mpich2/documentation/index.php?s=docs" target="_blank">MPICH2 Guides</a> for further details.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F26%2Fmpich2-mpi-with-gnu-compilers%2F&amp;title=MPICH2%20MPI%20With%20GNU%20Compilers" id="wpa2a_28"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/26/mpich2-mpi-with-gnu-compilers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Subversion &#8211; Changing Repository Location</title>
		<link>http://sgowtham.net/blog/2010/11/17/subversion-changing-repository-location/</link>
		<comments>http://sgowtham.net/blog/2010/11/17/subversion-changing-repository-location/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 04:33:39 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Subversion]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2874</guid>
		<description><![CDATA[Subversion (SVN) is a version control system initiated in 2000 by CollabNet Inc. It is used to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used Concurrent Versions System (CVS). Subversion is well-known in the open source [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://subversion.tigris.org/" target="_blank"><img src="http://sgowtham.net/blog/files/20090311/subversion.png" alt="Subversion" title="Subversion" align="left" width="100" vspace="10" hspace="10"></a> <strong>Subversion</strong> (SVN) is a version control system initiated in 2000 by CollabNet Inc. It is used to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used <strong>Concurrent Versions System</strong> (CVS). Subversion is well-known in the open source community and is used on many open source projects. </p>
<p>Subversion was started in 2000 as an effort to write a free version control system which operated much like CVS but with fixed bugs and misfeatures in CVS. By 2001, Subversion was sufficiently developed to be capable of hosting its own source code. More information, including this above paragraph, is <a href="http://en.wikipedia.org/wiki/Subversion_(software)" target="_blank">here</a>.</p>
<p><br clear="all"></p>
<h3 class="blog">Why Change Repository Location?</h3>
<p>It&#8217;s a really good question &#8211; especially since it adheres to the time tested &#038; trusted quote <em>If it ain&#8217;t broken, don&#8217;t fix it</em>. My repository was located at <code>${HOME}/svn_repo</code> since inception and recently, the linux box that housed this repository was upgraded. And as a result of this upgradation, the value of <code>${HOME}</code> is no longer the same. As such, it became necessary to change the location of this repository &#8211; to a place that has more storage space.</p>
<p><br clear="all"></p>
<h3 class="blog">Backup The Repository</h3>
<p>After committing all recent changes, I did backup the repository using a <a href="http://sgowtham.net/blog/2009/07/09/subversion-backing-up-repository/" target="_blank">script</a> I wrote over a year ago. Suppose that this repository dump is called <code>svnrepo.dump</code> and  that it&#8217;s stored in <code>/tnp</code>.</p>
<p><br clear="all"></p>
<h3 class="blog">Create The New Repository &#038; Import The Dump/Backup</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">svnadmin</span> <span style="color: #660033;">--fs-type</span> fsfs create <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo
<span style="color: #c20cb9; font-weight: bold;">svnadmin</span> load <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>svnrepo.dump</pre></td></tr></table></div>

<p>It&#8217;s more than likely &#8211; especially if you backed up your repository using my <a href="http://sgowtham.net/blog/2009/07/09/subversion-backing-up-repository/" target="_blank">script</a> &#8211; that you are presented with the following error message:</p>
<p><br clear="all"></p>
<blockquote><p>Dump stream contains a malformed header (with no &#8216;:&#8217;) at &#8216;* Dumped revision 0.&#8217;</p></blockquote>
<p>Prime reason for this error message is that my script dumped both <strong>stdout</strong> and <strong>stderr</strong> to the dump file. And the <code>svnadmin load</code> doesn&#8217;t like it. The fix is a one-liner:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">--binary-files</span>=text <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'^* Dumped revision'</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>svnrepo.dump <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>svnrepo_1.dump</pre></td></tr></table></div>

<p>Once that&#8217;s done [time to clean up depends on the size of the dump file as well as system resources], run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svnadmin</span> load <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>svnrepo_1.dump</pre></td></tr></table></div>

<p>Time to load the dump file contents into the repository depends on the size of the dump file as well as system resources.</p>
<p><br clear="all"></p>
<h3 class="blog">Inform The Clients</h3>
<p>Once the dump/backup was successfully loaded into the repository, the client [machines that check out a working copy and check in edits] needs to know about this change in repository location. My working copy is in <code>/scratch/$USER/svn_work</code>. So, all I did was</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_work
<span style="color: #7a0874; font-weight: bold;">cd</span> PROJECT
<span style="color: #c20cb9; font-weight: bold;">svn</span> switch <span style="color: #660033;">--relocate</span> <span style="color: #c20cb9; font-weight: bold;">svn</span>+<span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>hostname<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HOME}</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo \
                      <span style="color: #c20cb9; font-weight: bold;">svn</span>+<span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>hostname<span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo
<span style="color: #c20cb9; font-weight: bold;">svn</span> up</pre></td></tr></table></div>

<p>While the above process of informing the clients is relatively easier when there are few projects in a repository, it can get pretty tedious and time consuming when the number of projects is significantly higher. To that end, you may use the script I use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># svn_switchrepo.sh</span>
<span style="color: #666666; font-style: italic;"># BASH script to inform the subversion client about the change in repository location.</span>
<span style="color: #666666; font-style: italic;"># Automagically generates the list of projects</span>
<span style="color: #666666; font-style: italic;"># </span>
<span style="color: #666666; font-style: italic;"># USAGE: svn_switchrepo.sh</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># First written: Gowtham; Wed, 17 Nov 2010 12:40:04 -0500</span>
<span style="color: #666666; font-style: italic;"># Last modified: Gowtham; Wed, 17 Nov 2010 12:40:04 -0500</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Automagical generation of the project list</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MYPROJECTS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">svn</span> list <span style="color: #c20cb9; font-weight: bold;">svn</span>+<span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>hostname<span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo \
                   <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">'/'</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> 
<span style="color: #000000; font-weight: bold;">for</span> x <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$MYPROJECTS</span>
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">clear</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_work<span style="color: #000000; font-weight: bold;">/</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Switching repository for project <span style="color: #007800;">${x}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #800000;">${x}</span>
  <span style="color: #666666; font-style: italic;"># svn switch --relocate OLD_REPO_URL/PATH NEW_REPO_URL/PATH</span>
  <span style="color: #c20cb9; font-weight: bold;">svn</span> switch <span style="color: #660033;">--relocate</span> <span style="color: #c20cb9; font-weight: bold;">svn</span>+<span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>hostname<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HOME}</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo \
                        <span style="color: #c20cb9; font-weight: bold;">svn</span>+<span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>hostname<span style="color: #000000; font-weight: bold;">/</span>scratch<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$USER</span><span style="color: #000000; font-weight: bold;">/</span>svn_repo
  <span style="color: #c20cb9; font-weight: bold;">svn</span> up
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Done&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F11%2F17%2Fsubversion-changing-repository-location%2F&amp;title=Subversion%20%26%238211%3B%20Changing%20Repository%20Location" id="wpa2a_30"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/11/17/subversion-changing-repository-location/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MPI &#8211; The Message Passing Interface</title>
		<link>http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/</link>
		<comments>http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 07:00:48 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[MPI]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2914</guid>
		<description><![CDATA[MPI, message passing interface, is a language-independent communications protocol used to program parallel computers &#8211; supporting both point-to-point and collective communication modes. With high performance, scalability, and portability as its primary goals, MPI remains the dominant model used in high performance computing (HPC). For the technically oriented: MPI is a message-passing application programmer interface, together [...]]]></description>
			<content:encoded><![CDATA[<p>MPI, <em>message passing interface</em>, is a language-independent communications protocol used to program parallel computers &#8211; supporting both point-to-point and collective communication modes. With high performance, scalability, and portability as its primary goals, MPI remains the dominant model used in high performance computing (HPC). For the technically oriented:</p>
<blockquote><p>MPI is a message-passing application programmer interface, together with protocol and semantic specifications for how its features must behave in any implementation.</p></blockquote>
<p>Although MPI is a complex and multifaceted system, one can solve a wide variety of problems using just six of its functions. These six functions can also serve a way of introducing MPI:</p>
<p><br clear="all"></p>
<blockquote><p><code>MPI_Init : Initialize MPI process<br />
MPI_Comm_size : Determine number of processes<br />
MPI_Comm_rank : Determine the process ID<br />
MPI_Send : Send a message<br />
MPI_Recv : Receive a message<br />
MPI_Finalize : Terminate/Finalize MPI process</code></p></blockquote>
<p><br clear="all"><br />
<em>To be continued</em></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F09%2F28%2Fmpi-the-message-passing-interface%2F&amp;title=MPI%20%26%238211%3B%20The%20Message%20Passing%20Interface" id="wpa2a_32"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/09/28/mpi-the-message-passing-interface/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>MAC &#8211; Quick Fix For WPA2 Password Prompt Issue @ Michigan Tech</title>
		<link>http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/</link>
		<comments>http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 12:43:12 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[MichiganTech]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2823</guid>
		<description><![CDATA[Every once in a while, my Apple MacBook Pro &#8211; running Mac OS X [Snow Leopard] 10.6.4 &#8211; starts prompting for password before connecting to Michigan Tech&#8216;s (@MichiganTech) WPA2 Enterprise network. While it&#8217;s not a difficult task to enter the password once [or many times] a day, it nevertheless is painful. After some googling around [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while, my Apple MacBook Pro &#8211; running Mac OS X [Snow Leopard] 10.6.4 &#8211; starts prompting for password before connecting to <a href="http://www.mtu.edu/" target="_blank">Michigan Tech</a>&#8216;s (<a href="http://twitter.com/michigantech" target="_blank">@MichiganTech</a>) WPA2 Enterprise network. While it&#8217;s not a difficult task to enter the password once [or many times] a day, it nevertheless is painful. After some googling around and trial n&#8217; error, I have a solution &#8211; it worked for me and it might very well work for you. But as always, the instructions come with no guarantee and that you will use them at your very own discretion. More importantly, you will be solely responsible for any/all damage inflicted upon yourself by following this process, intellectual &#038;/or otherwise.</p>
<p><br clear="all"><br />

<a href='http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/fixingwpa2_00/' title='01. Open &#039;System Preferences&#039;; click on &#039;Network&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/FixingWPA2_00-150x150.png" class="attachment-thumbnail" alt="01. Open &#039;System Preferences&#039;; click on &#039;Network&#039;" title="01. Open &#039;System Preferences&#039;; click on &#039;Network&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/fixingwpa2_01/' title='02. Select &#039;Aiport&#039; and click on &#039;Advanced&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/FixingWPA2_01-150x150.png" class="attachment-thumbnail" alt="02. Select &#039;Aiport&#039; and click on &#039;Advanced&#039;" title="02. Select &#039;Aiport&#039; and click on &#039;Advanced&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/fixingwpa2_02/' title='03. Click on &#039;802.1X&#039; tab&#039;. If you see multiple entries for the same network under &#039;User Profiles&#039;, delete all but [the correct] one. Uncheck &#039;Always prompt for password&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/FixingWPA2_02-150x150.png" class="attachment-thumbnail" alt="03. Click on &#039;802.1X&#039; tab&#039;. If you see multiple entries for the same network under &#039;User Profiles&#039;, delete all but [the correct] one. Uncheck &#039;Always prompt for password&#039;" title="03. Click on &#039;802.1X&#039; tab&#039;. If you see multiple entries for the same network under &#039;User Profiles&#039;, delete all but [the correct] one. Uncheck &#039;Always prompt for password&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/fixingwpa2_03/' title='04. Click on &#039;Apply&#039; followed by &#039;Connect&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/FixingWPA2_03-150x150.png" class="attachment-thumbnail" alt="04. Click on &#039;Apply&#039; followed by &#039;Connect&#039;" title="04. Click on &#039;Apply&#039; followed by &#039;Connect&#039;" /></a>
</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F09%2F21%2Fmac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech%2F&amp;title=MAC%20%26%238211%3B%20Quick%20Fix%20For%20WPA2%20Password%20Prompt%20Issue%20%40%20Michigan%20Tech" id="wpa2a_34"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MAC &#8211; Syntax Highlighting Code Samples In Keynote Presentations</title>
		<link>http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/</link>
		<comments>http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 14:03:09 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Keynote]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2840</guid>
		<description><![CDATA[Ever wondered how cool an Apple Keynote presentation would look if the code samples were automagically syntax highlighted? Ever wondered if there was an automagical way to highlight the syntax? After having spent many a years highlighting the syntax manually, it dawned upon me that Apple is smart enough to have provided an elegant way [...]]]></description>
			<content:encoded><![CDATA[<p><em>Ever wondered how cool an Apple Keynote presentation would look if the code samples were automagically syntax highlighted?</em> <em>Ever wondered if there was an automagical way to highlight the syntax?</em> After having spent many a years highlighting the syntax manually, it dawned upon me that <em>Apple is smart enough to have provided an elegant way to get this done</em> and <em>may be I was being naive/dumb in not learning that way</em>. Turns out, both my suspicions were true. </p>
<p>Apple, as part of <a href="http://developer.apple.com/technologies/tools/xcode.html" target="_blank">XCode</a> [it's free; just need to create an account with <em>Apple Developer Connection</em>], provides a tool called <em>qlmanage</em> that accomplishes this &#8211; amongst other things. It probably has a graphical interface but personally, I&#8217;m a fan of command line. While I have no reason to expect that this won&#8217;t work on your Mac, these instructions come with no guarantee and that you will use them at your very own discretion. More importantly, you will be solely responsible for any/all damage inflicted upon yourself by following this process, intellectual &#038;/or otherwise.</p>
<p>Suppose that all code samples [<code>HelloDolly.f</code>, <code>HelloDolly.c </code>and <code>SimpleArithmetic.m</code> in this case] are in a folder called <strong>TestBed</strong> [Fig. 05] and the [BASH] script that will produce syntax highlighted version of these code samples is <code>code2keynote.sh</code> [Fig. 06]. Upon running <code>code2keynote.sh</code> in <strong>TestBed</strong> [using <strong>Terminal</strong> application], <strong>TestBed</strong> will have an additional folder for each code sample [Fig. 07]. One can get into any one of them &#8211; say <strong>HelloDolly.f.qlpreview</strong> [Fig. 08] and view the contents of <strong>Preview.rtf</strong> [Fig. 09] &#8211; copy the contents of it to be pasted in a Keynote presentation.</p>

<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/hellodolly_f/' title='01. HelloDolly.f - syntax highlighted in Terminal application'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/hellodolly_f-150x150.png" class="attachment-thumbnail" alt="01. HelloDolly.f - syntax highlighted in Terminal application" title="01. HelloDolly.f - syntax highlighted in Terminal application" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/hellodolly_c/' title='02. HelloDolly.c - syntax highlighted in Terminal application'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/hellodolly_c-150x150.png" class="attachment-thumbnail" alt="02. HelloDolly.c - syntax highlighted in Terminal application" title="02. HelloDolly.c - syntax highlighted in Terminal application" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/simplearithmetic_m/' title='03. SimpleArithmeic.m - syntax highlighted in Terminal application'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/simplearithmetic_m-150x150.png" class="attachment-thumbnail" alt="03. SimpleArithmeic.m - syntax highlighted in Terminal application" title="03. SimpleArithmeic.m - syntax highlighted in Terminal application" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_00/' title='04. man page for &#039;qlmanage&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_00-150x150.png" class="attachment-thumbnail" alt="04. man page for &#039;qlmanage&#039;" title="04. man page for &#039;qlmanage&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_01/' title='05. Contents of &#039;TestBed&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_01-150x150.png" class="attachment-thumbnail" alt="05. Contents of &#039;TestBed&#039;" title="05. Contents of &#039;TestBed&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_02/' title='06. code2keynote.sh - the automagical shell script'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_02-150x150.png" class="attachment-thumbnail" alt="06. code2keynote.sh - the automagical shell script" title="06. code2keynote.sh - the automagical shell script" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_03/' title='07. Contents of &#039;TestBed&#039;, after running &#039;code2keynote.sh&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_03-150x150.png" class="attachment-thumbnail" alt="07. Contents of &#039;TestBed&#039;, after running &#039;code2keynote.sh&#039;" title="07. Contents of &#039;TestBed&#039;, after running &#039;code2keynote.sh&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_04/' title='08. Contents of &#039;HelloDolly.f.qlpreview&#039;'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_04-150x150.png" class="attachment-thumbnail" alt="08. Contents of &#039;HelloDolly.f.qlpreview&#039;" title="08. Contents of &#039;HelloDolly.f.qlpreview&#039;" /></a>
<a href='http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/qlmanage_05/' title='09. Contents of &#039;Preview.rtf&#039; - copy and paste into Keynote presentation'><img width="150" height="150" src="http://sgowtham.net/blog/wp-content/uploads/2010/09/qlmanage_05-150x150.png" class="attachment-thumbnail" alt="09. Contents of &#039;Preview.rtf&#039; - copy and paste into Keynote presentation" title="09. Contents of &#039;Preview.rtf&#039; - copy and paste into Keynote presentation" /></a>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F09%2F18%2Fmac-syntax-highlighting-code-samples-in-keynote-presentations%2F&amp;title=MAC%20%26%238211%3B%20Syntax%20Highlighting%20Code%20Samples%20In%20Keynote%20Presentations" id="wpa2a_36"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BASH &#8211; Extracting Subversion Log</title>
		<link>http://sgowtham.net/blog/2010/01/28/bash-extracting-subversion-log/</link>
		<comments>http://sgowtham.net/blog/2010/01/28/bash-extracting-subversion-log/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 18:59:40 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[BASH]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2623</guid>
		<description><![CDATA[Subversion Subversion (SVN) is a version control system initiated in 2000 by CollabNet Inc. It is used to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used Concurrent Versions System (CVS). Subversion is well-known in the open [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Subversion</h3>
<p><a href="http://subversion.tigris.org/" target="_blank"><img src="http://sgowtham.net/blog/files/20090311/subversion.png" alt="Subversion" title="Subversion" align="left" width="100" vspace="10" hspace="10"></a> <strong>Subversion</strong> (SVN) is a version control system initiated in 2000 by CollabNet Inc. It is used to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used <strong>Concurrent Versions System</strong> (CVS). Subversion is well-known in the open source community and is used on many open source projects. </p>
<p>Subversion was started in 2000 as an effort to write a free version control system which operated much like CVS but with fixed bugs and misfeatures in CVS. By 2001, Subversion was sufficiently developed to be capable of hosting its own source code. More information, including this above paragraphs, is <a href="http://en.wikipedia.org/wiki/Subversion_(software)" target="_blank">here</a>.</p>
<p><br clear="all"></p>
<h3 class="blog">Why This Script?</h3>
<p>Per <a href="http://svnbook.red-bean.com/en/1.1/ch05s03.html" target="_blank">SVN Book</a>:</p>
<blockquote><p><code>svnlook</code> is a tool provided by Subversion for examining the various revisions and transactions in a repository. No part of this program attempts to change the repository—it&#8217;s a <em>read-only</em> tool. <code>svnlook</code>  is typically used by the repository hooks for reporting the changes that are about to be committed (in the case of the pre-commit hook) or that were just committed (in the case of the post-commit  hook) to the repository. A repository administrator may use this tool for diagnostic purposes.</p></blockquote>
<p>This BASH script uses <code>svnlook</code> and extends its usage to produce an elegant display of log of a Subversion repository.</p>
<p><br clear="all"></p>
<h3 class="blog">The Script</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># BASH script to generate SVN repository log</span>
<span style="color: #666666; font-style: italic;"># Refer to http://svnbook.red-bean.com/en/1.1/ch05s03.html for details</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Usage: svn_log.sh [REPO_PATH]</span>
<span style="color: #666666; font-style: italic;"># First written: Thu, 28 Jan 2010 12:19:37 -0500</span>
<span style="color: #666666; font-style: italic;"># Last modified: Thu, 28 Jan 2010 12:19:37 -0500</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  ERROR : REPOSITORY PATH missing&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Usage : svn_log.sh [REPO_PATH]&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">REPO</span>=<span style="color: #007800;">$1</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Determine the youngest/newest/latest revision number</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">REVISION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">svnlook</span> youngest <span style="color: #007800;">$REPO</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;----------------------------------------------------------------------------------------------------------------&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot; Rev #  Date/Time                                    Message&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;----------------------------------------------------------------------------------------------------------------&quot;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$REVISION</span> <span style="color: #660033;">-ge</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
  <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># Extract all information about the given revision</span>
    <span style="color: #c20cb9; font-weight: bold;">svnlook</span> info <span style="color: #007800;">$REPO</span> <span style="color: #660033;">-r</span> <span style="color: #007800;">$REVISION</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>sg_svnlook.tmp
&nbsp;
    <span style="color: #666666; font-style: italic;"># Pick out relevant information from the above</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DATETIME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>sg_svnlook.tmp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> -<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MESSAGE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>sg_svnlook.tmp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> -<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Print them to the screen</span>
    <span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;%6s %45s %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>  <span style="color: #ff0000;">&quot;<span style="color: #007800;">$REVISION</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DATETIME</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MESSAGE</span>&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Decrement the revision number by 1</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">REVISION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$REVISION</span> - <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;----------------------------------------------------------------------------------------------------------------&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F01%2F28%2Fbash-extracting-subversion-log%2F&amp;title=BASH%20%26%238211%3B%20Extracting%20Subversion%20Log" id="wpa2a_38"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/01/28/bash-extracting-subversion-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BASH &#8211; Router&#8217;s Public IP Address For Mac</title>
		<link>http://sgowtham.net/blog/2010/01/19/bash-routers-public-ip-address-for-mac/</link>
		<comments>http://sgowtham.net/blog/2010/01/19/bash-routers-public-ip-address-for-mac/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 16:47:09 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[AWK]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SED]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2601</guid>
		<description><![CDATA[Why This Script? While in its simplest form of usage, this script will only print out the public (&#038; private) IP address along with the SSID to which the machine is connected to, the results therefrom can be used for a variety of other purposes &#8211; I will let your imagination fly you to your [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Why This Script?</h3>
<p>While in its simplest form of usage, this script will only print out the public (&#038; private) IP address along with the SSID to which the machine is connected to, the results therefrom can be used for a variety of other purposes &#8211; I will let your imagination fly you to your dream destination!</p>
<p><br clear="all"></p>
<h3 class="blog">The Script</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># BASH script to display the IP Address - one that the machine has, </span>
<span style="color: #666666; font-style: italic;"># as well as router's public IP Address, if any.</span>
<span style="color: #666666; font-style: italic;"># Tested on Apple Mac Snow Leopard 10.6.2</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># First written : Gowtham, Thu Dec 31 12:05:35 EST 2005</span>
<span style="color: #666666; font-style: italic;"># Last modified : Gowtham, Thu Dec 31 12:05:35 EST 2005</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># en1 is the wireless interface. </span>
<span style="color: #666666; font-style: italic;"># Change appropriately, if different</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">WIFI_INTERFACE</span>=<span style="color: #ff0000;">&quot;en1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Details about the ${WIFI_INTERFACE}</span>
<span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #800000;">${WIFI_INTERFACE}</span> <span style="color: #000000; font-weight: bold;">&gt;</span> public_ip.txt
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">STATUS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> public_ip.txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;status&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $4}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Analyze the details and display appropriate information</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$STATUS</span>&quot;</span> = <span style="color: #ff0000;">&quot;active&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">IPADDRESS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> public_ip.txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> broadcast <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">SSID</span>=<span style="color: #000000; font-weight: bold;">`</span>system_profiler SPAirPortDataType <span style="color: #000000; font-weight: bold;">|</span> \
               <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-A1</span> <span style="color: #ff0000;">&quot;Current Network Information:&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> \
               <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">':'</span> <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">|</span> \
               <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/^[ \t]*//;s/[ \t]*$//'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot; <span style="color: #780078;">`date`</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot; You are not connected to the internet&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Get the public IP address, $NSIP</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">NSIP</span>=<span style="color: #000000; font-weight: bold;">`</span>curl <span style="color: #660033;">-s</span> http:<span style="color: #000000; font-weight: bold;">//</span>checkip.dyndns.org <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $6}'</span> <span style="color: #000000; font-weight: bold;">|</span> \
             <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">'&lt;'</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Compare $IPADDRESS &amp; $NSIP</span>
<span style="color: #666666; font-style: italic;"># Display appropriate information along with SSID</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$IPADDRESS</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NSIP</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #780078;">`date`</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  SSID       : <span style="color: #007800;">$SSID</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  IP Address : <span style="color: #007800;">$IPADDRESS</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #780078;">`date`</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  SSID                        : <span style="color: #007800;">$SSID</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Machine's static IP Address : <span style="color: #007800;">$IPADDRESS</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Router's  public IP Address : <span style="color: #007800;">$NSIP</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Delete the scratch file</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> public_ip.txt</pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Screenshots</h3>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20100119/WithoutRouter.png" alt="Direct connection to the internet" title="Direct connection to the internet" class="framed"></p>
<p class="bpcaption">Directly connected to the internet</p>
<p><br clear="all"></p>
<p><img src="http://sgowtham.net/blog/files/20100119/WithRouter.png" alt="Configured behind a router" title="Configured behind a router" class="framed"></p>
<p class="bpcaption">Configured behind a router</p>
<p><br clear="all"><br />
I must thank <a href="http://glasstheplanet.org/" target="_blank">Jon DeVree</a> [<a href="http://twitter.com/nuxi/" target="_blank">@nuxi</a>] for help with a <a href="http://sgowtham.net/blog/2006/01/04/bash-routers-public-ip-address/" target="_blank">previous version</a> of this script [for linux] as well as <a href="http://www.facebook.com/profile.php?id=6601332" target="_blank">Sarah</a> for graciously letting me use her WiFi network.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2010%2F01%2F19%2Fbash-routers-public-ip-address-for-mac%2F&amp;title=BASH%20%26%238211%3B%20Router%26%238217%3Bs%20Public%20IP%20Address%20For%20Mac" id="wpa2a_40"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2010/01/19/bash-routers-public-ip-address-for-mac/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>BASH &#8211; vasp2xyz, A Script To Extract Coordinates From VASP</title>
		<link>http://sgowtham.net/blog/2009/12/22/bash-vasp2xyz-a-script-to-extract-coordinates-from-vasp/</link>
		<comments>http://sgowtham.net/blog/2009/12/22/bash-vasp2xyz-a-script-to-extract-coordinates-from-vasp/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 22:00:54 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[AWK]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[VASP]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2547</guid>
		<description><![CDATA[Users of VASP are often familiar with the struggles/hardships to easily visualize the coordinates from the output file [OUTCAR]. To that effect, I wrote the following script [with help at a crucial stage from Pat Krogel, CEC @ MTU]. It expects OUTCAR &#038; POSCAR from a calculation to be in the same folder and when [...]]]></description>
			<content:encoded><![CDATA[<p>Users of <a href="http://cms.mpi.univie.ac.at/vasp/" target="_blank">VASP</a> are often familiar with the struggles/hardships to <em>easily</em> visualize the coordinates from the output file [<strong>OUTCAR</strong>]. To that effect, I wrote the following script [with help at a crucial stage from Pat Krogel, <a href="http://www.cec.mtu.edu"/ target="_blank">CEC</a> @ MTU]. It expects <strong>OUTCAR</strong> &#038; <strong>POSCAR</strong> from a calculation to be in the same folder and when successfully completed, it results in <strong>OUTPUT_FILENAME.xyz</strong> &#8211; containing frame-by-frame [corresponding to each (optimization) step] XYZ co-ordinates of the system.</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Script to extract the atomic positions (xyz) from </span>
<span style="color: #666666; font-style: italic;"># POSCAR &amp; OUTCAR files of VASP and write them to a </span>
<span style="color: #666666; font-style: italic;"># file, to be used with Jmol, MolDen and such other</span>
<span style="color: #666666; font-style: italic;"># third party programs</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># First written: Gowtham, Tue, 22 Dec 2009 11:11:12 -0500</span>
<span style="color: #666666; font-style: italic;"># Last modified: Gowtham, Tue, 22 Dec 2009 16:29:36 -0500</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Essential VASP Files</span>
<span style="color: #666666; font-style: italic;"># If missing, quit the program</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">POSCAR</span>=<span style="color: #ff0000;">&quot;POSCAR&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">OUTCAR</span>=<span style="color: #ff0000;">&quot;OUTCAR&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$POSCAR</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$OUTCAR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #007800;">$POSCAR</span> &amp; <span style="color: #007800;">$OUTCAR</span> found&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  ERROR : POSCAR &amp;/or OUTCAR missing&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Output Filename </span>
<span style="color: #666666; font-style: italic;"># If missing, quit the program</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">OUTPUT</span>=<span style="color: #007800;">$1</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  ERROR : OUTPUT_FILENAME Missing&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Usage : vasp2xyz.sh OUTPUT_FILENAME&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Delete scratch files</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> Final.tmp.<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$OUTPUT</span>.xyz
&nbsp;
<span style="color: #666666; font-style: italic;"># Atoms related information</span>
<span style="color: #666666; font-style: italic;"># Extracted from POSCAR &amp; OUTCAR</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Total number of atom types</span>
<span style="color: #666666; font-style: italic;"># Pat Krogel: Tue, 22 Dec 2009 13:13:48 -050</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">ATOM_TYPE</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">'6 p'</span> <span style="color: #007800;">$POSCAR</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ATOM_TYPE_TOTAL</span>=<span style="color: #800000;">${#ATOM_TYPE[@]}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Total number of atoms</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ATOMS_TOTAL</span>=<span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">i</span>=<span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> <span style="color: #660033;">-lt</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ATOM_TYPE_TOTAL</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">iATOM_TYPE</span>=<span style="color: #800000;">${ATOM_TYPE[$i]}</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ATOMS_TOTAL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$ATOMS_TOTAL</span> + <span style="color: #007800;">$iATOM_TYPE</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">i</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$i</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Atom labels</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">ATOM_LABEL</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;POTCAR:&quot;</span> <span style="color: #007800;">$OUTCAR</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> -<span style="color: #007800;">$ATOM_TYPE_TOTAL</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $3}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/_.*//g'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ATOM_LABEL_TOTAL</span>=<span style="color: #800000;">${#ATOM_LABEL[@]}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Print2Screen</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ATOM_TYPE_TOTAL</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ATOM_LABEL_TOTAL</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  ERROR : Atom Type Total (<span style="color: #007800;">$ATOM_TYPE_TOTAL</span>) does NOT&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;          match Atom Label Count (<span style="color: #007800;">$ATOM_LABEL_COUNT</span>)&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Please make sure OUTCAR &amp; POSCAR are from the same&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  system and calculation&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> 
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Number of Atom Types   :&quot;</span> <span style="color: #007800;">$ATOM_TYPE_TOTAL</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Atom Type              :&quot;</span> <span style="color: #800000;">${ATOM_LABEL[@]}</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Atom Type Count        :&quot;</span> <span style="color: #800000;">${ATOM_TYPE[@]}</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Total Number of Atoms  :&quot;</span> <span style="color: #007800;">$ATOMS_TOTAL</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># ATOM_LABEL_2 is an array of atom labels</span>
  <span style="color: #666666; font-style: italic;"># with each atom label appearing an appropriate</span>
  <span style="color: #666666; font-style: italic;"># number of times, to a total of $ATOMS_TOTAL</span>
  <span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-a</span> ATOM_LABEL_2
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">REPEAT</span>=<span style="color: #000000;">0</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">j</span>=<span style="color: #000000;">0</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$j</span>&quot;</span> <span style="color: #660033;">-lt</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ATOM_TYPE_TOTAL</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; 
  <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">k</span>=<span style="color: #000000;">1</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$k</span>&quot;</span> <span style="color: #660033;">-le</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${ATOM_TYPE[$j]}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; 
    <span style="color: #000000; font-weight: bold;">do</span>
      <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">REPEAT</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$REPEAT</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
      <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">k</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$k</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
      ATOM_LABEL_2<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">$REPEAT</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>=<span style="color: #800000;">${ATOM_LABEL[$j]}</span>
    <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">j</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$j</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #666666; font-style: italic;"># echo ${ATOM_LABEL_2[@]}</span>
<span style="color: #666666; font-style: italic;"># echo ${#ATOM_LABEL_2[@]}</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Record the line numbers where coordinates are written</span>
  <span style="color: #666666; font-style: italic;"># in OUTCAR. Actual coordinates begin 2 lines below</span>
  <span style="color: #666666; font-style: italic;"># the recorded numbers</span>
  <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;POSITION&quot;</span> <span style="color: #007800;">$OUTCAR</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/:/ /g'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/POSITION.*//g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> LINE_NUMBERS.tmp
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">FRAME_NUMBER</span>=<span style="color: #000000;">1</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> START
  <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">START_HERE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$START</span> + <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">END_HERE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$START_HERE</span> + <span style="color: #007800;">$ATOMS_TOTAL</span> - <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ATOMS_TOTAL</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$OUTPUT</span>.xyz
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;# Frame Number: <span style="color: #007800;">$FRAME_NUMBER</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$OUTPUT</span>.xyz
    <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$START_HERE</span>,<span style="color: #007800;">$END_HERE</span> p&quot;</span> <span style="color: #007800;">$OUTCAR</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{printf &quot;%12.8f %12.8f %12.8f\n&quot;, $1, $2, $3}'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> Final.tmp.<span style="color: #007800;">$FRAME_NUMBER</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">l</span>=<span style="color: #000000;">1</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> X Y Z
    <span style="color: #000000; font-weight: bold;">do</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${ATOM_LABEL_2[$l]}</span> <span style="color: #007800;">$X</span> <span style="color: #007800;">$Y</span> <span style="color: #007800;">$Z</span> <span style="color: #000000; font-weight: bold;">&gt;</span> Final.tmp.<span style="color: #007800;">$FRAME_NUMBER</span>.<span style="color: #007800;">$l</span>
      <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ printf &quot;%-3s %12.8f %12.8f %12.8f\n&quot;, $1, $2, $3, $4 }'</span> Final.tmp.<span style="color: #007800;">$FRAME_NUMBER</span>.<span style="color: #007800;">$l</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$OUTPUT</span>.xyz
      <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">l</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$l</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #000000; font-weight: bold;">done</span><span style="color: #000000; font-weight: bold;">&lt;</span>Final.tmp.<span style="color: #007800;">$FRAME_NUMBER</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">FRAME_NUMBER</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$FRAME_NUMBER</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #000000; font-weight: bold;">done</span><span style="color: #000000; font-weight: bold;">&lt;</span>LINE_NUMBERS.tmp
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">FRAMES_TOTAL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$FRAME_NUMBER</span> - <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  Total Number of Frames :&quot;</span> <span style="color: #007800;">$FRAMES_TOTAL</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Delete scratch files</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> LINE_NUMBERS.tmp 
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> Final.tmp.<span style="color: #000000; font-weight: bold;">*</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Test Case</h3>
<p>If POSCAR and OUTCAR aren&#8217;t handily available for testing, one could download the following:</p>
<p align="center"><a href="http://sgowtham.net/blog/files/20091222/POSCAR" target="_blank">POSCAR</a> | <a href="http://sgowtham.net/blog/files/20091222/OUTCAR" target="_blank">OUTCAR</a></p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20091222/vasp2xyz_00.png" title="vasp2xyz" alt="vasp2xyz" border="0">
<p class="bpcaption">Running vasp2xyz.sh</p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20091222/vasp2xyz_01.png" title="vasp2xyz" alt="vasp2xyz" border="0">
<p class="bpcaption">Resulting xyz file</p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20091222/ONCH.png" title="vasp2xyz" alt="vasp2xyz" border="0">
<p class="bpcaption">xyz file visualized with Jmol</p>
<p><br clear="all"><br />
If you have suggestions to make this better/efficient, please do post them as comments.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F12%2F22%2Fbash-vasp2xyz-a-script-to-extract-coordinates-from-vasp%2F&amp;title=BASH%20%26%238211%3B%20vasp2xyz%2C%20A%20Script%20To%20Extract%20Coordinates%20From%20VASP" id="wpa2a_42"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2009/12/22/bash-vasp2xyz-a-script-to-extract-coordinates-from-vasp/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>BASH &#8211; Wrappers For qstat In NPACI ROCKS 5.2.2</title>
		<link>http://sgowtham.net/blog/2009/12/10/bash-wrappers-for-qstat-in-npaci-rocks-5-2-2/</link>
		<comments>http://sgowtham.net/blog/2009/12/10/bash-wrappers-for-qstat-in-npaci-rocks-5-2-2/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 20:51:33 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[AWK]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rocks]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2536</guid>
		<description><![CDATA[What is ROCKS? Rocks Cluster Distribution (originally called NPACI Rocks) is a Linux distribution intended for high-performance computing clusters. It was started by NPACI and the SDSC in 2000, and was initially funded in part by an NSF grant (2000-2007) but is currently funded by the followup NSF grant. Rocks was initially based on the [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">What is ROCKS?</h3>
<p>Rocks Cluster Distribution (originally called NPACI Rocks) is a Linux distribution intended for high-performance computing clusters. It was started by NPACI and the SDSC in 2000, and was initially funded in part by an NSF grant (2000-2007)  but is currently funded by the followup NSF grant. Rocks was initially based on the Red Hat Linux distribution, however modern versions of Rocks are now based on CentOS, with a modified Anaconda installer that simplifies mass installation onto many computers. Rocks includes many tools (such as MPI) which are not part of CentOS but are integral components that make a group of computers into a cluster. Installations can be customized with additional software packages at install-time by using special user-supplied CDs (called <em>Roll CDs</em>). The Rolls extend the system by integrating seamlessly and automatically into the management and packaging mechanisms used by base software, greatly simplifying installation and configuration of large numbers of computers. Over a dozen Rolls have been created, including the SGE roll, the Condor roll, the Lustre roll, the Java roll, and the ganglia roll.</p>
<p><br clear="all"></p>
<h3 class="blog">Why wrappers?</h3>
<p>Functionality of <code>qstat</code> [part of the <strong>SGE Roll</strong>] changed in ROCKS 5.2.2 [it might have changed earlier than that, but that's when I first noticed]. To keep cluster users, supervisors and systems-administrators happy, I ended up writing these two wrappers:</p>
<p><br clear="all"></p>
<h3 class="blog">General Wrapper for <strong>qstat</strong></h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># A wrapper for 'qstat' command to show </span>
<span style="color: #666666; font-style: italic;"># the total number processors used/in use.</span>
<span style="color: #666666; font-style: italic;"># ROCKS 5.2.2</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Thu, 10 Dec 2009 12:27:59 -0500</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_TOTAL</span>=<span style="color: #ff0000;">&quot;142&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">USERS_LIST</span>=<span style="color: #ff0000;">&quot;
  user0
  user1
  user2
  user3
  user4&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_INUSE</span>=<span style="color: #000000; font-weight: bold;">`/</span>opt<span style="color: #000000; font-weight: bold;">/</span>gridengine<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lx26-amd64<span style="color: #000000; font-weight: bold;">/</span>qstat <span style="color: #660033;">-u</span> <span style="color: #007800;">$USERS_LIST</span> <span style="color: #000000; font-weight: bold;">|</span> \
                  <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print} (NR&gt;2) {used_procs+=\$9} END {print used_procs}'</span> <span style="color: #000000; font-weight: bold;">|</span> \
                  <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $NF}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_AVAIL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$PROC_TOTAL</span> - <span style="color: #007800;">$PROC_INUSE</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span> 
<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>gridengine<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lx26-amd64<span style="color: #000000; font-weight: bold;">/</span>qstat <span style="color: #660033;">-u</span> <span style="color: #007800;">$USERS_LIST</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span> 
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;   <span style="color: #007800;">$PROC_TOTAL</span> total processors :: <span style="color: #007800;">$PROC_INUSE</span> is/are in use :: <span style="color: #007800;">$PROC_AVAIL</span> is/are available&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span>
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">User Specific Wrapper for <strong>qstat</strong></h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># A wrapper for 'qstat' command to show only the jobs</span>
<span style="color: #666666; font-style: italic;"># submitted by a given user but show the total number</span>
<span style="color: #666666; font-style: italic;"># processors used/in use.</span>
<span style="color: #666666; font-style: italic;"># ROCKS 5.2.2</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Usage:</span>
<span style="color: #666666; font-style: italic;"># qu [USERNAME]</span>
<span style="color: #666666; font-style: italic;"># In the absence of any jobs submitted by USERNAME, or if</span>
<span style="color: #666666; font-style: italic;"># USERNAME is not specified at all, it just displays the jobs, </span>
<span style="color: #666666; font-style: italic;"># if any, by the currently logged in user.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Thu, 10 Dec 2009 12:27:59 -0500</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EXPECTED_ARGS</span>=<span style="color: #ff0000;">&quot;1&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_TOTAL</span>=<span style="color: #ff0000;">&quot;142&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">USERS_LIST</span>=<span style="color: #ff0000;">&quot;
  user0
  user1
  user2
  user3
  user4&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-ne</span> <span style="color: #007800;">$EXPECTED_ARGS</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span> 
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MYUSER</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$USER</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MYUSER</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_USER</span>=<span style="color: #000000; font-weight: bold;">`/</span>opt<span style="color: #000000; font-weight: bold;">/</span>gridengine<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lx26-amd64<span style="color: #000000; font-weight: bold;">/</span>qstat <span style="color: #660033;">-u</span> <span style="color: #007800;">$MYUSER</span> <span style="color: #000000; font-weight: bold;">|</span> \
                 <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print} (NR&gt;2) {used_procs+=\$9} END {print used_procs}'</span> <span style="color: #000000; font-weight: bold;">|</span> \
                 <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $NF}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_INUSE</span>=<span style="color: #000000; font-weight: bold;">`/</span>opt<span style="color: #000000; font-weight: bold;">/</span>gridengine<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lx26-amd64<span style="color: #000000; font-weight: bold;">/</span>qstat <span style="color: #660033;">-u</span> <span style="color: #007800;">$USERS_LIST</span> <span style="color: #000000; font-weight: bold;">|</span> \
                  <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print} (NR&gt;2) {used_procs+=\$9} END {print used_procs}'</span> <span style="color: #000000; font-weight: bold;">|</span> \
                  <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $NF}'</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PROC_AVAIL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$PROC_TOTAL</span> - <span style="color: #007800;">$PROC_INUSE</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$PROC_USER</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ;
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span> 
  <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>gridengine<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lx26-amd64<span style="color: #000000; font-weight: bold;">/</span>qstat <span style="color: #660033;">-u</span> <span style="color: #007800;">$MYUSER</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span> 
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot; <span style="color: #007800;">$PROC_TOTAL</span> total processors :: <span style="color: #007800;">$PROC_INUSE</span> is/are in use :: <span style="color: #007800;">$PROC_USER</span> is/are use by <span style="color: #007800;">$MYUSER</span> :: <span style="color: #007800;">$PROC_AVAIL</span> is/are available &quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'-----------------------------------------------------------------------------------------------------------------'</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #780078;">`hostname`</span> @ <span style="color: #780078;">`date -R`</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #007800;">$PROC_TOTAL</span> total processors&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #007800;">$PROC_INUSE</span> is/are in use &quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  0 is/are use by <span style="color: #007800;">$MYUSER</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  <span style="color: #007800;">$PROC_AVAIL</span> is/are available &quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F12%2F10%2Fbash-wrappers-for-qstat-in-npaci-rocks-5-2-2%2F&amp;title=BASH%20%26%238211%3B%20Wrappers%20For%20qstat%20In%20NPACI%20ROCKS%205.2.2" id="wpa2a_44"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2009/12/10/bash-wrappers-for-qstat-in-npaci-rocks-5-2-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Computing Total Travel Distance From GPS Tracks</title>
		<link>http://sgowtham.net/blog/2009/11/29/php-computing-total-travel-distance-from-gps-tracks/</link>
		<comments>http://sgowtham.net/blog/2009/11/29/php-computing-total-travel-distance-from-gps-tracks/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 01:33:47 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2503</guid>
		<description><![CDATA[Amongst other things, I like to keep a detailed track of where I have been [especially during hiking in the woods, venturing into areas that I have never been before, etc.]. For this purpose, I have configured my Garmin GPSMap 60CSx to record location/date-time data every three seconds and a while ago, I described another [...]]]></description>
			<content:encoded><![CDATA[<p>Amongst other things, I like to keep a detailed track of where I have been [especially during hiking in the woods, venturing into areas that I have never been before, etc.]. For this purpose, I have configured my  <a href="http://sgowtham.net/blog/2008/01/04/garmin-gpsmap-60csx-my-new-travel-companion/" target="_blank">Garmin GPSMap 60CSx</a> to record location/date-time data every three seconds and a while ago, I described another API I wrote to <a href="http://sgowtham.net/blog/2009/02/12/php-storing-gps-track-points-in-mysql/" target="_blank">store these track points in MySQL</a>. Added advantage of this, as I have mentioned before in previous posts, is that it can be used for <a href="http://en.wikipedia.org/wiki/Geotagging" target="_blank">geotagging</a> my <a href="http://sgowtham.net/showcase/" target="_blank">photographs</a>. For completeness sake, the MySQL table structure that holds track data is given below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`TRK`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`id`</span>          <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">11</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`latitude`</span>    <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`longitude`</span>   <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`altitude`</span>    <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`date`</span>        <span style="color: #999900; font-weight: bold;">date</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`time`</span>        <span style="color: #999900; font-weight: bold;">time</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> MYISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span> latin1 <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #CC0099;">=</span><span style="color: #008080;">1</span> <span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #FF9900; font-weight: bold;">UNIQUE</span> <span style="color: #990099; font-weight: bold;">INDEX</span> lldt <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`TRK`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`latitude`</span><span style="color: #000033;">,</span><span style="color: #008000;">`longitude`</span><span style="color: #000033;">,</span><span style="color: #008000;">`date`</span><span style="color: #000033;">,</span><span style="color: #008000;">`time`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Computing Total Travel Distance</h3>
<p>Suppose that my GPS recorded my thanksgiving travel &#8211; starting from 2009-11-25 at 08:00:00 am EST (<strong>$start_datetime = &#8220;2009-11-25 03:00:00&#8243;</strong> ; GPS units save date &amp; time in UTC) till 2009-11-29 at 06:00:00 pm EST (<strong>$end_time = &#8220;2009-11-29 13:00:00&#8243;</strong>) &#8211; recording track points every three seconds. While recording track points every three seconds in most cases might ensure that <em>straight line approximation</em> is good enough, the code below &#8211; for sake of mathematical rigor &#8211; uses Haversine formula.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Database connection
</span><span style="color: #000088;">$host</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbuser</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_USERNAME'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$connect</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbpasswd</span><span style="color: #009900;">&#41;</span> or
            <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;b&gt;MySQL Connection Error:&lt;/b&gt; '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span> or
            <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;b&gt;Database Connection Error:&lt;/b&gt; '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Radius of Earth (in miles)
</span><span style="color: #666666; font-style: italic;"># Use appropriate conversion factors if
</span><span style="color: #666666; font-style: italic;"># the distance is desired in other units.
</span><span style="color: #000088;">$earth_radius</span>  <span style="color: #339933;">=</span> <span style="color:#800080;">3960.00</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$distance_unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miles&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Date / Time in UTC 
</span><span style="color: #000088;">$start_datetime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2009-11-25 03:00:00&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end_datetime</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2009-11-29 13:00:00&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Running indices
</span><span style="color: #000088;">$i</span>         <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total_dis</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.00</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># SQL Query #1
</span><span style="color: #666666; font-style: italic;"># Get all GPS/Date Time points between start_datetime and
</span><span style="color: #666666; font-style: italic;"># end_datetime
</span><span style="color: #000088;">$sql1</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT latitude, longitude, date, time, &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;CONCAT(date, ' ', time) AS datetime &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;FROM TRK WHERE CONCAT(date, ' ', time) BETWEEN &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$start_datetime</span>' AND '<span style="color: #006699; font-weight: bold;">$end_datetime</span>' &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;ORDER BY UNIX_TIMESTAMP(datetime) ASC &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res1</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql1</span><span style="color: #009900;">&#41;</span> or
          <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Query : '</span> <span style="color: #339933;">.</span>
          <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span>
          <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myrow1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$lat1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$lon1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dat1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$tim1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dt_1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$dat1</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$tim1</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># SQL Query #2
</span>  <span style="color: #666666; font-style: italic;"># Get the GPS/Date Time point next to the one in question
</span>  <span style="color: #000088;">$sql2</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT latitude, longitude, date, time, &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;CONCAT(date, ' ', time) AS trk_dt_0, &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$dat1</span> <span style="color: #006699; font-weight: bold;">$tim1</span>' AS trk_dt_1 FROM TRK WHERE &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;UNIX_TIMESTAMP(CONCAT(date, ' ', time)) &gt; &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;UNIX_TIMESTAMP('<span style="color: #006699; font-weight: bold;">$dat1</span> <span style="color: #006699; font-weight: bold;">$tim1</span>') ORDER BY &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;UNIX_TIMESTAMP(trk_dt_0) ASC LIMIT 1 &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$res2</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql2</span><span style="color: #009900;">&#41;</span> or
             <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Query : '</span> <span style="color: #339933;">.</span>
             <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span>
             <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$nres2</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nres2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myrow2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$lat2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$lon2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$dat2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$tim2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$dt_2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$dat2</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$tim2</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Compute distance between two points in question using
</span>      <span style="color: #666666; font-style: italic;"># Haversine formula/function.
</span>      <span style="color: #666666; font-style: italic;"># Add the haversine distance to the total/cumulative distance
</span>      <span style="color: #000088;">$haver_dis</span> <span style="color: #339933;">=</span> distance_haversine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lat2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$total_dis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$total_dis</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$haver_dis</span> <span style="color: #339933;">;</span>
      <span style="color: #000088;">$i</span>         <span style="color: #339933;">=</span> <span style="color: #990000;">str_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">,</span> STR_PAD_LEFT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Present results at each step
</span>      <span style="color: #666666; font-style: italic;"># Can be piped out to a flat text file, if need be.
</span>      <span style="color: #666666; font-style: italic;"># Useful to check the speed of movement, etc.
</span>      <span style="color: #666666; font-style: italic;"># echo &quot;    $i :: $dt_1 : $lat1 : $lon1 : $dt_2 : $lat2 : $lon2 : $haver_dis : $total_dis\n&quot;;
</span>
      <span style="color: #666666; font-style: italic;"># Increment the running index
</span>      <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Function to accept latitue and longitude of 
</span><span style="color: #666666; font-style: italic;"># two locations and compute the distance between them.
</span><span style="color: #000000; font-weight: bold;">function</span> distance_haversine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lat2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$earth_radius</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$delta_lat</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lat2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$lat1</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$delta_lon</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lon2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$lon1</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$alpha</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$delta_lat</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$beta</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$delta_lon</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$a</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$alpha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$alpha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$beta</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$beta</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$c</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">asin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$distance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span><span style="color: #000088;">$earth_radius</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$distance</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$distance</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$distance</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Print the summary
</span><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  Start Date Time : <span style="color: #006699; font-weight: bold;">$start_datetime</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  End Date Time   : <span style="color: #006699; font-weight: bold;">$end_datetime</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  Total Distance  : <span style="color: #006699; font-weight: bold;">$total_dis</span> <span style="color: #006699; font-weight: bold;">$distance_unit</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Close the database connection
</span><span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
To keep the test case simple and computationally expenses minimal, I used a subset of my Thanksgiving travel &#8211; with <strong>$start_datetime = &#8220;2009-11-25 23:59:30&#8243;</strong> and <strong>$end_datetime   = &#8220;2009-11-27 00:00:30&#8243;</strong>. Remembering [clearly] that the odometer read <strong>100 miles</strong> for the duration and that we [<a href="http://someyooperbeach.com/" target="_blank">Nils</a> (<a href="http://twitter.com/UPBeaches/" target="_blank">@UPBeaches</a>) and yours truly] hiked about a mile [or two], the computed distance [<em>PHP script took 12+ hours on a Intel(R) Xeon(R) CPU L5420 2.50GHz Linux machine with 360MB RAM running x86 version of CentOS 4.7 operating system</em>] from the tracks came out to be <strong>101.9471 miles</strong> &#8211; a decent agreement I would say. Wouldn&#8217;t you agree?</p>
<p>Any suggestions/recommendations to optimize the SQL query and speed up the computation / execution of the PHP script will be greatly appreciated.</p>
<p><br clear="all"></p>
<h3 class="blog">Updates (2009.11.30)</h3>
<p>With significant suggestions from Srichand, Peter and Jon, I modified the MySQL table structure as well as the PHP script. With the first set of improvements, <strong>date</strong> and <strong>time</strong> were merged into one field. That reduced the overall execution time from 650+ minutes to 45 minutes. Second set of improvements included adding an index on date_time field as well as modifying the definition few fields. That reduced the overall execution time to a whopping 3 seconds.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`TRK`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`id`</span>          <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">11</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`latitude`</span>    <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`longitude`</span>   <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`altitude`</span>    <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`date<span style="color: #008080; font-weight: bold;">_</span>time`</span>   <span style="color: #999900; font-weight: bold;">float</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0000-00-00 00:00:00'</span> 
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> MYISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span> latin1 <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #CC0099;">=</span><span style="color: #008080;">1</span> <span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #FF9900; font-weight: bold;">UNIQUE</span> <span style="color: #990099; font-weight: bold;">INDEX</span> dt <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`TRK`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`date<span style="color: #008080; font-weight: bold;">_</span>time`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #FF9900; font-weight: bold;">UNIQUE</span> <span style="color: #990099; font-weight: bold;">INDEX</span> lldt <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`TRK`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`latitude`</span><span style="color: #000033;">,</span><span style="color: #008000;">`longitude`</span><span style="color: #000033;">,</span><span style="color: #008000;">`date<span style="color: #008080; font-weight: bold;">_</span>time`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;"># Database connection
</span><span style="color: #000088;">$host</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbuser</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_USERNAME'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$connect</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbpasswd</span><span style="color: #009900;">&#41;</span> or
            <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;b&gt;MySQL Connection Error:&lt;/b&gt; '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span> or
            <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;b&gt;Database Connection Error:&lt;/b&gt; '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span>
            <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Radius of Earth (in miles)
</span><span style="color: #666666; font-style: italic;"># Use appropriate conversion factors if
</span><span style="color: #666666; font-style: italic;"># the distance is desired in other units.
</span><span style="color: #000088;">$earth_radius</span>  <span style="color: #339933;">=</span> <span style="color:#800080;">3960.00</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$distance_unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miles&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Date / Time in UTC 
</span><span style="color: #000088;">$start_datetime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2009-11-25 03:00:00&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end_datetime</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2009-11-29 13:00:00&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Running indices
</span><span style="color: #000088;">$i</span>         <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total_dis</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.00</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># SQL Query #1
</span><span style="color: #666666; font-style: italic;"># Get all GPS/Date Time points between start_datetime and
</span><span style="color: #666666; font-style: italic;"># end_datetime
</span><span style="color: #000088;">$sql1</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT trk_latitude, trk_longitude, trk_datetime &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;FROM tracks WHERE trk_datetime BETWEEN &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$start_datetime</span>' AND '<span style="color: #006699; font-weight: bold;">$end_datetime</span>' &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;ORDER BY trk_datetime ASC &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res1</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql1</span><span style="color: #009900;">&#41;</span> or
          <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Query : '</span> <span style="color: #339933;">.</span>
          <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span>
          <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myrow1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$lat1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$lon1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dt1</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow1</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_datetime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># SQL Query #2
</span>  <span style="color: #666666; font-style: italic;"># Get the GPS/Date Time point next to the one in question
</span>  <span style="color: #000088;">$sql2</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT trk_latitude, trk_longitude, trk_datetime &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;FROM tracks WHERE trk_datetime &gt; '<span style="color: #006699; font-weight: bold;">$dt1</span>' ORDER BY &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql2</span>  <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;trk_datetime ASC LIMIT 1 &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$res2</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql2</span><span style="color: #009900;">&#41;</span> or
             <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Query : '</span> <span style="color: #339933;">.</span>
             <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span>
             <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$nres2</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nres2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myrow2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$lat2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$lon2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$dt2</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trk_datetime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Compute distance between two points in question using
</span>      <span style="color: #666666; font-style: italic;"># Haversine formula/function.
</span>      <span style="color: #666666; font-style: italic;"># Add the haversine distance to the total/cumulative distance
</span>      <span style="color: #000088;">$haver_dis</span> <span style="color: #339933;">=</span> distance_haversine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lat2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$total_dis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$total_dis</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$haver_dis</span> <span style="color: #339933;">;</span>
      <span style="color: #000088;">$i</span>         <span style="color: #339933;">=</span> <span style="color: #990000;">str_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">,</span> STR_PAD_LEFT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Present results at each step
</span>      <span style="color: #666666; font-style: italic;"># Can be piped out to a flat text file, if need be.
</span>      <span style="color: #666666; font-style: italic;"># Useful to check the speed of movement, etc.
</span>      <span style="color: #666666; font-style: italic;"># $time_now = date('r');  
</span>      <span style="color: #666666; font-style: italic;"># echo &quot; $i : $time_now : $dt1 : $lat1 : $lon1 : $dt2 : $lat2 : $lon2 : $haver_dis : $total_dis\n&quot;;
</span>
      <span style="color: #666666; font-style: italic;"># Increment the running index
</span>      <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Function to accept latitue and longitude of 
</span><span style="color: #666666; font-style: italic;"># two locations and compute the distance between them.
</span><span style="color: #000000; font-weight: bold;">function</span> distance_haversine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lat2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lon2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$earth_radius</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$delta_lat</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lat2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$lat1</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$delta_lon</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lon2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$lon1</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$alpha</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$delta_lat</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$beta</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$delta_lon</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$a</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$alpha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$alpha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lat2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$beta</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">deg2rad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$beta</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #000088;">$c</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">asin</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$distance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span><span style="color: #000088;">$earth_radius</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$distance</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$distance</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$distance</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Print the summary
</span><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  Start Date Time : <span style="color: #006699; font-weight: bold;">$start_datetime</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  End Date Time   : <span style="color: #006699; font-weight: bold;">$end_datetime</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  Total Distance  : <span style="color: #006699; font-weight: bold;">$total_dis</span> <span style="color: #006699; font-weight: bold;">$distance_unit</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Close the database connection
</span><span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F11%2F29%2Fphp-computing-total-travel-distance-from-gps-tracks%2F&amp;title=PHP%20%26%238211%3B%20Computing%20Total%20Travel%20Distance%20From%20GPS%20Tracks" id="wpa2a_46"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2009/11/29/php-computing-total-travel-distance-from-gps-tracks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS &#8211; Floating Next/Previous Links On Images</title>
		<link>http://sgowtham.net/blog/2009/10/22/css-floating-next-previous-links-on-images/</link>
		<comments>http://sgowtham.net/blog/2009/10/22/css-floating-next-previous-links-on-images/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 13:36:23 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2449</guid>
		<description><![CDATA[Why is this necessary, you ask? Ever since I first used PixelPost and deliciously delicious theme, I had been wanting to do something similar for my own photo gallery. Since PixelPost [nor any other photoblog software] ever came close to satisfying my needs, I ended up writing [still a work in progress to some extent] [...]]]></description>
			<content:encoded><![CDATA[<p><em>Why is this necessary</em>, you ask? Ever since I first used <a href="http://pixelpost.org/" target="_blank">PixelPost</a> and deliciously <a href="http://www.pixelpost.org/extend/templates/delicious/" target="_blank">delicious theme</a>, I had been wanting to do something similar for my own <a href="http://sgowtham.net/gallery/" target="_blank">photo gallery</a>. Since PixelPost [nor any other photoblog software] ever came close to satisfying my <em>needs</em>, I ended up writing [still a work in progress to some extent] one on my own and the desire to have a jazzy <em>Next</em> and <em>Previous</em> links floating on images. Not reading through the CSS and trying to understand how exactly it was to be accomplished, I was under the [wrong] assumption that knowledge of <a href="http://en.wikipedia.org/wiki/JavaScript" target="_blank">Javascript</a> was necessary/mandatory. </p>
<p>And recently, fine friends/members of <a href="http://lug.mtu.edu/" target="_blank">Linux Users Group @ Michigan Tech</a> set my assumptions straight and hinted that I could just use CSS to accomplish the same. As such, I ended up reading a bit more in detail about CSS, read carefully through the stylesheet of <a href="http://www.pixelpost.org/extend/templates/delicious/" target="_blank">delicious theme</a> and modified ever so slightly to fit my needs.</p>
<p><br clear="all"></p>
<h3 class="blog">CSS Part</h3>
<p>Preferably, this should go into the existing [new] CSS file and that file should then be included into the HTML/PHP file.</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#showcase_holder</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000000</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/loading.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;">50%</span> <span style="color: #933;">50%</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_image_border</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-10px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation</span> a <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation_prev</span><span style="color: #00AA00;">,</span> 
<span style="color: #cc00cc;">#showcase_navigation_next</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">49.9%</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
  <span style="color: #808080; font-style: italic;">/* Trick IE into showing hover */</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/blank.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation_prev</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation_next</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation_prev</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> 
<span style="color: #cc00cc;">#showcase_navigation_prev</span><span style="color: #3333ff;">:visited</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/left_arrow.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #933;">50%</span> <span style="color: #933;">50%</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#showcase_navigation_next</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> 
<span style="color: #cc00cc;">#showcase_navigation_next</span><span style="color: #3333ff;">:visited</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/right_arrow.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #933;">50%</span> <span style="color: #933;">50%</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
Download the following files, if you wish.</p>
<p><br clear="all"></p>
<p align="center">
<a href="http://sgowtham.net/blog/files/20091022/loading.gif" target="_blank" rel="lightbox[2449]">loading.gif</a> | <a href="http://sgowtham.net/blog/files/20091022/blank.gif" target="_blank" rel="lightbox[2449]">blank.gif</a> | <a href="http://sgowtham.net/blog/files/20091022/left_arrow.png" target="_blank" rel="lightbox[2449]">left_arrow.png</a> | <a href="http://sgowtham.net/blog/files/20091022/right_arrow.png" target="_blank" rel="lightbox[2449]">right_arrow.png</a>
</p>
<p><br clear="all"></p>
<h3 class="blog">HTML Part</h3>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">Terms in UPPER CASE indicate variables that need to be somehow/automagically </span>
<span style="color: #808080; font-style: italic;">supplied for proper functioning. If your image details are stored in a MySQL </span>
<span style="color: #808080; font-style: italic;">database and you are serving the page using PHP, you may use the getimagesize() </span>
<span style="color: #808080; font-style: italic;">function as follows:</span>
&nbsp;
<span style="color: #808080; font-style: italic;">list($width, $height, $type, $attribs) = getimagesize(&quot;filename.jpg&quot;);</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showcase_holder&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:IMAGE_WIDTHpx;height:IMAGE_HEIGHTpx;&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showcase_image_border&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:IMAGE_WIDTHpx;height:IMAGE_HEIGHTpx;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;CURRENT_IMAGE_URL&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Image Title&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Image Title&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;photo&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;photo&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background:black;&quot;</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showcase_navigation&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:IMAGE_WIDTHpx;height:IMAGE_HEIGHTpx;&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;PREV_IMAGE_URL&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showcase_navigation_prev&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;NEXT_IMAGE_URL&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showcase_navigation_next&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog">Working Example</h3>
<p>Kinda knew this was coming so, I will give you two &#8211; <a href="http://sgowtham.net/gallery/" target="_blank">here</a> and <a href="http://sgowtham.net/showcase/" target="_blank">here</a>,</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F10%2F22%2Fcss-floating-next-previous-links-on-images%2F&amp;title=CSS%20%26%238211%3B%20Floating%20Next%2FPrevious%20Links%20On%20Images" id="wpa2a_48"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2009/10/22/css-floating-next-previous-links-on-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MAC &#8211; Upgrading MacPorts</title>
		<link>http://sgowtham.net/blog/2009/10/21/mac-upgrading-macports/</link>
		<comments>http://sgowtham.net/blog/2009/10/21/mac-upgrading-macports/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 14:20:58 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2436</guid>
		<description><![CDATA[Wondering why is this necessary? Citing MacPorts, An installation of MacPorts and the ports installed by it are only designed to work on a single OS release and a single CPU architecture. If you upgrade to a new OS version (e.g. from Tiger to Leopard) or migrate to a new machine with a different type [...]]]></description>
			<content:encoded><![CDATA[<p>Wondering why is this necessary? Citing <a href="" target="_blank">MacPorts</a>,</p>
<blockquote><p>An installation of MacPorts and the ports installed by it are only designed to work on a single OS release and a single CPU architecture. If you upgrade to a new OS version (e.g. from Tiger to Leopard) or migrate to a new machine with a different type of CPU (e.g. PowerPC to Intel), you may get lucky and have your ports keep working, but in general, things will break. </p></blockquote>
<p>Fairly recently, my Mac underwent an upgrade from <a href="http://en.wikipedia.org/wiki/Mac_OS_X_v10.5" target="_blank">Leopard</a> to <a href="http://en.wikipedia.org/wiki/Mac_OS_X_v10.6" target="_blank">Snow Leopard</a> and not knowing that I needed to upgrade MacPorts to keep things sane, I tried installing Qt Octave &#8211; an open source equivalent of MATLAB [more about this in a subsequent post]. Needless to say, things started breaking and result was a [lot] more time consuming process of upgrading MacPorts.</p>
<p><br clear="all"></p>
<h3 class="blog">Part #0. Upgrading/Re-installing XCode</h3>
<p>First step is to download <em>XCode 3 for Snow Leopard</em> from <a href="http://developer.apple.com/mac/" target="_blank">Apple Developer Connection</a> [one needs to create an account to download the DMG but it's free]. One needs to ensure that the optional components for command line development are installed as well (<em>Unix Development</em> in the XCode 3.x installer). Unfortunately, XCode is not upgraded automagically during the regular/routine software updates but the installation went smooth and just for good measure, I rebooted the machine.</p>
<p><br clear="all"></p>
<h3 class="blog">Part #1. Upgrading MacPorts</h3>
<p>Download the appropriate <strong>.pkg</strong> file from <a href="http://www.macports.org/install.php" target="_blank">MacPorts</a> and complete the installation of base package. This went [should go] smooth as well. Once the installation is complete, run the following command in a terminal:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #660033;">-v</span> selfupdate</pre></div></div>

<p>If this results in an error <em>port: command not found</em>, update the definition of <strong>PATH</strong> variable [in <em>$HOME/.bashrc</em>] to include <strong>/opt/local/bin</strong>:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PATH}</span>:/opt/local/bin&quot;</span></pre></div></div>

<p>Save the <em>$HOME/.bashrc</em> and source it via</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>.bashrc</pre></div></div>

<p><br clear="all"></p>
<h3 class="blog">Part #2. Re-installing Ports</h3>
<p>Part of this stage is really easy &#8211; make a list of installed ports and remove all the installed ports. To accomplish these:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port installed <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>installed_ports.txt
<span style="color: #c20cb9; font-weight: bold;">sudo</span> port clean installed
<span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #660033;">-f</span> uninstall installed</pre></td></tr></table></div>

<p>The most time consuming [and as such most boring/entertaining] part is re-installing all the required/active ports &#8211; one at a time &#8211; by going through <strong>$HOME/installed_ports.txt</strong>. To significantly simplify this process and reduce the associated boredom/entertainment, I did the following:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;active&quot;</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>installed_ports.txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">' '</span> <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>active_ports.txt</pre></td></tr></table></div>

<p>I read through <strong>$HOME/active_ports.txt</strong> and removed duplicate entries if any as well as retained only the highest / most recent version of any given port. I then used the following shell script, <strong>port_reinstall.sh</strong>, to install them:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># BASH script to re-install previously installed ports on an Apple Mac</span>
<span style="color: #666666; font-style: italic;"># The list of active ports is imported from $HOME/active_ports.txt</span>
<span style="color: #666666; font-style: italic;"># and this list varies from person to person, depending on what (s)he</span>
<span style="color: #666666; font-style: italic;"># has installed previously. </span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Gowtham, Tue Oct 20 22:23:36 EDT 2009</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ACTIVE_PORTS</span>=<span style="color: #ff0000;">&quot;apr
  apr-util
  aquaterm
  arpack
  atk
  autoconf
  automake
  bluefish
  bzip2
  cairo
  cmake
  coreutils
  curl
  cyrus-sasl2
  db46
  docbook-xml
  docbook-xsl
  expat
  fftw-3
  fftw-3-single
  fontconfig
  freetype
  gawk
  gcc43
  gd2
  gettext
  ghostscript
  git-core
  glib2
  gmake
  gmp
  gnome-common
  gnome-icon-theme
  gnuplot
  gperf
  gqview
  GraphicsMagick
  groff
  gsed
  gtk-doc
  gtk2
  help2man
  hicolor-icon-theme
  icon-naming-utils
  ImageMagick
  intltool
  jmol
  jpeg
  libart_lgpl
  libglade2
  libiconv
  libidn
  libpixman
  libpng
  libtool
  libxml2
  libxslt
  lzmautils
  m4
  metis
  mpfr
  ncurses
  ncursesw
  neon
  openssl
  p5-compress-raw-zlib
  p5-compress-zlib
  p5-crypt-ssleay
  p5-error
  p5-html-parser
  p5-html-tagset
  p5-io-compress-base
  p5-io-compress-zlib
  p5-libwww-perl
  p5-locale-gettext
  p5-svn-simple
  p5-term-readkey
  p5-uri
  p5-xml-namespacesupport
  p5-xml-parser
  p5-xml-sax
  p5-xml-simple
  pango
  pcre
  pdflib
  perl5.8
  pkgconfig
  plotutils
  popt
  readline
  render
  rsync
  scrollkeeper
  serf
  shared-mime-info
  sqlite3
  subversion
  subversion-perlbindings
  SuiteSparse
  teTeX
  texi2html
  texinfo
  tiff
  urw-fonts
  wget
  Xft2
  xmlcatmgr
  xorg-bigreqsproto
  xorg-inputproto
  xorg-kbproto
  xorg-libice
  xorg-libsm
  xorg-libX11
  xorg-libXau
  xorg-libXaw
  xorg-libXdmcp
  xorg-libXext
  xorg-libXmu
  xorg-libXt
  xorg-renderproto
  xorg-util-macros
  xorg-xcmiscproto
  xorg-xextproto
  xorg-xf86bigfontproto
  xorg-xproto
  xorg-xtrans
  xpm
  xrender
  zlib&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> x <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$ACTIVE_PORTS</span>
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #007800;">$x</span>
  <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">5</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Once this file is saved, run this as</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>port_reinstall.sh <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>port_reinstall_00.txt</pre></div></div>

<p>and keep entering password when the process demands. Once the process is completed [could take hours, depending how many ports need to be installed, their dependencies and the network speed], make sure to read through <strong>$HOME/port_reinstall_00.txt</strong> &#8211; to learn about errors messages/warnings/notification of further actions, if any. After taking care of those things, run the script again &#8211; for good measure:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>port_reinstall.sh <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>port_reinstall_01.txt</pre></div></div>

<p>This process should preferably complete within minutes and the machine should be ready to use as before.</p>
<p><br clear="all"></p>
<h3 class="blog">Request</h3>
<p>If you find errors in this approach and/or know of better [more elegant] methods to accomplish the same, please do let me [and other readers] know of it by posing them as comments. They will be greatly appreciated.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F10%2F21%2Fmac-upgrading-macports%2F&amp;title=MAC%20%26%238211%3B%20Upgrading%20MacPorts" id="wpa2a_50"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2009/10/21/mac-upgrading-macports/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

