mpimpj-express

Sending an Array of Strings using Message Passing Interface


I would like to send an array of strings from the master to a slave thread using Messgae Passing Interface (MPI). i.e. String [] str = new String [10] str[0]= "XXX" ... etc

How can I do that while avoiding to send each of the elements in this array as a chain of characters?

I succeeded to send an array of integers in one send operation ... but I don't know how to do that when it is about an array of strings


Solution

  • I don't know Java, but I'll give you the C answer. The concepts -- particularly the two approaches one might take to solve this - are the same in any language, though.

    Imagine if this were a simple c-string (some characters terminated with '\0'). There are two approaches:

    1. over-provision memory and receive up to some limit,
    2. or send a message indicating how much data to expect.

    Do you have a maximum length? (e.g. PATH_MAX or something like that). If you do not need every byte of memory, you could do

    MPI_Send(str, strlen(str), MPI_CHAR, slave_rank, slave_tag, MPI_COMM_WORLD);
    

    and you'd pair that with

    MPI_Recv(str, MAX_LENGTH, MPI_CHAR, master_rank, slave_tag, MPI_COMM_WORLD);
    

    If you don't like having slop at the end, you'll have to do it in two messages:

    len=strlen(str) + 1;  /* +1 for the NULL byte */
    MPI_Send(&len, 1, MPI_INT, slave_rank, slave_tag, MPI_COMM_WORLD);
    MPI_Send(str, strlen(str), MPI_CHAR, slave_rank, slave_tag, MPI_COMM_WORLD);
    

    and you'd match that with

    MPI_Recv(&len, 1, MPI_INT, master_rank, slave_tag, MPI_COMM_WORLD);
    payload= malloc(len);
    MPI_Recv(&payload, len, MPI_CHAR, master_rank, slave_tag, MPI_COMM_WORLD);