Performance FAQs on Mira/Cetus/Vesta

Help Desk

Hours: 9:00am-5:00pm CT M-F
Email: support@alcf.anl.gov

Mira/Cetus/Vesta

How do I use MPI?

/* 
 * Very simple hello-world using C and MPI
 *
*/

#include <stdio.h>
#include <string.h>
#include "mpi.h"

main(int argc, char* argv[]) {
  int myRank, numProcs;
  int source, dest=0, tag=0;
  char message[100];
  MPI_Status status;

  MPI_Init(&argc,&argv);

  MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
  MPI_Comm_size(MPI_COMM_WORLD, &numProcs);

  if (myRank != 0) {
    sprintf(message,"Hello World from process %d", myRank);
    MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
  } else {
    printf("Hello World from process %d\n", myRank);
 
    for(source=1; source<numProcs; source++) {
      MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
      printf("%s\n", message);
    }
  }

  MPI_Finalize();

  return 0;
}

Fortran90 Helloworld MPI Example

program helloworldmpi
  implicit none

  integer ierr,myid,numprocs
  integer source, dest, tag
  CHARACTER*100 message

  include 'mpif.h'
  integer status(MPI_STATUS_SIZE)

  call mpi_init(ierr)
  call mpi_comm_rank(MPI_COMM_WORLD,myid,ierr)
  call mpi_comm_size(MPI_COMM_WORLD,numprocs,ierr)

  100  format('Hello from process ', I6)
  write(message,100) myid

  tag= 1
  dest= 0

  if(myid.eq.0) then
    write(6,*) message
    do source=1,numprocs-1
      call mpi_recv(message, 100, MPI_CHARACTER, source, tag, MPI_COMM_WORLD, status, ierr)
      write(6,*) message
    end do
  else
    call mpi_send(message, 100, MPI_CHARACTER, dest, tag, MPI_COMM_WORLD, ierr)
  end if

  call mpi_finalize(ierr)

  stop
end program

C++ Helloworld MPI Example

#include <mpi.h>
#include <iostream>
using namespace std;

#define BUFFER_SIZE 100

int main(int argc, char** argv) {

  MPI::Init(argc, argv);

  int rank = MPI::COMM_WORLD.Get_rank();
  int size = MPI::COMM_WORLD.Get_size();

  char message[BUFFER_SIZE];
  sprintf(message, "Hellow World! from process %d", rank);

  int messageSize= strlen(message)+1;
  int to= 0;
  int tag= 0;

  if (rank == 0) {
    cout << message << endl;

    MPI::Status status;
    for (int source=1; source < size; source++) { 
      MPI::COMM_WORLD.Recv(message, BUFFER_SIZE, MPI::CHAR, source, tag, status);
      cout << message << endl;
    }
  } else {
    MPI::COMM_WORLD.Send(message, messageSize, MPI::CHAR, to, tag);
  }

  MPI::Finalize();

  return 0;
}