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

<channel>
	<title>Seventh Sense &#187; Technology</title>
	<atom:link href="http://sgowtham.net/blog/category/technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://sgowtham.net/blog</link>
	<description>Rambling about life&#039;s little things, in 7 ≡ 1 (mod 6) fashion</description>
	<lastBuildDate>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>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_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/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_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/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_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/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_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/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_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-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_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-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_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-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_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/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_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/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_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/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_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/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_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/18/mac-syntax-highlighting-code-samples-in-keynote-presentations/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_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/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_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/01/19/bash-routers-public-ip-address-for-mac/feed/</wfw:commentRss>
		<slash:comments>6</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_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/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_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/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_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/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_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/2009/10/22/css-floating-next-previous-links-on-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MAC &#8211; Upgrading MacPorts</title>
		<link>http://sgowtham.net/blog/2009/10/21/mac-upgrading-macports/</link>
		<comments>http://sgowtham.net/blog/2009/10/21/mac-upgrading-macports/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 14:20:58 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Mac]]></category>

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

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

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

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

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

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

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

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

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

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

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># BASH script to re-install previously installed ports on an Apple Mac</span>
<span style="color: #666666; font-style: italic;"># The list of active ports is imported from $HOME/active_ports.txt</span>
<span style="color: #666666; font-style: italic;"># and this list varies from person to person, depending on what (s)he</span>
<span style="color: #666666; font-style: italic;"># has installed previously. </span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Gowtham, Tue Oct 20 22:23:36 EDT 2009</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ACTIVE_PORTS</span>=<span style="color: #ff0000;">&quot;apr
  apr-util
  aquaterm
  arpack
  atk
  autoconf
  automake
  bluefish
  bzip2
  cairo
  cmake
  coreutils
  curl
  cyrus-sasl2
  db46
  docbook-xml
  docbook-xsl
  expat
  fftw-3
  fftw-3-single
  fontconfig
  freetype
  gawk
  gcc43
  gd2
  gettext
  ghostscript
  git-core
  glib2
  gmake
  gmp
  gnome-common
  gnome-icon-theme
  gnuplot
  gperf
  gqview
  GraphicsMagick
  groff
  gsed
  gtk-doc
  gtk2
  help2man
  hicolor-icon-theme
  icon-naming-utils
  ImageMagick
  intltool
  jmol
  jpeg
  libart_lgpl
  libglade2
  libiconv
  libidn
  libpixman
  libpng
  libtool
  libxml2
  libxslt
  lzmautils
  m4
  metis
  mpfr
  ncurses
  ncursesw
  neon
  openssl
  p5-compress-raw-zlib
  p5-compress-zlib
  p5-crypt-ssleay
  p5-error
  p5-html-parser
  p5-html-tagset
  p5-io-compress-base
  p5-io-compress-zlib
  p5-libwww-perl
  p5-locale-gettext
  p5-svn-simple
  p5-term-readkey
  p5-uri
  p5-xml-namespacesupport
  p5-xml-parser
  p5-xml-sax
  p5-xml-simple
  pango
  pcre
  pdflib
  perl5.8
  pkgconfig
  plotutils
  popt
  readline
  render
  rsync
  scrollkeeper
  serf
  shared-mime-info
  sqlite3
  subversion
  subversion-perlbindings
  SuiteSparse
  teTeX
  texi2html
  texinfo
  tiff
  urw-fonts
  wget
  Xft2
  xmlcatmgr
  xorg-bigreqsproto
  xorg-inputproto
  xorg-kbproto
  xorg-libice
  xorg-libsm
  xorg-libX11
  xorg-libXau
  xorg-libXaw
  xorg-libXdmcp
  xorg-libXext
  xorg-libXmu
  xorg-libXt
  xorg-renderproto
  xorg-util-macros
  xorg-xcmiscproto
  xorg-xextproto
  xorg-xf86bigfontproto
  xorg-xproto
  xorg-xtrans
  xpm
  xrender
  zlib&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> x <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$ACTIVE_PORTS</span>
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #007800;">$x</span>
  <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">5</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

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

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

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

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

<p>This process should preferably complete within minutes and the machine should be ready to use as before.</p>
<p><br clear="all"></p>
<h3 class="blog">Request</h3>
<p>If you find errors in this approach and/or know of better [more elegant] methods to accomplish the same, please do let me [and other readers] know of it by posing them as comments. They will be greatly appreciated.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2009%2F10%2F21%2Fmac-upgrading-macports%2F&amp;title=MAC%20%26%238211%3B%20Upgrading%20MacPorts" id="wpa2a_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/2009/10/21/mac-upgrading-macports/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Producing Script r In LaTeX Expressions</title>
		<link>http://sgowtham.net/blog/2009/10/07/producing-script-r-in-latex-expressions/</link>
		<comments>http://sgowtham.net/blog/2009/10/07/producing-script-r-in-latex-expressions/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 19:50:22 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2418</guid>
		<description><![CDATA[When we have so many letters already, why would one need this fancy little Script r? While typesetting mathematical expressions that govern our (beautiful &#038; elegant) physical universe, often one runs out of symbols and as such, it becomes necessary to use the same letter/character in different styles to convey the intended meaning. Electrodynamics and [...]]]></description>
			<content:encoded><![CDATA[<p><em>When we have so many letters already, why would one need this fancy little <strong>Script r</strong>?</em> While typesetting mathematical expressions that govern our (beautiful &#038; elegant) physical universe, often one runs out of symbols and as such, it becomes necessary to use the same letter/character in different styles to convey the intended meaning. <a href="http://en.wikipedia.org/wiki/Classical_electromagnetism" target="_blank">Electrodynamics</a> and <a href="http://en.wikipedia.org/wiki/Gravitation" target="_blank">Gravitation</a> are just a couple such fields that often use &#8211; fortunately or unfortunately &#8211; a variation of alphabet <em>r</em>, called the <em>script r</em>. I did try to generate this elusive <em>script r</em> in LaTeX for quite a few years and failed miserably every time. However a few days ago, <a href="http://www.phy.mtu.edu/~cantrell/" target="_blank">Dr. Cantrell</a> was looking for a way to accomplish the same and as such revived my almost forgotten interest in <em>script r</em>. </p>
<p><br clear="all"></p>
<h3 class="blog"><em>Script r</em> &#8211; MS Windows</h3>
<p>A little bit of Google-ing led me to a <a href="http://www.latex-community.org/forum/viewtopic.php?f=5&#038;t=327#p10858" target="_blank">discussion forum</a>. The method described there [included below for completeness sake] works just fine in Microsoft Windows running [a full installation of] <a href="http://www.miktex.org/" target="_blank">MiKTeX</a> and <a href="http://www.winedt.com/" target="_blank">WinEdt</a>.</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\DeclareMathAlphabet</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\mathcalligra</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">T1</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">m</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">n</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\DeclareFontShape</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">T1</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">m</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">n</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">&lt;-&gt;s*[<span style="color: #C08020; font-weight: normal;">2.2</span>]callig15</span><span style="color: #E02020; ">}{</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #2C922C; font-style: italic;">% A new, shorthand command/macro to generate 'script r'</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">newcommand</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\scriptr</span></span><span style="color: #E02020; ">}[</span><span style="color: #C08020; font-weight: normal;">1</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\ensuremath</span><span style="color: #E02020; ">{</span><span style="color: #800000; font-weight: normal;">\mathcalligra</span>{#1</span><span style="color: #E02020; ">}}}</span></pre></td></tr></table></div>

<p><br clear="all"></p>
<h3 class="blog"><em>Script r</em> &#8211; Mac OS</h3>
<p>While the code that generates is the same as in Microsoft Windows [MiKTeX/WinEdt], there are few things that ones need to do to get this elusive thing. First off, download the following three files and save them on Desktop.</p>
<p align="center">
<a href="http://sgowtham.net/blog/files/20091007/calligra.sty" target="_blank">calligra.sty</a> | <a href="http://sgowtham.net/blog/files/20091007/calligra.mf" target="_blank">calligra.mf</a> | <a href="http://sgowtham.net/blog/files/20091007/callig15.mf" target="_blank">callig15.mf</a>
</p>
<p><br clear="all"><br />
Open a <strong>Terminal</strong> and type the following:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>tex<span style="color: #000000; font-weight: bold;">/</span>latex
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>mf
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>calligra.sty ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>tex<span style="color: #000000; font-weight: bold;">/</span>latex<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>calligra.mf ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>mf<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>callig15.mf ~<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>mf<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> texhash</pre></div></div>

<p>Once these steps were followed, I was able to successfully compile LaTeX document and generate <em>script r</em> using <a href="http://www.uoregon.edu/~koch/texshop/" target="_blank">TeXShop</a> as well as <a href="http://www.xm1math.net/texmaker/" target="_blank">texmaker</a>.</p>
<p><br clear="all"></p>
<h3 class="blog"><em>Script r</em> &#8211; Linux</h3>
<p>While the code that generates is the same as in Microsoft Windows [MiKTeX/WinEdt], just like in Mac OS, there are few things that ones need to do to get this elusive thing. First off, download the following three files and save them on Desktop [it should, however, be noted that I did a full/maximal installation of Linux (Red Hat Enterprise/CentOS) and as such, LaTeX (with all necessary dependencies) was installed by default].</p>
<p align="center">
<a href="http://sgowtham.net/blog/files/20091007/calligra.sty" target="_blank">calligra.sty</a> | <a href="http://sgowtham.net/blog/files/20091007/calligra.mf" target="_blank">calligra.mf</a> | <a href="http://sgowtham.net/blog/files/20091007/callig15.mf" target="_blank">callig15.mf</a>
</p>
<p><br clear="all"><br />
Open a <strong>Terminal</strong> and type the following as <strong>root</strong>:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>tex<span style="color: #000000; font-weight: bold;">/</span>calligra<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>calligra<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>calligra.sty <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>tex<span style="color: #000000; font-weight: bold;">/</span>calligra<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>calligra.mf <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>calligra<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>callig15.mf <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>texmf<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>calligra<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
texhash</pre></div></div>

<p>Add the following line to individual user&#8217;s <strong>.bashrc</strong> and source it before proceeding ahead.</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">TEXINPUTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${TEXINPUTS}</span>:/usr/local/share/texmf/tex/calligra/&quot;</span></pre></div></div>

<p>Once these steps were followed, I was able to successfully compile LaTeX document and generate <em>script r</em> using command line.</p>
<p><br clear="all"></p>
<h3 class="blog">Sample Input File &#038; Screenshot</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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #E02020; ">\</span><span style="color: #800000;">documentclass</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">letterpaper</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;">article</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #2C922C; font-style: italic;">% Packages Required</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">dvips</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;">graphicx</span><span style="color: #E02020; ">}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">amsmath,amssymb</span><span style="color: #E02020; ">}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">color,setspace</span><span style="color: #E02020; ">}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">natbib</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #2C922C; font-style: italic;">% For generating 'script r'</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\DeclareMathAlphabet</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\mathcalligra</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">T1</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">m</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">n</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\DeclareFontShape</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">T1</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">calligra</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">m</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">n</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">&lt;-&gt;s*[<span style="color: #C08020; font-weight: normal;">2.2</span>]callig15</span><span style="color: #E02020; ">}{</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #2C922C; font-style: italic;">% A new, shorthand command/macro to generate 'script r'</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">newcommand</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\scriptr</span></span><span style="color: #E02020; ">}[</span><span style="color: #C08020; font-weight: normal;">1</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\ensuremath</span><span style="color: #E02020; ">{</span><span style="color: #800000; font-weight: normal;">\mathcalligra</span>{#1</span><span style="color: #E02020; ">}}}</span>
&nbsp;
<span style="color: #2C922C; font-style: italic;">% Page Format Details</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\oddsidemargin</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">0.00in</span><span style="color: #E02020; ">}</span>      <span style="color: #2C922C; font-style: italic;">% Margin on the Odd Numbered Side</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\evensidemargin</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">0.00in</span><span style="color: #E02020; ">}</span>     <span style="color: #2C922C; font-style: italic;">% Margin on the Even Numbered Side</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\topmargin</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">0.00in</span><span style="color: #E02020; ">}</span>          <span style="color: #2C922C; font-style: italic;">% Margin from the top</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">\<span style="color: #800000;">textwidth</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">6.50in</span><span style="color: #E02020; ">}</span>          <span style="color: #2C922C; font-style: italic;">% Width of the text in a page</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\textheight</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">9.00in</span><span style="color: #E02020; ">}</span>         <span style="color: #2C922C; font-style: italic;">% Height of the text in a page</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\parindent</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">0.00in</span><span style="color: #E02020; ">}</span>          <span style="color: #2C922C; font-style: italic;">% New paragraph indentation</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">setlength</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">\<span style="color: #800000;">parskip</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">0.25in</span><span style="color: #E02020; ">}</span>            <span style="color: #2C922C; font-style: italic;">% Spacing between two paragraphs</span>
&nbsp;
<span style="color: #E02020; ">\</span><span style="color: #800000;">pagestyle</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">plain</span><span style="color: #E02020; ">}</span>                       <span style="color: #2C922C; font-style: italic;">% Page Style</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">center</span></span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\large</span> Script <span style="color: #8020E0; font-weight: normal;">$r$</span> (<span style="color: #8020E0; font-weight: normal;">$<span style="color: #800000; font-weight: normal;">\scriptr</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">r</span>}$</span>) in <span style="color: #E02020; ">\</span><span style="color: #800000;">LaTeX</span><span style="color: #E02020; ">\</span>
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">center</span></span><span style="color: #E02020; ">}</span>
&nbsp;
The magnitude of the electrostatic force, <span style="color: #8020E0; font-weight: normal;">$F$</span>, on a charge
<span style="color: #8020E0; font-weight: normal;">$q_{<span style="color: #2020C0; font-weight: normal;">1</span>}$</span> due to the presence of another charge <span style="color: #8020E0; font-weight: normal;">$q_{<span style="color: #2020C0; font-weight: normal;">2</span>}$</span> is given by
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">equation*</span></span><span style="color: #E02020; ">}</span>
F <span style="color: #E02020; ">\</span>;=<span style="color: #E02020; ">\</span>; <span style="color: #E02020; ">\</span><span style="color: #800000;">frac</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">1</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">4<span style="color: #800000; font-weight: normal;">\pi</span><span style="color: #800000; font-weight: normal;">\epsilon</span>_<span style="color: #E02020; ">{</span><span style="color: #800000; font-weight: normal;">\circ</span></span><span style="color: #E02020; ">}}\</span>:
        <span style="color: #E02020; ">\</span><span style="color: #800000;">frac</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">q_{1</span><span style="color: #E02020; ">}\</span>:q_<span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">2</span><span style="color: #E02020; ">}}{</span><span style="color: #2020C0; font-weight: normal;">\<span style="color: #800000;">left</span>| <span style="color: #800000; font-weight: normal;">\mathbf</span>{r</span><span style="color: #E02020; ">}</span> <span style="color: #E02020; ">\</span>:-<span style="color: #E02020; ">\</span>: 
        <span style="color: #800000; font-weight: normal;">\mathbf</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\scriptr</span>{r</span><span style="color: #E02020; ">}}\</span><span style="color: #800000;">right</span>|^<span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">2</span><span style="color: #E02020; ">}}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">label</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">EQN00</span><span style="color: #E02020; ">}</span>
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">equation*</span></span><span style="color: #E02020; ">}</span>
&nbsp;
where <span style="color: #8020E0; font-weight: normal;">$\<span style="color: #800000;">left</span>| <span style="color: #800000; font-weight: normal;">\mathbf</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">r</span><span style="color: #E02020; ">}</span> <span style="color: #E02020; ">\</span>:-<span style="color: #E02020; ">\</span>: <span style="color: #800000; font-weight: normal;">\mathbf</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\scriptr</span>{r</span><span style="color: #E02020; ">}}\</span><span style="color: #800000;">right</span>|$</span> is
the distance between <span style="color: #8020E0; font-weight: normal;">$q_{<span style="color: #2020C0; font-weight: normal;">1</span>}$</span> and <span style="color: #8020E0; font-weight: normal;">$q_{<span style="color: #2020C0; font-weight: normal;">2</span>}$</span>. A positive force implies
a repulsive interaction, while a negative force implies an
attractive interaction.
&nbsp;
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span></pre></td></tr></table></div>

<p><br clear="all"><br />
<img class="framed" src="http://sgowtham.net/blog/files/20091007/latex_script_r.png" title="Script r" alt="Script r" border="0"></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%2F07%2Fproducing-script-r-in-latex-expressions%2F&amp;title=Producing%20Script%20r%20In%20LaTeX%20Expressions" 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/10/07/producing-script-r-in-latex-expressions/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; GPS Tracks In Google KML Format</title>
		<link>http://sgowtham.net/blog/2009/08/10/php-gps-tracks-in-google-kml-format/</link>
		<comments>http://sgowtham.net/blog/2009/08/10/php-gps-tracks-in-google-kml-format/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 03:23:56 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[KML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2350</guid>
		<description><![CDATA[Amongst other things, I have the habit of keeping a detailed track of where I have been and for this purpose, I use my Garmin GPSMap 60CSx. I have configured this GPS to save track information every 3 seconds. If this information can be written in KML format, then the tracks can be visualized using [...]]]></description>
			<content:encoded><![CDATA[<p>Amongst other things, I have the habit of keeping a detailed track of where I have been and for this purpose,  I use my <a href="http://sgowtham.net/blog/2008/01/04/garmin-gpsmap-60csx-my-new-travel-companion/" target="_blank">Garmin GPSMap 60CSx</a>. I have configured this GPS to save track information every 3 seconds. If this information can be written in <a href="http://en.wikipedia.org/wiki/Keyhole_Markup_Language" target="_blank">KML</a> format, then the tracks can be visualized using <a href="http://earth.google.com/" target="_blank">Google Earth</a>.</p>
<p><br clear="all"></p>
<h3 class="blog">Part #0: GPS Tracks &rarr; MySQL</h3>
<p>Please refer to <a href="http://sgowtham.net/blog/2009/02/12/php-storing-gps-track-points-in-mysql/" target="_blank">this post</a>. For the sake of partial completeness, the MySQL table structure 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;">`DATABASE`</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;">`DATABASE`</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">Part #1: MySQL &rarr; KML</h3>
<p>Save the following into a file, <strong>mysql2kml.php</strong> and put in appropriate values for username, password, database name, start_date and end_date.</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
</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;"># Connect to the database
</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;">'USERNAME'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DATABASE'</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;"># The standard header - refer to Google Earth / KML documentation to modify it
</span><span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;kml xmlns=&quot;http://www.opengis.net/kml/2.2&quot;&gt;
  &lt;Document&gt;
    &lt;name&gt;Points with TimeStamps&lt;/name&gt;
    &lt;Style id=&quot;blue-dot&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://sgowtham.net/icon/blue-dot.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;1&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;red-dot&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://sgowtham.net/icon/red-dot.gif&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;2&quot; y=&quot;2&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;paddle-a&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/kml/paddle/A.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;32&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;paddle-b&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/kml/paddle/B.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;32&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;hiker-icon&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/ms/icons/hiker.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;0&quot; y=&quot;.5&quot; xunits=&quot;fraction&quot; yunits=&quot;fraction&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;check-hide-children&quot;&gt;
      &lt;ListStyle&gt;
        &lt;listItemType&gt;checkHideChildren&lt;/listItemType&gt;
      &lt;/ListStyle&gt;
    &lt;/Style&gt;
    &lt;styleUrl&gt;#check-hide-children&lt;/styleUrl&gt;
&nbsp;
    &lt;Folder&gt;\n
EOF</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set Start &amp; End Date values - set both of them to same
</span><span style="color: #666666; font-style: italic;"># if you need a specific day
</span><span style="color: #000088;">$start_date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-08-07'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end_date</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-08-09'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql1</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM `DATABASE`.`TRK` &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span>    <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE date BETWEEN '<span style="color: #006699; font-weight: bold;">$start_date</span>' AND '<span style="color: #006699; font-weight: bold;">$end_date</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 date, time&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result1</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> 
<span style="color: #000088;">$nresults</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result1</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;">$myrow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$date</span>        <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$time</span>        <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$latitude</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$longitude</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$altitude</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'altitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$date</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$time</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$latitude</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$longitude</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$altitude</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$altitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$datetime</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$date</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;T&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$time</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;Z&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$date</span>&quot;</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">&quot;0000-00-00&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
      &lt;Placemark&gt;
        &lt;TimeStamp&gt;
          &lt;when&gt;$datetime&lt;/when&gt;
        &lt;/TimeStamp&gt;
        &lt;styleUrl&gt;#blue-dot&lt;/styleUrl&gt;
        &lt;Point&gt;
          &lt;coordinates&gt;$longitude,$latitude,$altitude&lt;/coordinates&gt;
        &lt;/Point&gt;
      &lt;/Placemark&gt;\n
EOF</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
    &lt;/Folder&gt;
  &lt;/Document&gt;
&lt;/kml&gt;
EOF</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Run this file as</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> php<span style="color: #000000; font-weight: bold;">`</span> mysql2kml.php <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">20090807</span>_20090809.kml</pre></td></tr></table></div>

<p><br clear="all"><br />
If all goes well, the file <strong>20090807_20090809.kml</strong> should be written out and be waiting for you. Try a sample file, if you wish &#8211; <a href="http://sgowtham.net/blog/files/20090810/20090809.kml" target="_blank">20090809.kml</a> should be openable in Google Earth and should display something like below:</p>
<p><br clear="all"><br />
<img src="http://sgowtham.net/blog/files/20090810/20090809.png" alt="20090809.kml" title="20090809.kml" class="framed"></p>
<p><br clear="all"></p>
<h3 class="blog">Update (2009.11.30)</h3>
<p>Use the following MySQL table structure and accompanying PHP script for better performance.</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
112
113
114
115
116
117
118
119
120
121
122
123
</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;"># Connect to the database
</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;">'USERNAME'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DATABASE'</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;"># The standard header - refer to Google Earth / KML documentation to modify it
</span><span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;kml xmlns=&quot;http://www.opengis.net/kml/2.2&quot;&gt;
  &lt;Document&gt;
    &lt;name&gt;Points with TimeStamps&lt;/name&gt;
    &lt;Style id=&quot;blue-dot&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://sgowtham.net/icon/blue-dot.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;1&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;red-dot&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://sgowtham.net/icon/red-dot.gif&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;2&quot; y=&quot;2&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;paddle-a&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/kml/paddle/A.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;32&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;paddle-b&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/kml/paddle/B.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;32&quot; y=&quot;1&quot; xunits=&quot;pixels&quot; yunits=&quot;pixels&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;hiker-icon&quot;&gt;
      &lt;IconStyle&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://maps.google.com/mapfiles/ms/icons/hiker.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x=&quot;0&quot; y=&quot;.5&quot; xunits=&quot;fraction&quot; yunits=&quot;fraction&quot;/&gt;
      &lt;/IconStyle&gt;
    &lt;/Style&gt;
    &lt;Style id=&quot;check-hide-children&quot;&gt;
      &lt;ListStyle&gt;
        &lt;listItemType&gt;checkHideChildren&lt;/listItemType&gt;
      &lt;/ListStyle&gt;
    &lt;/Style&gt;
    &lt;styleUrl&gt;#check-hide-children&lt;/styleUrl&gt;
&nbsp;
    &lt;Folder&gt;\n
EOF</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set Start &amp; End Date values - set both of them to same
</span><span style="color: #666666; font-style: italic;"># if you need a specific day
</span><span style="color: #000088;">$start_date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-08-07'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end_date</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-08-09'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql1</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM `DATABASE`.`TRK` &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql1</span>    <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE DATE(date_time) BETWEEN '<span style="color: #006699; font-weight: bold;">$start_date</span>' AND '<span style="color: #006699; font-weight: bold;">$end_date</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 date_time &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result1</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> 
<span style="color: #000088;">$nresults</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result1</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;">$myrow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$date_time</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date_time'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$latitude</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$longitude</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</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;">$altitude</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'altitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$date_time</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date_time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$latitude</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$longitude</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$altitude</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$altitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date_time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$datetime</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$date</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;T&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$time</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;Z&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$date</span>&quot;</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">&quot;0000-00-00&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
      &lt;Placemark&gt;
        &lt;TimeStamp&gt;
          &lt;when&gt;$datetime&lt;/when&gt;
        &lt;/TimeStamp&gt;
        &lt;styleUrl&gt;#blue-dot&lt;/styleUrl&gt;
        &lt;Point&gt;
          &lt;coordinates&gt;$longitude,$latitude,$altitude&lt;/coordinates&gt;
        &lt;/Point&gt;
      &lt;/Placemark&gt;\n
EOF</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
    &lt;/Folder&gt;
  &lt;/Document&gt;
&lt;/kml&gt;
EOF</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%2F08%2F10%2Fphp-gps-tracks-in-google-kml-format%2F&amp;title=PHP%20%26%238211%3B%20GPS%20Tracks%20In%20Google%20KML%20Format" 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/08/10/php-gps-tracks-in-google-kml-format/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL &#8211; Finding Locations Nearest To A Given Pair Of GPS Coordinates</title>
		<link>http://sgowtham.net/blog/2009/08/08/mysql-finding-locations-nearest-to-a-given-pair-of-gps-coordinates/</link>
		<comments>http://sgowtham.net/blog/2009/08/08/mysql-finding-locations-nearest-to-a-given-pair-of-gps-coordinates/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 15:17:03 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=2279</guid>
		<description><![CDATA[Amongst other things, I have the habit of geotagging my photographs and for this purpose, I use my Garmin GPSMap 60CSx, an API I wrote to store tracks in MySQL &#038; Geonames data in a MySQL [please refer to this if you wish to do so as well]. For completeness sake, the MySQL table structure [...]]]></description>
			<content:encoded><![CDATA[<p>Amongst other things, I have the habit of <a href="http://en.wikipedia.org/wiki/Geotagging" target="_blank">geotagging</a> my photographs and for this purpose, I use my <a href="http://sgowtham.net/blog/2008/01/04/garmin-gpsmap-60csx-my-new-travel-companion/" target="_blank">Garmin GPSMap 60CSx</a>, an API I wrote to <a href="http://sgowtham.net/blog/2009/02/12/php-storing-gps-track-points-in-mysql/" target="_blank">store tracks in MySQL</a> &#038; Geonames data in a MySQL  [please refer to <a href="http://sgowtham.net/blog/2009/07/29/importing-geonames-org-data-into-mysql/" target="_blank">this</a> if you wish to do so as well]. For completeness sake, the MySQL table structure that holds Geonames data is given below:</p>
<p><br clear="all"></p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`MyDatabase`</span>.<span style="color: #008000;">`allCountries`</span> <span style="color: #000033;">;</span>
&nbsp;
<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;">`allCountries`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>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: #FF9900; font-weight: bold;">UNSIGNED</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;">PRIMARY KEY</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>name`</span>             <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>ansiname`</span>         <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>alternate<span style="color: #008080; font-weight: bold;">_</span>names`</span>  <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2000</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>latitude`</span>         <span style="color: #999900; font-weight: bold;">DOUBLE PRECISION</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #000033;">,</span><span style="color: #008080;">7</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>longitude`</span>        <span style="color: #999900; font-weight: bold;">DOUBLE PRECISION</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #000033;">,</span><span style="color: #008080;">7</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>feature<span style="color: #008080; font-weight: bold;">_</span>class`</span>    <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>feature<span style="color: #008080; font-weight: bold;">_</span>code`</span>     <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>country<span style="color: #008080; font-weight: bold;">_</span>code`</span>     <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>country<span style="color: #008080; font-weight: bold;">_</span>code2`</span>    <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">60</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>admin1<span style="color: #008080; font-weight: bold;">_</span>code`</span>      <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>admin2<span style="color: #008080; font-weight: bold;">_</span>code`</span>      <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">80</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>admin3<span style="color: #008080; font-weight: bold;">_</span>code`</span>      <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>admin4<span style="color: #008080; font-weight: bold;">_</span>code`</span>      <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>population`</span>       <span style="color: #999900; font-weight: bold;">BIGINT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</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;">`geo<span style="color: #008080; font-weight: bold;">_</span>elevation`</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: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`geo<span style="color: #008080; font-weight: bold;">_</span>gtopo30`</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: #990
