MPI/C – Saying ello!
November 28th, 2010 @ 10:02:21
MPI
For my understanding of what MPI is &/or does, please refer to this post.
Saying ello!
It’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’s just a manifestation of wishful/hopeful thinking:
Hello, World! Here begins my journey of learning this new language. Bear with my mistakes and help me along the way.
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 | /* 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) & 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 * */ /* 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 */ printf("Hello, World! From %d 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.c -o hello_world.x [guest@dirac mpi_samples]$ mpirun -np 1 ./hello_world.x Hello, World! From 0 out of 1 processors [guest@dirac mpi_samples]$ mpirun -np 2 ./hello_world.x Hello, World! From 0 out of 2 processors Hello, World! From 1 out of 2 processors [guest@dirac mpi_samples]$ mpirun -np 4 ./hello_world.x Hello, World! From 0 out of 4 processors Hello, World! From 1 out of 4 processors Hello, World! From 2 out of 4 processors Hello, World! From 3 out of 4 processors [guest@dirac mpi_samples]$ mpirun -np 8 ./hello_world.x Hello, World! From 1 out of 8 processors Hello, World! From 2 out of 8 processors Hello, World! From 4 out of 8 processors Hello, World! From 7 out of 8 processors Hello, World! From 3 out of 8 processors Hello, World! From 5 out of 8 processors Hello, World! From 0 out of 8 processors Hello, World! From 6 out of 8 processors [guest@dirac mpi_samples]$ |
[...] MPI/C – Saying ello! [...]