Pages

Saturday, June 25, 2016

Working with MPI

Message Passing Interface is great because it provides interoperability among different programming platforms (C++, C, and Fortran). There is also support provided for Java, Perl, and Python. Working with large scale Fortran and C++ applications, it can be bit slow to use Oracle for interoperability.

I was working towards having an example working that talks between Fortran and C++.

Downloaded and installed MPI to linux machine:

http://www.mpich.org/downloads/

Found a nice example

http://stackoverflow.com/questions/11944356/send-mpi-message-from-a-c-code-to-fortran-90-code

C++ (Notice, fixed a simple typo from float to double)
# include 
# include 
# include 
using namespace std;

void printarray (double arg[], int length) {
   for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

int main(int argc, char *argv[] ){
   double a[10];
   int myrank,i;
   MPI::Init ( argc, argv );
   myrank=MPI::COMM_WORLD.Get_rank();
   cout << "rank "<<myrank<<" is c++ rank."<<std::endl;
   for (i=0;i<10;i++){
      a[i]=10.0;
   }
   printarray(a,10);
   MPI::COMM_WORLD.Send(&a[0],1,MPI::DOUBLE_PRECISION,1,100);
   MPI::Finalize();
}
Fortran (Notice that there must be 7 spaces before writing line and if line is too long error use &. For this to work, the file has to be named as .f95
program main
implicit none
include "mpif.h"
integer:: ierr,stat(MPI_STATUS_SIZE)
real(8):: a(10)

call mpi_init(ierr)
a=0
print*,a
call mpi_recv(a(1),10,MPI_DOUBLE_PRECISION,0,100, &
MPI_COMM_WORLD,stat,ierr)
print*,a
call mpi_finalize(ierr)
end program

Compiled C++
mpic++ harmeet.cpp -o harmeet_cpp

Compiled Fortran
mpifort harmeet.f95 -o harmeet_f95

Run them both together
mpirun -n 1 ./harmeet_cpp : -n 1 harmeet_95

Notice there is a : between, otherwise we would get stuck at harmeet_cpp sending message that harmeet_95 never receives.

No comments:

Post a Comment