Quantcast
Channel: My experiments with life
Viewing all articles
Browse latest Browse all 78

MPI/C – Oddly [or Evenly] Saying ello!

$
0
0

MPI

For my understanding of what MPI is &/or does, please refer to this post.

Saying ello! & 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 or even.

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
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
/* 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) & 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
 *
*/
 
/* STANDARD HEADERS AND DEFINITIONS 
 * REFERENCE: http://en.wikipedia.org/wiki/C_standard_library
*/
#include <stdio.h>  /* Core input/output operations                         */
#include <stdlib.h> /* Conversions, random numbers, memory allocation, etc. */
#include <math.h>   /* Common mathematical functions                        */
#include <time.h>   /* Converting between various date/time formats         */
#include <mpi.h>    /* MPI functionality                                    */
 
/* MAIN PROGRAM BEGINS */
int main(int argc, char **argv) {
 
  /* VARIABLE DECLARATION */
  int proc_id,       /* Process identifier   */
      n_procs;       /* Number of processors */
 
  /* INITIALIZE MPI */
  MPI_Init(&argc, &argv);
 
  /* GET THE PROCESS ID AND NUMBER OF PROCESSORS */
  MPI_Comm_rank(MPI_COMM_WORLD, &proc_id);
  MPI_Comm_size(MPI_COMM_WORLD, &n_procs);
 
  /* PRINT 'HELLO, WORLD!' FROM EVERY PROCESSOR 
   * ALSO INDICATE WHETHER THE PROCESSOR ID IS ODD OR EVEN
  */
  if (proc_id % 2 == 0) {
    printf("Hello, World! From %d [e] out of %d processors\n", proc_id, n_procs);
  } else {
    printf("Hello, World! From %d [o] out of %d processors\n", proc_id, n_procs);
  }
 
  /* FINALIZE MPI */
  MPI_Finalize();
 
  /* INDICATE THE TERMINATION OF THE PROGRAM */
  return 0;
 
} /* MAIN PROGRAM ENDS */

Program Compilation & Execution

The machine where I am running this calculation, dirac, has 4 processors and has MPICH2 v1.3.1 compiled against GCC v4.1.2 compilers.

[guest@dirac mpi_samples]$ which mpicc
alias mpicc='mpicc -g -Wall -lm'
	~/mpich2/1.3.1/gcc/4.1.2/bin/mpicc
 
[guest@dirac mpi_samples]$ which mpirun
alias mpirun='mpirun -machinefile $HOME/machinefile'
	~/mpich2/1.3.1/gcc/4.1.2/bin/mpirun
 
[guest@dirac mpi_samples]$ mpicc hello_world_oddeven.c -o hello_world_oddeven.x
 
[guest@dirac mpi_samples]$ mpirun -np 1 ./hello_world_oddeven.x
Hello, World! From 0 [e] out of 1 processors
 
[guest@dirac mpi_samples]$ mpirun -np 2 ./hello_world_oddeven.x
Hello, World! From 0 [e] out of 2 processors
Hello, World! From 1 [o] out of 2 processors
 
[guest@dirac mpi_samples]$ mpirun -np 4 ./hello_world_oddeven.x
Hello, World! From 1 [o] out of 4 processors
Hello, World! From 2 [e] out of 4 processors
Hello, World! From 3 [o] out of 4 processors
Hello, World! From 0 [e] out of 4 processors
 
[guest@dirac mpi_samples]$ mpirun -np 8 ./hello_world_oddeven.x
Hello, World! From 1 [o] out of 8 processors
Hello, World! From 0 [e] out of 8 processors
Hello, World! From 2 [e] out of 8 processors
Hello, World! From 7 [o] out of 8 processors
Hello, World! From 3 [o] out of 8 processors
Hello, World! From 5 [o] out of 8 processors
Hello, World! From 6 [e] out of 8 processors
Hello, World! From 4 [e] out of 8 processors
 
[guest@dirac mpi_samples]$

Viewing all articles
Browse latest Browse all 78

Trending Articles