next up previous contents
Next: using the PGI, Absoft Up: Compiling an MPI code Previous: Compiling an MPI code   Contents

using the GNU compilers g77, gcc, g++

To compile and link code using the GNU compilers (g77, gcc, g++) use the following:

EXAMPLE: To compile and link the f77 code sum.f using g77:

      program sum
      include 'mpif.h'
      call mpi_init(ierr)
      call mpi_comm_rank(mpi_comm_world,myid,ierr)
      call mpi_comm_size(mpi_comm_world,np,ierr)
      x = myid + 1
      print *,' The value of x on processor ', myid, ' is ', x
      call mpi_allreduce(x,xsum,1,mpi_real,mpi_sum,mpi_comm_world,ierr)
      if ( myid.eq.0) then
         print *,' Sum of all the x''s is ' , xsum
      endif
      call mpi_finalize(ierr)
      stop
      end
To compile the above code simply use:

[nrcb@vmserve pbs]$ mpif77 -o sum sum.f
[nrcb@vmserve pbs]$
or if you want to add optimisation options:
[nrcb@vmserve pbs]$ mpif77 -O3 -funroll-all-loops -o sum sum.f
[nrcb@vmserve pbs]$

EXAMPLE: To compile and link the C code sum.c using gcc:

#include "mpi.h"
#include <stdio.h>
#include <math.h>
int main (argc,argv)
int argc;
char *argv[];
{
     int myid,np;
     float x,xsum;
     MPI_Init (&argc,&argv);
     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
     MPI_Comm_size(MPI_COMM_WORLD,&np);
     x = myid + 1;
     printf( "The value of x on processor %d is %.8f \n", myid,x);
     MPI_Allreduce(&x,&xsum,1,MPI_REAL,MPI_SUM,MPI_COMM_WORLD);
     if ( myid == 0) {     
         printf ("Sum of all the x's is  %8f \n" , xsum);
                     }
     MPI_Finalize();
}
To compile the code simply use:
[nrcb@vmserve pbs]$ mpicc -o sum  sum.c
[nrcb@vmserve pbs]$

Or to compile with additional options (for example):

[nrcb@vmserve pbs]$ mpicc -O4 -funroll-all-loops -o sum  sum.c
[nrcb@vmserve pbs]$



2004-06-17