spiderman123
spiderman123

Reputation: 19

Send / Receive MPI-Fortran-Java

I have a send / receive implementation in Open-MPI and Java. The goal is to send a Fortran character set to Java. When Java receives this String it prints the following result. Result send/receive

My Fortran code is:

program simple4 
  implicit none 
  include 'mpif.h' 

  integer ierr,my_rank,size 
  integer src, dest, tag, position, charSize 
  integer status(MPI_STATUS_SIZE) 
  CHARACTER*80 TEXT

  call mpi_init(ierr) 
  call mpi_comm_rank(MPI_COMM_WORLD,my_rank,ierr) 
  call mpi_comm_size(MPI_COMM_WORLD,size,ierr) 

  src = 0 
  dest = 1 
  tag = 999

  if(my_rank.eq.0) then
    position = 0 
    TEXT = "PRO" 
    call MPI_SEND(TEXT, 3, MPI_CHARACTER, dest, tag, MPI_COMM_WORLD, ierr) 
  else 
    print *, "Fortran process ", my_rank
  end if 

  call mpi_finalize(ierr)

end

My Java code is:

....
char[] textChar = new char[3];
MPI.COMM_WORLD.recv(textChar, 3, MPI.CHAR, sourceRank, messageTag);
String text = String.copyValueOf(textChar);
System.out.println("Java - Text receive: " + text);

What would be the problem in my code? Fortran and Java will not be able to communicate this way? What would be another solution?

Upvotes: 0

Views: 295

Answers (1)

Gilles Gouaillardet
Gilles Gouaillardet

Reputation: 8395

The root cause is a Fortran CHARACTER is in ASCII whereas a Java String is in Unicode, so some extra steps are involved.

Here is a snippet of what you could do in Java

byte[] textByte = new byte[3];
MPI.COMM_WORLD.recv(textByte, 3, MPI.BYTE, sourceRank, messageTag);
String text = new String(textByte, java.nio.charset.Charset.forName("UTF-8"));
System.out.println("Java - Text receive: " + text);

Upvotes: 1

Related Questions