Hari Gillala
Hari Gillala

Reputation: 11926

Convert C to MPI API C Program

How do we convert c Program with user defined function to a Parallel Program using MPI API to C. A Demo would be more useful

Thank you. Hari

Upvotes: 0

Views: 1673

Answers (1)

Jay D
Jay D

Reputation: 3297

It depends upon what you are trying to execute in parallel and what are your requirements.

There are lot of good tutorials available.

Here's a simple hello world program :

#include "stdio.h"
#include "mpi.h"
int main(int argc, char *argv[])
{
  int id, nprocs;
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &id);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  printf("Hello World from proc %d out of %d!\n", id, nprocs);

  MPI_Finalize();

  return 0;
}

The compile it as following

usr@xbc:~> mpicc -Wall -Werror -o mpihello mpihello.c

To Run it on 2 separate machines as

usr@xbc:~> mpirun_rsh  -ssh -np 2 node0 node1 /home/xbc/mpihello

or

usr@xbc:~> mpirun_rsh  -hostfile hostlist -n 2 /home/xbc/mpihello

where your hostlist file would have 2 entries as node0 and node

To Run it on the same machine as 2 processes

usr@xbc:~> mpirun_rsh  -ssh -np 2 node0:1 /home/xbc/mpihello

or

usr@xbc:~> mpirun_rsh  -hostfile hostlist -n 2 /home/xbc/mpihello

where your hostlist file would have 1 entry as node0:1

The output put would a Hello world with Ranks as 0 and 1 from either 2 machines or 2 processes depending upon how you run it.

Now you can run MPI over TCP/IP or MPI over IP over IB(InfiniBand) or MPI over IB Over Ethernet or in many other ways depending upon your requirements.

You will have to configure the mpi compilation and installation accordingly.

Upvotes: 1

Related Questions