mpi
collettivi
Ricerca…
Osservazioni
Le operazioni Collective sono chiamate MPI progettate comunicano i processi indicati da un comunicatore in una singola operazione o per eseguire una sincronizzazione tra di loro. Questi sono spesso usati per calcolare uno o più valori basati su dati forniti da altri processi o per distribuire o raccogliere dati da tutti gli altri processi.
Si noti che tutti i processi nel communicator devono richiamare le stesse operazioni collettive in ordine, altrimenti l'applicazione si bloccherebbe.
Trasmissione
Il seguente codice trasmette i contenuti nel buffer
tra tutti i processi che appartengono al comunicatore MPI_COMM_WORLD
(ovvero tutti i processi in esecuzione in parallelo) utilizzando l'operazione MPI_Bcast
.
int rank;
int res;
res = MPI_Comm_rank (MPI_COMM_WORLD, &rank);
if (res != MPI_SUCCESS)
{
fprintf (stderr, "MPI_Comm_rank failed\n");
exit (0);
}
int buffer[100];
if (rank == 0)
{
// Process with rank id 0 should fill the buffer structure
// with the data it wants to share.
}
res = MPI_Bcast (buffer, 100, MPI_INT, 0, MPI_COMM_WORLD);
if (res != MPI_SUCCESS)
{
fprintf (stderr, "MPI_Bcast failed\n");
exit (0);
}
Barriera
L'operazione MPI_Barrier
esegue una sincronizzazione tra i processi appartenenti al comunicatore specificato. Cioè, tutti i processi di un determinato comunicatore aspetteranno all'interno di MPI_Barrier
finché tutti non saranno all'interno e, a quel punto, lasceranno l'operazione.
int res;
res = MPI_Barrier (MPI_COMM_WORLD); /* all processes will wait */
if (res != MPI_SUCCESS)
{
fprintf (stderr, "MPI_Barrier failed\n");
exit (0);
}
spargimento
Il processo di root sparge i contenuti in sendbuf
in tutti i processi (incluso se stesso) usando l'operazione MPI_Scatter
.
int rank;
int size;
int sendcount = 1;
int recvcount = sendcount;
int sendbuf[3];
int recvbuf;
int root = 0;
MPI_Comm_size (MPI_COMM_WORLD, &size);
if (size != 3)
{
fprintf (stderr, "Number of processes must be 3\n");
exit (0);
}
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
if (rank == 0)
{
sendbuf[0] = 3;
sendbuf[1] = 5;
sendbuf[2] = 7;
}
MPI_Scatter (sendbuf, sendcount, MPI_INT, &recvbuf, recvcount, MPI_INT, root, MPI_COMM_WORLD);
printf ("rank: %i, value: %i\n", rank, recvbuf);
/* Output:
* rank: 0, value: 3
* rank: 1, value: 5
* rank: 2, value: 7
*/