Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include "mpi.h"
- main(int argc, char *argv[]) {
- int rank, size;
- int my_rank; // rank of process
- int p; // number of processes
- int source; // rank of sender
- int dest; // rank of receiver
- int tag = 0; // tag for messages
- char message[30]; // storage for message
- MPI_Status status; // return status for
- // receive
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- /* si el rank no es cero, deben enviarle el mensaje al rank 0 */
- if (my_rank != 0) {
- sprintf(message, "Greetings from process %d!",
- my_rank);
- dest = 0;
- /* MPI_SEND(datos del mensaje, tamaño del mensaje, tipo de mensaje(char, int, float) , destinatario, tag, MPI_COMM_WORLD) */
- MPI_Send(message, strlen(message)+1, MPI_CHAR,
- dest, tag, MPI_COMM_WORLD);
- /* si el rank es 0, entonces recibe los mensajes de los otros rank */
- } else {
- for (source = 1; source < p; source++) {
- /*MPI_RECV( mensaje, tamaño del mensaje, tipo de mensaje, MPI_ANY_SOURCE = asincrono MPI_SOURCE: sincrono (lleva orden), tag
- MPI_COM_WORD, estado) */
- MPI_Recv(message, 100, MPI_CHAR, MPI_ANY_SOURCE, tag,
- MPI_COMM_WORLD, &status);
- printf("Message received from %d\n", status.MPI_SOURCE);
- printf("%s\n", message);
- }
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement