Examples using MPI

Example 1:

This example is the simplest possible using many processors:

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

int main(int argc, char **argv)
{
  int rank, size;

  nice(30);

  MPI_Init( &argc, &argv );
  MPI_Comm_rank(MPI_COMM_WORLD, &rank );
  MPI_Comm_size(MPI_COMM_WORLD, &size );
  printf("I am processor no. %d of a total of %d processors!\n", rank, size);
  MPI_Finalize();
  return 0;
}

Compile using this command:

$ hcc -o mpi-test mpi-test.c -lmpi

Boot the cluster as in Getting started.

Running the program on 10 processors gives the output.

$ mpirun -np 10 mpi-test
I am processor no. 4 of a total of 10 processor
I am processor no. 5 of a total of 10 processor
I am processor no. 6 of a total of 10 processor
I am processor no. 8 of a total of 10 processor
I am processor no. 2 of a total of 10 processor
I am processor no. 7 of a total of 10 processor
I am processor no. 9 of a total of 10 processor
I am processor no. 1 of a total of 10 processor
I am processor no. 0 of a total of 10 processor
I am processor no. 3 of a total of 10 processor

Note the order of the processors seem random. You will probably get a slightly different output than this.




2007-11-26 17:48, Per Kristian Hove
NTNU, 7491 Trondheim, tlf. 73 59 50 00. Kontakt oss
Ansvarlig redaktør: Informasjonsdirektør Christian Fossen