<?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</title>
	<atom:link href="http://sgowtham.net/blog/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>Thu, 22 Dec 2011 14:48:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</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>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_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/2011/05/04/bash-smart-extract-utility/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>2010 &#8211; The Year That Was</title>
		<link>http://sgowtham.net/blog/2010/12/31/2010-the-year-that-was/</link>
		<comments>http://sgowtham.net/blog/2010/12/31/2010-the-year-that-was/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 04:59:54 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=3083</guid>
		<description><![CDATA[It&#8217;s that time of the year again. Although December 31st is just another day and is just as good as any of the other 364/365 days, last day of the year nevertheless serves as a good point in time-line to pause and reflect upon what the year brought along. The list of what the year [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s that time of the year again. Although December 31st is just another day and is just as good as any of the other 364/365 days, last day of the year nevertheless serves as a good point in time-line to pause and reflect upon what the year brought along. The list of what the year did to me is pretty long &#8211; some good, few not so good, some inexplicably fair and few apparently unfair &#8211; and to narrate each one of them in any detail would be an improbable task. Believing that <em>a picture is worth a thousand words</em>, I figured the collage below would summarize the year to a good extent.</p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20101231/ByeBye2010.jpg" alt="2010" title="2010"><br />
<br clear="all"></p>
<p>As for the next 365 days or so, here are some things I like to do, in no particular order:</p>
<ol>
<li>Continue to do the work I love while keeping my body in the same place as my heart &#038; soul, living amidst people that have gone out of their way to help me in more ways than one. I could be asking for a bit too much but guess I have to right to dream &amp; since <a href="http://sgowtham.net/blog/2009/12/31/2009-the-year-that-was/" target="_blank" class="underline">one of last year&#8217;s dream</a> did become a reality, I have no reason to believe this one won&#8217;t.</li>
<li>Pursue sports, photography &#038; computers in greater detail.</li>
<li>Since the <em>Thousand Mile Dream</em> remained just that, I will drop the last segment from last year&#8217;s such plan and retain just <em>run longer distances consistently</em>.</li>
<li>Be more organized, disciplined, consistent and efficient in things I do.</li>
<li>Experience what it feels to be stinkingly rich.</li>
</ol>
<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%2F31%2F2010-the-year-that-was%2F&amp;title=2010%20%26%238211%3B%20The%20Year%20That%20Was" 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/2010/12/31/2010-the-year-that-was/feed/</wfw:commentRss>
		<slash:comments>5</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_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/2010/12/11/mpi-c-2-dimensional-heat-equation-square-grid/feed/</wfw:commentRss>
		<slash:comments>1</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_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/2010/12/02/mpi-c-monte-carlo-method-of-finding-pi/feed/</wfw:commentRss>
		<slash:comments>6</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_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/2010/11/30/mpi-c-matrix-multiplication-order-n/feed/</wfw:commentRss>
		<slash:comments>3</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_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/2010/11/28/mpi-c-an-advanced-send-receive/feed/</wfw:commentRss>
		<slash:comments>1</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_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/11/28/mpi-c-a-simple-send-receive/feed/</wfw:commentRss>
		<slash:comments>1</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_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/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_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/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_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/26/mpich2-mpi-with-gnu-compilers/feed/</wfw:commentRss>
		<slash:comments>7</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_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/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_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/09/28/mpi-the-message-passing-interface/feed/</wfw:commentRss>
		<slash:comments>9</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_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/09/21/mac-quick-fix-for-wpa2-password-prompt-issue-at-michigan-tech/feed/</wfw:commentRss>
		<slash:comments>9</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_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/09/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>[Another] Perfect Season In Review</title>
		<link>http://sgowtham.net/blog/2010/08/19/another-perfect-season-in-review/</link>
		<comments>http://sgowtham.net/blog/2010/08/19/another-perfect-season-in-review/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 23:30:28 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[MichiganTech]]></category>
		<category><![CDATA[Sports]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2722</guid>
		<description><![CDATA[It was Thursday, 13th August 2009, the day we, as a team, played just well enough to keep our slates clean and win the Michigan Tech Graduate Softball League Championship. And in festivities that followed in The Downtowner Lounge in Downtown Houghton, a manager of our team [yes, we do have a few - each [...]]]></description>
			<content:encoded><![CDATA[<p>It was Thursday, 13th August 2009, the day we, as a team, played just well enough to keep our slates clean and <a href="http://sgowtham.net/blog/2009/08/14/a-perfect-season-in-review/" target="_blank">win the Michigan Tech Graduate Softball League Championship</a>. And in festivities that followed in <em>The Downtowner Lounge</em> in Downtown Houghton, a manager of our team [yes, we do have a few - each one, (un)beknownst to others - to manage a different aspect of the game] put forth a seemignly simple question &#8212; <em>of the three championships you have been part of so far, which is your favorite one?</em> The answer, albeit sounding cocky &#038; arrogant to those who don&#8217;t us/me very well, was a polite <em><strong>The Next One</strong></em>. </p>
<p><img src="http://sgowtham.net/blog/files/20100819/n21_108-9022.jpg" border="0" align="left" style="margin-right:10px;">And here we are little over a year later, sitting in the same general area in <em>The Downtowner Lounge</em>, celebrating that <em><strong>Next One</strong></em> &#8211; albeit for a short amount of time, thanks to numerous great souls that have helped us/me understand &#038; appreciate the value of the opportunity we have earned &#8211; our second successive perfect season.</p>
<p>The festivities in 2009 lasted about six hours and the following week saw us look for replacements &#8211; to fill in the void to be left later on by some good [and really smart &#038; committed as well, to say the least] players [<a href="http://www.facebook.com/profile.php?id=6608147" target="_blank">Leslie</a>, <a href="http://www.facebook.com/profile.php?id=6601480" target="_blank">Russ</a>, and so on] to graduation. </p>
<p>While we didn&#8217;t want to care any less about the rumors prevailing in the community that <em>our team is/was stacked with good players</em> and that <em>it was a fluke that we won the championship</em> and that<em> our success in championship games was only 50%</em> [the only non-rumor] and so on, it was [and still is] hard to ignore such preposterous remarks. There are few things in life that come close to matching the feeling of proving your detractors wrong, to do something that someone [or no one] thought you could do or didn&#8217;t give you the deserved credit for doing what you did. And it was 15th October 2009, about SIX months before the tentative start of 2010 season, that some of the core members of this wonderful team started our off-season work outs, almost in a religious fashion. Regiment [<em>as in its usage as a verb</em>] included runs, weights and attempts to be smarter, patient &#038; mature about things that needed to be done on the field in clutch situations. </p>
<p>With weather being unseasonably warm by middle of March of 2010 and much of snow having disappeared, we hit the fields for early practice sessions. Many countless hours of practice and <em>organized team activity</em> over next 20 some weeks or so brought us &#8211; with several new faces &#8211; closer and made us better, a lot better &#8211; to understand and appreciate what each one brought to the table and come up with a plan to best utilize those resources to win games, one at a time. Just like last season, I could write about what we did right and we did wrong in each of the 15 games but it would sound more like a broken record &#8211; as such, I will just tabulate the results and talk about reasons later.</p>
<p><br clear="all"></p>
<table border="0" align="center" cellpadding="3" cellspacing="3">
<tr>
<td align="center" class="library"><b>#</b></td>
<td align="center" class="library"><b>Date</b></td>
<td align="center" class="library"><b>Opponent</b></td>
<td align="center" class="library"><b>Result</b></td>
<td align="center" class="library"><b>Score</b></td>
</tr>
<tr>
<td align="center" class="sresults">01</td>
<td align="center" class="sresults">2010.05.27</td>
<td align="left" class="sresults">Hu-Tang</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">21-10</td>
</tr>
<tr>
<td align="center" class="sresults">02</td>
<td align="center" class="sresults">2010.05.27</td>
<td align="left" class="sresults">Electrical Engg</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">7-0; By Forfeit (team did not show up)</td>
</tr>
<tr>
<td align="center" class="sresults">03</td>
<td align="center" class="sresults">2010.06.03</td>
<td align="left" class="sresults">Hu-Tang</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">20-3</td>
</tr>
<tr>
<td align="center" class="sresults">04</td>
<td align="center" class="sresults">2010.06.10</td>
<td align="left" class="sresults">LockJawed Zombies</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">23-2</td>
</tr>
<tr>
<td align="center" class="sresults">05</td>
<td align="center" class="sresults">2010.06.17</td>
<td align="left" class="sresults">ROFLCopter</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">12-3</td>
</tr>
<tr>
<td align="center" class="sresults">06</td>
<td align="center" class="sresults">2010.06.24</td>
<td align="left" class="sresults">Aaron Dayton</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">20-3</td>
</tr>
<tr>
<td align="center" class="sresults">07</td>
<td align="center" class="sresults">2010.06.24</td>
<td align="left" class="sresults">Justin Clark</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">15-4</td>
</tr>
<tr>
<td align="center" class="sresults">08</td>
<td align="center" class="sresults">2010.07.01</td>
<td align="left" class="sresults">Aaron Dayton</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">7-0; By Forfeit (team did not show up)</td>
</tr>
<tr>
<td align="center" class="sresults">09</td>
<td align="center" class="sresults">2010.07.08</td>
<td align="left" class="sresults">Information Technology Services</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">13-8</td>
</tr>
<tr>
<td align="center" class="sresults">10</td>
<td align="center" class="sresults">2010.07.15</td>
<td align="left" class="sresults">Physical Education</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">16-5</td>
</tr>
<tr>
<td align="center" class="sresults">11</td>
<td align="center" class="sresults">2010.07.22</td>
<td align="left" class="sresults">Business Sox</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">30-5</td>
</tr>
<tr>
<td align="center" class="sresults">12</td>
<td align="center" class="sresults">2010.07.29</td>
<td align="left" class="sresults">ROFLCopter</td>
<td align="center" class="sresults">W</td>
<td align="center" class="sresults">24-3</td>
</tr>
<tr>
<td align="center" class="sresults"><strong>13</strong></td>
<td align="center" class="sresults"><strong>2010.08.05</strong></td>
<td align="left" class="sresults"><strong>LockJawed Zombies (Round #1)</strong></td>
<td align="center" class="sresults"><strong>W</strong></td>
<td align="center" class="sresults"><strong>12-0</strong></td>
</tr>
<tr>
<td align="center" class="sresults"><strong>14</strong></td>
<td align="center" class="sresults"><strong>2010.08.12</strong></td>
<td align="left" class="sresults"><strong>Electrical Engg (Round #2)</strong></td>
<td align="center" class="sresults"><strong>W</strong></td>
<td align="center" class="sresults"><strong>14-6</strong></td>
</tr>
<tr>
<td align="center" class="sresults"><strong>15</strong></td>
<td align="center" class="sresults"><strong>2010.08.19</strong></td>
<td align="left" class="sresults"><strong>Electrical Engg (Finals)</strong></td>
<td align="center" class="sresults"><strong>W</strong></td>
<td align="center" class="sresults"><strong>17-3</strong></td>
</tr>
</table>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20100819/english_alphabets.png" alt="Team Fiziks 2010" title="Team Fiziks 2010" align="right" width="275" style="margin-top:15px;"><br />
We are the first ones to concede &#8211; concede the fact that not everything we did was perfect and there is plenty of room for improvement. But we did put up the best offense and best defense &#8211; with a league high of 251 (-27 compared to 2009) runs scored and allowing only 60 (+13 compared to 2009), for a league&#8217;s best run differential of +191 (-14 compared to 2009). More importantly, most of us had fun [it might not have seemed like it but trust me, fun is a very relative/subjective entity], we got better as an individual &#038; as a team, and even more importantly, most of the teams we played seemed to have fun too. And while there are many smaller reasons behind our success, the most notable ones have remained the <a href="http://sgowtham.net/blog/2009/08/14/a-perfect-season-in-review/" target="_blank">same as in 2009</a> and many a years before. It&#8217;s noteworthy to mention that continuing to stick with/use <em><a href="http://en.wikipedia.org/wiki/Lagrangian_mechanics" target="_blank" class="underline">Lagrangian</a> / <a href="http://en.wikipedia.org/wiki/Hamiltonian_mechanics" target="_blank" class="underline">Hamiltonian</a> Mechanics</em> has continued to yield (very) rich dividends.</p>
<p>While it&#8217;s not the first time that someone initiated the smack talk against this very proud team, we respect three of the four that did so, including the one that we played when all the marbles were at stake &#8211; for they showed up to defend their words by being physically present at the game and as such, shall remain nameless. However, there was one that does need a specific mention:</p>
<div class="pod">
<p>It was on 10th of June, about four weeks before we played what he claimed to be <em>his</em> team [later realization: he was a player off the bench, if that and, <em>his</em> team didn't even consider him to be a part of it], that I was watching <em>his</em> team play some other team and in all honesty, was cheering for <em>his</em> team &#8211; as it comprised of many a good friends of mine, who were an integral part of the famed <a href="http://www.michigantechhuskies.com/SportSelect.dbml?SPSID=90466&#038;SPID=10930&#038;DB_OEM_ID=18800&#038;KEY=&#038;Q_SEASON=2009" target="_blank">2009-2010 Michigan Tech Huskies Women&#8217;s Basketball Program</a>. </p>
<p>His claim [or I must say, the guarantee] was that <em>he</em> would kick our a$$, that <em>he knew</em> all the secrets to the way we played, that we were beatable [a fairly good and reasonable assessment] and that we would be sorry that we played <em>his</em> team. And as if this weren&#8217;t sufficient, he made his claim / guarantee abundantly public during every opportunity he had, in spite of numerous polite reminders that our team didn&#8217;t need any further motivation to be playing in this league and that we considered it to be our privilege, <em>not a right</em>, to be playing in this league.</p>
<p>Then comes 22nd of July, <em>the game day</em> &#8211; and the dude was a <em>NO SHOW</em>. It didn&#8217;t stop <em>his</em> team from being at the wrong end of our wrath and the final score, at the end of just five innings, read 30-5 [yeah, THIRTY-to-FIVE]; 4 of their 5 runs coming off of our mental errors. <em>His</em> team, without any knowledge of his mouthing off for over a month, felt like we were showing off and running up the score &#8211; we did feel sorry for the trouble that <em>his</em> team went through but felt immensely proud of the way our team responded to a situation &#8212; that we could raise our level when circumstance(s) demanded. Later in the day, we learned that he was busy sipping some glamorous drinks on yacht in Stillwater, Minnesota [<em>under the pretense of a dislocated shoulder</em> - I am more than 100% certain that I saw him within the confines of Michigan Tech's Student Development Complex the same day around 11am].</p>
<p>At a later (personal &#038; public) meeting at our Athletic Director&#8217;s Camp, he went on to further claim that the result of this aforementioned game would have been different had he been able to play and more importantly that we, Team FIZIKS, should pray not to face <em>his</em> team in the playoffs. It could just be he was [and still is] so full of himself and living comfortably within the confines of the fictitious Universe, that he didn&#8217;t realize that <em>his</em> team didn&#8217;t qualify for the upper tier playoffs like ours did.</p>
<p>Just the way our team is built and made to think, we did learn our lessons from this experience. Not that we would like to be the first ones to do so [I'm not claiming that some of us have never done it before], we learned the rules of talking smack:</p>
<ol>
<li>You are allowed to talk smack before a game, almost as much as you like, within acceptable and reasonable limits.</li>
<li>The least you can do when you have talked smack before a game is to show up, in person, to the game &#8211; either to play or to support your own team.</li>
<li>You lose all rights of talking smack when/once the team [or an individual on the opposing team] responds to your smack talk, not by retaliating smack talk but by an actual performance on the field.</li>
<li>Any further smack talk against the same team, in spite of them having performed successfully against you, will be considered an act of stupidity and being full of sh%% &#8211; to say the least.</li>
</ol>
<p>So, <em>THANK YOU</em> for teaching us these invaluable lessons. It&#8217;s only fair that we teach you something as well, to only partially return your kindness:</p>
<ol>
<li><strong>You are neither as good nor as important to your team as you think you are</strong> AND <strong>it&#8217;s not your team, most certainly not after you left them high and dry the way you did</strong> &#8211; we might be the first ones to bring you to this realization but trust us, you will find people like us more in your <em>real</em> life more frequently than the ones that tell you that <em>you are the greatest creation that the mankind has ever witnessed</em>; especially so when you miss the hidden meaning / interpretation of the soft phrasing of latter kind &#8211; that <em>you are good today but could be better</em>.</li>
<li><strong>Be humble</strong> &#8211; a very simple definition of humility is <em>just doing your job and letting it largely speak for itself</em> [we learned this from <a href="http://en.wikipedia.org/wiki/Todd_Christensen" target="_blank" class="underline">Todd Christensen</a>, a member of the <a href="http://en.wikipedia.org/wiki/Super_Bowl_XVIII" target="_blank" class="underline">Super Bowl Champions, 1983 Los Angeles Raiders</a>].</li>
<li>Taking a bunch of your friends, on one Friday night, to a bar for a drink or two DOES NOT mean you know how to build a team.</li>
<li>Keep your mouth shut when there is no need to open it.</li>
<li>When you talk the talk, be sure to walk the walk &#8211; claim that dislocated shoulder [or any other part of your body] is never an acceptable reason to be, at least, not cheering for your team.</li>
<li>Stop making [lame] excuses and take responsibility for your actions.</li>
<li>It&#8217;s not too difficult to say <em>I am sorry, I f#$%^ed up; I will try my best not to repeat my mistakes and to be better in my next attempt</em>. People around you are more than likely to forgive you and give you a second [or <em>n</em>th] chance at doing something.</li>
<li>In other words, since you are a soon-to-be an/a <a href="http://www.mtu.edu/business/" target="_blank" class="underline">MBA from Michigan Tech</a> and supposedly understand $$ better than rest of us do, <em>do not write a check that cannot be cashed or that your team is not willing to cash it for you</em>.</li>
</ol>
<p>If this is not easy enough to understand, please feel free to come back next season and apply for an internship with our team. If all goes well, we will approve your request and teach you these things, in a <em>hands on</em> fashion, that you have either ignored to learn &#038;/or effortlessly slept through when life was gracious enough to teach you in a friendly classroom-like environment.
</div>
<p><br clear="all"></p>
<p align="center"><img src="http://sgowtham.net/blog/files/20100819/n21_108-9024.png" alt="Team Fiziks 2010" title="Team Fiziks 2010"></p>
<p class="bpcaption">Not In Picture: Dr. Robert Weidman, Kelsey Cox, Laura Hess</strong></p>
<p><br clear="all"><br />
While there are teams [calling them a team, in the true sense of the word, is an atrocity in itself] and some individuals [local and non-local] have had [and will continue to have] issues with the success of our team [see one such assault via facebook below], we have very strong reasons to believe that the league is better off because we are a part of it. Like a team-mate phrased it effortlessly in so few words, <em>the rising tides lifts all ships; for now, we are the big ship</em>. And when a team that does come along to beat us <em>fair and square</em>, trust me, we will be happy for them &#8211; albeit a momentary disappointment. </p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20100819/facebook_00.png" alt="Facebook Assault" title="Facebook Assault" width="600"></p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20100819/facebook_01.png" alt="Facebook Assault" title="Facebook Assault" width="600"></p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20100819/facebook_02.png" alt="Facebook Assault" title="Facebook Assault" width="600"></p>
<p><br clear="all"><br />
We take a lot of pride in the way we build in our team, train the players that have never played the game before [yours-truly was one of them, eight years ago] or haven&#8217;t played in quite a while, practice at least once a week, learn the rules and regulations and follow them religiously so that we keep the game safe for everyone involved, not be the first one to initiate any kind of smack talk against any team, be humble in victory and graceful in defeat [while it might not seem so from reading this post], get better and smarter with each week/season, put in an enviable performance week in &#038; week out and year in &#038; year out. As a by-product of all this, we are insanely proud of our 86-7-1 record over the last seven years. There is no magic to what we do &#038;/or achieve as a team but there sure as hell is plenty of hard work, dedication and sacrifice by everyone involved. </p>
<p>I believe what we do is a microcosm of progress and of what causes progress; to be not wanting to be better tomorrow than what we are today, to settle for something good instead of pursuing greatness, to hope that the competition gets lower so that we can look better and not having the ability to generalize this to almost every other aspect of our lives &#8230; is, according to us, a crime against ourselves, and making a mockery of all the wonderful opportunities that life has sent our way.</p>
<p>Along with <em>being just good enough is not good enough</em> (a Lake Superior Walk realization) and <em>failing to plan is planning to fail</em> (a friendly advice from undergraduate days), [<em>settling for</em>] <em>good is the enemy of great</em> is as invaluable a lesson as I have ever learned in my life &#8211; thanks to a book that a dear friend, <a href="http://www.facebook.com/profile.php?id=80402311" target="_blank">Ian Marks</a>, graciously let me borrow over Christmas 2009 break and it has come to be, sort of, mission statement / guiding light of our team &#038; lives. I&#8217;m pretty sure that the double play opportunities we missed in the championship game &#8212; 6-4-3 and 4-6-3, with latter having had the more serious impact had the game been closer &#8212; will haunt us [our Short Stop and yours truly] for times to come, at least until we get another opportunity to get it right. And just to remind ourselves of the consequences of missing out on opportunities, we imposed on ourselves an eight mile run as a <em>moderate punishment</em>.  And just like 2009, the festivities lasted about six hours or less and we are on the look out for replacements players to fill in the void that will be left by some good [and really smart &#038; committed as well, to say the least] players [<a href="http://www.facebook.com/profile.php?id=6606301" target="_blank">Josh Carlson</a>, <a href="http://www.facebook.com/emwinder" target="_blank">Eric Winder</a> and so on] to graduation.</p>
<p><em>Thus begins an earnest &#038; humble attempt to be better tomorrow, as an individual and as a team, than we are today.</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%2F08%2F19%2Fanother-perfect-season-in-review%2F&amp;title=%5BAnother%5D%20Perfect%20Season%20In%20Review" 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/08/19/another-perfect-season-in-review/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Not So Perfect Season But &#8230; Mission Accomplished</title>
		<link>http://sgowtham.net/blog/2010/08/01/a-not-so-perfect-season-but-mission-accomplished/</link>
		<comments>http://sgowtham.net/blog/2010/08/01/a-not-so-perfect-season-but-mission-accomplished/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 15:30:54 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Sports]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2710</guid>
		<description><![CDATA[Grief &#8230; most would define it as the feeling of loss &#8211; usually something that happens in our mind and body when something we hold ever so close to ourselves is lost. It happened to a bunch of us, one more time that we ever wanted to experience &#038;/or remember, last year during the Copper [...]]]></description>
			<content:encoded><![CDATA[<p>Grief &#8230; most would define it as the feeling of loss &#8211; usually something that happens in our mind <u>and</u> body when something we hold ever so close to ourselves is lost. It happened to a bunch of us, one more time that we ever wanted to experience &#038;/or remember, last year during the <em>Copper Country Church League Softball</em> championship games. We were the winners of the winners bracket and our arch nemesis had to beat us twice to win the championship. And beat us they did &#8211; twice &#8211; with painstakingly narrow margins to send us home, in grief, to say the least; and in turn, to deal with many stages of dealing with such grief. </p>
<p>As is common, our first reaction to this grief was shock [&#038; denial] &#8211; we couldn&#8217;t believe that we had lost both games with very uncharacteristic all round performance, given how good we thought we were. Shock, being the natural anesthesia, did its part in protecting most of us from immediate pain [with some help from breweries &#038; bars downtown]. But no sooner did shock and anger subside began a long and difficult period of pain and mourning &#8211; discussions often revolving around <em>would have, could have and should have</em>s. Pain showed no signs of relief as we heard the news about what our team&#8217;s pitcher and dear friend started going through. Returning a majority of the team from its 2009 edition, it wouldn&#8217;t be an overstatement to claim that the grief and the pain that life had sent our way brought us together, helped us stay closer and focused through out the 2010 season. </p>
<p>Regular season, like it had been in many a recent years, was a formality. We stayed undefeated through 12 games to win the regular season. Then again, we had won the regular season quite a few times before but winning the playoffs, for some reason, had always been a step [or two] too far away. </p>
<p>We beat the teams that we were expected to beat through much of play offs, including our arch nemesis to decide the winner of winners&#8217; bracket &#8211; by doing pretty much everything right which we hadn&#8217;t in previous occasions. To their credit, they won the losers&#8217; bracket and made it to the title game(s). Lo and behold, we did pretty much everything wrong and not taking anything away from them, they did every little thing right. And the result, a demoralizing 19-2 defeat. Bad side of this result was that that&#8217;s the worst we had ever played all season long. And the good side? We couldn&#8217;t play any worse. </p>
<p>So began the second [and final] game of the 2010 Church League &#8211; the ball took an odd bounce here and there, for either teams; the score swung like a not so <em>simple</em> pendulum and the lead changed teams, just like the weather in the UP. May be the good Lord wasn&#8217;t in the mood to call it a day and so, the game went extra innings with score tied at 9 apiece. An out, an intentional walk and a base hit saw our team have a runner in scoring position. With day light going down and heavens opening up with a few sprinkles, it was our rookie, Michael Rittenour, that jacked a warning track hit to bring the winning run home and bring home the team&#8217;s first ever championship.</p>
<p><br clear="all"></p>
<p align="center"><img src="http://sgowtham.net/blog/files/20100801/team_2010_cls.jpg" alt="Team Good Shepherd, 2010" title="Team Good Shepherd, 2010"></p>
<p><br clear="all"><br />
Relatively wilder party ensued &#8211; Gatorade dumped on our manager, pie in the face for Michael, and phone calls to let our beloved members of the team, now staying elsewhere &#038;/or that couldn&#8217;t play, know of the result. As many championship teams as I have ever been part of, this one meant [and still does] a whole lot more and extremely satisfying &#8211; for, the team as a whole came together to win it not just for ourselves, but for those that were once part of it and for one, that couldn&#8217;t play more. </p>
<p>To think that it was our destiny to win it all this season, would be a grave mistake &#8211; for, the same destiny &#8211; dressed up as our arch nemesis &#8211; kicked our <em>you-know-what</em>s in the first game. And for once, in 15 or so years for many of them and 6 for me, that we won&#8217;t have to worry about <em>would have, could have and should have</em>s; we won&#8217;t have to look at each other and hang our heads down &#8211; at least, not until we begin 2011 season.</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%2F08%2F01%2Fa-not-so-perfect-season-but-mission-accomplished%2F&amp;title=A%20Not%20So%20Perfect%20Season%20But%20%26%238230%3B%20Mission%20Accomplished" 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/08/01/a-not-so-perfect-season-but-mission-accomplished/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canal Run 2010 &#8211; A Case of Mind Over Body; And By Mind, I Mean&#8230;</title>
		<link>http://sgowtham.net/blog/2010/07/17/hancock-canal-run-2010/</link>
		<comments>http://sgowtham.net/blog/2010/07/17/hancock-canal-run-2010/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 15:30:33 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Sports]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2636</guid>
		<description><![CDATA[It was little under a year ago, July 18th, 2009 to be precise, that I saw many a good friends [Brian, Cal, Ian, Laura, Nils and more] effortlessly run past the (34th Annual) Hancock Canal Run finish line that my mind sew the seeds of being one such in its 35th edition. Training [if it [...]]]></description>
			<content:encoded><![CDATA[<p>It was little under a year ago, <em>July 18th, 2009</em> to be precise, that I saw many a good friends [<a href="http://www.facebook.com/profile.php?id=1110296360" target="_blank">Brian</a>, <a href="http://calarson.wordpress.com/" target="_blank">Cal</a>, <a href="http://www.facebook.com/profile.php?id=80402311" target="_blank">Ian</a>, <a href="http://www.facebook.com/lrpeterson" target="_blank">Laura</a>, <a href="http://someyooperbeach.com/" target="_blank">Nils</a> and more] effortlessly run past the (34th Annual) <a href="http://hancockcanalrun.com/" target="_blank">Hancock Canal Run</a> finish line that my mind sew the seeds of being one such in its 35th edition. Training [if it could be so called] ensued and after a considerable amount of it, I did take part in <a href="http://www.lakelinden5k10k.com/" target="_blank">Lake Linden 5k/10k</a> [organized by <a href="http://www.northwoodsendurance.com/" target="_blank">Rick, Chris</a> and many good friends] and finish it rather successfully [5k - 27:36 min]. Summer soon ended, fall then followed, colors changed, life got busier and the seeds that were once sown soon were buried deep under the snow.</p>
<p>When the Spring 2010 rolled in [yes, for once, there was actually Spring in Houghton/Keewenaw], snow melted and the way certain things transpired in later parts of March forced myself and a dear friend, Sarah, to (re)find structure in life, to regain and be more focused. Many a conversations with <a href="http://thisnorthernlife.com/" target="_blank">Amy</a>, <a href="http://runforshelterbox.org/2010/06/07/and-so-it-begins/" target="_blank">Scott</a>, <a href="http://www.facebook.com/lakshmikrishna" target="_blank">Lakshmi</a>, aforementioned friends, several members of Michigan Tech Huskies family and of local community started nurturing the once buried seeds of running/completing the [10 mile version of] Hancock Canal Run. </p>
<p>Being the athlete she is, Sarah did all the literature search, had discussions with established runners and dug out few detailed training plans from the depths of internet. Being busy with lives and at the same time not yet having complete control over the events that take place in our lives, we decided to train individually by honor system &#8211; keeping each other in check often &#8212; in person, or via phone calls or text messages or facebook. And our aim was to <del datetime="2010-07-19T07:07:42+00:00">not</del> just finish <del datetime="2010-07-19T07:09:06+00:00">but be within top ten of our respective age group</del>.</p>
<p>It worked really well for the first few weeks and we stuck to the training plan we had agreed upon. With all that happened from last ten days of May through first ten days of June [procrastination, <a href="http://sgowtham.net/gallery/20100522" target="_blank">this</a>, <a href="http://sgowtham.net/gallery/20100607" target="_blank">that</a>, and <em>The Legends of Table 23</em>], to say that our training took a back seat is an understatement of cosmological proportions. If not for <a href="http://www.facebook.com/profile.php?id=582725439" target="_blank">Terry</a> posting the countdown to the race day and reminding us every single day till the last day, training may have never resumed and this post may have never been written. In spite of skipping few weeks in our training plan and jumping ahead to week six [or seven, I don't remember], we honored the honor system and still had plenty of fun until the very last evening leading up to the run [see evidence below].</p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20100717/SMS.png" alt="Canal Run 2010" title="Canal Run 2010"></p>
<p class="bpcaption">Preparations and Thoughts via SMS</p>
<p><br clear="all"></p>
<p><img class="framed" src="http://sgowtham.net/blog/files/20100717/FacebookChats.png" alt="Canal Run 2010" title="Canal Run 2010"></p>
<p class="bpcaption">Final Preparations and Thoughts via Facebook</p>
<p><br clear="all"></p>
<p><img src="http://sgowtham.net/blog/files/20100717/PreRunMeal.jpg" alt="Canal Run 2010" title="Canal Run 2010" align="right" width="360"> 2009-2010 turned out to be the season that I had the most interactions ever with my beloved Huskies. Having never been an athlete but always wanting to emulate one, I decided to follow in their footsteps. While the reasons for which I woke up at about 2:30am on race day are immaterial, I did have my pre-run carb loading meal. Trust me, it did taste good and I have reasons to believe it did its job. </p>
<p>Weather on race day morning was near perfect &#8211; a bit chillier with west winds and relatively open areas of McLain State Park exposed to Lake Superior, all but last mile [or thereabouts] was well shaded from the sunshine. And this race re-taught me an important lesson &#8211; that <em>there is a real difference between being an athlete and acting like one</em>. Just like the previous &#038; my only race, I started out running at the group pace (~9:10 min/mile) instead of the pace I was comfortable with (~11:30 min/mile). Carrying my trustworthy GPS may have slowed me down a bit but it certainly helped gather useful data.</p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20100717/GProgress.png" alt="Canal Run 2010" title="Canal Run 2010"></p>
<p class="bpcaption">Mile by mile progress via <a href="http://sgowtham.net/blog/2008/01/04/garmin-gpsmap-60csx-my-new-travel-companion/" target="_blank">Garmin GPSMap 60CSx</a> &#8211; yeah, my life is a science experiment!</p>
<p><br clear="all"></p>
<p><a href="http://sgowtham.net/blog/files/20100717/minpermile.png" target="_blank" rel="lightbox[2636]"><img class="framed" src="http://sgowtham.net/blog/files/20100717/minpermile_s.png" alt="Canal Run 2010" title="Canal Run 2010"></a></p>
<p class="bpcaption">Some detailed stats of the run &#8211; click on the graph to see a larger, legible version</p>
<p><br clear="all"></p>
<p>As is evident from the <a href="http://earth.google.com/" target="_blank">Google Earth</a> screenshot and <a href="http://mathworks.com/" target="_blank">Matlab</a> graph generated using the data from my GPS, I did make adjustments to my speed, literally on the run, to conserve just enough energy to complete the run/race in under two hours. While I was [still am] very disappointed that I did not pass a single person during the entire length of the run, that I was the penultimate dude to complete it [121 out of 122 @ 1:52:27] and that there were only five more people who completed it after I did, I very well understand that I didn&#8217;t deserve to finish any sooner &#8211; given the [erratic] way in which I trained. And I am happy as I ever could be that I did finish it in under two hours as planned and that I wasn&#8217;t the last one to do so. Thanks to dear friend, <a href="http://mblanchard.com/" target="_blank">Michael</a>, for showing up on short notice to photograph my sorry you-know-what cross the finish line and one of me with <a href="http://www.superiortiming.com/searchable/receipt.asp?bib=92&#038;race_id=3" target="_blank">Sarah</a> [#92/35, 10 miles in 1:22:20], sharing the same stage [err, the same sidewalk].</p>
<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20100717/SarahG.jpg" alt="Canal Run 2010" title="Canal Run 2010"></p>
<p class="bpcaption">Dynamic Duo of Hancock Canal Run 2010<br /><em>Dynamic</em> on your right and <em>Duo</em> on your left</p>
<p><br clear="all"></p>
<p><img class="framed" src="http://sgowtham.net/blog/files/20100717/AmyG.jpg" alt="Canal Run 2010" title="Canal Run 2010"></p>
<p class="bpcaption">With <a href="http://www.superiortiming.com/searchable/receipt.asp?bib=87&#038;race_id=3" target="_blank">Amy</a> [#87/100, 10 miles in 1:41:55] and Sophie</p>
<p><br clear="all"></p>
<p>In summary, this one was definitely a case of <em>mind over body</em> and by mind, I mean my super-massive ego &#8211; a well-fed super-massive ego. While the training for and running in Canal Run 2010 did do its part in realizing my <a href="http://sgowtham.net/blog/2009/12/31/2009-the-year-that-was/" target="_blank"><em>Thousand Mile Dream</em></a> in part, it certainly has helped me respect my friends &#8212; supreme athletes, runners, swimmers, bikers, triathletes &#038; iron (wo)men and, members of this wonderful community, who yet again helped me realize one more dream &#8212; a lot more than I ever have.</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%2F07%2F17%2Fhancock-canal-run-2010%2F&amp;title=Canal%20Run%202010%20%26%238211%3B%20A%20Case%20of%20Mind%20Over%20Body%3B%20And%20By%20Mind%2C%20I%20Mean%26%238230%3B" 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/07/17/hancock-canal-run-2010/feed/</wfw:commentRss>
		<slash:comments>5</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_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/01/28/bash-extracting-subversion-log/feed/</wfw:commentRss>
		<slash:comments>1</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_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/19/bash-routers-public-ip-address-for-mac/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Mystic River &#8211; One of National Geographic&#8217;s Best Wallpapers In 2009</title>
		<link>http://sgowtham.net/blog/2010/01/19/mystic-river-one-of-national-geographics-best-wallpapers-in-2009/</link>
		<comments>http://sgowtham.net/blog/2010/01/19/mystic-river-one-of-national-geographics-best-wallpapers-in-2009/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 13:57:49 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2592</guid>
		<description><![CDATA[I wasn&#8217;t aware of this until I [rather carefully] going through the Photo of the Month notification email from National Geographic, that my Mystic River was picked as one of the best Wallpapers from 2009 [personally, I don't like the phrase one of the best, for by definition of best, there is only one] &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>I wasn&#8217;t aware of this until I [rather carefully] going through the <em>Photo of the Month</em> notification email from National Geographic, that my <a href="http://sgowtham.net/showcase/2007/07/14/19/56/41/mystic-river" target="_blank">Mystic River</a> was picked as <a href="" target="_blank">one of the best Wallpapers from 2009</a> [personally, I don't like the phrase <em>one of the best</em>, for by definition of best, there is only one] &#8211; indeed a pleasant bit of news! </p>
<p><br clear="all"><br />
<a href="http://photography.nationalgeographic.com/photography/photos/best-wallpapers-of-2009" target="_blank"><img src="http://sgowtham.net/blog/files/20100119/2009_NGMBestWallPapers.png" alt="National Geographic Best Wallpapers of 2009" title="National Geographic Best Wallpapers of 2009" class="framed"></a></p>
<p><br clear="all"><br />
And it goes without saying &#8211; I would have never seen this day but for constant support from <a href="http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan" target="_blank">the best place</a> on God&#8217;s green earth and very many friends from around the world &#8211; Thank you!!</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%2Fmystic-river-one-of-national-geographics-best-wallpapers-in-2009%2F&amp;title=Mystic%20River%20%26%238211%3B%20One%20of%20National%20Geographic%26%238217%3Bs%20Best%20Wallpapers%20In%202009" 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/mystic-river-one-of-national-geographics-best-wallpapers-in-2009/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>2009 &#8211; The Year That Was</title>
		<link>http://sgowtham.net/blog/2009/12/31/2009-the-year-that-was/</link>
		<comments>http://sgowtham.net/blog/2009/12/31/2009-the-year-that-was/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 04:59:59 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2567</guid>
		<description><![CDATA[It&#8217;s that time of the year again. Although December 31st is just another day and is just as good as any of the other 364/365 days, last day of the year nevertheless serves as a good point in time-line to pause and reflect upon what the year brought along. The list of what the year [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s that time of the year again. Although December 31st is just another day and is just as good as any of the other 364/365 days, last day of the year nevertheless serves as a good point in time-line to pause and reflect upon what the year brought along. The list of what the year did to me is pretty long &#8211; some good, few not so good, some inexplicably fair and few apparently unfair &#8211; and to narrate each one of them in any detail would be an improbable task. Believing that <em>a picture is worth a thousand words</em>, I figured the collage below would summarize the year to a good extent.</p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20091231/ByeBye2009.png" alt="2009" title="2009"><br />
<br clear="all"></p>
<p>As for the next 365 days or so, here are some things I like to do, in no particular order; thanks to dear friend <a href="http://someyooperbeach.com/" target="_blank">Nils Stenvig</a> (<a href="http://twitter.com/UPBeaches/" target="_blank">@UPBeaches</a>) for the coffee mug.</p>
<p><img style="margin-left:auto; margin-right:auto;" src="http://sgowtham.net/blog/files/20091231/n2c_106-3762.jpg" alt="SISU - Courtesy: Nils Stenvig" title="SISU - Courtesy: Nils Stenvig" align="right"></p>
<ol>
<li>Continue to do the work I love while keeping my body in the same place as my heart &#038; soul, living amidst people that have gone out of their way to help me in more ways than one. I could be asking for a bit too much but guess I have to right to dream &amp; since <a href="http://sgowtham.net/blog/2008/12/31/2008-the-year-that-was/" target="_blank" class="underline">one of last year&#8217;s dream</a> did become a reality, I have no reason to believe this one won&#8217;t.</li>
<li>Pursue sports, photography &#038; computers in greater detail.</li>
<li>Run <em>longer</em> distances consistently and for a cumulative of 1,000 miles.</li>
<li>Be more organized, disciplined, consistent and efficient in things I do.</li>
<li>Experience what it feels to be stinkingly rich.</li>
</ol>
<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%2F31%2F2009-the-year-that-was%2F&amp;title=2009%20%26%238211%3B%20The%20Year%20That%20Was" 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/31/2009-the-year-that-was/feed/</wfw:commentRss>
		<slash:comments>9</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_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/22/bash-vasp2xyz-a-script-to-extract-coordinates-from-vasp/feed/</wfw:commentRss>
		<slash:comments>17</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_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/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_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/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_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/22/css-floating-next-previous-links-on-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

