Commit 9bffb675 authored by mohamadbashar.disoki's avatar mohamadbashar.disoki

HelloWorld MPI

parents
# MPI LAB
## Prepare your User
To establish ssh connection between master and slaves without prompt required password we copy
public id on slave machines as following
```
$ ssh-keygen (dont use any passphrase)
$ ssh-copy-id mpiuser@slave1
$ ssh-copy-id mpiuser@slave2
```
## Running MPI programs
if you want to compile your own code, the name of which let’s say is *mpi_sample.c*, you will compile it
the way given below, to generate an executable *mpi_sample*.
```
mpicc -o mpi_sample mpi_sample.c
```
**Note**: compile your code within the **/nfs** shared directory
To run it only in your machine (with 2 processes), you do
```
mpirun -np 2 ./mpi_sample
```
Now, to run it within a cluster (add the hostname of the cluster's node you want to use),
```
mpirun -np 5 -hosts master,localhost ./mpi_sample
```
Or specify the same in a hostfile
```
mpirun -np 3 -f mpi_hosts ./mpi_sampl
```
// An intro MPI hello world program that uses MPI_Init, MPI_Comm_size,
// MPI_Comm_rank, MPI_Finalize, and MPI_Get_processor_name.
//
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment. The two arguments to MPI Init are not
// currently used by MPI implementations, but are there in case future
// implementations might need the arguments.
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
\ No newline at end of file
master
slave1
slave2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment