Pergunta

I am trying to share an array of characters among processes in MPI using MPI_Bcast(). Can anyone please let me know what must be passed as count and datatype in this case?

Array:

char * Var;

Broadcast function in MPI:

int MPI_Bcast( void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm )
Foi útil?

Solução

You want the MPI_Datatype MPI_BYTE and int count should be strlen(Var) + 1

Here is the explanation why: For the datatype you need something that resembles a char much like you would send MPI_INT to transport integer arrays. This is what MPI_BYTE is for:

MPI_BYTE: This is an 8-bit positive integer betwee 0 and 255, i.e., a byte.

As for the length, that would have to be the size of the array. If I understand you correctly, this could vary depending on the length of the string you are sending. You therefore have to get the length of the string first by calling strlen(Var). As

The strlen() function calculates the length of the string s, excluding the terminating null byte ('\0').

You have to add one byte for the null character terminator of the string.

Problem: The above code likely leads to memory leaks: Your problem is, that it lies in the nature of MPI_Broadcast that only one of the participating processes is aware of the message being transmitted. Therefore only this process is able to determine the correct count all other processes are at loss as they have nothing to call strlen on. So what can we do about this?

  1. broadcast the length of the string determined by the root processor via strlen first and then broadcast the actual string
  2. use MPI_Send in combination with MPI_Recv allowing you to poll for the message size on the receiving proceses.

I would suggest using the second alternative as this avoids unnecessary barriers. A good how-to on this topic can be found here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top